Consider the following FreeRTOS code snippet that waits for event bits with AND condition:
EventBits_t bits = xEventGroupWaitBits(
eventGroup,
BIT_1 | BIT_2,
pdFALSE,
pdTRUE,
portMAX_DELAY
);
if ((bits & (BIT_1 | BIT_2)) == (BIT_1 | BIT_2)) {
printf("Both bits set\n");
} else {
printf("Bits not set as required\n");
}Assuming BIT_1 and BIT_2 are set in the event group before this call, what will be printed?
EventBits_t bits = xEventGroupWaitBits(
eventGroup,
BIT_1 | BIT_2,
pdFALSE,
pdTRUE,
portMAX_DELAY
);
if ((bits & (BIT_1 | BIT_2)) == (BIT_1 | BIT_2)) {
printf("Both bits set\n");
} else {
printf("Bits not set as required\n");
}Remember that xEventGroupWaitBits with pdTRUE for the waitForAllBits parameter waits until all bits are set.
The function waits until both BIT_1 and BIT_2 are set because waitForAllBits is pdTRUE. Since both bits are set before the call, the function returns immediately with those bits set, so the condition is true and "Both bits set" is printed.
Given this code snippet:
EventBits_t bits = xEventGroupWaitBits(
eventGroup,
BIT_1 | BIT_2,
pdFALSE,
pdFALSE,
portMAX_DELAY
);
if (bits & (BIT_1 | BIT_2)) {
printf("At least one bit set\n");
} else {
printf("No bits set\n");
}If only BIT_2 is set in the event group before the call, what will be printed?
EventBits_t bits = xEventGroupWaitBits(
eventGroup,
BIT_1 | BIT_2,
pdFALSE,
pdFALSE,
portMAX_DELAY
);
if (bits & (BIT_1 | BIT_2)) {
printf("At least one bit set\n");
} else {
printf("No bits set\n");
}Check the waitForAllBits parameter and what it means for waiting on bits.
With waitForAllBits set to pdFALSE, the function returns when any of the bits in the mask are set. Since BIT_2 is set, the function returns and the condition is true, printing "At least one bit set".
Analyze this code snippet:
EventBits_t bits = xEventGroupWaitBits(
eventGroup,
BIT_1 | BIT_2,
pdTRUE,
pdTRUE,
100 / portTICK_PERIOD_MS
);
printf("Bits before wait: 0x%X\n", bits);
// Assume BIT_1 and BIT_2 were set before the callWhat will be printed for "Bits before wait:"?
EventBits_t bits = xEventGroupWaitBits(
eventGroup,
BIT_1 | BIT_2,
pdTRUE,
pdTRUE,
100 / portTICK_PERIOD_MS
);
printf("Bits before wait: 0x%X\n", bits);Remember that pdTRUE for the clearOnExit parameter clears the bits after returning.
The function returns the bits that were set before clearing them. Since BIT_1 and BIT_2 correspond to bits 0x1 and 0x2, the combined mask is 0x3. So it prints "Bits before wait: 0x3".
Consider this code:
EventBits_t bits = xEventGroupWaitBits(
NULL,
BIT_1,
pdFALSE,
pdTRUE,
portMAX_DELAY
);
printf("Bits: 0x%X\n", bits);What will happen when this code runs?
EventBits_t bits = xEventGroupWaitBits(
NULL,
BIT_1,
pdFALSE,
pdTRUE,
portMAX_DELAY
);
printf("Bits: 0x%X\n", bits);Check what happens if the event group handle is invalid or NULL.
Passing NULL as the event group handle is invalid and will cause a runtime error or crash because the function tries to access memory through a NULL pointer.
Given an event group initially with no bits set, the following calls are made:
xEventGroupSetBits(eventGroup, BIT_1);
xEventGroupSetBits(eventGroup, BIT_2);
EventBits_t bits = xEventGroupWaitBits(
eventGroup,
BIT_1 | BIT_2 | BIT_3,
pdTRUE,
pdFALSE,
0
);
// After this, how many bits remain set in the event group?xEventGroupSetBits(eventGroup, BIT_1);
xEventGroupSetBits(eventGroup, BIT_2);
EventBits_t bits = xEventGroupWaitBits(
eventGroup,
BIT_1 | BIT_2 | BIT_3,
pdTRUE,
pdFALSE,
0
);Consider the clearOnExit parameter and the bits set before the wait.
The wait is for all bits BIT_1, BIT_2, and BIT_3 with clearOnExit set to pdFALSE. Since BIT_3 is not set, the wait times out immediately (timeout=0). No bits are cleared. So BIT_1 and BIT_2 remain set.
