Skip to content
systemcoding.dev
RTOS

Clear bits on exit with xClearOnExit in xEventGroupWaitBits

xEventGroupWaitBits with xClearOnExit set to pdTRUE clears every awaited bit on return, including ones that did not trigger the wake-up.

1 min read
Summarize in AI

Passing pdTRUE as xClearOnExit makes xEventGroupWaitBits() clear the awaited bits before it returns, which gives you a self-resetting flag without a second call. The part the first screen of the manual skips is that it clears all the bits in the wait mask that are set, not only the one that satisfied a wait-for-any condition. The returned value is the group as it stood when the wait was satisfied, before the clear.

evt_clear.cc
#define BIT_A (1u << 0)
#define BIT_B (1u << 1)
#define BIT_C (1u << 2)

EventGroupHandle_t g = xEventGroupCreate();
xEventGroupSetBits(g, BIT_A | BIT_B | BIT_C);

EventBits_t got = xEventGroupWaitBits(g, BIT_A | BIT_B,
                                      pdTRUE,   /* clear on exit */
                                      pdFALSE,  /* any one bit */
                                      0);
printf("returned 0x%x, left 0x%x\n", (unsigned) got,
       (unsigned) xEventGroupGetBits(g));
Text
returned 0x7, left 0x4

Both BIT_A and BIT_B were set, so both were cleared even though either alone would have woken the task, and the unmasked BIT_C survived. If another task was also waiting on BIT_B, it can miss the event. Where several consumers share a bit, let the setter clear it, or use one bit per consumer. Behaviour is as documented for FreeRTOS Kernel V11.2.0 in the xEventGroupWaitBits reference.

Taggedcfreertos
Share

More snippets

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.