Skip to content

PWM ISR watchdog - #3681

Merged
pkendall64 merged 5 commits into
ExpressLRS:masterfrom
pkendall64:pwm-isr-watchdog
Jul 3, 2026
Merged

pkendall64 merged 5 commits into
ExpressLRS:masterfrom
pkendall64:pwm-isr-watchdog

Conversation

@pkendall64

@pkendall64 pkendall64 commented Jun 27, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

This PR should fix the runaway behaviour seen on ESP8285-based receivers using PWM/servo outputs.

Also included in this PR is an early reset of PWM modules (MCPWM, LEDC & RMT) on ESP32 devices.

Problem

On ESP8266/ESP8285, PWM is provided by a timer ISR, PWM output generation continues inside the waveform ISR after the last pulse configuration has been written. If channel updates stop, or the servo task stops servicing outputs, the last programmed waveform can keep running indefinitely. In the worst case that leaves motors/servos holding the last command instead of cleanly entering failsafe. This has been seen when there is a semi-brownout condition on the RX; not enough for the MCU to detect a brownout, but enough for the MCU main loop to hang, and the timer interrupts still fire.

On ESP32 devices when the device reboots the ROM, and bootloader do NOT reset the modules! This means that PWM signals are running until ExpressLRS's setup function starts and clears the pins. This is also after some initialization on our side, so could be about 1 second!

What changed

  • Added a PWMController::feedWatchdog() API.
  • Implemented a 50 ms watchdog in the ESP8266 waveform driver.
  • The watchdog is armed whenever PWM waveforms are started/updated.
  • If the watchdog expires, all active waveforms are stopped and outputs are driven low.
  • Updated devServoOutput to:
    • explicitly enter failsafe whenever the receiver is no longer fully connected/model-matched
    • keep running on a short timeout while disconnected so intentional failsafe PWM continues to service the watchdog
    • avoid leaving stale channel data pending across disconnect/failsafe transitions
    • ESP32; high-priority constructor function to reset MCPWM, LEDC & RMT devices starts at about 450ms after boot time.

Expected result

  • Stale PWM output should no longer continue forever (or the hardware watchdog fires and reboots the RX) if channel updates stop.
  • Receiver outputs should either:
    • continue the configured failsafe output intentionally while the RX is disconnected, or
    • be shut down by the ISR watchdog if output servicing stops unexpectedly.

@CapnBry CapnBry left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a nice safety measure. I'm willing to bet that the ESP is doing just fine because its low voltage limit is insanely low, but the RF chip usually will start glitching first and it is likely doing something that's causing our code to just get caught in a loop somewhere.

At least this should stop it, but I did not figure out a way to reproduce the behavior for testing. It ran for an hour though against my ESP32 capture tester with no weird behavior and max 1600ns deviation in any sampling period (150 cycles)

(ns) mean=1499962 min=1499212 max=1500662 range=1450 (1600)
(ns) mean=1499987 min=1499225 max=1500675 range=1450 (1600)
(ns) mean=1499962 min=1499250 max=1500687 range=1437 (1600)
(ns) mean=1500012 min=1499362 max=1500712 range=1350 (1600)
(ns) mean=1499962 min=1499225 max=1500712 range=1487 (1600)
(ns) mean=1500000 min=1499237 max=1500675 range=1438 (1600)
(ns) mean=1500000 min=1499237 max=1500687 range=1450 (1600)
(ns) mean=1499975 min=1499262 max=1500637 range=1375 (1600)
(ns) mean=1500012 min=1499212 max=1500712 range=1500 (1600)
(ns) mean=1499987 min=1499525 max=1500662 range=1137 (1600)
(ns) mean=1499987 min=1499275 max=1500600 range=1325 (1600)
(ns) mean=1500000 min=1499187 max=1500700 range=1513 (1600)
(ns) mean=1500012 min=1499312 max=1500675 range=1363 (1600)
(ns) mean=1499975 min=1499250 max=1500687 range=1437 (1600)
(ns) mean=1499975 min=1499187 max=1500700 range=1513 (1600)
(ns) mean=1499975 min=1499200 max=1500700 range=1500 (1600)

Comment thread src/lib/PWM/waveform_8266.cpp Resolved
Comment thread src/lib/PWM/waveform_8266.cpp Resolved
@mha1

mha1 commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

I set up an experiment with a BetaFPV SuperP and a digital servo connected to CH1/throttle stick. The SuperP is powered with 5V by a lab power supply AND current is limited to 350mA to force a brownout at more ambitious servo movements. Failsafe is set to throttle stick low (990us). I force a brownout by wiggling the throttle stick a bit at roughly 80% throttle position.

You can clearly see the brownout event at about 2.34s. PWM output stops briefly leaving the output low (sometimes high) for some 20-40ms. This is the point were servo movement stops. But a frozen PWM 1770us output (my approx. position triggering the brownout at 80% throttle) continues for another 1.2s before the receiver reboots and resumes normal PWM operation 3.5s later. The 1.2s PWM is really frozen with identical PWM duty cycles all along.

image image image

I wonder why it takes 1.2s before the rx starts rebooting. It looks like the ESP freezes and never gets a chance to stop the PWM after the brownout event until it fully resets.

@CapnBry

CapnBry commented Jul 1, 2026

Copy link
Copy Markdown
Member

Whoa that's sort of nuts that PWM keeps, but the SuperP has register-based hardware PWM so a brown out event could possibly leave some of those registers and peripheral mux values set until software boots and clears it all back to a known state. I wonder if it is in the bootloader doing PWM because that would be pretty nuts and difficult to stop. Is UART0 connected to the logic analyzer? There's that ets ... it prints when it hits the bootloader and it might help to see a reference point to where the chip is restarting in relation to that middle PWM block.

There's no place we could put this PR's code on ESP32 though, maybe attach a pin interrupt to the first servo pin and check this PWMWD there? Or create another RTOS task on the other core with a tiny stack that checks the same thing? I'm not sure the disposition of the other core in this situation but we might have to put that off if we want to get 4.1 out and save ESP32 for 4.2.

@mha1

mha1 commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

The first red box is the very first bootloader debug message (ets) at 115200 baud. Boot reason is CPU reset. The second one is already a message from the Arduino core reporting hardware.json was opened successfully. All while PWM is still going.

The 40ms gap is the exact moment where the brownout forces the mcu restart with the Bootloader starting immediately. PWM keeps going on well after the bootloader has finished. It's hard to imagine the MCU wouldn't reset all of the hardware before starting the firmware.

image

@CapnBry

CapnBry commented Jul 3, 2026

Copy link
Copy Markdown
Member

I still approve of this change

@mha1 mha1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ESP32: high-priority constructor function to reset MCPWM, LEDC & RMT devices starts at about 450ms after boot time.

462ms time to reset PWM confirmed.

Image

@pkendall64
pkendall64 merged commit 3069ab6 into ExpressLRS:master Jul 3, 2026
26 checks passed
@pkendall64
pkendall64 deleted the pwm-isr-watchdog branch July 3, 2026 20:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants