Getting started with WDG

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

Info white.png Information
The WDG (or the dog) needs to be refreshed (pet) or the system will be reset (bark)

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.

WWDG Refresh.png

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.

WWDG.png

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 : IWDG.png

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.

Info white.png Information
For more details, check the dedicated IWDG chapter in the product reference manual.

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.

create STM32CubeIDE project.png

This example uses the NUCLEO-L476RG board.

  • Select NUCLEO-L476RG using the Board Selector as shown in the figure below:

Select NUCLEO-L476RG board.png

  • Save the project.

WWDG name.png

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

GPIO Config.png

LedPinGPIO.png LED UM GPIO.png

Info white.png Information
The user label is defined to have a more meaningful name
Info white.png Information
For more information on GPIO, refer to the Getting started with GPIO page

3.4 Configure Clock

  • In the Clock Configuration window, in the HCLK box, enter the maximum value for frequency : 80MHz

CLK Config.png

3.5 Configure WWDG

  • In the same window, select WWDG and click on Activated
  • Then go to Parameter Settings, and enter the following configuration

WWDG param.png

Info white.png Information
Refer to the 2.1 chapter WWDG : Window watchdog for more information on the parameters

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

WWDG Toggle.png

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
Info white.png Information
Insert your code between /* USER CODE BEGIN 3 */ and /* USER CODE END 3 */ tags

The WWDG is set as following :

  • Clock counter prescaler (Nwwdg_prescaler) : 8
  • Window value (Nwindow) : 80
  • Free running downcounter value (Nrefresh) : 128

Formula.png

Try the application by replacing the values in the formula and calculate the maximum delay.

Info white.png Information
Try other delays such as 10 or 30 and then conclude
 /* 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 */
Info white.png Information
Make sure to keep User Code when re-generating !

Code genreation panel.png

3.7 Compile and flash

  • Click on Build button Built.png
  • Click on Debug button (to run step by step) Debug.png
  • Or on Run button (to execute) Run.png

The green LED state changes every 20ms. In the same time, the WWDG is refreshed.

Info white.png Information
Refer to the 2.2 chapter IWDG : Independant WatchDoG for more information on the parameters
Warning white.png Warning
All GPIOs are able to drive 5 V and 3.3 V in input mode, but they are only able to generate 3.3V in output push-pull mode

ST10543 NUCLEO L476RG top D2Blink.png

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.

create STM32CubeIDE project.png

In this example the NUCLEO-L476RG board is used.

  • Select NUCLEO-L476RG in board Selector.

Select NUCLEO-L476RG board.png

  • Save the project.

IWDG name.png

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

GPIO Config.png

LedPinGPIO.png LED UM GPIO.png

Info white.png Information
The user label is defined to have a more meaningful name
Info white.png Information
For more information on GPIO, refer to the Getting started with GPIO page

4.4 Configure IWDG

  • In the same window, select IWDG and click on Activated
  • Then go to Parameter Settings, and enter the following configuration

IWDG Param.png

Info white.png Information
Refer to the 2.2 chapter IWDG : Independant watchdog for more information on the parameters

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

IWDG Toggle.png

The IWDG is set as following :

  • Clock counter prescaler : 128
  • Window value : 100
  • Downcounter reload value : 100
Info white.png Information
The window value is set to the same value as the downcounter in order to define the widest window !

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
Info white.png Information
Insert your code between /* USER CODE BEGIN 3 */ and /* USER CODE END 3 */ tags
Info white.png Information
Try other delays such as 50, 200 or 450 and then conclude
 /* 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 */
Info white.png Information
Make sure to keep User Code when re-generating !

Code genreation panel.png

4.6 Compile and flash

  • Click on Build button Built.png
  • Click on Debug button (to run step by step) Debug.png
  • Or on Run button (to execute) Run.png

The green LED state changes every 300ms. The IWDG is also refreshed at the same time.

Warning white.png Warning
All GPIOs are able to drive 5 V and 3.3 V in input mode, but they are only able to generate 3.3V in output push-pull mode

ST10543 NUCLEO L476RG top D2Blink.png