2.6   Watchdog Timers

VxWorks includes a watchdog-timer mechanism that allows any C function to be connected to a specified time delay. Watchdog timers are maintained as part of the system clock ISR. Normally, functions invoked by watchdog timers execute as interrupt service code at the interrupt level of the system clock. However, if the kernel is unable to execute the function immediately for any reason (such as a previous interrupt or kernel state), the function is placed on the tExcTask work queue. Functions on the tExcTask work queue execute at the priority level of the tExcTask (usually 0). Restrictions on ISRs apply to routines connected to watchdog timers. The functions in Table 2-24 are provided by the wdLib library.

Table 2-24:  Watchdog Timer Calls


Call
Description

wdCreate( )  
Allocate and initialize a watchdog timer. 
wdDelete( )  
Terminate and deallocate a watchdog timer. 
wdStart( )  
Start a watchdog timer. 
wdCancel( )  
Cancel a currently counting watchdog timer. 

A watchdog timer is first created by calling wdCreate( ). Then the timer can be started by calling wdStart( ), which takes as arguments the number of ticks to delay, the C function to call, and an argument to be passed to that function. After the specified number of ticks have elapsed, the function is called with the specified argument. The watchdog timer can be canceled any time before the delay has elapsed by calling wdCancel( ).

Example 2-12:  Watchdog Timers

/* This example creates a watchdog timer and sets it to go off in 
 * 3 seconds. 
 */
/* includes */ #include "vxWorks.h" #include "logLib.h" #include "wdLib.h"
/* defines */ #define  SECONDS (3)
WDOG_ID myWatchDogId; task (void) { /* Create watchdog */
if ((myWatchDogId = wdCreate( )) == NULL) return (ERROR);
/* Set timer to go off in SECONDS - printing a message to stdout */
if (wdStart (myWatchDogId, sysClkRateGet( ) * SECONDS, logMsg,              "Watchdog timer just expired\n") == ERROR) return (ERROR);
/* ... */ }