STM32实现看门狗(HAL库)

文章目录

  • [一. 看门狗](#一. 看门狗)
    • [1. 独立看门狗(IWDG)](#1. 独立看门狗(IWDG))
      • [1.1 原理](#1.1 原理)
      • [1.2 相关配置](#1.2 相关配置)
      • [1.3 相关函数](#1.3 相关函数)
    • [2. 窗口看门狗(WWDG)](#2. 窗口看门狗(WWDG))
      • [2.1 原理](#2.1 原理)
      • [2.2 相关配置](#2.2 相关配置)
      • [2.3 相关函数](#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溢出时间计算公式:

T o u t = N p r e s c a l e r ∗ N r e l o a d f I W D G T_{out}= \frac{N_{prescaler}*N_{reload}}{f_{IWDG}} Tout=fIWDGNprescaler∗Nreload

其中 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 相关函数

初始化函数:

c 复制代码
/**
  * @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)

喂狗函数:

c 复制代码
/**
  * @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主要有五个参数:

  1. WWDG counter clock prescaler:分频系数

  2. WWDG window value : 窗口上限值,即W[6:0](即喂狗的窗口区间在0x3F ~ WWDG window value之间)

  3. WWDG free-running downcounter value:每次复位后重新装载的值,递减到窗口区间内才可以刷新。这个值必须大于0x40和WWDG window value。

  4. Early wakeup interrupt :早期唤醒中断,开启后当计数器到达0x40时会产生中断,可以在中断中进行喂狗或者函数操作。

  5. Window watchdog interrupt:窗口看门狗全局中断。在NVIC Settings中,若Early wakeup interrupt为Enable,则该中断需要打开。

2.3 相关函数

1. 初始化函数:

c 复制代码
/**
  * @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. 喂狗函数:

c 复制代码
/**
  * @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. 看门狗中断服务函数:

c 复制代码
/**
  * @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. 早期唤醒回调函数:

c 复制代码
/**
  * @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
   */
}
相关推荐
Daemon.Chen1 小时前
【STM32开发之寄存器版】(五)-窗口看门狗WWDG
stm32·单片机·嵌入式硬件
嵌入式杂谈3 小时前
STM32中断编程详解:配置外部中断和中断服务例程
stm32·单片机·嵌入式硬件
光子物联单片机4 小时前
传感器模块编程实践(三)舵机+超声波模块融合DIY智能垃圾桶模型
stm32·单片机·嵌入式硬件·mcu
嵌入式杂谈6 小时前
STM32中断编程指南:NVIC和中断优先级
stm32·单片机·嵌入式硬件
xiaobuding_QAQ7 小时前
自用Proteus(8.15)常用元器件图示和功能介绍(持续更新...)
单片机·嵌入式硬件·学习·proteus
zxfly20139 小时前
STM32的DMA技术介绍
stm32·单片机·嵌入式硬件
CV金科14 小时前
蓝桥杯—STM32G431RBT6(IIC通信--EEPROM(AT24C02)存储器进行通信)
stm32·单片机·嵌入式硬件·算法·蓝桥杯
嵌入式详谈16 小时前
基于STM32的智能风扇控制系统设计
stm32·单片机·嵌入式硬件
小小怪大梦想16 小时前
RTC实时时钟
stm32·单片机·嵌入式硬件
水饺编程20 小时前
【英特尔IA-32架构软件开发者开发手册第3卷:系统编程指南】2001年版翻译,1-2
linux·嵌入式硬件·fpga开发