4444#include " ets_sys.h"
4545#include < atomic>
4646
47+ // Timer is 80MHz fixed. 160MHz CPU frequency need scaling.
48+ constexpr bool ISCPUFREQ160MHZ = clockCyclesPerMicrosecond() == 160 ;
4749// Maximum delay between IRQs, Timer1, <= 2^23 / 80MHz
4850constexpr int32_t MAXIRQTICKSCCYS = microsecondsToClockCycles(10000 );
4951// Maximum servicing time for any single IRQ
@@ -181,7 +183,7 @@ int startWaveformClockCycles(uint8_t pin, uint32_t highCcys, uint32_t lowCcys,
181183 if (!waveform.timer1Running ) {
182184 initTimer ();
183185 }
184- else if (T1V > (( clockCyclesPerMicrosecond () == 160 ) ? IRQLATENCYCCYS >> 1 : IRQLATENCYCCYS) ) {
186+ else if (T1V > IRQLATENCYCCYS) {
185187 // Must not interfere if Timer is due shortly
186188 timer1_write (IRQLATENCYCCYS);
187189 }
@@ -218,7 +220,7 @@ int ICACHE_RAM_ATTR stopWaveform(uint8_t pin) {
218220 waveform.toDisableBits = 1UL << pin;
219221 std::atomic_thread_fence (std::memory_order_release);
220222 // Must not interfere if Timer is due shortly
221- if (T1V > (( clockCyclesPerMicrosecond () == 160 ) ? IRQLATENCYCCYS >> 1 : IRQLATENCYCCYS) ) {
223+ if (T1V > IRQLATENCYCCYS) {
222224 timer1_write (IRQLATENCYCCYS);
223225 }
224226 while (waveform.toDisableBits ) {
@@ -240,12 +242,11 @@ int ICACHE_RAM_ATTR stopWaveform(uint8_t pin) {
240242// For dynamic CPU clock frequency switch in loop the scaling logic would have to be adapted.
241243// Using constexpr makes sure that the CPU clock frequency is compile-time fixed.
242244static 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 );
246247 }
247248 else {
248- return ((CPU2X & 1 ) ? ccys : ccys >> 1 );
249+ return ((CPU2X & 1 ) ? ccys << 1 : ccys );
249250 }
250251}
251252
@@ -406,8 +407,7 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
406407 }
407408
408409 // 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 ) {
411411 nextTimerCcys >>= 1 ;
412412 }
413413
0 commit comments