@@ -103,6 +103,7 @@ namespace {
103
103
int startPin = 0 ;
104
104
int endPin = 0 ;
105
105
int nextPin = 0 ;
106
+ uint32_t nextEventCcy;
106
107
} waveform;
107
108
108
109
}
@@ -259,15 +260,21 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
259
260
waveform.toDisable = -1 ;
260
261
}
261
262
262
- uint32_t now = ESP.getCycleCount ();
263
-
264
263
if (toSetMask) {
265
264
Waveform& wave = waveform.pins [waveform.toSet ];
266
265
switch (wave.mode ) {
267
266
case WaveformMode::INIT:
268
267
waveform.states &= ~toSetMask; // Clear the state of any just started
269
- wave.nextPeriodCcy = (wave.alignPhase >= 0 && waveform.enabled & (1UL << wave.alignPhase )) ?
270
- waveform.pins [wave.alignPhase ].nextPeriodCcy + wave.nextPeriodCcy : now;
268
+ if (wave.alignPhase >= 0 && waveform.enabled & (1UL << wave.alignPhase )) {
269
+ wave.nextPeriodCcy = waveform.pins [wave.alignPhase ].nextPeriodCcy + wave.nextPeriodCcy ;
270
+ if (static_cast <int32_t >(waveform.nextEventCcy - wave.nextPeriodCcy ) > 0 ) {
271
+ waveform.nextEventCcy = wave.nextPeriodCcy ;
272
+ }
273
+ }
274
+ else {
275
+ wave.nextPeriodCcy = ESP.getCycleCount ();
276
+ waveform.nextEventCcy = wave.nextPeriodCcy ;
277
+ }
271
278
wave.nextEventCcy = wave.nextPeriodCcy ;
272
279
if (!wave.expiryCcy ) {
273
280
wave.mode = WaveformMode::INFINITE;
@@ -286,18 +293,25 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
286
293
287
294
// Exit the loop if the next event, if any, is sufficiently distant.
288
295
const uint32_t isrTimeoutCcy = isrStartCcy + ISRTIMEOUTCCYS;
289
- uint32_t nextTimerCcy ;
290
- bool busy = waveform.enabled ;
291
- if (! busy) {
292
- nextTimerCcy = now + MAXIRQCCYS;
296
+ bool busy ;
297
+ if (! waveform.enabled ) {
298
+ busy = false ;
299
+ waveform. nextEventCcy = ESP. getCycleCount () + MAXIRQCCYS;
293
300
}
294
- else if (!(waveform.enabled & (1UL << waveform.nextPin ))) {
295
- waveform.nextPin = waveform.startPin ;
301
+ else {
302
+ busy = static_cast <int32_t >(isrTimeoutCcy - waveform.nextEventCcy ) > 0 ;
303
+ if (!(waveform.enabled & (1UL << waveform.nextPin ))) {
304
+ waveform.nextPin = waveform.startPin ;
305
+ }
296
306
}
297
307
while (busy) {
298
- nextTimerCcy = now + MAXIRQCCYS;
299
308
int stopPin = waveform.nextPin ;
300
309
int pin = waveform.nextPin ;
310
+ uint32_t now;
311
+ do {
312
+ now = ESP.getCycleCount ();
313
+ } while (static_cast <int32_t >(waveform.nextEventCcy - now) > 0 );
314
+ waveform.nextEventCcy = now + MAXIRQCCYS;
301
315
do {
302
316
// If it's not on, ignore
303
317
if (!(waveform.enabled & (1UL << pin)))
@@ -394,33 +408,26 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
394
408
}
395
409
}
396
410
397
- if (static_cast <int32_t >(nextTimerCcy - wave.nextEventCcy ) > 0 ) {
398
- nextTimerCcy = wave.nextEventCcy ;
411
+ if (static_cast <int32_t >(waveform. nextEventCcy - wave.nextEventCcy ) > 0 ) {
412
+ waveform. nextEventCcy = wave.nextEventCcy ;
399
413
waveform.nextPin = pin;
400
414
}
401
- now = ESP.getCycleCount ();
402
415
} while ((pin = (pin < waveform.endPin ) ? pin + 1 : waveform.startPin , pin != stopPin));
403
416
404
- const int32_t timerMarginCcys = isrTimeoutCcy - nextTimerCcy;
405
- busy = timerMarginCcys > 0 ;
406
- if (busy) {
407
- while (static_cast <int32_t >(nextTimerCcy - now) > 0 ) {
408
- now = ESP.getCycleCount ();
409
- }
410
- }
417
+ busy = static_cast <int32_t >(isrTimeoutCcy - waveform.nextEventCcy ) > 0 ;
411
418
}
412
419
413
420
int32_t nextTimerCcys;
414
421
if (waveform.timer1CB ) {
415
422
int32_t callbackCcys = microsecondsToClockCycles (waveform.timer1CB ());
416
423
// Account for unknown duration of timer1CB().
417
- nextTimerCcys = nextTimerCcy - ESP.getCycleCount ();
424
+ nextTimerCcys = waveform. nextEventCcy - ESP.getCycleCount ();
418
425
if (nextTimerCcys > callbackCcys) {
419
426
nextTimerCcys = callbackCcys;
420
427
}
421
428
}
422
429
else {
423
- nextTimerCcys = nextTimerCcy - now ;
430
+ nextTimerCcys = waveform. nextEventCcy - ESP. getCycleCount () ;
424
431
}
425
432
426
433
// Firing timer too soon, the NMI occurs before ISR has returned.
0 commit comments