A deployed embedded system has nobody watching it. If the program gets stuck, in a loop that never exits or waiting for an event that never occurs, the system stops responding, and on a shelf or a rooftop there is no one to press the reset button. The independent watchdog (IWDG) is the hardware answer. It is a 12-bit downcounter with an 8-bit prescaler, running on its own 32 kHz internal clock, the LSI, separate from the clock that runs the processor. That is what independent means here: the watchdog keeps counting even if the main clock stops. A healthy program reloads the counter often, which is called petting the dog. If the program gets stuck and the reload stops coming, the counter reaches zero and the watchdog resets the processor.
A deployed embedded system has nobody watching it. If the program gets stuck, the system stops responding, and there is no one to press the reset button. The independent watchdog (IWDG) is the hardware answer.
It is a 12-bit downcounter with an 8-bit prescaler, and it runs on its own 32 kHz (slow!) internal clock, the LSI. That is what independent means: the watchdog keeps counting even if the main clock stops.
The IWDG also has a window option, which allows petting the dog only during a specified window in the countdown: petting too early or too late will generate a reset. There is another watchdog, the system window watchdog (WWDG), which runs on the main clock and can generate early interrupts before a reset occurs (Reference Manual §21; the IWDG is §20).
Make a copy of TemplateProject, call the copy Watchdog, and copy watchdog.c from the book’s starter files into the new project’s Src folder. Compile, upload to your Nucleo, and open CoolTerm at 9600 baud. You’ll see a Hello dog world message that seems to repeat every 4 seconds.
What actually happens: the Hello dog world message is printed at the very start of the program, before the event loop, so seeing the message over and over means the device is rebooting regularly. When you press the blue user button, the program is petting the watchdog, thereby resetting the countdown that would otherwise have ended in a reboot. If you pet the watchdog frequently enough, the program continues running without rebooting.
The Hello dog world message is printed at the very start of the program, before the event loop. Seeing the message over and over means the device is rebooting regularly.
The configuration steps are visible in the listing, and each one is a write to one of the watchdog’s registers (Reference Manual §20). Writing 0x0000 5555 to the key register IWDG_KR unlocks the protected prescaler and reload registers. The prescaler IWDG_PR sets the counting rate; only certain divider values are allowed, listed in the Reference Manual’s IWDG_PR description, and 0b011 divides the 32 kHz LSI by 32, giving a 1 ms tick. The reload register IWDG_RLR is set to the length of the countdown in ticks, 4000 in the listing, so a full countdown is 4 seconds. The watchdog needs a moment to absorb each of these writes, and that is what the two while loops in the listing wait for. Writing 0x0000 CCCC to IWDG_KR starts the watchdog. Writing 0x0000 AAAA to IWDG_KR reloads the counter to the value in IWDG_RLR, and that is what petting the dog does in the program: it restarts the 4 second countdown before it can reach zero.
Set the counting rate using the prescaler IWDG_PR: only certain divider values are allowed, listed in the Reference Manual’s IWDG_PR description. 0b011 divides the 32 kHz LSI by 32, giving a 1 ms tick.
#include "ES28.h"
#include "uart.h"
#include <stdio.h>
// A few magic numbers for the watchdog (from the datasheet)
#define KEY_ACCESS 0x00005555
#define KEY_UPDATE 0x0000AAAA
#define KEY_START 0x0000CCCC
int main( void ) {
uart2_init();
printf( "Hello dog world\r\n");
// Use the built-in blue user button as a proxy for petting the dog.
RCC->IOPENR |= RCC_IOPENR_GPIOCEN;
GPIOC->MODER = (GPIOC->MODER & ~GPIO_MODER_MODE13_Msk) | (GPIO_INPUT << GPIO_MODER_MODE13_Pos);
// The watchdog is always accessible, so no need to turn any clocks on for it.
// First, unlock the watchdog registers by writing a special value to the key register
IWDG->KR = KEY_ACCESS;
// Set up the prescaler - the watchdog clock runs at 32 kHz. We will set up a 1 ms tick.
while( IWDG->SR & IWDG_SR_PVU ); // Can only update when the PVU status bit is clear.
IWDG->PR = 0b011;
// Reload register is set to 4000: we need to pet the dog at least once every 4 secs
while( IWDG->SR & IWDG_SR_RVU ); // Can only update when the RVU status bit is clear
IWDG->RLR = 4000;
// Wake the dog...
IWDG->KR = KEY_START;
// Pet once, so the first countdown also runs from IWDG_RLR (4 seconds)
IWDG->KR = KEY_UPDATE;
// Now, loop and pet the dog whenever the blue user button is pushed.
while(1){
if ( (GPIOC->IDR & GPIO_IDR_ID13) == 0 ){
// Pet the dog
IWDG->KR = KEY_UPDATE;
printf( "Nice Doggy\r\n");
delay_ms(100); // be nice to the terminal.
}
}
return 0;
}
Here is the top of watchdog.c: the three key values the watchdog registers expect, and the blue user button set up as an input, which we’ll use as a proxy for petting the dog.
// A few magic numbers for the watchdog (from the datasheet)
#define KEY_ACCESS 0x00005555
#define KEY_UPDATE 0x0000AAAA
#define KEY_START 0x0000CCCC
int main( void ) {
uart2_init();
printf( "Hello dog world\r\n");
// Use the built-in blue user button as a proxy for petting the dog.
RCC->IOPENR |= RCC_IOPENR_GPIOCEN;
GPIOC->MODER = (GPIOC->MODER & ~GPIO_MODER_MODE13_Msk)
| (GPIO_INPUT << GPIO_MODER_MODE13_Pos);
This is where we configure the dog: unlock the registers, set the prescaler, set the reload value, and start it. The watchdog needs a moment to absorb each write, and that is what the two while loops wait for.
// The watchdog is always accessible, so no need to turn any clocks on for it.
// First, unlock the watchdog registers by writing a special value
// to the key register
IWDG->KR = KEY_ACCESS;
// Set up the prescaler - the watchdog clock runs at 32 kHz.
// We will set up a 1 ms tick.
// Can only update when the PVU status bit is clear.
while( IWDG->SR & IWDG_SR_PVU );
IWDG->PR = 0b011;
// Reload register is set to 4000: we need to pet the dog at least once every 4 secs
// Can only update when the RVU status bit is clear
while( IWDG->SR & IWDG_SR_RVU );
IWDG->RLR = 4000;
// Wake the dog...
IWDG->KR = KEY_START;
// Pet once, so the first countdown also runs from IWDG_RLR (4 seconds)
IWDG->KR = KEY_UPDATE;
watchdog.c, the loop: pet the dog whenever the blue user button is pushed.
// Now, loop and pet the dog whenever the blue user button is pushed.
while(1){
if ( (GPIOC->IDR & GPIO_IDR_ID13) == 0 ){
// Pet the dog
IWDG->KR = KEY_UPDATE;
printf( "Nice Doggy\r\n");
delay_ms(100); // be nice to the terminal.
}
}
return 0;
}
The IWDG also has a window option, which generates a reset if the dog is petted too early as well as too late. The STM32C031C6 carries a second watchdog, the system window watchdog (WWDG), which runs on the main clock and can generate an early interrupt before it resets the processor; it is described in Reference Manual §21.