[资料分享] CC2640R2f 看门狗

Jacktang   2020-2-3 18:52 楼主

在TI的官方例程中就有看门狗的用法,例如:

C:\ti\simplelink_cc2640r2_sdk_2_40_00_32\examples\rtos\CC2640R2_LAUNCHXL\drivers\watchdog

这是我的SDK目录下的看门狗工程,

因为官方已经封装好了代码,所以在我们使用 看门狗的时候,直接调用即可,

头文件

#include <ti/drivers/Watchdog.h>

函数

    Watchdog_init();

    /* Open a Watchdog driver instance */
    Watchdog_Params_init(&params);
    params.callbackFxn = (Watchdog_Callback) watchdogCallback;
    params.debugStallMode = Watchdog_DEBUG_STALL_ON;
    params.resetMode = Watchdog_RESET_ON;

    watchdogHandle = Watchdog_open(Board_WATCHDOG0, &params);
    if (watchdogHandle == NULL) {
        /* Error opening Watchdog */
        while (1) {}
    }

 

还需要一个看门狗回调函数

void watchdogCallback(uintptr_t watchdogHandle)
{
    /*
     * If the Watchdog Non-Maskable Interrupt (NMI) is called,
     * loop until the device resets. Some devices will invoke
     * this callback upon watchdog expiration while others will
     * reset. See the device specific watchdog driver documentation
     * for your device.
     */
    while (1) {}
}

以上函数就开启了看门狗,使用:

 Watchdog_clear(watchdogHandle);

清楚看门狗计数器,这种模式下的看门狗是重启的,

例如说:想让看门狗报警十次再重启设备,可以修改参数

 params.resetMode = Watchdog_RESET_ON;

typedef enum Watchdog_ResetMode_ {
    Watchdog_RESET_OFF, /*!< Timeouts generate interrupts only */
    Watchdog_RESET_ON   /*!< Generates reset after timeout */
} Watchdog_ResetMode;

修改成

params.resetMode = Watchdog_RESET_OFF;

则看门狗就成中断函数,然后在看门狗回调函数里面加上

    Watchdog_init();
    /* Create and enable a Watchdog with resets disabled */
    Watchdog_Params_init(&params);
    params.callbackFxn = (Watchdog_Callback)watchdogCallback;
    params.resetMode = Watchdog_RESET_OFF;
    watchdogHandle = Watchdog_open(Board_WATCHDOG0, &params);

//---------------看门狗回调---------------

void watchdogCallback(uintptr_t unused)
{

    /* Clear watchdog interrupt flag */
    Watchdog_clear(watchdogHandle);

    static int WDT_flag = 0;
    if(WDT_flag++ > 10){
        SystemReset();//重启函数
    }
    /* Insert timeout handling code here. */
}

回复评论

暂无评论,赶紧抢沙发吧
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复