0
0
FreeRTOSprogramming~30 mins

Critical sections and interrupt disabling in FreeRTOS - Mini Project: Build & Apply

Choose your learning style9 modes available
Critical Sections and Interrupt Disabling in FreeRTOS
📖 Scenario: You are working on a small embedded system using FreeRTOS. You need to protect a shared variable from being changed by an interrupt while your task is updating it. This is important to avoid errors caused by simultaneous access.
🎯 Goal: Build a simple FreeRTOS program that safely updates a shared variable by disabling interrupts during the update, demonstrating the use of critical sections.
📋 What You'll Learn
Create a shared variable called sharedCounter initialized to 0
Create a variable called interruptMask to hold the interrupt state
Use taskENTER_CRITICAL() and taskEXIT_CRITICAL() to protect the update of sharedCounter
Print the value of sharedCounter after the update
💡 Why This Matters
🌍 Real World
Embedded systems often have tasks and interrupts accessing the same data. Protecting shared data with critical sections prevents errors and crashes.
💼 Career
Understanding how to disable interrupts and use critical sections is essential for embedded software engineers working with real-time operating systems like FreeRTOS.
Progress0 / 4 steps
1
Create the shared variable
Create a global variable called sharedCounter and set it to 0.
FreeRTOS
Need a hint?

Use volatile to tell the compiler this variable can change unexpectedly.

2
Create the interrupt mask variable
Create a variable called interruptMask of type UBaseType_t to hold the interrupt state before disabling interrupts.
FreeRTOS
Need a hint?

This variable will store the interrupt state returned by taskENTER_CRITICAL_FROM_ISR().

3
Protect the update with critical section
Use taskENTER_CRITICAL() and taskEXIT_CRITICAL() to disable interrupts while increasing sharedCounter by 1. Use taskENTER_CRITICAL() and taskEXIT_CRITICAL() to protect the update.
FreeRTOS
Need a hint?

Wrap the update inside the critical section calls to prevent interrupts during the change.

4
Print the updated value
Print the value of sharedCounter using printf to show the result after the critical section update.
FreeRTOS
Need a hint?

Use printf with the format specifier %d to print the integer value.