Firmware watchdog design that recovers devices in the field
How to design a firmware watchdog that checks real progress, survives sleep modes, records why it fired and keeps OTA updates safe.

Every connected device will eventually hang in the field: a radio driver waits forever for an interrupt that never comes, a heap fragments after three weeks, a sensor holds the I²C bus low. A firmware watchdog is the last line of defence that turns a bricked unit on a customer’s wall into a two-second reboot. The trouble is that many watchdogs are configured in a way that hides faults rather than recovering from them.
This post is for engineers and product leads shipping small fleets who want devices that heal themselves and tell you why they had to.
What a watchdog should actually prove
A watchdog is a hardware counter that resets the microcontroller unless firmware “kicks” or “feeds” it before it expires. The naive implementation feeds it from a timer interrupt or at the top of the main loop. That proves almost nothing: a timer ISR keeps firing happily while every application task is deadlocked, and a main loop can spin forever on a retry without ever doing useful work.
The question a good watchdog answers is not “is the CPU running?” but “is the device still doing its job?”. That means the feed should depend on evidence that each critical activity has made progress since the last feed.
Hardware options on common microcontrollers
Before designing the software side, know what the silicon gives you. The details matter, particularly around clocks and sleep modes.
- STM32 independent watchdog (IWDG): clocked from the internal low-speed RC oscillator (around 32 kHz, with noticeable tolerance across temperature), so it survives a failure of the main clock. Once started it cannot be stopped short of a reset. On an STM32L4 the maximum timeout is roughly 30 seconds.
- STM32 window watchdog (WWDG): runs from the APB clock and resets the part if you feed it too early as well as too late. Useful for catching a runaway loop that feeds compulsively, but it needs careful timing.
- ESP32 family: ESP-IDF provides a Task Watchdog that monitors subscribed FreeRTOS tasks, an Interrupt Watchdog that catches interrupts blocked for too long, and hardware timer-group and RTC watchdogs underneath. The task watchdog is a good start, but by default it may only log a backtrace rather than reset, so check the panic configuration.
- nRF52840: a single watchdog on the 32.768 kHz low-frequency clock, configurable to pause while the CPU sleeps or is halted by a debugger. Like the IWDG, it cannot be stopped once running.
- External supervisor ICs: a separate chip with its own oscillator that pulls the reset line if a GPIO stops toggling. It costs a few tens of cents and protects you from silicon-level lockups and badly corrupted option bytes.
For most battery-powered products we use the internal independent watchdog. An external supervisor earns its place on mains-powered or safety-relevant devices, or anywhere a stuck unit means a truck roll.
Designing a firmware watchdog that checks progress
The pattern we use on RTOS-based firmware is a supervisor task that owns the hardware watchdog. Every critical task registers with it and must check in within its own deadline. The supervisor feeds the hardware only when all registered tasks have reported in.
- Each task gets a bit in an event group or a counter in a shared struct, plus its own deadline: perhaps 2 seconds for a sensor loop, 60 seconds for a cellular task that legitimately blocks on network attach.
- Tasks check in at a point that proves real work happened, such as after a sample has been processed or a message queued, not at the top of their loop.
- The supervisor runs at a moderate priority, checks every flag, and feeds the hardware watchdog only if every task is within its deadline.
- If a task misses, the supervisor records which one into memory that survives reset, then deliberately stops feeding.
Set the hardware timeout comfortably longer than the supervisor’s check period, but short enough that a hang is cleared before a user notices. For most products that lands somewhere between 4 and 30 seconds. The slowest legitimate operation in the system, typically a flash erase, a modem attach or an OTA write, sets the floor.
Long operations
Resist the temptation to disable the watchdog around slow operations. You often cannot, and if you can, that is exactly where a hang will happen. Instead, break the work into chunks and feed between them, or have the task check in with a “still busy” state that the supervisor accepts for a bounded time.
Sleep modes and the watchdog
Low-power firmware is where watchdogs cause the most surprises. On many STM32 parts the IWDG keeps counting in Stop and Standby, so a device that sleeps for five minutes will reset every time it sleeps, unless you either wake periodically to feed it or freeze it via option bytes (the L4 series offers IWDG_STOP and IWDG_STDBY bits). Freezing it in sleep is convenient but means a hang inside a sleep transition is not covered.
Our usual compromise is an RTC wake-up slightly shorter than the watchdog timeout. Each wake costs a little energy, which is worth measuring rather than assuming. We covered how small periodic wakes add up in where the microamps really go in low-power design.
Make every reset leave evidence
A watchdog that silently reboots a device hides the bug forever. The fleet looks healthy while units lose data a few times a day. Every reset should leave a trail.
- Read the reset cause on boot: the RCC_CSR flags on STM32,
esp_reset_reason()in ESP-IDF, or the RESETREAS register on nRF52. Clear the flags after reading, otherwise they accumulate. - Keep a small record in retained RAM: a section excluded from start-up zeroing, holding the culprit task, the last few log lines and a magic value with a checksum to detect a cold boot.
- Count resets by cause: store counters in flash sparingly, and report them in the next telemetry message.
- Surface it on the dashboard: a rising watchdog reset rate after a firmware release is one of the clearest regression signals you will get.
Watchdogs, OTA and boot loops
The watchdog is also what makes over-the-air updates safe. A new image that hangs during start-up gets reset by the watchdog, and the bootloader must notice that the image never confirmed itself as healthy and roll back. MCUboot’s test-and-confirm swap and ESP-IDF’s app rollback both work this way. We explain the full update flow in secure OTA updates for small fleets.
The flip side is a boot loop: a fault that triggers on every start, such as a corrupted configuration record, resets the device endlessly. Count consecutive watchdog resets in retained memory and, after three to five in a row, boot into a safe mode that skips the suspect subsystem, falls back to default settings and still connects so you can diagnose it remotely.
How we test it on the bench
A watchdog that has never fired in testing is an assumption, not a feature. Before handover we deliberately break things:
- Add a debug command that makes each registered task spin forever, one at a time, and confirm the reset happens within the expected time and names the right task.
- Hold an interrupt line, block the I²C bus and pull the modem’s power to simulate peripheral lockups.
- Run sleep cycles for hours to confirm there are no spurious resets from wake timing.
- Flash a deliberately broken OTA image and confirm the rollback.
- Check debugger behaviour: configure the watchdog to freeze when the core is halted, or every breakpoint becomes a reset.
A checklist before you ship
- The hardware watchdog is enabled in production builds and started as early in boot as practical.
- It is fed from one supervisor, never from a timer interrupt.
- Every critical task checks in after doing real work, with its own deadline.
- The timeout sits above the slowest legitimate operation and below what a user would notice.
- Sleep behaviour is designed and measured, not left to defaults.
- Reset cause and culprit are recorded and reported to the backend.
- Repeated resets lead to safe mode, and failed updates roll back.
- Each failure path has been triggered on purpose on the bench.