@@ -65,7 +65,6 @@ enum class WaveformMode : uint8_t {INFINITE = 0, EXPIRES = 1, UPDATEEXPIRY = 2,
6565
6666// Waveform generator can create tones, PWM, and servos
6767typedef struct {
68- uint32_t nextEventCcy; // ESP clock cycle when switching wave cycle, or expiring wave.
6968 uint32_t nextPeriodCcy; // ESP clock cycle when a period begins. If WaveformMode::INIT, temporarily holds positive phase offset ccy count
7069 uint32_t endDutyCcy; // ESP clock cycle when going from duty to off
7170 uint32_t dutyCcys; // Set next off cycle at low->high to maintain phase
@@ -292,7 +291,6 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
292291 waveform.nextEventCcy = wave.nextPeriodCcy ;
293292 waveform.nextPin = waveform.toSet ;
294293 }
295- wave.nextEventCcy = wave.nextPeriodCcy ;
296294 if (!wave.expiryCcy ) {
297295 wave.mode = WaveformMode::INFINITE;
298296 break ;
@@ -344,33 +342,34 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
344342
345343 Waveform& wave = waveform.pins [pin];
346344
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 ) {
350349 // Disable any waveforms that are done
351350 waveform.enabled ^= pinBit;
352351 busyPins ^= pinBit;
353352 }
354353 else {
354+ const uint32_t overshootCcys = now - waveNextEventCcy;
355355 if (static_cast <int32_t >(overshootCcys) >= 0 ) {
356- uint32_t nextEdgeCcy;
357356 if (waveform.states & pinBit) {
358357 // active configuration and forward are 100% duty
359358 if (wave.periodCcys == wave.dutyCcys ) {
360359 wave.nextPeriodCcy += wave.periodCcys ;
361- nextEdgeCcy = wave.endDutyCcy = wave.nextPeriodCcy ;
360+ waveNextEventCcy = wave.endDutyCcy = wave.nextPeriodCcy ;
362361 }
363362 else if (wave.autoPwm && static_cast <int32_t >(now - wave.nextPeriodCcy ) >= 0 ) {
364363 const uint32_t adj = (overshootCcys + wave.dutyCcys ) / wave.periodCcys ;
365364 // 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 ;
367366 wave.nextPeriodCcy += adj * wave.periodCcys ;
368367 // adapt expiry such that it occurs during intended cycle
369368 if (WaveformMode::EXPIRES == wave.mode )
370369 wave.expiryCcy += adj * wave.periodCcys ;
371370 }
372371 else {
373- nextEdgeCcy = wave.nextPeriodCcy ;
372+ waveNextEventCcy = wave.nextPeriodCcy ;
374373 waveform.states ^= pinBit;
375374 if (16 == pin) {
376375 GP16O = 0 ;
@@ -407,41 +406,39 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
407406 GPOS = pinBit;
408407 }
409408 }
410- nextEdgeCcy = wave.endDutyCcy ;
409+ waveNextEventCcy = wave.endDutyCcy ;
411410 }
412411
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+ }
416415 }
417416
418- if (static_cast <int32_t >(wave. nextEventCcy - isrTimeoutCcy) >= 0 ) {
417+ if (static_cast <int32_t >(waveNextEventCcy - isrTimeoutCcy) >= 0 ) {
419418 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 ;
422421 waveform.nextPin = pin;
423422 }
424423 }
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 ;
427426 }
428427 }
429428
430429 now = getScaledCcyCount (isrStartCcy);
431430 } while ((pin = (pin < waveform.endPin ) ? pin + 1 : waveform.startPin ) != stopPin);
432431 }
433432
434- int32_t nextTimerCcys ;
433+ int32_t callbackCcys = 0 ;
435434 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 ());
442436 }
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;
445442 }
446443
447444 // Firing timer too soon, the NMI occurs before ISR has returned.
0 commit comments