📖 Scenario: You are working on a FreeRTOS-based embedded system where tasks communicate using event groups. You want to control task synchronization by setting and clearing event bits.
🎯 Goal: Build a simple FreeRTOS program that creates an event group, sets specific event bits, clears some bits, and then prints the final event bits value.
📋 What You'll Learn
Create an event group variable called eventGroup
Create a configuration variable BIT_0 with value (1 << 0)
Set the event bit BIT_0 in eventGroup
Clear the event bit BIT_0 in eventGroup
Print the final value of the event bits
💡 Why This Matters
🌍 Real World
Event groups are used in embedded systems to signal and synchronize tasks efficiently without busy waiting.
💼 Career
Understanding event bits is important for embedded software engineers working with real-time operating systems like FreeRTOS.
Progress0 / 4 steps
1
Create an event group
Create an event group variable called eventGroup using xEventGroupCreate().
FreeRTOS
Hint
Use xEventGroupCreate() to create the event group and assign it to eventGroup.
2
Define the event bit mask
Create a macro called BIT_0 with the value (1 << 0) to represent the first event bit.
FreeRTOS
Hint
Use #define BIT_0 (1 << 0) to create the bit mask.
3
Set and clear the event bit
Use xEventGroupSetBits(eventGroup, BIT_0) to set the event bit BIT_0. Then use xEventGroupClearBits(eventGroup, BIT_0) to clear the same bit.
FreeRTOS
Hint
Call xEventGroupSetBits() and xEventGroupClearBits() with eventGroup and BIT_0.
4
Print the final event bits value
Use uxEventGroupGetBits(eventGroup) to get the current event bits and print the value using printf.
FreeRTOS
Hint
Use printf("Final event bits value: %u\n", (unsigned int)bits); to print the bits.
Practice
(1/5)
1. What is the primary purpose of xEventGroupSetBits() in FreeRTOS?
easy
A. To create a new event group
B. To clear (reset) specific bits in an event group
C. To set (signal) specific bits in an event group
D. To delete an existing event group
Solution
Step 1: Understand the function name
xEventGroupSetBits() suggests setting bits, which means signaling events.
Step 2: Compare with other functions
xEventGroupClearBits() is used to clear bits, so setting bits is done by xEventGroupSetBits().
Final Answer:
To set (signal) specific bits in an event group -> Option C
Quick Check:
Set bits = signal events [OK]
Hint: Set bits means signal events, clear bits means reset [OK]
Common Mistakes:
Confusing setting bits with clearing bits
Thinking it creates or deletes event groups
Mixing up function names
2. Which of the following is the correct syntax to clear bits 0x03 in an event group named myEventGroup?
easy
A. xEventGroupClear(myEventGroup, 0x03);
B. xEventGroupSetBits(myEventGroup, 0x03);
C. xClearEventBits(myEventGroup, 0x03);
D. xEventGroupClearBits(myEventGroup, 0x03);
Solution
Step 1: Identify the correct function for clearing bits
The function to clear bits is xEventGroupClearBits().
Step 2: Check the syntax
The correct syntax is xEventGroupClearBits(eventGroupHandle, bitsToClear);. xEventGroupClearBits(myEventGroup, 0x03); matches this exactly.
Final Answer:
xEventGroupClearBits(myEventGroup, 0x03); -> Option D
C. xEventGroupClearBits requires a pointer, not a value
D. Missing include for FreeRTOS headers
Solution
Step 1: Check function parameter order
xEventGroupClearBits() expects (EventGroupHandle_t, EventBits_t). The code passes (0x01, myEventGroup), which is reversed.
Step 2: Confirm return type of xEventGroupCreate()
xEventGroupCreate() returns an EventGroupHandle_t, so assignment is correct.
Final Answer:
Arguments to xEventGroupClearBits are reversed -> Option A
Quick Check:
Function parameters order matters [OK]
Hint: Check parameter order carefully for FreeRTOS API calls [OK]
Common Mistakes:
Swapping parameters in API calls
Assuming wrong return types
Ignoring compiler warnings
5. You want to signal two events represented by bits 0x01 and 0x04, then immediately clear bit 0x01 without affecting others. Which sequence correctly achieves this?
hard
A. xEventGroupClearBits(myEventGroup, 0x05); xEventGroupSetBits(myEventGroup, 0x01);
B. xEventGroupSetBits(myEventGroup, 0x05); xEventGroupClearBits(myEventGroup, 0x01);
C. xEventGroupSetBits(myEventGroup, 0x01); xEventGroupClearBits(myEventGroup, 0x04);
D. xEventGroupClearBits(myEventGroup, 0x01); xEventGroupSetBits(myEventGroup, 0x05);
Solution
Step 1: Set bits 0x01 and 0x04 together
Setting bits 0x05 (binary 0101) signals both events at once.
Step 2: Clear only bit 0x01
Clearing bit 0x01 removes that event signal but leaves 0x04 set.
Final Answer:
xEventGroupSetBits(myEventGroup, 0x05); xEventGroupClearBits(myEventGroup, 0x01); -> Option B
Quick Check:
Set both bits then clear one bit [OK]
Hint: Set all bits first, then clear only the unwanted bit [OK]
Common Mistakes:
Clearing bits before setting them
Clearing wrong bits
Not combining bits correctly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.