Complete the code to create a watchdog timer with FreeRTOS API.
TimerHandle_t watchdogTimer = xTimerCreate("Watchdog", pdMS_TO_TICKS([1]), pdFALSE, NULL, watchdogCallback);
The watchdog timer is set to 5000 milliseconds (5 seconds) to monitor task health.
Complete the code to start the watchdog timer after creation.
if (xTimerStart(watchdogTimer, [1]) != pdPASS) { // Handle error }
Passing 0 as block time means the call will not block if the timer command queue is full.
Fix the error in the watchdog task function to reset the timer correctly.
void WatchdogTask(void *pvParameters) {
for (;;) {
if (xTimerReset(watchdogTimer, [1]) != pdPASS) {
// Handle reset failure
}
vTaskDelay(pdMS_TO_TICKS(1000));
}
}Using 0 as block time ensures the reset command is sent without blocking.
Fill both blanks to create a dictionary of task names and their last heartbeat times.
typedef struct {
char name[16];
TickType_t lastHeartbeat;
} TaskStatus;
TaskStatus taskStatusArray[[1]];
void updateHeartbeat(const char* taskName, TickType_t tick) {
for (int i = 0; i < [2]; i++) {
if (strcmp(taskStatusArray[i].name, taskName) == 0) {
taskStatusArray[i].lastHeartbeat = tick;
break;
}
}
}The array size and loop limit must match the number of tasks monitored, here 10.
Fill all three blanks to implement the watchdog check function that resets system on timeout.
void checkWatchdog() {
TickType_t currentTick = xTaskGetTickCount();
for (int i = 0; i < [1]; i++) {
if (currentTick - taskStatusArray[i].lastHeartbeat > [2]) {
[3]();
}
}
}The function loops over 10 tasks, checks if heartbeat is older than 5 seconds, and calls systemReset if so.