Bird
Raised Fist0
FreeRTOSprogramming~10 mins

Event group for task synchronization in FreeRTOS - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Event group for task synchronization
Task 1 waits for event bits
↓
Task 2 sets event bits
↓
Event group updates bits
↓
Task 1 unblocked, continues
↓
Tasks synchronize on events
Tasks wait for specific event bits; when another task sets these bits, waiting tasks unblock and synchronize.
Execution Sample
FreeRTOS
EventBits_t bits;
bits = xEventGroupWaitBits(eventGroup, BIT_0 | BIT_1, pdTRUE, pdTRUE, portMAX_DELAY);
if ((bits & (BIT_0 | BIT_1)) == (BIT_0 | BIT_1)) {
    // Both bits set, proceed
}
Task waits for two event bits to be set before continuing.
Execution Table
StepActionEvent Group Bits BeforeEvent Group Bits AfterTask StateOutput/Result
1Task 1 calls xEventGroupWaitBits for BIT_0 and BIT_100000000Blocked waitingNo output
2Task 2 sets BIT_000000001Task 1 still blockedNo output
3Task 2 sets BIT_100010011Task 1 unblockedTask 1 continues
4Task 1 checks bits00110000 (bits cleared)RunningBoth bits set, proceed
5Task 1 completes synchronization00000000RunningSynchronization done
6No more events, tasks wait00000000Tasks blocked or running as per logicIdle or running
💡 Task 1 unblocks when both BIT_0 and BIT_1 are set; bits cleared due to pdTRUE clear-on-exit flag.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
eventGroupBits00000001001100000000
Task1StateRunningBlocked waitingRunningRunningRunning
Key Moments - 2 Insights
Why does Task 1 stay blocked after BIT_0 is set but before BIT_1 is set?
Because Task 1 waits for both BIT_0 and BIT_1 (see execution_table step 2 and 3). Only when both bits are set (step 3) does Task 1 unblock.
What happens to the event bits after Task 1 unblocks?
The bits are cleared automatically because the pdTRUE flag is used in xEventGroupWaitBits (see execution_table step 4). This prevents repeated unblocking on the same bits.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of eventGroupBits after Task 2 sets BIT_1?
A0001
B0011
C0000
D0100
💡 Hint
Check execution_table row 3 under 'Event Group Bits After'
At which step does Task 1 unblock and continue execution?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look at 'Task State' column in execution_table rows 2 and 3
If pdFALSE was used instead of pdTRUE for clearing bits, what would happen after Task 1 unblocks?
ABits would be cleared automatically
BTask 1 would never unblock
CBits would remain set
DEvent group would reset
💡 Hint
Refer to key_moments about the clear-on-exit flag behavior
Concept Snapshot
Event groups let tasks wait for one or more bits to be set.
Tasks call xEventGroupWaitBits with bits to wait for.
When bits are set by other tasks, waiting tasks unblock.
Use pdTRUE to clear bits on exit, pdFALSE to keep them.
This helps tasks synchronize on events easily.
Full Transcript
In FreeRTOS, event groups help tasks wait for specific events represented by bits. A task calls xEventGroupWaitBits to wait for bits like BIT_0 and BIT_1. If those bits are not set, the task blocks. Another task can set bits using xEventGroupSetBits. When all bits the task waits for are set, the task unblocks and continues. If pdTRUE is used, the bits are cleared automatically after unblocking to avoid repeated triggers. This mechanism allows tasks to synchronize their actions based on shared events efficiently.

Practice

(1/5)
1. What is the primary purpose of an event group in FreeRTOS for task synchronization?
easy
A. To allocate memory dynamically for tasks
B. To allow tasks to wait for multiple events using bits
C. To create queues for message passing
D. To manage task priorities automatically

Solution

  1. Step 1: Understand event groups in FreeRTOS

    Event groups use bits to represent events that tasks can wait for or signal.
  2. Step 2: Identify the main use of event groups

    They help tasks synchronize by waiting on or setting bits representing events.
  3. Final Answer:

    To allow tasks to wait for multiple events using bits -> Option B
  4. Quick Check:

    Event groups = wait for multiple events [OK]
Hint: Event groups use bits to sync tasks, not memory or priorities [OK]
Common Mistakes:
  • Confusing event groups with queues
  • Thinking event groups manage memory
  • Assuming event groups change task priorities
2. Which of the following is the correct function to wait for bits in an event group in FreeRTOS?
easy
A. xEventGroupClearBits()
B. xEventGroupSetBits()
C. xEventGroupWaitBits()
D. xEventGroupCreate()

Solution

  1. Step 1: Recall FreeRTOS event group functions

    xEventGroupWaitBits() is used to wait for specific bits to be set.
  2. Step 2: Differentiate from other functions

    xEventGroupSetBits() sets bits, ClearBits clears bits, Create creates the group.
  3. Final Answer:

    xEventGroupWaitBits() -> Option C
  4. Quick Check:

    Wait for bits = xEventGroupWaitBits() [OK]
Hint: Wait uses xEventGroupWaitBits(), set uses xEventGroupSetBits() [OK]
Common Mistakes:
  • Using xEventGroupSetBits() to wait
  • Confusing create with wait functions
  • Mixing clear bits with wait bits
3. Given the following code snippet, what will be the value of bits after xEventGroupWaitBits() returns?
EventBits_t bits;
bits = xEventGroupWaitBits(eventGroup, BIT_0 | BIT_1, pdTRUE, pdFALSE, portMAX_DELAY);
Assume BIT_0 and BIT_1 are both set in eventGroup before the call.
medium
A. Only BIT_0 set
B. Only BIT_1 set
C. No bits set
D. Both BIT_0 and BIT_1 set

Solution

  1. Step 1: Understand xEventGroupWaitBits parameters

    The function waits for BIT_0 or BIT_1 to be set, clears bits on exit (pdTRUE), and does not wait for all bits (pdFALSE).
  2. Step 2: Analyze the bits set before call

    Both BIT_0 and BIT_1 are set, so the function returns with both bits set in the return value.
  3. Final Answer:

    Both BIT_0 and BIT_1 set -> Option D
  4. Quick Check:

    WaitBits returns bits set before clearing [OK]
Hint: WaitBits returns bits set before clearing them [OK]
Common Mistakes:
  • Assuming only one bit is returned
  • Ignoring bits cleared on exit
  • Confusing wait for all bits with wait for any
4. Identify the error in the following FreeRTOS event group usage:
EventGroupHandle_t eventGroup = xEventGroupCreate();

void Task1(void *pvParameters) {
    xEventGroupWaitBits(eventGroup, BIT_0, pdFALSE, pdTRUE, portMAX_DELAY);
    // Task code
}

void Task2(void *pvParameters) {
    xEventGroupSetBits(eventGroup, BIT_0);
}
medium
A. Not checking if eventGroup creation succeeded
B. Using portMAX_DELAY as wait time
C. Using pdFALSE for clearOnExit in xEventGroupWaitBits
D. Setting bits before event group creation

Solution

  1. Step 1: Check event group creation

    xEventGroupCreate() can return NULL if creation fails; code does not check this.
  2. Step 2: Verify other parameters

    Using pdFALSE for clearOnExit is valid; portMAX_DELAY is valid; bits are set after creation.
  3. Final Answer:

    Not checking if eventGroup creation succeeded -> Option A
  4. Quick Check:

    Always check event group creation success [OK]
Hint: Always verify event group creation returned non-NULL [OK]
Common Mistakes:
  • Ignoring NULL return from xEventGroupCreate
  • Misusing clearOnExit parameter
  • Setting bits before creating event group
5. You want two tasks to synchronize so that Task A waits until Task B signals both BIT_0 and BIT_1. Which xEventGroupWaitBits() call correctly waits for both bits before continuing?
hard
A. xEventGroupWaitBits(eventGroup, BIT_0 | BIT_1, pdTRUE, pdTRUE, portMAX_DELAY);
B. xEventGroupWaitBits(eventGroup, BIT_0 & BIT_1, pdTRUE, pdTRUE, portMAX_DELAY);
C. xEventGroupWaitBits(eventGroup, BIT_0 | BIT_1, pdTRUE, pdFALSE, portMAX_DELAY);
D. xEventGroupWaitBits(eventGroup, BIT_0 & BIT_1, pdFALSE, pdFALSE, portMAX_DELAY);

Solution

  1. Step 1: Understand wait for all bits parameter

    The fourth parameter (xWaitForAllBits) must be pdTRUE to wait for both bits together.
  2. Step 2: Check bit mask and parameters

    Use BIT_0 | BIT_1 to specify both bits; pdTRUE for clearOnExit clears bits after wait.
  3. Final Answer:

    xEventGroupWaitBits(eventGroup, BIT_0 | BIT_1, pdTRUE, pdTRUE, portMAX_DELAY); -> Option A
  4. Quick Check:

    Wait for all bits = xEventGroupWaitBits with xWaitForAllBits = pdTRUE [OK]
Hint: Set xWaitForAllBits to pdTRUE to wait for all bits [OK]
Common Mistakes:
  • Using bitwise AND (&) instead of OR (|) for bit mask
  • Setting xWaitForAllBits to pdFALSE when waiting for all bits
  • Confusing clearOnExit with wait for all bits
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