Updated Jun 2025
xEventGroupWaitBits()
event_groups.h
1EventBits_t xEventGroupWaitBits(2 const EventGroupHandle_t xEventGroup,3 const EventBits_t uxBitsToWaitFor,4 const BaseType_t xClearOnExit,5 const BaseType_t xWaitForAllBits,6 TickType_t xTicksToWait );
Read bits within an RTOS event group, optionally entering the Blocked state (with a timeout) to wait for a bit or group of bits to become set.
This function cannot be called from an interrupt.
The RTOS source file FreeRTOS/source/event_groups.c must be included in the build for the
xEventGroupWaitBits()
Parameters:
-
xEventGroup
The event group in which the bits are being tested. The event group must have previously been created using a call to xEventGroupCreate().
-
uxBitsToWaitFor
A bitwise value that indicates the bit or bits to test inside the event group. For example, to wait for bit 0 and/or bit 2 set
to 0x05. To wait for bits 0 and/or bit 1 and/or bit 2 setuxBitsToWaitForto 0x07. Etc.uxBitsToWaitFormust not be set to 0.uxBitsToWaitFor -
xClearOnExit
If
is set toxClearOnExitthen any bits set in the value passed as thepdTRUEparameter will be cleared in the event group beforeuxBitsToWaitForreturns ifxEventGroupWaitBits()returns for any reason other than a timeout. The timeout value is set by thexEventGroupWaitBits()parameter. IfxTicksToWaitis set toxClearOnExitthen the bits set in the event group are not altered when the call topdFALSEreturns.xEventGroupWaitBits() -
xWaitForAllBits
is used to create either a logical AND test (where all bits must be set) or a logical OR test (where one or more bits must be set) as follows:xWaitForAllBitsIf
is set toxWaitForAllBitsthenpdTRUEwill return when either all the bits set in the value passed as thexEventGroupWaitBits()parameter are set in the event group or the specified block time expires.uxBitsToWaitForIf
is set toxWaitForAllBitsthenpdFALSEwill return when any of the bits set in the value passed as thexEventGroupWaitBits()parameter are set in the event group or the specified block time expires.uxBitsToWaitFor -
xTicksToWait
The maximum amount of time (specified in 'ticks') to wait for one/all (depending on the
value) of the bits specified byxWaitForAllBitsto become set.uxBitsToWaitFor
Returns:
-
The value of the event group at the time either the event bits being waited for became set, or the block time expired. The current value of the event bits in an event group will be different to the returned value if a higher priority task or interrupt changed the value of an event bit between the calling task leaving the Blocked state and exiting the
function.xEventGroupWaitBits()Test the return value to know which bits were set. If
returned because its timeout expired then not all the bits being waited for will be set. IfxEventGroupWaitBits()returned because the bits it was waiting for were set then the returned value is the event group value before any bits were automatically cleared because thexEventGroupWaitBits()parameter was set toxClearOnExit.pdTRUE
Example usage:
1#define BIT_0 ( 1 << 0 )2#define BIT_4 ( 1 << 4 )34void aFunction( EventGroupHandle_t xEventGroup )5{6 EventBits_t uxBits;7 const TickType_t xTicksToWait = 100 / portTICK_PERIOD_MS;89 /* Wait a maximum of 100ms for either bit 0 or bit 4 to be set within10 the event group. Clear the bits before exiting. */11 uxBits = xEventGroupWaitBits(12 xEventGroup, /* The event group being tested. */13 BIT_0 | BIT_4, /* The bits within the event group to wait for. */14 pdTRUE, /* BIT_0 & BIT_4 should be cleared before returning. */15 pdFALSE, /* Don't wait for both bits, either bit will do. */16 xTicksToWait );/* Wait a maximum of 100ms for either bit to be set. */1718 if( ( uxBits & ( BIT_0 | BIT_4 ) ) == ( BIT_0 | BIT_4 ) )19 {20 /* xEventGroupWaitBits() returned because both bits were set. */21 }22 else if( ( uxBits & BIT_0 ) != 0 )23 {24 /* xEventGroupWaitBits() returned because just BIT\_0 was set. */25 }26 else if( ( uxBits & BIT_4 ) != 0 )27 {28 /* xEventGroupWaitBits() returned because just BIT\_4 was set. */29 }30 else31 {32 /* xEventGroupWaitBits() returned because xTicksToWait ticks passed33 without either BIT\_0 or BIT\_4 becoming set. */34 }35}