Skip to content

Commit 81cec44

Browse files
committed
Improved duty cycle overshoot mitigation.
Case for investigation: 3% (shl 5) vs. 1.5% (shl 6), either less fuzz, but a few marked stray spots, or more fuzz, but no bumps in counter-PWM travel test.
1 parent 08da9cc commit 81cec44

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

cores/esp8266/core_esp8266_waveform.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,13 +339,18 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
339339
wave.nextPeriodCcy += wave.periodCcys;
340340
nextEdgeCcy = wave.endDutyCcy;
341341
}
342+
else if (wave.autoPwm && (overshootCcys << 6) > wave.periodCcys && wave.nextEventCcy == wave.endDutyCcy) {
343+
uint32_t adjPeriods = ((overshootCcys << 6) - 1) / wave.periodCcys;
344+
wave.nextPeriodCcy += adjPeriods * wave.periodCcys;
345+
// adapt expiry such that it occurs during intended cycle
346+
if (WaveformMode::EXPIRES == wave.mode) {
347+
wave.expiryCcy += adjPeriods * wave.periodCcys;
348+
}
349+
nextEdgeCcy = wave.endDutyCcy + adjPeriods * wave.dutyCcys;
350+
}
342351
else {
343352
waveform.states ^= 1UL << pin;
344353
nextEdgeCcy = wave.nextPeriodCcy;
345-
// the idle cycle code updating for the next period will approximate the duty/idle ratio.
346-
if (wave.autoPwm && wave.dutyCcys >= microsecondsToClockCycles(3)) {
347-
nextEdgeCcy += (overshootCcys / wave.dutyCcys) * idleCcys;
348-
}
349354
if (pin == 16) {
350355
GP16O &= ~1; // GPIO16 write slow as it's RMW
351356
}

0 commit comments

Comments
 (0)