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
   */
}
相关推荐
智商偏低2 小时前
单片机之helloworld
单片机·嵌入式硬件
青牛科技-Allen3 小时前
GC3910S:一款高性能双通道直流电机驱动芯片
stm32·单片机·嵌入式硬件·机器人·医疗器械·水泵、
白鱼不小白5 小时前
stm32 USART串口协议与外设(程序)——江协教程踩坑经验分享
stm32·单片机·嵌入式硬件
S,D6 小时前
MCU引脚的漏电流、灌电流、拉电流区别是什么
驱动开发·stm32·单片机·嵌入式硬件·mcu·物联网·硬件工程
芯岭技术8 小时前
PY32F002A单片机 低成本控制器解决方案,提供多种封装
单片机·嵌入式硬件
youmdt9 小时前
Arduino IDE ESP8266连接0.96寸SSD1306 IIC单色屏显示北京时间
单片机·嵌入式硬件
嘿·嘘9 小时前
第七章 STM32内部FLASH读写
stm32·单片机·嵌入式硬件
Meraki.Zhang9 小时前
【STM32实践篇】:I2C驱动编写
stm32·单片机·iic·驱动·i2c
几个几个n12 小时前
STM32-第二节-GPIO输入(按键,传感器)
单片机·嵌入式硬件
Despacito0o15 小时前
ESP32-s3摄像头驱动开发实战:从零搭建实时图像显示系统
人工智能·驱动开发·嵌入式硬件·音视频·嵌入式实时数据库