Skip to content

Commit 0d96dc1

Browse files
committed
Break out of ISR if timespan to next event allows, instead of busy waiting and stealing CPU cycles from userland.
1 parent c17dd59 commit 0d96dc1

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

cores/esp8266/core_esp8266_waveform.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,14 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
312312
waveform.nextEventCcy = isrStartCcy + MAXIRQTICKSCCYS;
313313
}
314314

315+
uint32_t now = ESP.getCycleCount();
316+
uint32_t isrNextEventCcy = now;
315317
while (busyPins) {
318+
if (static_cast<int32_t>(isrNextEventCcy - now) > IRQLATENCYCCYS + DELTAIRQCCYS) {
319+
waveform.nextEventCcy = isrNextEventCcy;
320+
break;
321+
}
322+
isrNextEventCcy = waveform.nextEventCcy;
316323
for (int pin = waveform.startPin; pin <= waveform.endPin; ++pin) {
317324
const uint32_t pinBit = 1UL << pin;
318325
// If it's not on, ignore
@@ -322,7 +329,6 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
322329
Waveform& wave = waveform.pins[pin];
323330

324331
uint32_t waveNextEventCcy = (waveform.states & pinBit) ? wave.endDutyCcy : wave.nextPeriodCcy;
325-
const uint32_t now = ESP.getCycleCount();
326332
if (WaveformMode::EXPIRES == wave.mode &&
327333
static_cast<int32_t>(waveNextEventCcy - wave.expiryCcy) >= 0 &&
328334
static_cast<int32_t>(now - wave.expiryCcy) >= 0) {
@@ -396,15 +402,19 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
396402
waveform.nextEventCcy = waveNextEventCcy;
397403
}
398404
}
405+
else if (static_cast<int32_t>(isrNextEventCcy - waveNextEventCcy) > 0) {
406+
isrNextEventCcy = waveNextEventCcy;
407+
}
399408
}
409+
now = ESP.getCycleCount();
400410
}
401411
}
402412

403413
int32_t callbackCcys = 0;
404414
if (waveform.timer1CB) {
405415
callbackCcys = scaleCcys(microsecondsToClockCycles(waveform.timer1CB()));
406416
}
407-
const uint32_t now = ESP.getCycleCount();
417+
now = ESP.getCycleCount();
408418
int32_t nextTimerCcys = waveform.nextEventCcy - now;
409419
// Account for unknown duration of timer1CB().
410420
if (waveform.timer1CB && nextTimerCcys > callbackCcys) {

0 commit comments

Comments
 (0)