In TI's official routines, there is a usage of watchdog, for example:
C:\ti\simplelink_cc2640r2_sdk_2_40_00_32\examples\rtos\CC2640R2_LAUNCHXL\drivers\watchdog
This is the watchdog project in my SDK directory.
Because the official code has been packaged, when we use the watchdog, we can call it directly.
Header Files
#include <ti/drivers/Watchdog.h>
function
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) {}
}
A watchdog callback function is also required
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) {}
}
The above function turns on the watchdog, use:
Watchdog_clear(watchdogHandle);
Clear the watchdog counter. The watchdog in this mode is restarted.
For example, if you want the watchdog to alarm ten times before restarting the device, you can modify the parameters
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;
Modified to
params.resetMode = Watchdog_RESET_OFF;
Then the watchdog becomes an interrupt function, and then add it to the watchdog callback function
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);
//---------------Watchdog callback---------------
void watchdogCallback(uintptr_t unused)
{
/* Clear watchdog interrupt flag */
Watchdog_clear(watchdogHandle);
static int WDT_flag = 0;
if(WDT_flag++ > 10){
SystemReset();//Restart function
}
/* Insert timeout handling code here. */
}
|