๐ Introduction #
In embedded systems, ensuring that tasks stay alive and responsive is critical.
VxWorks provides watchdog timers to detect when a task is not behaving as expected and trigger corrective action.
A watchdog works like this:
- You create and start a watchdog timer.
- If the timer is not reset (or โkickedโ) within a time period, it expires.
- The expiration runs a callback function to handle recovery.
๐งฉ Why Use Watchdog Timers? #
- Detect stalled tasks โ restart or reset them.
- Increase reliability in safety-critical applications.
- Fail-safe behavior in case of deadlocks or infinite loops.
๐ป Example: Using Watchdog Timers in VxWorks #
Weโll demonstrate:
- A task monitored by a watchdog.
- A watchdog callback that runs when the task โhangs.โ
Code Example #
#include <vxWorks.h>
#include <wdLib.h>
#include <taskLib.h>
#include <stdio.h>
WDOG_ID wdId;
int monitoredTid;
// Watchdog callback function
void wdCallback(int arg)
{
printf("Watchdog expired! Restarting monitored task...\n");
// Restart monitored task
taskRestart(monitoredTid);
}
// Task that is being monitored
void monitoredTask()
{
int counter = 0;
while (1)
{
printf("Monitored task running, counter=%d\n", counter++);
// Simulate hang condition
if (counter == 5)
{
printf("Task is stuck! Watchdog should trigger soon...\n");
while (1); // infinite loop โ hang
}
taskDelay(100); // ~1s delay
}
}
// Watchdog management task
void watchdogTask()
{
wdId = wdCreate();
if (wdId == NULL)
{
printf("Failed to create watchdog\n");
return;
}
// Spawn monitored task
monitoredTid = taskSpawn("tMon", 100, 0, 4000, (FUNCPTR)monitoredTask,
0,0,0,0,0,0,0,0,0,0);
// Periodically restart watchdog
while (1)
{
wdStart(wdId, 200, (FUNCPTR)wdCallback, 0); // expire after ~2s
printf("Watchdog started/reset\n");
taskDelay(150); // Reset before timeout (unless task hangs)
}
}
void usrAppInit(void)
{
taskSpawn("tWd", 90, 0, 4000, (FUNCPTR)watchdogTask,
0,0,0,0,0,0,0,0,0,0);
}
๐ Explanation of the Code #
-
Watchdog Setup
wdCreate()creates a watchdog object.wdStart()arms it with a timeout and callback.
-
Monitored Task
- Prints a counter until it intentionally hangs at counter = 5.
-
Watchdog Callback
- Prints a warning and restarts the hung task using
taskRestart().
- Prints a warning and restarts the hung task using
-
Watchdog Management Task
- Periodically restarts the watchdog.
- If the task hangs and watchdog isnโt reset โ callback executes.
โก What Youโll See #
Expected console output:
Monitored task running, counter=0
Watchdog started/reset
Monitored task running, counter=1
Watchdog started/reset
...
Task is stuck! Watchdog should trigger soon...
Watchdog expired! Restarting monitored task...
Monitored task running, counter=0
Watchdog started/reset
๐ Key Takeaways #
- Watchdog timers help ensure task responsiveness.
- If a task hangs, the watchdog recovers the system automatically.
- Essential for safety-critical and real-time applications.
โ Wrap-Up #
In this tutorial, you learned:
- How to create and manage watchdog timers in VxWorks.
- How to detect and recover from a hung task.
- How watchdogs improve system reliability.
In the next blog, weโll cover Task Priorities and Scheduling in VxWorks to better understand how the RTOS manages execution order.