Arduino for PKE8720DF-C13-F10——Watchdog:Simple WDT

Watchdog简介

Watchdog Timer(WDT)是一种硬件定时器,用于检测软件故障的发生,然后在编程周期到期时自动生成系统重置或 Watchdog 中断。

芯片在执行某些任务时可能会发生卡住的情况,或者有可能会陷入无限循环的混乱情况。正常的故障排除方法是按下重置按钮并跳出无限循环。但是,实际上不可能一直都按下按钮,因此,嵌入在 MCU 内的 Watchdog timer 将有助于解决这种情况。

AmebaWatchdog example "Watchdog Timer" —— set 5s timer,reset system when timeout

在这个例子中,我们创建了 2 个任务,其中包含一些重复运行的循环,一个是 “Small_Task”,另一个是 “Big_Task”。我们启用 watchdog timer 加载的初始值为 5 秒,大于 “Small_Task” 中的总延迟,但小于 “Big_Task” 中的总延迟。
对于成功的情况,watchdog 在 5 秒内刷新;对于失败的情况,循环正在处理中,并且 watchdog 在 5 秒后没有刷新,就会触发 watchdog(watchdog barks),中断生成并且 reset system。

Arduino IDE 配置方法已经在之前的文章中进行了详细的介绍:使用PKE8720DF-C13-F10实现Arduino example——Button_deer_vickey的博客-CSDN博客

1)所需材料

  • 1 × PKE8720DF-C13-F10开发板
  • 1 × USB转type-C数据线 

2)代码解析

本例使用 WDT Library 定义的相关函数实现,具体内容可以在ambd_arduino源代码中查询,github下载地址:GitHub - ambiot/ambd_arduino: AmebaD Arduino third-party package SDK 

include file and global variables

  • 包含 WDT.h 头文件,使用在 WDT.cpp 中定义的类 WDT:
#include "WDT.h"

WDT wdt;

Small_Task() and Big_Task() 

  • Small_Task() 中包含一个 dummy task,使用 nop 指令来实现延时,循环结束后 refresh watchdog
  • Big_Task() 是把 dummy task 循环 10 次,循环结束后 refresh watchdog
    void Small_Task(void)
    {
        Serial.println("......doing small task......");
        for (int i = 0; i < 50000000; i++)   // dummy task
        {
            asm(" nop");
        }
        Serial.println("Small_Task finished refresh watchdog.");
        wdt.RefreshWatchdog();
    }

    /* If Big_Task unable to reach #10, watchdog barks. */
    void Big_Task(void)
    {
        Serial.println("......doing big task, up to 10......");
        for (int i = 1; i <= 10; i++)
        {
            Serial.print("doing dummy task #");
            Serial.println(i, DEC);

            for (int j = 0; j < 50000000; j++)    // dummy task
            {
                asm(" nop");
            }
        }
        Serial.println("Big_Task finished refresh watchdog.");
        wdt.RefreshWatchdog();
    }

setup() 

  • 先把串口的波特率设置成 115200,方便观察输出
  • 调用 InitWatchdog() 把 watchdog timer 设为 5s,使用 StartWatchdog() 来 enable timer
  • 之后依次调用 Small_Task() 和 Big_Task()
    Serial.begin(115200);
    wdt.InitWatchdog(5000);  // setup 5s watchdog

    wdt.StartWatchdog();  // enable watchdog timer
    Small_Task();
    Big_Task();

3)实验结果

Upload image 之后,直接在 Arduino IDE 的 Serial Monitor 中观察输出的 log。可以看到 Small_Task() 在 5 秒内结束,可以成功刷新 watchdog;而 Big_Task() 在 5 秒内循环还没有结束,无法刷新 watchdog,在 watchdog timer 超时后会 reset 系统。打印出 #calibration_ok 之后重新开始执行 setup() 函数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值