STM32: How to use Watchdogs IWDG and WWDG

STM32 HAL Tutorial: Ensuring System Reliability with Watchdogs

Abstract

Learn how to use Independent Watchdog (IWDG) and Window Watchdog (WWDG) on STM32 using CubeMX and HAL. Step-by-step guide to prevent system hang-ups and crashes.

1. Introduction

Embedded systems can freeze or crash due to software bugs, memory corruption, or unexpected events.

Watchdogs are hardware timers that reset the system if the firmware fails to respond.

STM32 provides two types of watchdogs:

  1. Independent Watchdog (IWDG) – runs independently of the main system clock, ensures robust reset.
  2. Window Watchdog (WWDG) – allows refresh only within a defined time window, prevents early or late refresh errors.

By the end of this episode, you’ll be able to:

  • Configure IWDG and WWDG using CubeMX
  • Use HAL functions to refresh watchdogs
  • Implement safe, responsive embedded systems

2. Prerequisites

  • STM32 board
  • STM32CubeIDE installed
  • Knowledge of HAL, timers, and interrupts

3. Independent Watchdog (IWDG)

Step 1 – Concept

  • IWDG uses a low-speed internal clock (LSI).
  • Runs independently of the main system clock.
  • Triggers reset if not refreshed within a set timeout.

Step 2 – CubeMX Configuration

  1. Enable IWDG under System Core → Independent Watchdog
  2. Set prescaler and reload value to define timeout (e.g., 8s)
  3. Generate code

Step 3 – HAL Usage

// Initialize IWDG (CubeMX generates MX_IWDG_Init())
MX_IWDG_Init();

// Refresh IWDG periodically in main loop or tasks
while(1)

{

    // Your application code

    HAL_IWDG_Refresh(&hiwdg); // Prevent system reset

}

If HAL_IWDG_Refresh() is not called in time, MCU resets.

4. Window Watchdog (WWDG)

Step 1 – Concept

  • WWDG monitors firmware execution within a specific time window.
  • Refreshing too early or too late triggers a reset.
  • Useful for detecting timing errors or infinite loops.

Step 2 – CubeMX Configuration

  1. Enable WWDG under System Core → Window Watchdog
  2. Set prescaler, counter, and window
  3. Generate code

Step 3 – HAL Usage

// Initialize WWDG

MX_WWDG_Init();

// Refresh inside main loop or ISR

while(1)

{

    // Refresh only within the allowed window

    HAL_WWDG_Refresh(&hwwdg);

}

Refresh too early or too late → MCU resets

5. Example: Using IWDG + WWDG Together

  • IWDG → Ensures reset if main loop hangs
  • WWDG → Ensures loop executes at expected timing
				
					while(1)
{
    HAL_IWDG_Refresh(&hiwdg);  // Refresh independent watchdog
    HAL_WWDG_Refresh(&hwwdg);  // Refresh window watchdog at correct interval
    // Main application tasks
}
				
			

6. Hands-On Lab

  1. Enable IWDG and WWDG in CubeMX
  2. Set IWDG timeout = 8s
  3. Implement main loop refreshing with a HAL_Delay where you can change the time
  4. Simulate hang → MCU resets automatically
  5. Monitor the RCC IWDG flag and make sure to use the macro to clean it upon reset
				
					/* USER CODE BEGIN PV */
uint32_t Time2Wait = 4000;
/* USER CODE END PV */

				
			
				
					 /* USER CODE BEGIN 2 */
 HAL_IWDG_Refresh(&hiwdg);
 /* USER CODE END 2 */

				
			
				
					/* Infinite loop */
 /* USER CODE BEGIN WHILE */
 while (1)
 {
   /* USER CODE END WHILE */
   /* USER CODE BEGIN 3 */
	  HAL_IWDG_Refresh(&hiwdg);
	  HAL_Delay(Time2Wait);
 }
 /* USER CODE END 3 */

				
			

if the HAL_WWDG_Refresh(&hwwdg); is called before the HAL_Delay(Time2Wait); it will reset the MCU due to the early refresh:

Full Source Code: hackerembedded/STM32_EP16

7. Advantages of Using Watchdogs

  • Protects against software freezes and unresponsive firmware
  • Improves system reliability in industrial and critical applications
  • IWDG: Robust against clock failures
  • WWDG: Detects timing issues early

8. Common Issues & Fixes

Issue Cause Solution
MCU resets immediately IWDG not initialized Call MX_IWDG_Init() early in main()
WWDG reset occurs too early Refresh before window opens Adjust refresh timing according to window
HAL_IWDG_Refresh fails Watchdog handle incorrect Ensure &hiwdg is used correctly
Infinite reset loop IWDG timeout too short Increase prescaler or reload value

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top