In this article, I will talk about the watchdog timer.
Here is the development environment at the time of posting.
PC: Windows 10 OS
IDE: STM32CubeIDE Version1.3.0
Configurator: STM32CubeMX Version5.5.0
Board: STM32Nucleo-F401RE
What is a watchdog timer?
We can call it a watchdog timer, or WDT for short.
It is a counter that is monitored and when it overflows, the microcontroller is reset.
The WDT is responsible for resetting the microcontroller to make it work properly when, for example, the program runs out of control and stops working.
Normally, the counter is refreshed to prevent the reset from occurring.
Until a long time ago, dedicated ICs that included a function to monitor the drop in power supply voltage were the mainstream, but nowadays, many of them are built into microcontrollers.
There are two types of watchdog timers available for the STM32
IWDG:
Independent Watchdog Timer
The first I stands for independence.
This is the more common of the two.
IWDG is a counter that runs and resets itself when it overflows.
The reason it is called an independent type is because the clock that runs the timer is separate from the main clock of the microcontroller.
Even if the microcontroller’s clock stops due to some fault, it can still be reset if the IWDG is running.
WWDG:
Window Watchdog Timer
Needless to say, the W at the beginning stands for window.
The meaning of window is a window frame rather than a window.
WWDG detects not only that a counter is overflowing, but also that its timing is earlier than expected.
In more robust systems, it seems to be recommended to use a combination of these two WDTs.
Independent Watchdog Timer
In this article, we will check the behavior of the standalone watchdog timer.
As usual, create a project in the IDE (STM32CubeIDE).
Select File – New – STM32 Project.
In the Board Selector, select NUCLEO-F401RE and press Next.
Enter F401IWDT as the Project Name and press Finish.
When it asks “Initialize all peripherals with their default Mode ? and press Yes.
(Do you want to initialize the peripherals with their default values?
This kind of project is associated with the STM32CubeMx perspective.
Press Yes when it asks “Do you want to open this perspective now?
(This kind of project is associated with the STM32CubeMx perspective. Do you want to open it now?
I’m not sure what this means, but I interpreted it as “Do you want to import the configurator STM32CubeMX into the IDE layout? I interpreted it as “Do you want to import the STM32CubeMX configurator into the IDE layout?
Now I have created a project with certain specifications for this board, including the initialization of peripheral functions.
Click on IWDG in Pinout & Configuration – Categories – System Core.
Check the Mode – Activated checkbox.
Leave the Parameter Settings checkbox untouched.
In the Project Explorer, click on the project name F401IWDT, and select Project – Build Project to build it.
MX_IWDG_Init() is now created in main().
Double-click on MX_IWDG_Init() to select it, and select Open Declaration from the right-click menu to see the inside of the function.
IWDT initialization code
static void MX_IWDG_Init(void)
{
hiwdg.Instance = IWDG;
Init.Prescaler = IWDG_PRESCALER_4;
Prescaler = IWDG_PRESCALER_4; hiwdg.Init.Reload = 4095;
if (HAL_IWDG_Init(&hiwdg) ! = HAL_OK)
{
Error_Handler();
}
}
Leaving out the comments, the code above is written.
hiwdg is a structure of type IWDG_HandleTypeDef.
Double-click on it to select it, then right-click on the Open Declaration menu to go to the declaration part and check it out.
The IWDG has parameters for the prescaler and the count value to be reloaded.
The clock and these parameters determine the timeout (overflow) value of the watchdog timer.
Since the IWDG clock is 32 kHz, the timeout value is 4 * 4095 * 1/32k = 0.511875 [sec].
There seem to be several choices of prescalers.
Double-click on IWDG_PRESCALER_4 to select it, then choose Open Declaration from the right-click menu.
You can choose between 4, 8, 16, 32, 64, 128, or 256.
Then the timeout value will be
4 * 1 * 1/32k = 125 usec to
256 * 4095 * 1/32k = 32.76 sec
It seems that the timeout value can be set in the range of
Checking the reset operation
In order to prevent the watchdog timer from working, the counter needs to be refreshed, but we don’t have the code for that at this stage.
Also, the code to start the timer in the watchdog timer initialization process has been written.
Therefore, when you run the program, it will reset after about 0.5 seconds.
Let’s check its behavior first.
Connect the board to the PC with a USB cable.
Double-click on the line number of HAL_Init() just below main() (line 79 in our environment) to place a breakpoint.
If you select Run – Debug, it will break at the HAL_Init() line.
Then Run – Resume (or press F8) to break at the HAL_Init() line again.
There is a while(1) loop below, which means that the program is reset.
If you comment and disable the MX_IWDG_Init() part and do the same thing, you will see that HAL_Init() only breaks the first time, so the reset is caused by the IWDT timeout.
Add code to refresh the counter
This is useless, so let’s add some code to prevent the reset from happening.
Double-click on Drivers – STM32F4xx_HAL_Driver – Src – stm32f4xx_hal_iwdg.c in Project Explorer to open it.
Check for a function called HAL_IWDG_Refresh().
Place it in the while(1) loop of main().
The argument of the function is the structure address of IWDG, &hiwdg.
/* Infinite loop */ /* USER CODE BEGIN WHILE
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_IWDG_Refresh(&hiwdg);
/* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ /* USER CODE END WHILE
/* USER CODE BEGIN 3 */ while (1)
}
/* USER CODE END 3 */ }
Now, when we run the program, we can see that HAL_Init() only comes once at the beginning, indicating that the counter refresh operation is working.
However, the refresh operation does not have to be done frequently in the while(1) loop, but should be done periodically or otherwise considered by the system.
What do you think of IWDG?
If you have not used it before, please give it a try.





