@@ -241,16 +241,17 @@ int ICACHE_RAM_ATTR stopWaveform(uint8_t pin) {
241
241
242
242
// For dynamic CPU clock frequency switch in loop the scaling logic would have to be adapted.
243
243
// 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 ) {
245
245
if (ISCPUFREQ160MHZ) {
246
- return ((CPU2X & 1 ) ? ccys : ccys >> 1 );
246
+ return isCPU2X ? ccys : ( ccys >> 1 );
247
247
}
248
248
else {
249
- return ((CPU2X & 1 ) ? ccys << 1 : ccys) ;
249
+ return isCPU2X ? ( ccys << 1 ) : ccys;
250
250
}
251
251
}
252
252
253
253
static ICACHE_RAM_ATTR void timer1Interrupt () {
254
+ const bool isCPU2X = CPU2X & 1 ;
254
255
const uint32_t isrStartCcy = ESP.getCycleCount ();
255
256
if ((waveform.toSetBits && !(waveform.enabled & waveform.toSetBits )) || waveform.toDisableBits ) {
256
257
// Handle enable/disable requests from main app.
@@ -282,7 +283,7 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
282
283
// fall through
283
284
case WaveformMode::UPDATEEXPIRY:
284
285
// 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 );
286
287
wave.mode = WaveformMode::EXPIRES;
287
288
break ;
288
289
default :
@@ -325,7 +326,7 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
325
326
else {
326
327
const int32_t overshootCcys = now - waveNextEventCcy;
327
328
if (overshootCcys >= 0 ) {
328
- const int32_t periodCcys = scaleCcys (wave.periodCcys );
329
+ const int32_t periodCcys = scaleCcys (wave.periodCcys , isCPU2X );
329
330
if (waveform.states & pinBit) {
330
331
// active configuration and forward are 100% duty
331
332
if (wave.periodCcys == wave.dutyCcys ) {
@@ -352,7 +353,7 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
352
353
wave.endDutyCcy = wave.nextPeriodCcy ;
353
354
}
354
355
else {
355
- int32_t dutyCcys = scaleCcys (wave.dutyCcys );
356
+ int32_t dutyCcys = scaleCcys (wave.dutyCcys , isCPU2X );
356
357
if (dutyCcys <= wave.adjDutyCcys ) {
357
358
dutyCcys >>= 1 ;
358
359
wave.adjDutyCcys -= dutyCcys;
@@ -397,7 +398,7 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
397
398
398
399
int32_t callbackCcys = 0 ;
399
400
if (waveform.timer1CB ) {
400
- callbackCcys = scaleCcys (microsecondsToClockCycles (waveform.timer1CB ()));
401
+ callbackCcys = scaleCcys (microsecondsToClockCycles (waveform.timer1CB ()), isCPU2X );
401
402
}
402
403
now = ESP.getCycleCount ();
403
404
int32_t nextTimerCcys = waveform.nextEventCcy - now;
@@ -407,7 +408,7 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
407
408
}
408
409
409
410
// Timer is 80MHz fixed. 160MHz CPU frequency need scaling.
410
- if (ISCPUFREQ160MHZ || CPU2X & 1 ) {
411
+ if (ISCPUFREQ160MHZ || isCPU2X ) {
411
412
nextTimerCcys >>= 1 ;
412
413
}
413
414
0 commit comments