Watchdog timer
On this page
A night guard must press a button on the wall every hour. If the button is not pressed, an alarm sounds, because the guard may have fallen asleep. A watchdog timer does this for a program: it is a countdown inside the chip that the program must start again and again, and if the program stops doing so, the countdown reaches zero and restarts the chip.
See it
A healthy program needs 3 s for one loop and clears the watchdog once in each loop. Someone sets the watchdog timeout to 2 s. What happens?
The line is the time left on the watchdog. It falls all the time. At the end of each loop the program clears the watchdog and the line jumps back to the top. When the program hangs, nothing clears it, the line reaches zero and the chip restarts. The restart is drawn as taking no time.
The idea
A program can hang: it waits for a sensor that never answers, or a fault sends it round the same few lines for ever. At a remote site there is nobody to press the reset button.
The watchdog is a timer with its own clock, so it keeps running when the rest of the chip is stuck. You choose its timeout, from 16 ms to 8 s on the ATmega328P, and from 0.1 ms to about 26 s on the STM32F103, whose watchdog clock (nominally 40 kHz) may run anywhere from 30 to 60 kHz, so its timeout can be a third shorter than planned. The program must clear the watchdog, which starts the countdown again, before the timeout passes. If it fails to, the watchdog resets the chip, and the program starts again from the beginning.
the longest time a working program needs between two clears. Two times that is a common margin
A restart does not repair anything. If a sensor is broken, the program will hang again, so a good program counts its restarts and reports them.
One worked number
A logger clears its watchdog at the end of every loop, and one loop takes 0.5 s. The timeout is 2 s. At 4.2 s the program hangs. When does the chip restart, and for how long was the logger stopped?
Show the worked solution, 3 steps
- Find the last clear before the hang
Clears come at 0.5 s, 1.0 s, 1.5 s and so on. The last one before 4.2 s is at 4.0 s.
- Add the timeout
The countdown started again at 4.0 s and nothing clears it after that. It reaches zero at 4.0 s + 2 s = 6.0 s, and the chip restarts.
- Find the time lost
6.0 s − 4.2 s = 1.8 s stopped. Check it in the figure: press the button that makes the program hang.
The mistake to avoid
A second trap: a timeout shorter than the longest healthy loop restarts a program that has no fault.
Check yourself
A program clears its watchdog every 0.25 s. The timeout is 1 s. The program hangs 0.1 s after a clear. How long after the hang does the chip restart?
A controller has a watchdog, and it still stopped sending data for two days until someone switched it off and on. Its watchdog is cleared inside a timer interrupt that runs every 10 ms. Why did the watchdog not help?
You add a watchdog with a timeout of 2 s to a device that worked well. Now it restarts every 2 s. Its loop includes a wait of 3 s for a network modem to answer. What is wrong?
Where you meet it
- Remote monitoringA logger at a borehole or a solar plant restarts itself after a hang, so nobody has to travel to press reset.
- Machine controllersWhen the program of a controller stops, its watchdog switches the outputs to a safe state, so a motor is not left running.
- Routers and modemsThe box that connects a site to the network restarts itself in the same way.
- A watchdog is a countdown with its own clock. The program must clear it before it reaches zero.
- If the program hangs, nothing clears it, and the watchdog restarts the chip.
- The timeout must be longer than the longest healthy loop, with a margin.
- Clear it from the main loop, never from a timer interrupt. A restart is a recovery, not a repair.
Discussion
No comments yet. Questions, results from your own build, a better way to explain it: all welcome.
My notes on this page
Kept on this device only. Nothing is sent anywhere.
Tip: select words in the lesson, or tap a figure or an equation, to put a note right there.
Notes on earlier wording
The lesson has changed and these words are no longer in it. Your notes are kept, with the words you chose.