Watchdog 看门狗

一.介绍

WatchDog是为了能够防止程序跑飞而使用的一种硬件模块。如果你的程序没有跑飞,那么你的程序会定时的去喂看门狗;如果你的程序跑飞了,那么就不会再去喂狗了,如果超过了喂狗的时间, 那么狗就会自己生成一个信号来重新reset你的CPU,让程序重新开始。 这是一种在很重要的情况下防止系统跑飞的一种方法。

二.Watchdog Timer

The Watchdog Timer (WDOG) protects against system failures by providing a method by which to escape from unexpected events or programming errors.

看门狗定时器(WDOG)通过提供一种方法来避免意外事件或编程错误,从而防止系统故障。

Once the WDOG is activated, it must be serviced by the software on a periodic basis. If servicing does not take place, the timer times out. Upon timeout, the WDOG asserts the internal system reset signal, WDOG_RESET_B_DEB to the System Reset Controller (SRC).

一旦激活了WDOG,就必须由软件定期对其进行服务。如果不进行维修,计时器将超时。超时时,WDOG向系统复位控制器(SRC)断言内部系统复位信号WDOG_RESET_B_DEB。

1.Clocks

 

 2.Timeout event

The WDOG provides timeout periods from 0.5 to 128 seconds with a time resolution of 0.5 seconds.

WDOG提供0.5秒到128秒的超时周期,时间分辨率为0.5秒。

The user can determine the timeout period by writing to the WDOG timeout field (WT[7:0]) in the Watchdog Control Register (WDOG_WCR). The WDOG must be enabled by setting the WDE bit of Watchdog Control Register (WDOG_WCR) for the timeout counter to start running. After the WDOG is enabled, the counter is activated, loads the timeout value and begins to count down from this programmed value. The timer will time out when the counter reaches zero and the WDOG outputs a system reset signal, WDOG_RESET_B_DEB and asserts WDOG_B (WDT bit should be set in Watchdog Control Register (WDOG_WCR)).

用户可以通过写入看门狗控制寄存器(WDOG_WCR)中的WDOG超时字段(WT[7:0])来确定超时周期。必须通过设置看门狗控制寄存器(WDOG_WCR)的WDE位来启用WDOG,超时计数器才能开始运行。启用WDOG后,计数器被激活,加载超时值并从该编程值开始倒计时。计时器将在计数器达到零并且WDOG输出系统复位信号时超时,WDOG_RESET_B_DEB并断言WDOG_B(WDT位应在看门狗控制寄存器(WDOG_WCR)中设置)。

3.Servicing WDOG to reload the counter

To reload a timeout value to the counter the proper service sequence begins by writing 0x_5555 followed by 0x_AAAA to the Watchdog Service Register (WDOG_WSR). Any number of instructions can be executed between the two writes. If the WDOG_WSR is not loaded with 0x_5555 prior to writing 0x_AAAA to the WDOG_WSR, the counter is not reloaded. If any value other than 0x_AAAA is written to the WDOG_WSR after 0x_5555, the counter is not reloaded. This service sequence will reload the counter with the timeout value WT[7:0] of Watchdog Control Register (WDOG_WCR). The timeout value can be changed at any point; it is reloaded when WDOG is serviced by the core.

要将超时值重新加载到计数器,正确的服务序列首先写入0x_5555,然后写入0x_AAAA到看门狗服务寄存器(WDOG_WSR)。两次写入之间可以执行任意数量的指令。如果WDOG_WSR在写入0x_AAA之前,没有加载0x_5555到WDOG_WSR ,则不会重新加载计数器。如果在写入0x_5555后,写了0x_AAAA以外的任何值写入WDOG_WSR,则不会重新加载计数器。此服务序列将使用看门狗控制寄存器(WDOG_WCR)的超时值WT[7:0]重新加载计数器。超时值可以在任何时候更改;当WDOG由内核提供服务时,它会被重新加载。

三、Watchdog Timer 重点寄存器

1.Watchdog Control Register (WDOGx_WCR)

 

2.Watchdog Service Register (WDOGx_WSR)


 

3. SRC Control Register (SRC_SCR)

四.看门狗复位系统 

1.编程思路

a.使能看门狗时钟

b.设置复位信号产生后以冷启动方式复位

c.关闭看门狗控制器

d.设置看门狗定时器定时时间

e.使能看门狗

f.进行喂狗服务,重置看门狗定时器

g.不喂狗看门狗定时器超时,产生复位信号,复位开发板

2.代码实现

#include "wdog.h"

//初始化wdog
void wdog_init(){
    //使能看门狗时钟 CCGR3 [CG8]
    CCM->CCGR3 |= (0x3 <<16); 
    
    /*
    设置复位信号产生后以冷启动方式复位
    WARM reset enable bit. WARM reset will be enabled only if warm_reset_enable bit is set. 
    Otherwise all WARM reset sources will generate COLD reset.
    0 WARM reset disabled. 冷启动
    1 WARM reset enabled. 热启动
    */
    SRC->SCR &= ~(0x1 <<0);
    
    /*
    WDOGx_WCR     
    */
    //关闭看门狗控制器 WDE [2] 0不使能,1使能
    WDOG1->WCR &= ~(0x1 <<2);
    //设置看门狗定时器定时时间 WT [15–8]
    WDOG1->WCR &= ~(0xff <<8);
    WDOG1->WCR |= (0x9 <<8);//5s
    //使能看门狗
    WDOG1->WCR |= (0x1 <<2);
}

//进行喂狗服务,重置看门狗定时器
void wdog_servering(){
    /*
    WDOGx_WSR
    <1> 0x5555
    ......
    <2> 0xAAAA
    */
    WDOG1->WSR = 0x5555;
    uart_printf("servering watchdog......\r\n");
    WDOG1->WSR = 0xAAAA;
}

//不喂狗看门狗定时器超时,产生复位信号,复位开发板
void wdog_test(){
    wdog_init();
    int i;
    for(i=0;i<10;i++){
        wdog_servering();
        uart_printf("%3d",i);
        gpt_delay_seconds(1);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值