@@ -241,16 +241,17 @@ int ICACHE_RAM_ATTR stopWaveform(uint8_t pin) {
241241
242242// For dynamic CPU clock frequency switch in loop the scaling logic would have to be adapted.
243243// Using constexpr makes sure that the CPU clock frequency is compile-time fixed.
244- static inline ICACHE_RAM_ATTR int32_t scaleCcys (int32_t ccys) {
244+ static inline ICACHE_RAM_ATTR int32_t scaleCcys (const int32_t ccys, const bool isCPU2X ) {
245245 if (ISCPUFREQ160MHZ) {
246- return ((CPU2X & 1 ) ? ccys : ccys >> 1 );
246+ return isCPU2X ? ccys : ( ccys >> 1 );
247247 }
248248 else {
249- return ((CPU2X & 1 ) ? ccys << 1 : ccys) ;
249+ return isCPU2X ? ( ccys << 1 ) : ccys;
250250 }
251251}
252252
253253static ICACHE_RAM_ATTR void timer1Interrupt () {
254+ const bool isCPU2X = CPU2X & 1 ;
254255 const uint32_t isrStartCcy = ESP.getCycleCount ();
255256 if ((waveform.toSetBits && !(waveform.enabled & waveform.toSetBits )) || waveform.toDisableBits ) {
256257 // Handle enable/disable requests from main app.
@@ -282,7 +283,7 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
282283 // fall through
283284 case WaveformMode::UPDATEEXPIRY:
284285 // in WaveformMode::UPDATEEXPIRY, expiryCcy temporarily holds relative CPU cycle count
285- wave.expiryCcy = wave.nextPeriodCcy + scaleCcys (wave.expiryCcy );
286+ wave.expiryCcy = wave.nextPeriodCcy + scaleCcys (wave.expiryCcy , isCPU2X );
286287 wave.mode = WaveformMode::EXPIRES;
287288 break ;
288289 default :
@@ -325,7 +326,7 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
325326 else {
326327 const int32_t overshootCcys = now - waveNextEventCcy;
327328 if (overshootCcys >= 0 ) {
328- const int32_t periodCcys = scaleCcys (wave.periodCcys );
329+ const int32_t periodCcys = scaleCcys (wave.periodCcys , isCPU2X );
329330 if (waveform.states & pinBit) {
330331 // active configuration and forward are 100% duty
331332 if (wave.periodCcys == wave.dutyCcys ) {
@@ -352,7 +353,7 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
352353 wave.endDutyCcy = wave.nextPeriodCcy ;
353354 }
354355 else {
355- int32_t dutyCcys = scaleCcys (wave.dutyCcys );
356+ int32_t dutyCcys = scaleCcys (wave.dutyCcys , isCPU2X );
356357 if (dutyCcys <= wave.adjDutyCcys ) {
357358 dutyCcys >>= 1 ;
358359 wave.adjDutyCcys -= dutyCcys;
@@ -397,7 +398,7 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
397398
398399 int32_t callbackCcys = 0 ;
399400 if (waveform.timer1CB ) {
400- callbackCcys = scaleCcys (microsecondsToClockCycles (waveform.timer1CB ()));
401+ callbackCcys = scaleCcys (microsecondsToClockCycles (waveform.timer1CB ()), isCPU2X );
401402 }
402403 now = ESP.getCycleCount ();
403404 int32_t nextTimerCcys = waveform.nextEventCcy - now;
@@ -407,7 +408,7 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
407408 }
408409
409410 // Timer is 80MHz fixed. 160MHz CPU frequency need scaling.
410- if (ISCPUFREQ160MHZ || CPU2X & 1 ) {
411+ if (ISCPUFREQ160MHZ || isCPU2X ) {
411412 nextTimerCcys >>= 1 ;
412413 }
413414
0 commit comments