0
0
FreeRTOSprogramming~30 mins

Watchdog task pattern in FreeRTOS - Mini Project: Build & Apply

Choose your learning style9 modes available
Watchdog Task Pattern in FreeRTOS
📖 Scenario: You are building a simple embedded system using FreeRTOS. To keep the system reliable, you want to create a watchdog task that monitors if another task is running properly by checking a shared flag.
🎯 Goal: Build a FreeRTOS program with two tasks: a workerTask that sets a flag periodically, and a watchdogTask that checks this flag to detect if the worker is alive. If the worker stops setting the flag, the watchdog will print a warning message.
📋 What You'll Learn
Create a global volatile flag variable called workerAlive initialized to 0
Create a workerTask that sets workerAlive to 1 every 1000 milliseconds
Create a watchdogTask that checks workerAlive every 1500 milliseconds
If workerAlive is 1, the watchdog resets it to 0 and prints "Worker is alive"
If workerAlive is 0, the watchdog prints "Warning: Worker not responding"
💡 Why This Matters
🌍 Real World
Watchdog tasks are used in embedded systems to detect if critical tasks stop working and to trigger recovery actions.
💼 Career
Understanding watchdog patterns is important for embedded software engineers working on reliable real-time systems.
Progress0 / 4 steps
1
Create the shared flag variable
Create a global volatile integer variable called workerAlive and initialize it to 0.
FreeRTOS
Need a hint?

Use volatile int workerAlive = 0; to declare the flag.

2
Create the workerTask function
Write a function called workerTask that runs an infinite loop. Inside the loop, set workerAlive to 1, then call vTaskDelay(pdMS_TO_TICKS(1000)) to wait 1000 milliseconds.
FreeRTOS
Need a hint?

Use an infinite for (;;) loop and set workerAlive = 1; inside it.

3
Create the watchdogTask function
Write a function called watchdogTask that runs an infinite loop. Inside the loop, check if workerAlive is 1. If yes, print "Worker is alive" and reset workerAlive to 0. If no, print "Warning: Worker not responding". Then call vTaskDelay(pdMS_TO_TICKS(1500)) to wait 1500 milliseconds.
FreeRTOS
Need a hint?

Use if (workerAlive == 1) to check the flag and printf to print messages.

4
Create tasks and start scheduler
In the main function, create the workerTask and watchdogTask using xTaskCreate. Use stack size 1000, priority 1, and no parameters. Then start the scheduler with vTaskStartScheduler(). Finally, print "Scheduler started" before starting the scheduler.
FreeRTOS
Need a hint?

Use xTaskCreate to create tasks and vTaskStartScheduler() to start FreeRTOS.