0
0
FreeRTOSprogramming~5 mins

Watchdog task pattern in FreeRTOS

Choose your learning style9 modes available
Introduction

A watchdog task helps keep your system safe by checking if other tasks are working properly. If something goes wrong, it can restart or fix the problem.

When you want to make sure your program does not freeze or get stuck.
When you have multiple tasks and want to monitor their health.
When you want to automatically recover from errors without manual intervention.
Syntax
FreeRTOS
void WatchdogTask(void *pvParameters) {
    for (;;) {
        // Check other tasks' status
        if (tasksAreHealthy()) {
            // Reset watchdog timer
            ResetWatchdogTimer();
        } else {
            // Take recovery action
            HandleError();
        }
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}

The watchdog task runs in an infinite loop to continuously monitor other tasks.

Use vTaskDelay() to wait between checks without blocking the system.

Examples
This example checks tasks every 500 milliseconds and restarts the system if a problem is found.
FreeRTOS
void WatchdogTask(void *pvParameters) {
    for (;;) {
        if (AllTasksResponding()) {
            ResetWatchdogTimer();
        } else {
            RestartSystem();
        }
        vTaskDelay(pdMS_TO_TICKS(500));
    }
}
This version logs errors and tries to recover instead of restarting immediately.
FreeRTOS
void WatchdogTask(void *pvParameters) {
    for (;;) {
        if (CheckTaskStatus()) {
            ResetWatchdogTimer();
        } else {
            LogError();
            AttemptRecovery();
        }
        vTaskDelay(pdMS_TO_TICKS(2000));
    }
}
Sample Program

This program simulates a watchdog task checking other tasks. It prints a message when resetting the watchdog timer or when an error is detected. The main function runs the check 7 times to show both normal and error cases.

FreeRTOS
#include "FreeRTOS.h"
#include "task.h"
#include <stdio.h>

// Simulated function to check if tasks are healthy
int tasksAreHealthy() {
    static int count = 0;
    count++;
    // Simulate a failure every 5 checks
    return (count % 5) != 0;
}

void ResetWatchdogTimer() {
    printf("Watchdog timer reset.\n");
}

void HandleError() {
    printf("Error detected! Taking recovery action.\n");
}

void WatchdogTask(void *pvParameters) {
    for (;;) {
        if (tasksAreHealthy()) {
            ResetWatchdogTimer();
        } else {
            HandleError();
        }
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}

int main() {
    // Normally FreeRTOS scheduler would be started here
    // For demonstration, run WatchdogTask loop 7 times manually
    for (int i = 0; i < 7; i++) {
        if (tasksAreHealthy()) {
            ResetWatchdogTimer();
        } else {
            HandleError();
        }
    }
    return 0;
}
OutputSuccess
Important Notes

Make sure the watchdog task runs with enough priority to check other tasks regularly.

Use simple and fast checks inside the watchdog task to avoid delays.

Resetting the watchdog timer usually prevents the system from resetting.

Summary

The watchdog task pattern helps keep your system running smoothly by monitoring other tasks.

It runs in a loop, checking tasks and resetting a timer or taking action if something is wrong.

This pattern improves system reliability and helps recover from errors automatically.