 
 
 
文章目录
- 一. 看门狗
- 1. 独立看门狗(IWDG)
- 1.1 原理
- 1.2 相关配置
- 1.3 相关函数
 
- 2. 窗口看门狗(WWDG)
- 2.1 原理
- 2.2 相关配置
- 2.3 相关函数
 
 
一. 看门狗
 
 
单片机在日常工作中常常会因为用户配置代码出现BUG,而导致芯片无法正常工作;或者会受到来自外界电磁场的干扰,造成程序跑飞 ,或陷入死循环 ,如果无法系统复位,那么整个系统都会卡死,这对产品的使用是灾难性的后果。
出于对单片机运行状态进行实时监测的考虑,产生了一种专门用于监测单片机程序运行状态的模块或者芯片,俗称看门狗(WatchDog) 。
在STM32中,看门狗分为独立看门狗(IWDG)还有窗口看门狗(WWDG) ,二者的主要区别如下:
| 独立看门狗 IWDG | 窗口看门狗 WWDG | |
|---|---|---|
| 时钟源 | 独立时钟LSI (40KZ低速时钟) | PCLK1 时钟 | 
| 中断 | 没有中断,超时直接复位 | 超时产生中断,可做复位前函数操作或重新喂狗 | 
| 复位条件 | 递减计数到0 | 窗口期外或者递减到0x3F | 
| 计数器位数 | 12位 (最大计数范围4096-0) | 6位(最大计数范围127-63) | 
| 应用场合 | 防止程序跑飞、死循环 | 检测程序时效,防止软件异常 | 
1. 独立看门狗(IWDG)
1.1 原理
低速时钟 LSI经过预分频器 分频后用作驱动12 位的递减计数器,当递减至重装载值时,若还没有及时喂狗,系统就会产生一个复位信号,CPU收到复位信号,系统复位重新运行。具体步骤可以参考下面的框图。
独立看门狗(IWDG)由专用的低速时钟(LSI)驱动,即使主时钟发生故障它仍有效。额外注意的是,独立看门狗由 V D D V_{DD} VDD电压域供电,即使MCU在停止模式和待机模式下依然工作,除非使用RTC唤醒后喂狗,否则STM32进入低功耗模式后会因为没有及时喂狗而被唤醒。
 
 
1.2 相关配置
HAL库使用独立看门狗非常简单,使用STM32CubeMX配置IWDG只有三个参数:
 · IWDG counter clock prescaler:分频系数
 · IWDG down-counter reload value:重装载值
 · IWDG window value : 窗口值(默认不修改。当计数器的值大于窗口值时,如果执行重载操作,则会产生复位)
 
 
IWDG溢出时间计算公式:
其中 N p r e s c a l e r N_{prescaler} Nprescaler是分频系数, N r e l o a d N_{reload} Nreload为重装载值, f I W D G f_{IWDG} fIWDG为低速时钟源的频率。
1.3 相关函数
初始化函数:
/**
  * @brief  Initialize the IWDG according to the specified parameters in the
  *         IWDG_InitTypeDef and start watchdog. Before exiting function,
  *         watchdog is refreshed in order to have correct time base.
  * @param  hiwdg  pointer to a IWDG_HandleTypeDef structure that contains
  *                the configuration information for the specified IWDG module.
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
喂狗函数:
/**
  * @brief  Refresh the IWDG.
  * @param  hiwdg  pointer to a IWDG_HandleTypeDef structure that contains
  *                the configuration information for the specified IWDG module.
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
2. 窗口看门狗(WWDG)
2.1 原理
窗口看门狗的流程框图如下图所示:
 
 
窗口看门狗的的时钟信号来自PCLK1,然后经过看门狗预分频器进行分频处理。
看门狗控制寄存器(WWDG_CR)为8位寄存器,其中首位为使能位,用于开启看门狗。后面为T[6:0] 的值。寄存器后六位为递减计数器(CNT),由分频后的时钟信号进行驱动。
看门狗配置寄存器(WWDG_CFR),寄存器的值为窗口上限值,当看门狗控制寄存器的值W6大于看门狗配置寄存器的值T6,则说明已经进入窗口期(比较器输出0),否则为非窗口期(比较器输出1)。
图中三个逻辑门电路(A、B、C)的作用分别如下:
A: 与门,输出1的条件是看门狗被使能的同时B输出为1。
B: 或门,输出1的条件要么是递减计数器减至窗口下限值(T6为0),要么是c门输出1。
C: 与门,输出1的条件是递减计数器减至窗口上限值的同时被喂狗(即WWDG_CR被写入)。
 
 
相比于独立看门狗,窗口看门狗(WWDG)是一个既能产生系统复位信号和提前唤醒中断的六位递减计数器。
  (1)复位的条件: 1.当递减计数器值从0x40递减到0x3F时复位(即T6位跳变到0)
             2. 计数器的值大于W[6:0]值时喂狗会复位。
  (2)中断的条件: 1.当递减计数器等于0x40时可产生提前唤醒中断 (EWI)。
  (3)喂狗条件 : 需要在窗口期(0x3F<窗口期< W[6:0])重装载计数器的值,才能防止复位。
2.2 相关配置
 
 
使用STM32CubeMX配置WWDG主要有五个参数:
-  WWDG counter clock prescaler:分频系数 
-  WWDG window value : 窗口上限值,即W[6:0](即喂狗的窗口区间在0x3F ~ WWDG window value之间) 
-  WWDG free-running downcounter value:每次复位后重新装载的值,递减到窗口区间内才可以刷新。这个值必须大于0x40和WWDG window value。 
-  Early wakeup interrupt :早期唤醒中断,开启后当计数器到达0x40时会产生中断,可以在中断中进行喂狗或者函数操作。 
-  Window watchdog interrupt:窗口看门狗全局中断。在NVIC Settings中,若Early wakeup interrupt为Enable,则该中断需要打开。 
2.3 相关函数
1. 初始化函数:
/**
  * @brief  Initialize the WWDG according to the specified.
  *         parameters in the WWDG_InitTypeDef of  associated handle.
  * @param  hwwdg  pointer to a WWDG_HandleTypeDef structure that contains
  *                the configuration information for the specified WWDG module.
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_WWDG_Init(WWDG_HandleTypeDef *hwwdg)
2. 喂狗函数:
/**
  * @brief  Refresh the WWDG.
  * @param  hwwdg  pointer to a WWDG_HandleTypeDef structure that contains
  *                the configuration information for the specified WWDG module.
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_WWDG_Refresh(WWDG_HandleTypeDef *hwwdg)
3. 看门狗中断服务函数:
/**
  * @brief  Handle WWDG interrupt request.
  * @note   The Early Wakeup Interrupt (EWI) can be used if specific safety operations
  *         or data logging must be performed before the actual reset is generated.
  *         The EWI interrupt is enabled by calling HAL_WWDG_Init function with
  *         EWIMode set to WWDG_EWI_ENABLE.
  *         When the downcounter reaches the value 0x40, and EWI interrupt is
  *         generated and the corresponding Interrupt Service Routine (ISR) can
  *         be used to trigger specific actions (such as communications or data
  *         logging), before resetting the device.
  * @param  hwwdg  pointer to a WWDG_HandleTypeDef structure that contains
  *                the configuration information for the specified WWDG module.
  * @retval None
  */
void HAL_WWDG_IRQHandler(WWDG_HandleTypeDef *hwwdg)
4. 早期唤醒回调函数:
/**
  * @brief  WWDG Early Wakeup callback.
  * @param  hwwdg  pointer to a WWDG_HandleTypeDef structure that contains
  *                the configuration information for the specified WWDG module.
  * @retval None
  */
__weak void HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef *hwwdg)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(hwwdg);
  /* NOTE: This function should not be modified, when the callback is needed,
           the HAL_WWDG_EarlyWakeupCallback could be implemented in the user file
   */
}


















