Event bits help tasks communicate by signaling events. Setting and clearing bits lets tasks share information simply and clearly.
Setting and clearing event bits in FreeRTOS
EventGroupHandle_t xEventGroup;
// To set bits:
xEventGroupSetBits(xEventGroup, uxBitsToSet);
// To clear bits:
xEventGroupClearBits(xEventGroup, uxBitsToClear);xEventGroup is the handle to the event group created earlier.
uxBitsToSet and uxBitsToClear are bit masks indicating which bits to set or clear.
xEventGroupSetBits(xEventGroup, 0x01);xEventGroupClearBits(xEventGroup, 0x02);xEventGroupSetBits(xEventGroup, 0x05);This program creates an event group and two tasks. Task1 sets bit 0, waits 1 second, then clears bit 0. Task2 waits for bit 0 to be set, then prints a message and clears the bit automatically.
#include "FreeRTOS.h" #include "event_groups.h" #include "task.h" #include <stdio.h> // Create an event group handle static EventGroupHandle_t xEventGroup; void vTask1(void *pvParameters) { printf("Task1: Setting bit 0\n"); xEventGroupSetBits(xEventGroup, 0x01); // Set bit 0 vTaskDelay(pdMS_TO_TICKS(1000)); printf("Task1: Clearing bit 0\n"); xEventGroupClearBits(xEventGroup, 0x01); // Clear bit 0 vTaskDelete(NULL); } void vTask2(void *pvParameters) { EventBits_t uxBits; printf("Task2: Waiting for bit 0 to be set\n"); uxBits = xEventGroupWaitBits( xEventGroup, 0x01, pdTRUE, // Clear bit on exit pdFALSE, // Wait for any bit portMAX_DELAY ); if ((uxBits & 0x01) != 0) { printf("Task2: Bit 0 was set and cleared\n"); } vTaskDelete(NULL); } int main(void) { // Create the event group xEventGroup = xEventGroupCreate(); if (xEventGroup == NULL) { printf("Failed to create event group\n"); return 1; } // Create tasks xTaskCreate(vTask1, "Task1", 1000, NULL, 1, NULL); xTaskCreate(vTask2, "Task2", 1000, NULL, 1, NULL); // Start scheduler vTaskStartScheduler(); // Should never reach here return 0; }
Setting bits does not block the task; it just signals the event.
Clearing bits removes the signal so tasks know the event was handled.
Use bit masks carefully to avoid changing unintended bits.
Event bits let tasks signal and wait for events easily.
Use xEventGroupSetBits() to set (signal) bits.
Use xEventGroupClearBits() to clear (reset) bits.
Practice
xEventGroupSetBits() in FreeRTOS?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 byxEventGroupSetBits().Final Answer:
To set (signal) specific bits in an event group -> Option CQuick Check:
Set bits = signal events [OK]
- Confusing setting bits with clearing bits
- Thinking it creates or deletes event groups
- Mixing up function names
myEventGroup?Solution
Step 1: Identify the correct function for clearing bits
The function to clear bits isxEventGroupClearBits().Step 2: Check the syntax
The correct syntax isxEventGroupClearBits(eventGroupHandle, bitsToClear);. xEventGroupClearBits(myEventGroup, 0x03); matches this exactly.Final Answer:
xEventGroupClearBits(myEventGroup, 0x03); -> Option DQuick Check:
Clear bits syntax = xEventGroupClearBits(handle, bits) [OK]
- Using xEventGroupSetBits to clear bits
- Using incorrect function names
- Missing parameters or wrong order
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?Solution
Step 1: First call - xEventGroupSetBits(myEventGroup, 0x05)
Initial event group: 0x00. Returns bits BEFORE setting: 0x00 (event group becomes 0x05).bits = 0x00.Step 2: Second call - xEventGroupClearBits(myEventGroup, 0x01)
Event group now: 0x05. Returns bits BEFORE clearing: 0x05 (event group becomes 0x04).bits = 0x05.Final Answer:
0x00 -> Option AQuick Check:
Return value = pre-operation bits [OK]
- Confusing return value (pre-op) with post-op event group state
- Miscalculating bitwise operations on event group
- Forgetting return value overwrites 'bits' each call
EventGroupHandle_t myEventGroup; myEventGroup = xEventGroupCreate(); xEventGroupClearBits(0x01, myEventGroup);
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 AQuick Check:
Function parameters order matters [OK]
- Swapping parameters in API calls
- Assuming wrong return types
- Ignoring compiler warnings
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 BQuick Check:
Set both bits then clear one bit [OK]
- Clearing bits before setting them
- Clearing wrong bits
- Not combining bits correctly
Ready to prove it?
Pass the test and this pattern moves unknown
