Getting started with WDG

Revision as of 08:52, 17 November 2022 by Registered User
Under construction.png Coming soon

This article explains what WDOG is and how to use it through examples

1. Introduction of WDOG

WDOG stands for Watch DOG. The main goal of this IP is to detect and resolve malfunctions due to software failures. The principle is to refresh periodically the WDG (or pet the dog), if the counter isn't refreshed, a system reset is generated. Also, the WDG act as a protection because it avoid to stay stuck in a particular stage of processing that it performs. The configuration using option bytes can launch the WDG by hardware or software. Once start, it cannot be disabled.

2. WDOG type

There are two types of WDOG :

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 it works?

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 we can see 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 it works?

The IWDG architecture is represented below : IWDG.png

As we can see on the above block diagram, 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 WWDOG with LED indication

3.1. Objective

  • In this project, you will learn how to setup WWDG in STM32CubeIDE
  • How to Generate Code in STM32CubeIDE and use HAL functions
  • Create simple application to test WWDG

3.2. How

  • Configure WWDG in STM32CubeIDE and Generate Code
  • Learn how to activate and refresh WWDG
  • Add into the project the HAL_Delay function and HAL_GPIO_Toggle function
  • Verify the correct functionality on toggling LED

3.3. Create the project in CSTM32ubeIDE

File > New > STM32 Project in main panel.
File:GPIO2.png

  • Once the download is done, you can select NUCLEO-L476RG in board Selector.
  • In case you haven't downloaded the STM32L476 Cube library, it will be downloaded automatically, it may take some time.

File:GPIO3.png

  • Choose a name and save the project as follows.

WWDG name.png

3.4. GPIO Configuration

  • 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
    • Very High
    • 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, please refer to the Getting started with GPIO page

3.5. WWDG Configuration

  • Always in the same window, select WWDG and click on activate
  • Then go in the Parameters 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. Clock Configuration

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

CLK Config.png

3.7. Set the project details for generation

The easiest way to the generate the code is 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
File:GPIO10.png

3.8. Open the main.c in our IDE

Now, open the main.c file which is the main source file for this application

  • We do the LED toggling in a function inside main.c
Info white.png Information
Between /* USER CODE BEGIN 3 */ and /* USER CODE END 3 */ tags
Info white.png Information
Try other delay like 10 or 30 and then conclude.
 /* USER CODE BEGIN 3 */
  /* Infinite loop */
  while (1)
  {
    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 !

File:GPIO12.png

3.9. Compile and flash

Every 500 ms the green LED state changes.

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 IWDOG with LED indication

4.1. Objective

  • In this project, you will learn how to setup IWDG in STM32CubeIDE
  • How to Generate Code in STM32CubeIDE and use HAL functions
  • Create simple application to test IWDG

4.2. How

  • Configure IWDG in STM32CubeIDE and Generate Code
  • Learn how to activate and refresh IWDG
  • Add into the project the HAL_Delay function and HAL_GPIO_Toggle function
  • Verify the correct functionality on toggling LED

4.3. Create the project in CSTM32ubeIDE

4.4. GPIO Configuration

4.5. IWDG Configuration

4.6. Clock Configuration

4.7. Set the project details for generation

The easiest way the generate the code is 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
File:GPIO10.png

4.8. Open the main.c in our IDE

4.9. Compile and flash

5. Configure IWDOG with LED indication

6. Conclusion

Keep in mind this :



[[category:Getting_started_with_STM32_system_peripherals | 15]]