This article explains what WDG is and how to use it through examples.
1. What is a WDG ?
WDG stands for watchdog. The main goal of this IP is to detect and resolve malfunctions due to software failures. The principle is to periodically refresh the WDG (or pet the dog), if the counter isn't refreshed, a system reset is generated. Also, the WDG acts as a protection since it avoids to stay stuck in a particular stage of processing. The configuration using option bytes can launch the WDG by hardware or software. Once enabled, it can only be disabled by a reset
2. WDG type
There are two types of WDG :
- WWDG : Window watchdog
- IWDG : Independent watchdog
2.1. WWDG : Window watchdog
2.1.1. Definition
The window watchdog is based on a 7-bit downcounter that can be set as free running. It can be used as a watchdog to reset the device when a problem occurs. It is clocked from the main clock. It has an early warning interrupt capability and the counter can be frozen in debug mode.
2.1.2. Application benefits
- Best suited for applications which require the watchdog to react within an accurate time window.
- Configurable time-window thanks to the prescaler value (For example, the STM32L476xx have a programmable timeout range from 51.2 us to 28.2ms)
- Selectable hardware or software start
- Early Wakeup Interrupt (EWI) available before reset happens
2.1.3. How does it work ?
The diagram below illustrates how the WWDG operates. If the downcounter is reloaded too early or too late, the window watchdog will initiate a reset.
As can be seen on the following block diagram, the counter value and the window value are compared in order to evaluate the time to refresh the downcounter in the configurable window.
2.2. IWDG : Independent Watchdog
2.2.1. Definition
The independent watchdog is based on a 12-bit downcounter and 8-bit prescaler. It is clocked from an independent 32 kHz internal RC (LSI) and as it operates independently from the main clock, it can operate in Stop and Standby modes. It can be used either as a watchdog to reset the device when a problem occurs, or as a free running timer for application timeout management. It is hardware or software configurable through the option bytes. The counter can be frozen in debug mode.
2.2.2. Application benefits
- Totally independent process outside the main application
- Configurable timeout period thanks to the prescaler value (For example, the STM32L476xx have a programmable timeout range from 125us to 32.7s)
- Selectable hardware or software start
- Selectable low-power freeze in Standbye or Stop modes
2.2.3. How does it work ?
The IWDG architecture is represented below :
As can be seen on the block diagram above, IWDG registers are located in the CORE voltage domain while its functions are in the VDD voltage domain. The 8-bit prescaler is used to divide the LSI oscillator frequency. When the IWDG is started, the 12-bit counter starts counting down from the reset value of 0xFFF. To refresh the IWDG counter, the Key value (0xAAAA) must be written in the Key register to reload the counter value. If the downcounter reaches the end of the count value (0x000), a system reset is generated.
3. Configure WWDG with LED indication
3.1. Objectives
- In this project, you will learn how to setup WWDG in STM32CubeIDE
- How to Generate Code in STM32CubeIDE and use HAL functions
- Create a simple application to test and periodically refresh the WWDG
- Verify the correct functionality on toggling LED
3.2. Create the project in STM32CubeIDE
- File > New > STM32 Project in main panel.
This example uses the NUCLEO-L476RG board.
- Select NUCLEO-L476RG using the Board Selector as shown in the figure below:
- Save the project.
3.3. Configure GPIO
- In the Pinout & Configuration window, select GPIO and define PA5 as a GPIO_Output
- Keep the same configuration as following
- Low
- Output push pull
- No pull-up and pull-down
- Low
- LD2
3.4. Configure Clock
- In the Clock Configuration window, in the HCLK box, enter the maximum value for frequency : 80MHz
3.5. Configure WWDG
- In the same window, select WWDG and click on Activated
- Then go to Parameter Settings, and enter the following configuration
3.6. Generate project and edit main.c
The easiest way to generate the code is to save your current project : Ctrl + S
The code is generated so you can see it in the left side of the screen in the project explorer
Now, open the main.c file which is the main source file for this application
- The LED toggling is done with a HAL function inside main.c
The WWDG is set as following :
- Clock counter prescaler (Nwwdg_prescaler) : 8
- Window value (Nwindow) : 80
- Free running downcounter value (Nrefresh) : 128
Try the application by replacing the values in the formula and calculate the maximum delay.
/* USER CODE BEGIN 3 */
/* Infinite loop */
{
HAL_Delay(20); // because (1/80000000)*4096*8*(127-80+1)) ~ 20ms
HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
if (HAL_WWDG_Refresh(&hwwdg) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE END 3 */
3.7. Compile and flash
The green LED state changes every 20ms. In the same time, the WWDG is refreshed.
4. Configure IWDG with LED indication
4.1. Objectives
- In this project, you will learn how to setup IWDG in STM32CubeIDE
- How to Generate Code in STM32CubeIDE and use HAL functions
- Create a simple application to test and periodically refresh the IWDG
- Verify the correct functionality on toggling LED
4.2. Create the project in STM32CubeIDE
- File > New > STM32 Project in main panel.
In this example the NUCLEO-L476RG board is used.
- Select NUCLEO-L476RG in board Selector.
- Save the project.
4.3. Configure GPIO
- In the Pinout & Configuration window, select GPIO and define PA5 as a GPIO_Output
- Keep the same configuration as following
- Low
- Output Push Pull
- No pull-up and pull-down
- Low
- LD2
4.4. Configure IWDG
- In the same window, select IWDG and click on Activated
- Then go to Parameter Settings, and enter the following configuration
4.5. Generate project and edit main.c
The easiest way to generate the code is to save your current project : Ctrl + S
The code is generated so you can see it in the left side of the screen in the project explorer
The IWDG is set as following :
- Clock counter prescaler : 128
- Window value : 100
- Downcounter reload value : 100
Maximum time calculate before reset (if no refresh):
According to 2.2.3 How does it work ? section, the downcounter frequency is calculated first. It is equal to the LSI divided by the Clock Prescaler, (32000/128) = 250Hz. This also defines the time for one clock cycle , 1/F = 1/250 = 4ms. The maximum time before reset is now defined by (time for one clock cycle) x (Reload value) = 0.004 x 100 = 400ms. The IWDG must be refreshed before the downcounter reaches the end of the counter value. So, the delay must be shorter than 400ms.
Open the main.c file which is the main source file for this application.
- The LED toggling is done with a HAL function inside main.c
/* USER CODE BEGIN 3 */
/* Infinite loop */
{
HAL_Delay(300); // because (1/32000)*128*100 so max ~400ms
HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
if (HAL_IWDG_Refresh(&hiwdg) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE END 3 */
4.6. Compile and flash
The green LED state changes every 300ms. The IWDG is also refreshed at the same time.