Bird
Raised Fist0
FreeRTOSprogramming~5 mins

Setting and clearing event bits in FreeRTOS

Choose your learning style10 modes available
Introduction

Event bits help tasks communicate by signaling events. Setting and clearing bits lets tasks share information simply and clearly.

When one task needs to tell another task that something happened.
When you want to wait for multiple events before continuing.
When you want to reset event signals after handling them.
When coordinating tasks that depend on shared flags.
When you want to avoid complex message passing and use simple flags.
Syntax
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.

Examples
Sets bit 0 (the first bit) in the event group.
FreeRTOS
xEventGroupSetBits(xEventGroup, 0x01);
Clears bit 1 (the second bit) in the event group.
FreeRTOS
xEventGroupClearBits(xEventGroup, 0x02);
Sets bits 0 and 2 (binary 0101) at the same time.
FreeRTOS
xEventGroupSetBits(xEventGroup, 0x05);
Sample Program

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.

FreeRTOS
#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;
}
OutputSuccess
Important Notes

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.

Summary

Event bits let tasks signal and wait for events easily.

Use xEventGroupSetBits() to set (signal) bits.

Use xEventGroupClearBits() to clear (reset) bits.

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