Bird
Raised Fist0
FreeRTOSprogramming~10 mins

Setting and clearing event bits in FreeRTOS - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Setting and clearing event bits
Start Task
↓
Wait for Event Bits
↓
Event Bits Set?
No→Block/Wait
Yes↓
Process Event
↓
Clear Event Bits
↓
Loop or End
The task waits for specific event bits, processes when set, then clears bits before looping or ending.
Execution Sample
FreeRTOS
EventBits_t bits = xEventGroupWaitBits(eventGroup, BIT_0 | BIT_1, pdTRUE, pdFALSE, portMAX_DELAY);
if(bits & BIT_0) {
    // Handle BIT_0 event
}
// BIT_0 and BIT_1 cleared automatically due to pdTRUE
Waits for BIT_0 or BIT_1, processes BIT_0 if set, and clears bits automatically after.
Execution Table
StepActionEvent Bits BeforeWait ConditionEvent Bits AfterNotes
1Task starts and waits for BIT_0 or BIT_10000 0000Wait for BIT_0 or BIT_10000 0000No bits set, task blocks
2External event sets BIT_00000 0000Wait for BIT_0 or BIT_10000 0001BIT_0 set, task unblocks
3Task wakes, reads bits0000 0001Check bits0000 0001BIT_0 detected
4Task processes BIT_0 event0000 0001Process event0000 0001Processing logic runs
5Task clears BIT_0 and BIT_1 (pdTRUE)0000 0001Clear bits0000 0000Bits cleared automatically
6Task loops back to wait0000 0000Wait for BIT_0 or BIT_10000 0000Ready for next event
7Exit0000 0000No more events0000 0000Task blocks indefinitely
💡 Task blocks waiting for event bits; no bits set to unblock.
Variable Tracker
VariableStartAfter Step 2After Step 5Final
eventGroup bits0000 00000000 00010000 00000000 0000
Key Moments - 3 Insights
Why does the task unblock at step 2?
Because BIT_0 was set externally, matching the wait condition in step 1, so the task wakes up (see execution_table row 2).
How are event bits cleared automatically?
The pdTRUE parameter in xEventGroupWaitBits causes the specified bits to be cleared after the task wakes, as shown between steps 4 and 5.
What happens if bits are not cleared?
If bits are not cleared, the task might immediately unblock again or misinterpret events, but here bits are cleared at step 5 to avoid that.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of eventGroup bits after step 2?
A0000 0001
B0000 0000
C0000 0010
D0000 0011
💡 Hint
Check the 'Event Bits After' column at step 2 in the execution_table.
At which step does the task clear the event bits?
AStep 4
BStep 5
CStep 3
DStep 6
💡 Hint
Look for the 'Clear bits' action in the execution_table rows.
If pdTRUE was changed to pdFALSE in xEventGroupWaitBits, what would happen to the bits after step 5?
ATask would never unblock
BBits would be cleared automatically
CBits would remain set
DBits would toggle randomly
💡 Hint
Refer to the explanation in key_moments about pdTRUE clearing bits automatically.
Concept Snapshot
xEventGroupWaitBits(eventGroup, bitsToWaitFor, clearOnExit, waitForAll, timeout)
- Waits for bits to be set in eventGroup
- clearOnExit (pdTRUE) clears bits after waking
- Task blocks until bits set or timeout
- Use bitwise AND to check which bits set
- Clearing bits prevents repeated triggers
Full Transcript
This visual trace shows how a FreeRTOS task waits for event bits using xEventGroupWaitBits. Initially, no bits are set, so the task blocks. When an external event sets BIT_0, the task wakes and reads the bits. It processes the event and then clears the bits automatically because the clearOnExit parameter is true. The task then loops back to wait for new events. This cycle helps tasks synchronize with events efficiently by setting and clearing bits in an event group.

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

  1. Step 1: Understand the function name

    xEventGroupSetBits() suggests setting bits, which means signaling events.
  2. Step 2: Compare with other functions

    xEventGroupClearBits() is used to clear bits, so setting bits is done by xEventGroupSetBits().
  3. Final Answer:

    To set (signal) specific bits in an event group -> Option C
  4. 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

  1. Step 1: Identify the correct function for clearing bits

    The function to clear bits is xEventGroupClearBits().
  2. Step 2: Check the syntax

    The correct syntax is xEventGroupClearBits(eventGroupHandle, bitsToClear);. xEventGroupClearBits(myEventGroup, 0x03); matches this exactly.
  3. Final Answer:

    xEventGroupClearBits(myEventGroup, 0x03); -> Option D
  4. Quick Check:

    Clear bits syntax = xEventGroupClearBits(handle, bits) [OK]
Hint: Clear bits uses xEventGroupClearBits with group and bit mask [OK]
Common Mistakes:
  • Using xEventGroupSetBits to clear bits
  • Using incorrect function names
  • Missing parameters or wrong order
3. Given the code snippet:
EventBits_t bits;
bits = xEventGroupSetBits(myEventGroup, 0x05);
bits = xEventGroupClearBits(myEventGroup, 0x01);

What is the value of bits after these calls if the initial bits were 0x00?
medium
A. 0x00
B. 0x05
C. 0x04
D. 0x01

Solution

  1. Step 1: First call - xEventGroupSetBits(myEventGroup, 0x05)

    Initial event group: 0x00. Returns bits BEFORE setting: 0x00 (event group becomes 0x05). bits = 0x00.
  2. Step 2: Second call - xEventGroupClearBits(myEventGroup, 0x01)

    Event group now: 0x05. Returns bits BEFORE clearing: 0x05 (event group becomes 0x04). bits = 0x05.
  3. Final Answer:

    0x00 -> Option A
  4. Quick Check:

    Return value = pre-operation bits [OK]
Hint: xEventGroupSetBits/ClearBits return bits BEFORE operation [OK]
Common Mistakes:
  • Confusing return value (pre-op) with post-op event group state
  • Miscalculating bitwise operations on event group
  • Forgetting return value overwrites 'bits' each call
4. Identify the error in this code snippet:
EventGroupHandle_t myEventGroup;
myEventGroup = xEventGroupCreate();
xEventGroupClearBits(0x01, myEventGroup);
medium
A. Arguments to xEventGroupClearBits are reversed
B. xEventGroupCreate() returns void, not a handle
C. xEventGroupClearBits requires a pointer, not a value
D. Missing include for FreeRTOS headers

Solution

  1. Step 1: Check function parameter order

    xEventGroupClearBits() expects (EventGroupHandle_t, EventBits_t). The code passes (0x01, myEventGroup), which is reversed.
  2. Step 2: Confirm return type of xEventGroupCreate()

    xEventGroupCreate() returns an EventGroupHandle_t, so assignment is correct.
  3. Final Answer:

    Arguments to xEventGroupClearBits are reversed -> Option A
  4. 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

  1. Step 1: Set bits 0x01 and 0x04 together

    Setting bits 0x05 (binary 0101) signals both events at once.
  2. Step 2: Clear only bit 0x01

    Clearing bit 0x01 removes that event signal but leaves 0x04 set.
  3. Final Answer:

    xEventGroupSetBits(myEventGroup, 0x05); xEventGroupClearBits(myEventGroup, 0x01); -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard