44
44
#include " ets_sys.h"
45
45
#include < atomic>
46
46
47
+ // Timer is 80MHz fixed. 160MHz CPU frequency need scaling.
48
+ constexpr bool ISCPUFREQ160MHZ = clockCyclesPerMicrosecond() == 160 ;
47
49
// Maximum delay between IRQs, Timer1, <= 2^23 / 80MHz
48
50
constexpr int32_t MAXIRQTICKSCCYS = microsecondsToClockCycles(10000 );
49
51
// Maximum servicing time for any single IRQ
@@ -181,7 +183,7 @@ int startWaveformClockCycles(uint8_t pin, uint32_t highCcys, uint32_t lowCcys,
181
183
if (!waveform.timer1Running ) {
182
184
initTimer ();
183
185
}
184
- else if (T1V > (( clockCyclesPerMicrosecond () == 160 ) ? IRQLATENCYCCYS >> 1 : IRQLATENCYCCYS) ) {
186
+ else if (T1V > IRQLATENCYCCYS) {
185
187
// Must not interfere if Timer is due shortly
186
188
timer1_write (IRQLATENCYCCYS);
187
189
}
@@ -218,7 +220,7 @@ int ICACHE_RAM_ATTR stopWaveform(uint8_t pin) {
218
220
waveform.toDisableBits = 1UL << pin;
219
221
std::atomic_thread_fence (std::memory_order_release);
220
222
// Must not interfere if Timer is due shortly
221
- if (T1V > (( clockCyclesPerMicrosecond () == 160 ) ? IRQLATENCYCCYS >> 1 : IRQLATENCYCCYS) ) {
223
+ if (T1V > IRQLATENCYCCYS) {
222
224
timer1_write (IRQLATENCYCCYS);
223
225
}
224
226
while (waveform.toDisableBits ) {
@@ -240,12 +242,11 @@ int ICACHE_RAM_ATTR stopWaveform(uint8_t pin) {
240
242
// For dynamic CPU clock frequency switch in loop the scaling logic would have to be adapted.
241
243
// Using constexpr makes sure that the CPU clock frequency is compile-time fixed.
242
244
static inline ICACHE_RAM_ATTR int32_t scaleCcys (int32_t ccys) {
243
- constexpr bool cpuFreq80MHz = clockCyclesPerMicrosecond () == 80 ;
244
- if (cpuFreq80MHz) {
245
- return ((CPU2X & 1 ) ? ccys << 1 : ccys);
245
+ if (ISCPUFREQ160MHZ) {
246
+ return ((CPU2X & 1 ) ? ccys : ccys >> 1 );
246
247
}
247
248
else {
248
- return ((CPU2X & 1 ) ? ccys : ccys >> 1 );
249
+ return ((CPU2X & 1 ) ? ccys << 1 : ccys );
249
250
}
250
251
}
251
252
@@ -406,8 +407,7 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
406
407
}
407
408
408
409
// Timer is 80MHz fixed. 160MHz CPU frequency need scaling.
409
- constexpr bool cpuFreq160MHz = clockCyclesPerMicrosecond () == 160 ;
410
- if (cpuFreq160MHz || CPU2X & 1 ) {
410
+ if (ISCPUFREQ160MHZ || CPU2X & 1 ) {
411
411
nextTimerCcys >>= 1 ;
412
412
}
413
413
0 commit comments