0
0
FreeRTOSprogramming~30 mins

Why design patterns ensure reliable multi-tasking in FreeRTOS - See It in Action

Choose your learning style9 modes available
Why design patterns ensure reliable multi-tasking
📖 Scenario: You are working on a small embedded system using FreeRTOS. You want to understand how design patterns help make multi-tasking reliable and safe. This project will guide you through creating tasks, using synchronization, and showing how design patterns prevent problems like data corruption.
🎯 Goal: Build a simple FreeRTOS program with two tasks sharing a variable safely using a mutex. This shows how design patterns like mutual exclusion ensure reliable multi-tasking.
📋 What You'll Learn
Create two FreeRTOS tasks named Task1 and Task2
Create a shared integer variable called sharedCounter initialized to 0
Create a mutex called xMutex to protect access to sharedCounter
In each task, safely increment sharedCounter 5 times using the mutex
Print the value of sharedCounter after each increment
Use FreeRTOS API functions like xTaskCreate, xSemaphoreCreateMutex, xSemaphoreTake, and xSemaphoreGive
💡 Why This Matters
🌍 Real World
Embedded systems often run multiple tasks that share resources. Using design patterns like mutexes prevents bugs and crashes.
💼 Career
Understanding synchronization and design patterns is essential for embedded software engineers working with real-time operating systems like FreeRTOS.
Progress0 / 4 steps
1
Create shared variable and include FreeRTOS headers
Write code to include FreeRTOS.h and task.h. Then create a global integer variable called sharedCounter and set it to 0.
FreeRTOS
Need a hint?

Remember to include the FreeRTOS headers first. Then declare sharedCounter as a global variable initialized to zero.

2
Create a mutex to protect sharedCounter
Create a mutex handle variable called xMutex of type SemaphoreHandle_t. Then create the mutex using xSemaphoreCreateMutex() and assign it to xMutex.
FreeRTOS
Need a hint?

Use SemaphoreHandle_t to declare the mutex variable. Then call xSemaphoreCreateMutex() to create it.

3
Create two tasks that safely increment sharedCounter
Write two task functions named Task1 and Task2. Each task should loop 5 times. Inside the loop, use xSemaphoreTake(xMutex, portMAX_DELAY) to lock the mutex, increment sharedCounter by 1, print the value using printf, then release the mutex with xSemaphoreGive(xMutex). Add a small delay of 100ms between increments using vTaskDelay(pdMS_TO_TICKS(100)).
FreeRTOS
Need a hint?

Each task must lock the mutex before changing sharedCounter and unlock it after. Use printf to show the current value.

4
Create tasks and start the scheduler
In main(), call setupMutex(). Then create Task1 and Task2 using xTaskCreate with stack size 1000 and priority 1. Finally, start the FreeRTOS scheduler with vTaskStartScheduler(). Print "Scheduler started" before starting the scheduler.
FreeRTOS
Need a hint?

Call setupMutex() first. Then create both tasks with xTaskCreate. Finally, start the scheduler with vTaskStartScheduler().