Bird
Raised Fist0
FreeRTOSprogramming~10 mins

Waiting for event bits (AND/OR) in FreeRTOS - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Waiting for event bits (AND/OR)
Task calls xEventGroupWaitBits()
↓
Check event bits in event group
↓
All bits set?
↓
Return bits
↓
Task continues
↓
If timeout, return 0
↓
Task continues or blocks
The task waits for specific bits in an event group to be set, either all bits (AND) or any bit (OR), then continues or times out.
Execution Sample
FreeRTOS
EventBits_t bits = xEventGroupWaitBits(
    eventGroup,
    BIT_1 | BIT_2,
    pdTRUE,
    pdFALSE,
    portMAX_DELAY
);
Task waits for BIT_1 or BIT_2 to be set in eventGroup, clears bits on exit, waits forever.
Execution Table
StepEvent Group BitsWait ConditionCondition Met?ActionReturned Bits
10b0000Wait for BIT_1 or BIT_2 (OR)NoBlock task, wait0b0000
20b0001 (BIT_1 set)Wait for BIT_1 or BIT_2 (OR)YesReturn bits, clear bits if requested0b0001
30b0010 (BIT_2 set)Wait for BIT_1 and BIT_2 (AND)NoBlock task, wait0b0010
40b0011 (BIT_1 and BIT_2 set)Wait for BIT_1 and BIT_2 (AND)YesReturn bits, clear bits if requested0b0011
50b0000Timeout reachedNoReturn 0, no bits set0b0000
💡 Execution stops when condition is met or timeout occurs.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
eventGroupBits0b00000b00000b00000b00100b00000b0000
returnedBitsN/A0b00000b00010b00100b00110b0000
Key Moments - 3 Insights
Why does the task block at step 1 even though bits are zero?
Because the wait condition (BIT_1 or BIT_2) is not met, the task blocks waiting for bits to be set, as shown in execution_table row 1.
What happens to the event bits after the task returns when pdTRUE is set for clear on exit?
The bits that caused the task to unblock are cleared from the event group, as seen after step 2 and step 4 in variable_tracker where eventGroupBits reset.
How does the AND condition differ from the OR condition in waiting?
AND requires all specified bits to be set before continuing (step 4), while OR requires any one bit set (step 2), as shown in execution_table conditions.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what bits are returned by xEventGroupWaitBits?
A0b0001
B0b0010
C0b0011
D0b0000
💡 Hint
Check the 'Returned Bits' column at step 2 in execution_table.
At which step does the AND condition become true?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the 'Condition Met?' column for AND condition in execution_table.
If pdFALSE was used for clearing bits on exit, how would eventGroupBits change after step 4?
ABits would be cleared to 0b0000
BBits would toggle to 0b0001
CBits would remain 0b0011
DBits would become 0b0010
💡 Hint
Refer to variable_tracker and the effect of pdTRUE vs pdFALSE on bits clearing.
Concept Snapshot
xEventGroupWaitBits(eventGroup, bitsToWaitFor, clearOnExit, waitForAllBits, timeout)
- Waits for bits in eventGroup
- waitForAllBits = pdTRUE waits for all bits (AND)
- waitForAllBits = pdFALSE waits for any bit (OR)
- clearOnExit clears bits that caused unblock
- Returns bits that were set when unblocked
Full Transcript
This visual execution shows how a FreeRTOS task waits for event bits using xEventGroupWaitBits. The task calls the function with bits to wait for and conditions (AND or OR). It blocks if the condition is not met, and continues when bits are set. The bits causing unblock can be cleared if requested. The execution table traces bits in the event group, conditions checked, and returned bits step-by-step. Variable tracker shows how event bits and returned bits change over time. Key moments clarify common confusions about blocking, bit clearing, and AND vs OR conditions. The quiz tests understanding of bits returned, condition satisfaction, and clearing behavior. The snapshot summarizes the function usage and parameters.

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

  1. Step 1: Understand event bits purpose

    Event bits are signals that tasks can wait for to synchronize actions.
  2. Step 2: Identify what waiting means

    Waiting means the task pauses until the required bits are set.
  3. Final Answer:

    Pause execution until specific event bits are set -> Option A
  4. 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

  1. Step 1: Identify the AND mode parameter

    The fourth parameter is pdTRUE to wait for all bits (AND mode).
  2. Step 2: Confirm function usage

    xEventGroupWaitBits waits for bits with parameters: event group, bits, clear flag, wait mode, timeout.
  3. Final Answer:

    xEventGroupWaitBits(eventGroup, bitsToWaitFor, clearOnExit, pdTRUE, timeout); -> Option C
  4. Quick Check:

    pdTRUE means AND mode in wait bits [OK]
Hint: pdTRUE means wait for all bits (AND) [OK]
Common Mistakes:
  • Using pdFALSE for AND mode
  • Confusing SetBits with WaitBits
  • Mixing up clearOnExit parameter
3. Given the code snippet:
EventBits_t bits = xEventGroupWaitBits(eventGroup, BIT_1 | BIT_2, pdTRUE, pdFALSE, 1000);

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

  1. Step 1: Understand wait mode pdFALSE

    pdFALSE means OR mode: wait for any bit set.
  2. 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.
  3. 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.
  4. 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.
  5. Final Answer:

    Only BIT_1 set in bits -> Option A
  6. Quick Check:

    OR mode returns bits set among requested [OK]
Hint: pdFALSE means OR mode, returns any set bits [OK]
Common Mistakes:
  • Assuming both bits must be set for return
  • Thinking function blocks if not all bits set
  • Confusing pdTRUE and pdFALSE meanings
4. Identify the error in this code snippet:
EventBits_t bits = xEventGroupWaitBits(eventGroup, BIT_1 | BIT_2, pdTRUE, pdTRUE, 500);

What is wrong?
medium
A. pdTRUE for waitForAll means wait for all bits, but clearOnExit should be false to avoid clearing
B. pdTRUE for clearOnExit clears bits on exit, which is valid; no error
C. Using pdTRUE for both clearOnExit and waitForAll bits is correct
D. pdTRUE for waitForAll is invalid; must be pdFALSE

Solution

  1. Step 1: Understand clearOnExit parameter

    pdTRUE means bits are cleared when function returns.
  2. Step 2: Understand waitForAll parameter

    pdTRUE means wait for all bits (AND mode), which is valid.
  3. Final Answer:

    pdTRUE for clearOnExit clears bits on exit, which is valid; no error -> Option B
  4. Quick Check:

    clearOnExit pdTRUE is allowed and common [OK]
Hint: clearOnExit pdTRUE clears bits on return, allowed [OK]
Common Mistakes:
  • Thinking clearOnExit pdTRUE is always wrong
  • Confusing waitForAll with clearOnExit
  • Assuming pdTRUE for waitForAll is invalid
5. You want a task to wait until both BIT_1 and BIT_2 are set, then clear those bits automatically on exit. Which call achieves this correctly?
hard
A. xEventGroupWaitBits(eventGroup, BIT_1 | BIT_2, pdFALSE, pdTRUE, portMAX_DELAY);
B. xEventGroupWaitBits(eventGroup, BIT_1 | BIT_2, pdFALSE, pdFALSE, portMAX_DELAY);
C. xEventGroupWaitBits(eventGroup, BIT_1 | BIT_2, pdTRUE, pdFALSE, portMAX_DELAY);
D. xEventGroupWaitBits(eventGroup, BIT_1 | BIT_2, pdTRUE, pdTRUE, portMAX_DELAY);

Solution

  1. Step 1: Identify wait for all bits

    Fourth parameter pdTRUE means wait for all bits (AND mode).
  2. Step 2: Identify clear bits on exit

    Third parameter pdTRUE means clear bits on exit.
  3. Step 3: Match parameters to options

    xEventGroupWaitBits(eventGroup, BIT_1 | BIT_2, pdTRUE, pdTRUE, portMAX_DELAY); uses clearOnExit = pdTRUE and waitForAll = pdTRUE, correct for requirement.
  4. Final Answer:

    xEventGroupWaitBits(eventGroup, BIT_1 | BIT_2, pdTRUE, pdTRUE, portMAX_DELAY); -> Option D
  5. Quick Check:

    Clear bits and wait all bits = clearOnExit pdTRUE, waitForAll pdTRUE [OK]
Hint: clearOnExit and waitForAll both pdTRUE for AND + clear [OK]
Common Mistakes:
  • Mixing up clearOnExit and waitForAll parameters
  • Using pdFALSE for waitForAll when AND needed
  • Not setting timeout to wait indefinitely
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