STM32: Detailed usage of independent watchdog (IWDG)

1. Basic knowledge of independent watchdog (IWDG)

1. Introduction to IWDG

  • The independent watchdog is driven by a special low-speed bus, that is, the LSI bus (clock frequency 40KHz ), which can still work in the case of a main clock failure.
  • The independent watchdog is suitable for applications that require the watchdog to work completely independently outside the main program and require low time accuracy.

2. IWDG function description

Write 0xCCCC in the key value register ( IWDG_KR ) to start the independent watchdog. At this time, the counter starts to decrement from its reset value 0xFFF, and a reset signal ( IWDG_RESET ) is generated when the counter value counts to the end value 0x000 .
Whenever you write 0xAAAA in the key value register IWDG_KR (usually referred to as feeding the dog), the value in the automatic reload register IWDG_RLR will be reloaded to the counter to avoid a watchdog reset.
If the program is abnormal, the dog cannot be fed normally and the system is reset.

3. IWDG register

The introduction of the IWDG register in the STM32 Chinese Reference Manual is as follows:
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Insert picture description here

4. IWDG timeout

The introduction of the IWDG timeout time in the STM32 Chinese Reference Manual is as follows:
Insert picture description here

  • Overflow time calculation: Tout=((4*2^ prer )* rlr )/40 (ms)
    • prer : Prescaler coefficient: 0~7 (only the lower 3 bits are valid), set by the prescaler register (IWDG_PR).
    • rlr : Reload register (IWDG_RLR) value (the lower 11 bits are valid).

Second, independent watchdog (IWDG) program implementation

1. IWDG related library functions

void IWDG_WriteAccessCmd(uint16_t IWDG_WriteAccess);//取消写保护:0x5555使能
void IWDG_SetPrescaler(uint8_t IWDG_Prescaler);	//设置预分频系数:写PR
void IWDG_SetReload(uint16_t Reload);		//设置重装载值:写RLR
void IWDG_ReloadCounter(void);			//喂狗:写0xAAAA到KR
void IWDG_Enable(void);				//使能看门狗:写0xCCCC到KR

2. IWDG control program

iwdg.h

#ifndef __IWDG_H
#define __IWDG_H	
 
#include "stm32f10x.h"

void IWDG_Init(u8 prer,u16 rlr) ;
void IWDG_Feed(void);

#endif

iwdg.c

#include "iwdg.h"

/*******************************************************************************
* 函 数 名         : IWDG_Init
* 函数功能		   : IWDG初始化
* 输    入         : pre:预分频系数(0-6)
					 rlr:重装载值(12位范围0xfff)
					 独立看门狗复位时间计算公式:t=(4*2^pre*rlr)/40 (ms)
* 输    出         : 无
*******************************************************************************/
void IWDG_Init(u8 prer,u16 rlr) 
{
    
    	
 	IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);  	//使能对寄存器IWDG_PR和IWDG_RLR的写操作,即取消写保护
	IWDG_SetPrescaler(prer);  					//设置IWDG预分频值
	IWDG_SetReload(rlr);  					//设置IWDG重装载值
	IWDG_ReloadCounter();  				//按照IWDG重装载寄存器的值重装载IWDG计数器
	IWDG_Enable();  				//使能IWDG
}

//喂独立看门狗
void IWDG_Feed(void)
{
    
       
 	IWDG_ReloadCounter();//重装载初值									   
}

main.c

#include "stm32f10x.h"

#include "iwdg.h"

int main()
{
    
    
	/**
	其它初始化省略
	**/
	IWDG_Init(IWDG_Prescaler_64,625);		//独立看门狗初始化,超时时间1s
	
	while(1)
	{
    
    
		/**
		其它程序省略
		如果其中程序的运行时间超过1s,需要在其中添加喂看门狗函数 IWDG_Feed();
		**/
		IWDG_Feed();	//喂看门狗
	}
}

Guess you like

Origin blog.csdn.net/MQ0522/article/details/111194940