@@ -65,7 +65,6 @@ enum class WaveformMode : uint8_t {INFINITE = 0, EXPIRES = 1, UPDATEEXPIRY = 2,
65
65
66
66
// Waveform generator can create tones, PWM, and servos
67
67
typedef struct {
68
- uint32_t nextEventCcy; // ESP clock cycle when switching wave cycle, or expiring wave.
69
68
uint32_t nextPeriodCcy; // ESP clock cycle when a period begins. If WaveformMode::INIT, temporarily holds positive phase offset ccy count
70
69
uint32_t endDutyCcy; // ESP clock cycle when going from duty to off
71
70
uint32_t dutyCcys; // Set next off cycle at low->high to maintain phase
@@ -292,7 +291,6 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
292
291
waveform.nextEventCcy = wave.nextPeriodCcy ;
293
292
waveform.nextPin = waveform.toSet ;
294
293
}
295
- wave.nextEventCcy = wave.nextPeriodCcy ;
296
294
if (!wave.expiryCcy ) {
297
295
wave.mode = WaveformMode::INFINITE;
298
296
break ;
@@ -344,33 +342,34 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
344
342
345
343
Waveform& wave = waveform.pins [pin];
346
344
347
- const uint32_t overshootCcys = now - wave.nextEventCcy ;
348
- if (WaveformMode::EXPIRES == wave.mode && wave.nextEventCcy == wave.expiryCcy &&
349
- static_cast <int32_t >(overshootCcys) >= 0 ) {
345
+ uint32_t waveNextEventCcy = (waveform.states & pinBit) ? wave.endDutyCcy : wave.nextPeriodCcy ;
346
+ if (WaveformMode::EXPIRES == wave.mode &&
347
+ static_cast <int32_t >(waveNextEventCcy - wave.expiryCcy ) >= 0 &&
348
+ static_cast <int32_t >(now - wave.expiryCcy ) >= 0 ) {
350
349
// Disable any waveforms that are done
351
350
waveform.enabled ^= pinBit;
352
351
busyPins ^= pinBit;
353
352
}
354
353
else {
354
+ const uint32_t overshootCcys = now - waveNextEventCcy;
355
355
if (static_cast <int32_t >(overshootCcys) >= 0 ) {
356
- uint32_t nextEdgeCcy;
357
356
if (waveform.states & pinBit) {
358
357
// active configuration and forward are 100% duty
359
358
if (wave.periodCcys == wave.dutyCcys ) {
360
359
wave.nextPeriodCcy += wave.periodCcys ;
361
- nextEdgeCcy = wave.endDutyCcy = wave.nextPeriodCcy ;
360
+ waveNextEventCcy = wave.endDutyCcy = wave.nextPeriodCcy ;
362
361
}
363
362
else if (wave.autoPwm && static_cast <int32_t >(now - wave.nextPeriodCcy ) >= 0 ) {
364
363
const uint32_t adj = (overshootCcys + wave.dutyCcys ) / wave.periodCcys ;
365
364
// maintain phase, maintain duty/idle ratio, temporarily reduce frequency by fwdPeriods
366
- nextEdgeCcy = wave.endDutyCcy = wave.nextPeriodCcy + adj * wave.dutyCcys ;
365
+ waveNextEventCcy = wave.endDutyCcy = wave.nextPeriodCcy + adj * wave.dutyCcys ;
367
366
wave.nextPeriodCcy += adj * wave.periodCcys ;
368
367
// adapt expiry such that it occurs during intended cycle
369
368
if (WaveformMode::EXPIRES == wave.mode )
370
369
wave.expiryCcy += adj * wave.periodCcys ;
371
370
}
372
371
else {
373
- nextEdgeCcy = wave.nextPeriodCcy ;
372
+ waveNextEventCcy = wave.nextPeriodCcy ;
374
373
waveform.states ^= pinBit;
375
374
if (16 == pin) {
376
375
GP16O = 0 ;
@@ -407,41 +406,39 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
407
406
GPOS = pinBit;
408
407
}
409
408
}
410
- nextEdgeCcy = wave.endDutyCcy ;
409
+ waveNextEventCcy = wave.endDutyCcy ;
411
410
}
412
411
413
- wave.nextEventCcy =
414
- (WaveformMode::EXPIRES == wave.mode && static_cast < int32_t >(nextEdgeCcy - wave. expiryCcy ) > 0 ) ?
415
- wave. expiryCcy : nextEdgeCcy;
412
+ if (WaveformMode::EXPIRES == wave.mode && static_cast < int32_t >(waveNextEventCcy - wave. expiryCcy ) > 0 ) {
413
+ waveNextEventCcy = wave.expiryCcy ;
414
+ }
416
415
}
417
416
418
- if (static_cast <int32_t >(wave. nextEventCcy - isrTimeoutCcy) >= 0 ) {
417
+ if (static_cast <int32_t >(waveNextEventCcy - isrTimeoutCcy) >= 0 ) {
419
418
busyPins ^= pinBit;
420
- if (static_cast <int32_t >(waveform.nextEventCcy - wave. nextEventCcy ) > 0 ) {
421
- waveform.nextEventCcy = wave. nextEventCcy ;
419
+ if (static_cast <int32_t >(waveform.nextEventCcy - waveNextEventCcy ) > 0 ) {
420
+ waveform.nextEventCcy = waveNextEventCcy ;
422
421
waveform.nextPin = pin;
423
422
}
424
423
}
425
- else if (static_cast <int32_t >(isrNextEventCcy - wave. nextEventCcy ) > 0 ) {
426
- isrNextEventCcy = wave. nextEventCcy ;
424
+ else if (static_cast <int32_t >(isrNextEventCcy - waveNextEventCcy ) > 0 ) {
425
+ isrNextEventCcy = waveNextEventCcy ;
427
426
}
428
427
}
429
428
430
429
now = getScaledCcyCount (isrStartCcy);
431
430
} while ((pin = (pin < waveform.endPin ) ? pin + 1 : waveform.startPin ) != stopPin);
432
431
}
433
432
434
- int32_t nextTimerCcys ;
433
+ int32_t callbackCcys = 0 ;
435
434
if (waveform.timer1CB ) {
436
- int32_t callbackCcys = microsecondsToClockCycles (waveform.timer1CB ());
437
- // Account for unknown duration of timer1CB().
438
- nextTimerCcys = waveform.nextEventCcy - getScaledCcyCount (isrStartCcy);
439
- if (nextTimerCcys > callbackCcys) {
440
- nextTimerCcys = callbackCcys;
441
- }
435
+ callbackCcys = microsecondsToClockCycles (waveform.timer1CB ());
442
436
}
443
- else {
444
- nextTimerCcys = waveform.nextEventCcy - getScaledCcyCount (isrStartCcy);
437
+ const uint32_t now = getScaledCcyCount (isrStartCcy);
438
+ int32_t nextTimerCcys = waveform.nextEventCcy - now;
439
+ // Account for unknown duration of timer1CB().
440
+ if (waveform.timer1CB && nextTimerCcys > callbackCcys) {
441
+ nextTimerCcys = callbackCcys;
445
442
}
446
443
447
444
// Firing timer too soon, the NMI occurs before ISR has returned.
0 commit comments