STM32 window watchdog, working process, configuration, experiment

Window Watchdog

Overview of Window Watchdog

This is called a window because the dog feeding time is within a limited range (window), and you can set the upper limit (fixed lower limit) by setting the relevant registers. Feed the dog either too early or too late.
Independent watchdog restricts feeding time to 0-x, which is determined by the relevant registers. Feed the dog too late.

Window Watchdog Working Diagram

The counter counts from the initial value to the value of the upper window and cannot feed the dog. It must feed the dog during the process from the upper window value to the value of 0x3f. 0x3f=0011 1111, so the number above 0x3f is 0100 0000, which means the t6 bit jumps from 1 to 0. If t6 jumps from 1 to 0 and the dog is still not fed, it is reset.

Window Watchdog Diagram:

Summary of the working process of the window watchdog

The window watchdog of STM32F has a 7-bit decrement counter T[6:0], which generates a watchdog reset when either of the following two conditions occurs:
When feeding a dog, if the counter value is greater than a set value W[6:0], the set value is in WWDG_CFR register definition.
When the counter value decreases from 0x40 to 0x3F [T6 bit jumps to 0].
If the watchdog is started and interrupts are allowed, an early wake-up interrupt (EWI) occurs when the decrement counter equals 0x40, which can be used to feed the dog to avoid resetting the WWDG. The interruption time should not be too long.

Window Watchdog Timeout

Why the window watchdog?

For a normal watchdog, the program can refresh the watchdog at any time before it resets, but there is a hidden danger. It is possible that the program runs out of order and returns to normal place, or that the program runs out of order and just performs the refresh watchdog operation, in which case the normal watchdog cannot be detected.
If a window watchdog is used, the programmer can set a time window to refresh the watchdog based on the normal execution time of the program, ensuring that the watchdog does not refresh in advance or lag in refreshing the watchdog. This can detect that the program is not running along the normal path and skipped some segments abnormally.

Notes for window watchdog:

The upper window value W[6:0] must be greater than the lower window value 0x40. Otherwise there would be no window.
The window watchdog clock source PCLK1 (APB1 bus clock) after crossover.

Common Register and Library Function Configuration

Control Register WWDG_CR

Configure Register WWWDG_ CFR

Status Register WWWDG_ SR

You can check this flag to see if you have reached 0x40.

WWDG Operations HAL Library Functions

HAL_WWDG_Start is the startup watchdog, HAL_WWDG_Start_IT also turns on early wake-up interrupts

HAL_StatusTypeDef   HAL_WWDG_Init(WWDG_HandleTypeDef *hwwdg);
void     HAL_WWDG_MspInit(WWDG_HandleTypeDef *hwwdg);
HAL_StatusTypeDef HAL_WWDG_Start(WWDG_HandleTypeDef *hwwdg);
HAL_StatusTypeDef HAL_WWDG_Start_IT(WWDG_HandleTypeDef *hwwdg);
HAL_StatusTypeDef HAL_WWDG_Refresh(WWDG_HandleTypeDef *hwwdg);
void     HAL_WWDG_IRQHandler(WWDG_HandleTypeDef *hwwdg);
void     HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef* hwwdg);

Configuration process of window watchdog

1. Enable window watchdog clock:
HAL_ WWDG_ In MspInit
2. Initialize the window watchdog: set the crossover factor, window value, count value, etc.
HAL_WWDG_Init();
This function also interrupts the early wake-up of the window watchdog.
3. Set the priority of early wake-up interrupt:
HAL_WWDG_MspInit
4. Write an early wake-up interrupt handler to feed the dog:
HAL_WWDG_EarlyWakeupCallback();
HAL_WWDG_Refresh();

Window Watchdog Experiment

#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "key.h"
#include "exti.h"

void HAL_WWDG_MspInit(WWDG_HandleTypeDef *hwwdg)
{
	__HAL_RCC_WWDG_CLK_ENABLE();//Enable clock
	
	HAL_NVIC_EnableIRQ(WWDG_IRQn);				//Make WWDG_IRQn Channel Break
	HAL_NVIC_SetPriority(WWDG_IRQn,3,3);			//Preemption Priority 3, Subpriority 3
}
WWDG_HandleTypeDef wwdg_handler;

void WWDG_IRQHandler(void)
{
	HAL_WWDG_IRQHandler(&wwdg_handler);

}
void HAL_WWDG_WakeupCallback(WWDG_HandleTypeDef* hwwdg)
{
	HAL_WWDG_Refresh(&wwdg_handler,0x7f);
	LED1=!LED1;
}
int main(void)
{

  HAL_Init();                     //Initialize HAL Library   
  Stm32_Clock_Init(360,25,2,8);   //Set clock, 180Mhz
  delay_init(180);                //Initialization Delay Function
  uart_init(115200);              //Initialize USART
  LED_Init();                     //Initialize LED 
	EXTI_Init();
	LED0 = 0;
	delay_ms(300);
	wwdg_handler.Instance=WWDG; 
	wwdg_handler.Init.Counter=0x7f;//initial value
	wwdg_handler.Init.Prescaler=WWDG_PRESCALER_8;
	wwdg_handler.Init.Window=0x5f;//Upper window value
	HAL_WWDG_Init(&wwdg_handler);
	
	HAL_WWDG_Start_IT(&wwdg_handler);
	
	while(1)
	{
		LED0=1;
	}
}

PCLK1=180/4=45MHZ
45MHZ/4096/8=1373hz
0x7f-0x40+1 clock=64 clocks
64/1373=0.0466s=22hz
The interrupt service function is executed after 0.0466s.
There must have been a while cycle before to turn off the LED 0 light.
After executing the interrupt service function, the LED 1 lamp reverses and refreshes the count value from 0x7f again.
LED 1 flips every 0.0466s. LED 0 only flashes once when turned on.
If the dog feeding operation in the callback function is deleted, it will be reset after 0x3f and led0 will continue to blink.
When it reaches 0x40, LED 1 flips, then goes to 0x3f, and the program resets again. Ultimately both led0 and LED1 flash, and their frequencies are similar.

Keywords: stm32

Added by noelswanson on Tue, 11 Jan 2022 19:07:48 +0200