Event groups help tasks work together by letting them wait for or signal events easily. This keeps tasks in sync without wasting time.
Why event groups coordinate tasks in FreeRTOS
EventGroupHandle_t xEventGroupCreate(void);
EventBits_t xEventGroupWaitBits(
EventGroupHandle_t xEventGroup,
const EventBits_t uxBitsToWaitFor,
const BaseType_t xClearOnExit,
const BaseType_t xWaitForAllBits,
TickType_t xTicksToWait
);
EventBits_t xEventGroupSetBits(
EventGroupHandle_t xEventGroup,
const EventBits_t uxBitsToSet
);xEventGroupCreate makes a new event group.
xEventGroupWaitBits lets a task wait for specific bits (events) to be set.
EventGroupHandle_t xEventGroup = xEventGroupCreate(); // Create an event group to hold event bits.
xEventGroupSetBits(xEventGroup, BIT_0);
// Set bit 0 to signal an event happened.xEventGroupWaitBits(xEventGroup, BIT_0 | BIT_1, pdTRUE, pdFALSE, portMAX_DELAY); // Wait for bit 0 or bit 1 to be set, then clear them.
This program creates two tasks. Task1 waits for BIT_0 to be set in the event group. Task2 waits 1 second, then sets BIT_0. Task1 then continues and prints a message.
#include "FreeRTOS.h" #include "task.h" #include "event_groups.h" #include <stdio.h> #define BIT_0 (1 << 0) #define BIT_1 (1 << 1) EventGroupHandle_t xEventGroup; void vTask1(void *pvParameters) { printf("Task1: Waiting for BIT_0...\n"); xEventGroupWaitBits(xEventGroup, BIT_0, pdTRUE, pdFALSE, portMAX_DELAY); printf("Task1: BIT_0 received, running task1 code.\n"); vTaskDelete(NULL); } void vTask2(void *pvParameters) { printf("Task2: Setting BIT_0 after delay...\n"); vTaskDelay(pdMS_TO_TICKS(1000)); xEventGroupSetBits(xEventGroup, BIT_0); printf("Task2: BIT_0 set.\n"); vTaskDelete(NULL); } int main(void) { xEventGroup = xEventGroupCreate(); xTaskCreate(vTask1, "Task1", 1000, NULL, 1, NULL); xTaskCreate(vTask2, "Task2", 1000, NULL, 1, NULL); vTaskStartScheduler(); for(;;) {} return 0; }
Event groups use bits to represent events, so you can wait for multiple events at once.
Clearing bits on exit helps avoid repeated triggers for the same event.
Use event groups to reduce CPU usage by avoiding busy waiting.
Event groups let tasks wait for or signal events using bits.
They help tasks work together smoothly and efficiently.
Use event groups to coordinate multiple tasks in FreeRTOS easily.
Practice
Solution
Step 1: Understand event groups role
Event groups use bits to represent events that tasks can wait for or signal.Step 2: Compare with other options
Creating tasks, managing memory, and handling interrupts are different FreeRTOS features.Final Answer:
To let tasks wait for or signal events using bits -> Option CQuick Check:
Event groups coordinate tasks by signaling bits [OK]
- Confusing event groups with task creation
- Thinking event groups manage memory
- Assuming event groups handle interrupts
Solution
Step 1: Recall FreeRTOS naming conventions
Functions returning handles usually start with 'x' and create functions end with 'Create'.Step 2: Identify correct function
The correct function to create an event group is xEventGroupCreate().Final Answer:
xEventGroupCreate() -> Option BQuick Check:
Event group creation uses xEventGroupCreate() [OK]
- Using 'v' prefix which is for void functions
- Swapping function name order
- Assuming event groups are created with task functions
EventGroupHandle_t xEventGroup = xEventGroupCreate(); const EventBits_t BIT_0 = (1 << 0); xEventGroupSetBits(xEventGroup, BIT_0); EventBits_t uxBits = xEventGroupWaitBits(xEventGroup, BIT_0, pdTRUE, pdFALSE, portMAX_DELAY);
What will be the value of
uxBits after xEventGroupWaitBits returns?Solution
Step 1: Understand bit setting and waiting
BIT_0 is set before waiting, so the wait function will return with BIT_0 set.Step 2: Analyze parameters
pdTRUE clears the bits after return, but uxBits holds the bits that were set when the function returned.Final Answer:
BIT_0 -> Option AQuick Check:
Wait returns bits that were set, here BIT_0 [OK]
- Assuming bits clear before return
- Confusing return value with delay constant
- Ignoring bit mask value
EventGroupHandle_t xEventGroup; xEventGroupSetBits(xEventGroup, (1 << 1));
Solution
Step 1: Check event group initialization
xEventGroup is declared but never created or assigned a valid handle.Step 2: Understand function requirements
xEventGroupSetBits requires a valid event group handle, which is missing here.Final Answer:
xEventGroup is not initialized before use -> Option DQuick Check:
Event group must be created before setting bits [OK]
- Using uninitialized event group handle
- Misunderstanding bit mask validity
- Passing pointer instead of handle
Solution
Step 1: Understand event group bits for coordination
Each task sets a unique bit representing its readiness.Step 2: Wait for both bits set
Tasks wait for the combination of both bits to be set before continuing.Final Answer:
Each task sets a unique bit; both wait for both bits set before proceeding -> Option AQuick Check:
Unique bits per task coordinate readiness [OK]
- Using same bit for multiple tasks
- Using separate event groups losing coordination
- Ignoring event groups and using variables
Ready to prove it?
Pass the test and this pattern moves unknown
