📖 Scenario: You are building a simple embedded system using FreeRTOS. Your system needs to wait for certain events to happen before proceeding. These events are represented by bits in an event group.For example, a sensor might set bit 0 when data is ready, and a button might set bit 1 when pressed. Your task is to wait for these bits using AND or OR logic.
🎯 Goal: Create a FreeRTOS program that waits for specific event bits using both AND and OR conditions. You will set up an event group, define which bits to wait for, and then wait for those bits using the correct FreeRTOS API calls.
📋 What You'll Learn
Create an event group called eventGroup.
Define two event bits: BIT_0 as 0x01 and BIT_1 as 0x02.
Create a variable waitBits to specify which bits to wait for.
Use xEventGroupWaitBits to wait for bits with AND logic.
Use xEventGroupWaitBits to wait for bits with OR logic.
Print the result of waiting for bits.
💡 Why This Matters
🌍 Real World
Waiting for event bits is common in embedded systems where multiple sensors or inputs signal events. Using event groups helps tasks synchronize efficiently.
💼 Career
Understanding event groups and bitwise operations is important for embedded software engineers working with real-time operating systems like FreeRTOS.
Progress0 / 4 steps
1
Create the event group and define event bits
Create an event group called eventGroup using xEventGroupCreate(). Define two event bits: BIT_0 with value 0x01 and BIT_1 with value 0x02.
FreeRTOS
Hint
Use xEventGroupCreate() to create the event group. Use #define to define the bits.
2
Set the bits to wait for using a config variable
Create a variable called waitBits and set it to wait for both BIT_0 and BIT_1 using bitwise OR.
FreeRTOS
Hint
Use the bitwise OR operator | to combine bits.
3
Wait for event bits using AND and OR logic
Use xEventGroupWaitBits to wait for waitBits with AND logic (all bits set). Then use xEventGroupWaitBits to wait for waitBits with OR logic (any bit set). Store the results in bitsAnd and bitsOr respectively.
FreeRTOS
Hint
Use pdTRUE for xWaitForAllBits to wait for all bits (AND). Use pdFALSE to wait for any bit (OR).
4
Print the results of waiting for event bits
Print the values of bitsAnd and bitsOr using printf to show which bits were set.
FreeRTOS
Hint
Use printf("bitsAnd: 0x%02X\n", bitsAnd); and similarly for bitsOr.
Practice
(1/5)
1. What does waiting for event bits in FreeRTOS allow a task to do?
easy
A. Pause execution until specific event bits are set
B. Run continuously without interruption
C. Automatically clear all event bits on start
D. Create new event bits dynamically
Solution
Step 1: Understand event bits purpose
Event bits are signals that tasks can wait for to synchronize actions.
Step 2: Identify what waiting means
Waiting means the task pauses until the required bits are set.
Final Answer:
Pause execution until specific event bits are set -> Option A
Quick Check:
Waiting for event bits = pause task until bits set [OK]
Hint: Waiting means pausing until bits signal event [OK]
Common Mistakes:
Thinking waiting means running continuously
Confusing event bits with task creation
Assuming bits clear automatically on start
2. Which of the following is the correct syntax to wait for event bits using AND mode in FreeRTOS?
easy
A. xEventGroupClearBits(eventGroup, bitsToWaitFor);
B. xEventGroupWaitBits(eventGroup, bitsToWaitFor, clearOnExit, pdFALSE, timeout);
C. xEventGroupWaitBits(eventGroup, bitsToWaitFor, clearOnExit, pdTRUE, timeout);
D. xEventGroupSetBits(eventGroup, bitsToWaitFor);
Solution
Step 1: Identify the AND mode parameter
The fourth parameter is pdTRUE to wait for all bits (AND mode).
Step 2: Confirm function usage
xEventGroupWaitBits waits for bits with parameters: event group, bits, clear flag, wait mode, timeout.
Final Answer:
xEventGroupWaitBits(eventGroup, bitsToWaitFor, clearOnExit, pdTRUE, timeout); -> Option C
What does bits contain if only BIT_1 is set in the event group before the call?
medium
A. Only BIT_1 set in bits
B. Both BIT_1 and BIT_2 set in bits
C. Zero (no bits set)
D. Function blocks indefinitely
Solution
Step 1: Understand wait mode pdFALSE
pdFALSE means OR mode: wait for any bit set.
Step 2: Check which bits are set before call
Only BIT_1 is set, but the third parameter is pdTRUE (clearOnExit), and the fourth parameter is pdFALSE (waitForAll = false), so the function waits for any bit set.
Step 3: Analyze the code
However, the third parameter is pdTRUE (clearOnExit), so bits are cleared on exit. But since waitForAll is pdFALSE, the function returns when any bit is set.
Step 4: But the question is what bits contains if only BIT_1 is set before call
Since waitForAll is false, the function returns immediately with bits containing BIT_1 set.
Final Answer:
Only BIT_1 set in bits -> Option A
Quick Check:
OR mode returns bits set among requested [OK]
Hint: pdFALSE means OR mode, returns any set bits [OK]