在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(¶ms);
params.callbackFxn = (Watchdog_Callback) watchdogCallback;
params.debugStallMode = Watchdog_DEBUG_STALL_ON;
params.resetMode = Watchdog_RESET_ON;
watchdogHandle = Watchdog_open(Board_WATCHDOG0, ¶ms);
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(¶ms);
params.callbackFxn = (Watchdog_Callback)watchdogCallback;
params.resetMode = Watchdog_RESET_OFF;
watchdogHandle = Watchdog_open(Board_WATCHDOG0, ¶ms);
//---------------看门狗回调---------------
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. */
}