Bird
Raised Fist0
FreeRTOSprogramming~10 mins

Watchdog task pattern in FreeRTOS - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Watchdog task pattern
Start Watchdog Task
↓
Wait for Notification
↓
Check if Task Responded
↓
Reset Timer
↓
Loop to Wait Again
The watchdog task waits for a signal from monitored tasks. If it receives timely signals, it resets its timer. If not, it triggers a system reset.
Execution Sample
FreeRTOS
void WatchdogTask(void *pvParameters) {
  for(;;) {
    if(xTaskNotifyWait(0, 0, NULL, pdMS_TO_TICKS(1000)) == pdTRUE) {
      ResetWatchdogTimer();
    } else {
      SystemReset();
    }
  }
}
This FreeRTOS task waits for a notification every 1000 ms; if received, it resets the watchdog timer, else it resets the system.
Execution Table
StepActionNotification Received?Watchdog Timer Reset?System Reset Triggered?
1Wait for notification (timeout 1000ms)NoNoNo
2Timeout expired without notificationNoNoYes
3System reset triggered---
4Restart after reset, begin waiting again---
5Wait for notification (timeout 1000ms)YesNoNo
6Notification received, reset watchdog timerYesYesNo
7Loop back to wait for next notification---
💡 System reset triggered when no notification is received within 1000 ms timeout
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 5After Step 6Final
Notification ReceivedFalseFalseFalseTrueTrueTrue
Watchdog Timer ResetFalseFalseFalseFalseTrueTrue
System Reset TriggeredFalseFalseTrueFalseFalseFalse
Key Moments - 3 Insights
Why does the system reset if no notification is received?
Because the watchdog task waits for a notification within a timeout (step 1 and 2). If it times out without receiving it, it triggers a system reset (step 3).
What happens when a notification is received on time?
The watchdog timer is reset (step 6), preventing system reset and allowing the task to wait again (step 7).
Why is the watchdog timer reset important?
Resetting the watchdog timer signals the system is healthy. Without it, the watchdog assumes a fault and triggers a reset (see steps 2 and 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 2?
ANotification received and watchdog timer reset
BTimeout expired without notification
CSystem reset completed
DTask waits for notification
💡 Hint
Check the 'Notification Received?' and 'System Reset Triggered?' columns at step 2
At which step does the system reset get triggered?
AStep 1
BStep 5
CStep 3
DStep 7
💡 Hint
Look at the 'System Reset Triggered?' column in the execution table
If the notification is always received on time, what will never happen?
ASystem reset triggered
BWatchdog timer reset
CWaiting for notification
DLooping back to wait again
💡 Hint
Refer to variable_tracker and execution_table rows where 'System Reset Triggered' is false when notification is true
Concept Snapshot
Watchdog Task Pattern in FreeRTOS:
- Task waits for notification with timeout
- If notification received: reset watchdog timer
- If timeout: trigger system reset
- Loop repeats indefinitely
- Ensures system health by monitoring task responsiveness
Full Transcript
The watchdog task pattern in FreeRTOS involves a dedicated task that waits for notifications from monitored tasks within a set timeout period. If the notification arrives on time, the watchdog timer is reset, indicating the system is healthy. If the notification does not arrive before the timeout expires, the watchdog task triggers a system reset to recover from potential faults. This cycle repeats indefinitely, ensuring the system remains responsive and safe. The execution table shows steps where the task waits, checks notifications, resets timers, or triggers resets. Variables track notification receipt, timer resets, and system resets. Key moments clarify why resets happen and the importance of timely notifications. The visual quiz tests understanding of these steps and outcomes.

Practice

(1/5)
1. What is the main purpose of a watchdog task in FreeRTOS?
easy
A. To create new tasks dynamically during runtime
B. To monitor other tasks and reset the system if a task fails
C. To handle user input events
D. To manage memory allocation for tasks

Solution

  1. Step 1: Understand the watchdog task role

    The watchdog task monitors the health of other tasks to ensure they are running correctly.
  2. Step 2: Identify the action taken on failure

    If a task fails or stops responding, the watchdog task can reset the system or take corrective action.
  3. Final Answer:

    To monitor other tasks and reset the system if a task fails -> Option B
  4. Quick Check:

    Watchdog monitors and resets = A [OK]
Hint: Watchdog watches tasks and resets on failure [OK]
Common Mistakes:
  • Confusing watchdog with task creation
  • Thinking watchdog handles user input
  • Assuming watchdog manages memory
2. Which of the following is the correct way to reset a watchdog timer in a FreeRTOS task?
easy
A. Call watchdog_reset() periodically from monitored tasks
B. Use xTaskCreate() to reset the timer
C. Call vTaskDelay() inside the watchdog task
D. Call vTaskDelete() on the watchdog task

Solution

  1. Step 1: Identify how watchdog timers are reset

    Monitored tasks must periodically signal the watchdog by calling a reset function like watchdog_reset().
  2. Step 2: Eliminate incorrect options

    vTaskDelay() delays a task but does not reset the watchdog timer. xTaskCreate() creates tasks, not reset timers. vTaskDelete() deletes tasks, not reset timers.
  3. Final Answer:

    Call watchdog_reset() periodically from monitored tasks -> Option A
  4. Quick Check:

    Reset watchdog by calling reset function = C [OK]
Hint: Reset watchdog by calling its reset function in tasks [OK]
Common Mistakes:
  • Using delay functions to reset watchdog
  • Trying to reset watchdog by creating or deleting tasks
  • Confusing task management functions with watchdog reset
3. Consider this simplified watchdog task code snippet in FreeRTOS:
void WatchdogTask(void *pvParameters) {
    for (;;) {
        if (xSemaphoreTake(watchdogSemaphore, pdMS_TO_TICKS(1000)) == pdFALSE) {
            // No reset signal received
            vTaskDelay(pdMS_TO_TICKS(500));
            // Take recovery action
        } else {
            // Reset signal received
            vTaskDelay(pdMS_TO_TICKS(100));
        }
    }
}

What happens if the monitored task never calls xSemaphoreGive(watchdogSemaphore)?
medium
A. The watchdog task will detect no reset signal and take recovery action repeatedly
B. The watchdog task will keep waiting forever without action
C. The watchdog task will crash due to semaphore misuse
D. The watchdog task will reset the semaphore automatically

Solution

  1. Step 1: Analyze semaphore wait behavior

    The watchdog task waits 1000 ms for a semaphore signal. If none arrives, xSemaphoreTake returns pdFALSE.
  2. Step 2: Understand the consequence of no signal

    When no signal is received, the watchdog delays 500 ms and then takes recovery action repeatedly in the loop.
  3. Final Answer:

    The watchdog task will detect no reset signal and take recovery action repeatedly -> Option A
  4. Quick Check:

    No semaphore signal triggers recovery = A [OK]
Hint: No semaphore signal means watchdog triggers recovery [OK]
Common Mistakes:
  • Assuming watchdog waits forever without action
  • Thinking semaphore misuse causes crash here
  • Believing semaphore resets automatically
4. Given this watchdog task snippet:
void WatchdogTask(void *pvParameters) {
    for (;;) {
        if (xSemaphoreTake(watchdogSemaphore, 0) == pdTRUE) {
            // Reset received
        } else {
            // No reset, take action
        }
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}

What is the main problem with this code?
medium
A. The task deletes itself after one loop
B. The task delays too long after receiving a reset signal
C. The semaphore wait time is zero, so it never blocks to wait for reset signals
D. The semaphore is given incorrectly in the monitored task

Solution

  1. Step 1: Examine semaphore wait time parameter

    The call xSemaphoreTake(watchdogSemaphore, 0) means it does not wait and returns immediately.
  2. Step 2: Understand impact on watchdog behavior

    Because it never blocks, the watchdog may miss reset signals if they arrive after the check, causing false recovery actions.
  3. Final Answer:

    The semaphore wait time is zero, so it never blocks to wait for reset signals -> Option C
  4. Quick Check:

    Zero wait means no blocking = D [OK]
Hint: Semaphore wait time zero means no blocking, causing missed signals [OK]
Common Mistakes:
  • Thinking delay length is the problem
  • Assuming semaphore is given incorrectly without evidence
  • Believing task deletes itself here
5. You want to implement a watchdog task that monitors three tasks: TaskA, TaskB, and TaskC. Each task must send a heartbeat signal every 500 ms. Which approach best ensures the watchdog detects a failure in any task within 2 seconds?
hard
A. Use one binary semaphore shared by all tasks; each task gives it every 500 ms
B. Use a single counting semaphore incremented by all tasks; watchdog resets counter every 2 seconds
C. Use three separate semaphores; watchdog waits up to 2 seconds for each semaphore in sequence
D. Use three separate semaphores; watchdog checks each semaphore with a 700 ms timeout and flags failure if any miss

Solution

  1. Step 1: Understand heartbeat timing requirements

    Each task sends a heartbeat every 500 ms. The watchdog must detect failure within 2 seconds.
  2. Step 2: Evaluate semaphore usage options

    Using separate semaphores allows independent monitoring. Checking each with a 700 ms timeout ensures missing any heartbeat triggers failure detection quickly.
  3. Step 3: Compare with other options

    One shared semaphore (Use one binary semaphore shared by all tasks; each task gives it every 500 ms) cannot distinguish which task failed. Waiting 2 seconds per semaphore (Use three separate semaphores; watchdog waits up to 2 seconds for each semaphore in sequence) delays detection. Counting semaphore (Use a single counting semaphore incremented by all tasks; watchdog resets counter every 2 seconds) loses individual task info.
  4. Final Answer:

    Use three separate semaphores; watchdog checks each semaphore with a 700 ms timeout and flags failure if any miss -> Option D
  5. Quick Check:

    Separate semaphores + short timeout = fast failure detection = B [OK]
Hint: Separate semaphores with short timeout catch failures fast [OK]
Common Mistakes:
  • Using one semaphore for all tasks losing failure detail
  • Waiting too long per semaphore delaying detection
  • Using counting semaphore losing individual task info
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard