C12 · Lesson 41 · Watchdogs and robust firmware
Recovering from the bugs you didn't find
Firmware runs for years without anyone watching, through electrical noise, brownouts and the one input sequence nobody tested. Sooner or later something makes it hang: a loop waiting for a device that never answers, a corrupted pointer, a deadlock. A watchdog timer is the last line of defense: a hardware countdown the program must restart ("kick") regularly. If the program stops kicking, because it's stuck, the watchdog resets the whole chip and the device starts again. Good firmware also avoids needing it, by never waiting forever for anything.
- The program kicks a 50 ms watchdog every 10 ms while it works. A planted bug makes it hang after 200 ms. It prints the watchdog's reset count each time it starts.
- Run it: it prints 0 at the start, then the hang stops the kicks, and 50 ms later the watchdog resets the board. It restarts, prints 1, and so on: four resets in a second, visible as gaps in P0's toggling.
- Delete the
if (loops == 20)hang and run again: one start, no resets.
Predict
A watchdog times out after 50 ms, and the program kicks it at the end of each pass of its main loop. A pass normally takes 10 ms, but occasionally 70 ms. What happens?
The board resets during each 70 ms pass: from the last kick, 70 ms is longer than the 50 ms timeout. The timeout must be longer than the longest legitimate gap between kicks, with margin, or the watchdog becomes the bug.
Name it
The watchdog
A hardware counter, independent of the program, counting down from a reload value. The program writes a special value to restart it (here WDT_KICK = 0x5A, a value unlikely to be written by accident); if the count reaches zero, the chip resets. On this board: WDT_LOAD sets the timeout in cycles, WDT_CTRL = 1 starts it, and SYS_RESETS counts watchdog resets, so the restarted program can tell it recovered from a hang and log it.
Using it well
- Timeout: longer than the longest normal gap between kicks, with margin; shorter than the outage the device can tolerate.
- Where to kick: in the main loop, once every task has shown it's alive, never from a timer interrupt, which keeps running even when the main loop is stuck.
- After a reset: put outputs in a safe state first (heater off, motor stopped), then read the reset cause.
- Windowed watchdogs also reset if kicked too early, catching code that's running wild and kicking in a tight loop.
Not needing it
- Every wait gets a timeout:
while (busy && SYS_CYCLES - start < limit), then handle the failure. - Check every input's range and every function's result.
- Keep the stack well clear of other data; some systems fill it with a pattern at start-up and check how much is ever overwritten.
- A brown-out detector resets the chip cleanly when the supply sags, instead of letting it run on corrupt logic levels.
Worked example
A main loop normally takes up to 12 ms, and the device may be out of action for at most 100 ms. Choose a watchdog timeout.
Show the solution
- Bracket the timeout. Anywhere comfortably above 12 ms and below 100 ms.
- Pick 50 ms. That leaves a 4× margin over the longest pass and recovers within half the allowed outage.
- Convert to cycles. At 10 MHz: so WDT_LOAD = 500,000.
Answer 50 ms, WDT_LOAD = 500,000.
Worked example
Rewrite while (!(UART_STATUS & UART_RX_READY)) ; so it can't hang.
Show the solution
- Give up after a time limit. Note the cycle count when the wait starts, and return an error once 100,000 cycles (10 ms) have passed.
uint32_t start = SYS_CYCLES;
while (!(UART_STATUS & UART_RX_READY))
if (SYS_CYCLES - start > 100000) return -1; // 10 ms: give up
- Let the caller decide. The caller then decides what a missing byte means, instead of the whole device freezing.
Answer check SYS_CYCLES - start inside the loop and return −1 after 10 ms.
Worked example
A windowed watchdog must be kicked between 5 ms and 20 ms after the last kick. What does the window catch that a plain timeout doesn't?
Show the solution
- What a plain watchdog catches. Only a program that stops kicking.
- What a window adds. It also catches one that kicks too often, for example a runaway loop that calls the kick function repeatedly while doing nothing useful.
- How it acts. Kicks closer than 5 ms reset the device as surely as a missing kick.
Answer a program that kicks too often, such as a runaway loop that calls the kick function repeatedly while doing nothing useful.
Check
What WDT_LOAD gives a 50 ms timeout on the 10 MHz board?
Check
A program hangs 200 ms after every start, and its watchdog resets it 20 ms after the last kick (at the hang). How many resets happen in 3 s?
Check
The longest normal gap between kicks is 15 ms. With a 2× margin, what timeout?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
Check
A program waits for 1 bytes at 9600 baud, with a timeout of 2 times the time they should take. What timeout is that?
Write units the way you normally would: 1.5 µs, 1500 ns and 0.0015 ms are the same answer.
Check
A fault makes a device reset 20 times a day, and each reset leaves it out of action for 50 ms while it restarts. What percentage of the time is it out of action?
Check
Where should a program kick its watchdog?
Check
This program waits for a byte from the terminal, which never sends one, so it hangs. Give the wait a 10 ms timeout: print the byte if it comes, or -1 if it doesn't, and return.
C on the course's RISC-V board (10 MHz, no floating point). #include "board.h" for the registers; print(x) shows a number. Tab indents; press Esc and then Tab to move on.
Check
Start a watchdog with a 100 ms timeout, then run the loop below for the whole second without being reset.
MicroPython on the course's board: machine (Pin, PWM, ADC, DAC, UART, I2C, SPI, Timer, WDT) and time. Each call into the hardware takes about 40 µs. Tab indents; press Esc and then Tab to move on.
Check
A main loop takes up to 12 ms, and the device may be out of action for at most 100 ms.
(a)
What is the shortest safe watchdog timeout?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
(b)
What is the longest allowed?
Check
A windowed watchdog must be kicked between 5 ms and 20 ms after the last kick. A loop kicks every 30 ms.
(a)
Is that inside the window (1 for yes)?
(b)
How wide is the window?
Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.
Explain why
Why kick the watchdog from the main loop rather than from a timer interrupt?
The watchdog's job is to detect that the program has stopped doing its work. A timer interrupt keeps firing on schedule even when the main loop is stuck in an endless wait or a corrupted loop, because interrupts preempt the stuck code, so kicking from there would keep the watchdog satisfied forever while the device does nothing useful. Kicking from the main loop, ideally only after checking that each task has run since the last kick, ties the kicks to real progress: if any part of the program stalls, the kicks stop and the reset happens.
Practice
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.