Skip to main content

Subsection 16.1.3 Part 3: The Watchdog

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.
Figure 16.1.22. The watchdog at rest.

Activity 16.1.1. A quick example.

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.

(a)

Press the blue user button repeatedly, more frequently than every 4 seconds, and observe what happens.

(b)

Now press it less frequently than every 4 seconds, and observe the difference.
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 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.
The blue user button is on PC13, which has its own pull-up resistor built in.
#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;
}
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.