STM32 Getting Started Tutorial (WDG Watchdog)

Reference tutorial:[14-1] WDG watchdog_bilibili_bilibili

1. WDG (Watchdog) watchdog:

(1) The watchdog can monitor the running status of the program.When the program gets stuck or runs away due to design loopholes, hardware failures, electromagnetic interference, etc., The watchdog can reset the program in time to prevent the program from falling into a long-term strike state and ensure the reliability and safety of the system.

(2) The watchdog is essentially a timer.When the program does not perform the dog feeding (resetting the counter) operation within the specified time range, the watchdog hardware The circuit automatically generates a reset signal.

(3) STM32 has two built-in watchdogs:

①Independent watchdog (IWDG): works independently and has low requirements on time accuracy.

②Window watchdog (WWDG): The watchdog is required to function in a precise timing window.

2. Independent watchdog (IWDG):

(1) IWDG block diagram:

① There are 4 oscillation sources in the clock generation circuit, one of which is the internal 40kHz low-speed RC oscillator (LSI RC 40kHz), which is responsible for providing a clock to the independent watchdog. This clock does not need to be turned on manually by the user.

②The LSI clock enters the prescaler for division, and the division coefficient is configured by the prescaler register.

The LSI clock is pre-divided and drives the down counter to count down (the maximum value of the 12-bit counter is 4095). , when the counter value decrements to 0, the IWDG reset signal will be generated

If you do not want IWDG to generate a reset signal, then reload the counter with a value in time before the counter value decrements to 0 ( That is, "feed the dog"). This value is written into the down counter through the reload register. When to reload the down counter is controlled by the key register .

(2) IWDG key register:

①The key register is essentially a control register, used tocontrol the work of the hardware circuit.

② In the case of possible interference, generally replaces the function of writing one bit in the control register by writing a specific value in the entire key register to reduce interference to the hardware circuit. The probability.

Value written to key register

effect

0xCCCC

Enable independent watchdog

0xAAAA

The value in IWDG_RLR is reloaded into the counter (feed the dog)

0x5555

Remove write protection from IWDG_PR and IWDG_RLR

Value other than 0x5555

Enable write protection for IWDG_PR and IWDG_RLR

(3) IWDG timeout:

3. Window Watchdog (WWDG):

(1) WWDG block diagram:

①The clock source of the window watchdog is PCLK1 (36MHz), and the clock will be divided by 4096 before entering the prescaler (not shown in the figure).

WWDG_CR will decrement the counter (T0~T5 are valid counting bits, T6 is the overflow flag bit, the T6 bit is 0 during the count value decrement process, indicating overflow) and control register (WDGA bit is 1 , the window watchdog starts) combined into one , needs to satisfy that the T6 bit is 0 and the WDGA bit is 1 WWDG will generate a reset signal only under two conditions; the window watchdog does not reload the register, and needs to be reloaded (feeding the dog) directly to the down counter Just write the value in .

The earliest dog feeding time of the window watchdog is written in WWDG_CFR. When the dog feeding time is too early, the window watchdog A reset signal will be generated.

When writing to the down counter (feeding the dog), the comparator is enabled, and the comparator will compare the down counter (The moment before feeding the dog) The value of and the value of the configuration register. If the value of the down counter is larger, it means that the time for feeding the dog is too early, and the comparator outputs high level, if WDGA=1, a reset signal will be generated.

(2) WWDG working characteristics:

①When the value of the down counter T[6:0] is less than 0x40 (that is, T6=0) (The dog was not fed in time) , WWDG generates a reset.

②When the down counter T[6:0] is reloaded outside the window W[6:0] (The dog feeding time is too early), WWDG generates a reset.

③When the down counter T[6:0] is equal to 0x40, an early wake-up interrupt (EWI, also known as "pre-death interrupt") can be generated. The counter can be reloaded in the interrupt function to avoid WWDG reset (at the latest time of dog feeding) Use interruptions to feed the dog).

④Write to the WWDG_CR register regularly (feed the dog) to avoid WWDG reset.

(3) WWDG timeout time:

4. Comparison between IWDG and WWDG:

IWDG independent watchdog

WWDG window watchdog

reset

After the counter decreases to 0

After the counter T[5:0] decreases to 0, the counter is reloaded prematurely.

interrupt

none

early wakeup interrupt

clock source

LSI(40KHz)

PCLK1(36MHz)

Prescaler coefficient

4、8、32、64、128、256

1、2、4、8

counter

12 bits

6 bits (effective count)

overtime time

0.1ms~26214.4ms

113us~58.25ms

How to feed your dog

Write key register, reload fixed value RLR

Write directly to the counter and reload as much as you write.

Prevent misuse

Key registers and write protection

none

use

Work independently and have low requirements on time accuracy

Require the watchdog to function within a precise timing window

5. Independent watchdog usage examples:

(1) Connect the circuit as shown in the figure below, and make a copy of the OLED display project folder to use as a template.

(2) There are functions related to the IWDG module in the stm32f10x_iwdg.h file.

[1]IWDG_WriteAccessCmd function: Write enable control (write 0x5555 to the key register to release or enable the write protection of IWDG_PR and IWDG_RLR) .

[2]IWDG_SetPrescaler function: Set the frequency division coefficient of the prescaler (write IWDG_PR).

[3]IWDG_SetReload function: Set the reload value (write IWDG_RLR).

[4]IWDG_ReloadCounter function: Reload the down counter (write 0xAAAA in the key register, that is, feed the dog).

[5]IWDG_Enable function: Start the independent watchdog (the key register is written with 0xCCCC).

[6]IWDG_GetFlagStatus function: Get the status flag bit.

(3) Paste the following code in the main.c file, compile it, and download the program to the development board for debugging.

① Under normal circumstances, after downloading the program to the development board, the OLED screen will display "RST" once, and then it will continuously flash "FFED". If the reset button is pressed, the OLED screen will display "RST" again, and then continue to flash. "FFED" is displayed.

If you hold down the key, the program will get stuck in the infinite loop of the Key_GetNum function, The program cannot feed the dog in time, so the watchdog will generate a reset signal and set IWDGRST to 1, the program Starting from the beginning, due to the reset caused by IWDG, the OLED screen does not display "RST" but displays "IWDGRST", and then continuously flashes "FFED".

#include "stm32f10x.h"                  // Device headerCmd
#include "OLED.h"
#include "Delay.h"
#include "Key.h"

int main()
{
	OLED_Init();
Key_Init();
	
	OLED_ShowString(1,1,"IWDG TEXT");
	
	if(RCC_GetFlagStatus(RCC_FLAG_IWDGRST) == SET)  //判断IWDG是否产生复位
	{
		OLED_ShowString(2,1,"IWDGRST");  //看门狗导致的复位
		Delay_ms(500);
		OLED_ShowString(2,1,"       ");
		Delay_ms(100);
		
		RCC_ClearFlag();  //清除复位标志
	}
	else
	{
		OLED_ShowString(3,1,"RST");  //不是看门狗导致的复位
		Delay_ms(500);
		OLED_ShowString(3,1,"   ");
		Delay_ms(100);
	}
	
	//解除IWDG的写保护
	IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
	
	//配置分频系数(40MHz经过16分频为2.5MHz)
	IWDG_SetPrescaler(IWDG_Prescaler_16);
	
	//配置重装值
	IWDG_SetReload(2500 - 1);  //超时时间为1000ms
	
	//先喂一次狗(键寄存器写入0x5555以外的值,同时开启写保护)
	IWDG_ReloadCounter();
	
	//启动看门狗
	IWDG_Enable();
	
	while(1)
	{
		Key_GetNum();
		//按下按键不放,程序会卡死在Key_GetNum函数的死循环中
		
		IWDG_ReloadCounter();   //喂狗
		
		OLED_ShowString(4,1,"FFED");
		Delay_ms(400);
		OLED_ShowString(4,1,"FFED");
		Delay_ms(200);
	}
}

6. Window watchdog usage examples:

(1) Connect the circuit as shown in the figure below, and copy the project folder of the above example to use as a template.

(2) There are functions related to the WWDG module in the stm32f10x_wwdg.h file.

[1]WWDG_DeInit function: restore the default configuration.

[2]WWDG_SetPrescaler function: Configure the frequency division coefficient of the prescaler.

[3]WWDG_SetWindowValue function: Configure the window value, that is, set the earliest dog feeding time (write WWDG_CFR).

[4]WWDG_EnableIT function: Enable early wake-up interrupt.

[5]WWDG_SetCounter function: Reload the down counter (write WWDG_CR, that is, feed the dog).

[6]WWDG_Enable function: Start the window watchdog (and give an initial value to the down counter).

[7]WWDG_GetFlagStatus function: Get the flag bit.

[8]WWDG_ClearFlag function: clear the flag bit.

(3) Paste the following code in the main.c file, compile it, and download the program to the development board for debugging.

① Under normal circumstances, after downloading the program to the development board, the OLED screen will display "RST" once, and then it will continuously flash "FFED". If the reset button is pressed, the OLED screen will display "RST" again, and then continue to flash. "FFED" is displayed.

If you hold down the key, the program will get stuck in the infinite loop of the Key_GetNum function, The program cannot feed the dog in time, so the watchdog will generate a reset signal and set WWDGRST to 1, The program runs from the beginning. Due to the reset caused by WWDG, the OLED screen does not display "RST" but displays "WWDGRST", and then continuously flashes "FFED".

③ Modify the delay function parameters in the while loop, so that the dog feeding time is earlier than 30ms, then the reset will occur after feeding the dog The signal is concatenated WWDGRST to 1, and the program starts from the beginning. Due to the reset caused by WWDG, the OLED screen does not display "RST" but displays "WWDGRST" , and then flashes "FFED".

#include "stm32f10x.h"                  // Device headerCmd
#include "OLED.h"
#include "Delay.h"
#include "Key.h"

int main()
{
	OLED_Init();
	Key_Init();
	
	OLED_ShowString(1,1,"WWDG TEXT");
	
	if(RCC_GetFlagStatus(RCC_FLAG_WWDGRST) == SET)  //判断WWDG是否产生复位
	{
		OLED_ShowString(2,1,"WWDGRST");  //看门狗导致的复位
		Delay_ms(500);
		OLED_ShowString(2,1,"       ");
		Delay_ms(100);
		
		RCC_ClearFlag();  //清除复位标志
	}
	else
	{
		OLED_ShowString(3,1,"RST");  //不是看门狗导致的复位
		Delay_ms(500);
		OLED_ShowString(3,1,"   ");
		Delay_ms(100);
	}
	
	//开启WWDG时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE);
	
	//配置分频系数(36MHz经过8×4096分频为1098.63Hz)
	WWDG_SetPrescaler(WWDG_Prescaler_8);
	
	//配置窗口值(这个值多多少少会有误差,但不大)
	WWDG_SetWindowValue(21|0x40);  //最早喂狗时间为30ms,过早喂狗会复位(0x40的作用是置W6为1,21由公式计算而得)
	
	//启动看门狗(这个值多多少少会有误差,但不大)
	WWDG_Enable(54|0x40);          //最晚喂狗时间为50ms,过晚喂狗会复位(0x40的作用是置T6为1,54由公式计算而得)
	
	while(1)
	{
		Key_GetNum();
		//按下按键不放,程序会卡死在Key_GetNum函数的死循环中
		
		OLED_ShowString(4,1,"FFED");
		Delay_ms(20);
		OLED_ShowString(4,1,"    ");
		Delay_ms(20);
		
		WWDG_SetCounter(54|0x40);   //喂狗(0x40的作用是置T6为1)
	}
}

Guess you like

Origin blog.csdn.net/Zevalin/article/details/134804944