From d37ee89e845ef83b75b1ad42345f73ad82c71629 Mon Sep 17 00:00:00 2001 From: Will Miles Date: Sat, 9 Nov 2024 19:53:47 -0500 Subject: [PATCH] ESP8266PWM: Fix phase shift glitches In some cases it was possible for the computed phase shift to skip a cycle. Update the shift calculation logic to prevent this. --- .../src/core_esp8266_waveform_phase.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/ESP8266PWM/src/core_esp8266_waveform_phase.cpp b/lib/ESP8266PWM/src/core_esp8266_waveform_phase.cpp index b89ec8bc1..68cb9010e 100644 --- a/lib/ESP8266PWM/src/core_esp8266_waveform_phase.cpp +++ b/lib/ESP8266PWM/src/core_esp8266_waveform_phase.cpp @@ -324,7 +324,7 @@ static IRAM_ATTR void timer1Interrupt() { case WaveformMode::INIT: waveform.states &= ~waveform.toSetBits; // Clear the state of any just started if (waveform.alignPhase >= 0 && waveform.enabled & (1UL << waveform.alignPhase)) { - wave.nextPeriodCcy = waveform.pins[waveform.alignPhase].nextPeriodCcy + wave.nextPeriodCcy; + wave.nextPeriodCcy = waveform.pins[waveform.alignPhase].nextPeriodCcy + scaleCcys(waveform.phaseCcy, isCPU2X); } else { wave.nextPeriodCcy = waveform.nextEventCcy; @@ -342,15 +342,15 @@ static IRAM_ATTR void timer1Interrupt() { // @willmmiles new feature case WaveformMode::UPDATEPHASE: // in WaveformMode::UPDATEPHASE, we recalculate the targets - if (waveform.alignPhase >= 0 && waveform.enabled & (1UL << waveform.alignPhase)) { + if ((waveform.alignPhase >= 0) && (waveform.enabled & (1UL << waveform.alignPhase))) { // Compute phase shift to realign with target - auto& align_wave = waveform.pins[waveform.alignPhase]; - int32_t shift = static_cast(align_wave.nextPeriodCcy + scaleCcys(waveform.phaseCcy, isCPU2X) - wave.nextPeriodCcy); - const int32_t periodCcys = scaleCcys(wave.periodCcys, isCPU2X); - if (shift > periodCcys/2) shift -= periodCcys; - else if (shift <= -periodCcys/2) shift += periodCcys; - wave.nextPeriodCcy += shift; - wave.endDutyCcy += shift; + auto const newPeriodCcy = waveform.pins[waveform.alignPhase].nextPeriodCcy + scaleCcys(waveform.phaseCcy, isCPU2X); + auto const period = scaleCcys(wave.periodCcys, isCPU2X); + auto shift = ((static_cast (newPeriodCcy - wave.nextPeriodCcy) + period/2) % period) - (period/2); + wave.nextPeriodCcy += static_cast(shift); + if (static_cast(wave.endDutyCcy - wave.nextPeriodCcy) > 0) { + wave.endDutyCcy = wave.nextPeriodCcy; + } } default: break;