0
0
FreeRTOSprogramming~30 mins

Common RTOS bugs and debugging strategies in FreeRTOS - Mini Project: Build & Apply

Choose your learning style9 modes available
Common RTOS Bugs and Debugging Strategies
📖 Scenario: You are working on a small embedded system using FreeRTOS. Your system has multiple tasks running concurrently, sharing resources like variables and peripherals. Sometimes, the system behaves unexpectedly, such as tasks not running or data getting corrupted.Understanding common bugs in RTOS and how to debug them is important to make your system reliable.
🎯 Goal: Build a simple FreeRTOS program with two tasks sharing a variable. You will add a configuration to protect the shared variable, implement the core logic to update it safely, and finally print the result to observe correct behavior.
📋 What You'll Learn
Create two FreeRTOS tasks named Task1 and Task2
Use a shared variable called sharedCounter initialized to 0
Add a mutex called xMutex to protect access to sharedCounter
In each task, increment sharedCounter 5 times safely using the mutex
Print the final value of sharedCounter after both tasks complete
💡 Why This Matters
🌍 Real World
Embedded systems often run multiple tasks that share resources. Bugs like race conditions or deadlocks can cause system crashes or wrong data. Using mutexes and debugging carefully helps build reliable devices.
💼 Career
Understanding RTOS bugs and debugging is essential for embedded software engineers working on IoT devices, automotive systems, medical devices, and industrial controllers.
Progress0 / 4 steps
1
DATA SETUP: Create shared variable and tasks
Create a global integer variable called sharedCounter and set it to 0. Then create two FreeRTOS tasks named Task1 and Task2 with empty task functions.
FreeRTOS
Need a hint?

Define sharedCounter as a global variable. Create two task functions Task1 and Task2 with infinite loops. Use xTaskCreate to create the tasks in main.

2
CONFIGURATION: Add a mutex to protect shared variable
Create a mutex handle called xMutex using SemaphoreHandle_t. Initialize xMutex by calling xSemaphoreCreateMutex() in main before starting the scheduler.
FreeRTOS
Need a hint?

Include semphr.h. Declare xMutex as SemaphoreHandle_t. Initialize it in main before starting the scheduler.

3
CORE LOGIC: Increment sharedCounter safely using mutex
In both Task1 and Task2, write a loop that increments sharedCounter 5 times. Use xSemaphoreTake(xMutex, portMAX_DELAY) before incrementing and xSemaphoreGive(xMutex) after incrementing to protect the shared variable. Add a small delay of 100 ms between increments.
FreeRTOS
Need a hint?

Use a for loop to increment sharedCounter 5 times in each task. Protect the increment with xSemaphoreTake and xSemaphoreGive. Add a delay of 100 ms between increments.

4
OUTPUT: Print the final value of sharedCounter
After starting the scheduler in main, add a task called PrintTask that waits for both Task1 and Task2 to finish, then prints the final value of sharedCounter using printf. The expected final value is 10.
FreeRTOS
Need a hint?

Create a PrintTask that waits long enough for the other tasks to finish, then prints sharedCounter using printf. The expected output is Final sharedCounter value: 10.