Consider the following FreeRTOS code snippet:
EventGroupHandle_t xEventGroup;
xEventGroup = xEventGroupCreate();
if (xEventGroup == NULL) {
printf("Failed to create event group\n");
} else {
printf("Event group created successfully\n");
}What will be printed when this code runs on a properly configured FreeRTOS system?
EventGroupHandle_t xEventGroup; xEventGroup = xEventGroupCreate(); if (xEventGroup == NULL) { printf("Failed to create event group\n"); } else { printf("Event group created successfully\n"); }
Think about what xEventGroupCreate() returns when successful.
The function xEventGroupCreate() returns a valid handle if the event group is created successfully. It returns NULL only if creation fails (e.g., insufficient memory). On a properly configured system, it should succeed and print "Event group created successfully".
Given this code snippet:
EventGroupHandle_t xEventGroup = xEventGroupCreate();
printf("Handle value: %p\n", (void *)xEventGroup);Which statement about the printed handle value is true?
EventGroupHandle_t xEventGroup = xEventGroupCreate();
printf("Handle value: %p\n", (void *)xEventGroup);Remember what type EventGroupHandle_t is and how it is printed.
EventGroupHandle_t is a pointer type (usually a pointer to an internal structure). When printed with %p, it shows a non-NULL pointer if creation succeeded. It is not an integer zero or NULL unless creation failed.
Examine this code snippet:
EventGroupHandle_t xEventGroup; xEventGroupCreate(); xEventGroupSetBits(xEventGroup, BIT_0);
What is the cause of the runtime error?
EventGroupHandle_t xEventGroup; xEventGroupCreate(); xEventGroupSetBits(xEventGroup, BIT_0);
Check if the event group handle is properly initialized before use.
The function xEventGroupCreate() returns a handle that must be assigned to xEventGroup. In this code, the return value is ignored, so xEventGroup remains uninitialized. Using it in xEventGroupSetBits() causes undefined behavior and likely a runtime error.
In FreeRTOS, if xEventGroupCreate() fails because there is not enough heap memory, what is the expected behavior?
Consider what the function returns on failure.
xEventGroupCreate() returns NULL if it cannot allocate memory for the event group. It does not block or reset the system. The caller must check for NULL to handle failure gracefully.
Given this code snippet:
EventGroupHandle_t xEventGroup = xEventGroupCreate();
xEventGroupSetBits(xEventGroup, (1 << 0) | (1 << 2));
EventBits_t uxBits = xEventGroupGetBits(xEventGroup);
printf("Bits set: %u\n", uxBits);What is the printed output?
EventGroupHandle_t xEventGroup = xEventGroupCreate(); xEventGroupSetBits(xEventGroup, (1 << 0) | (1 << 2)); EventBits_t uxBits = xEventGroupGetBits(xEventGroup); printf("Bits set: %u\n", uxBits);
Calculate the bitwise OR of (1 << 0) and (1 << 2).
(1 << 0) is 1 (binary 0001), (1 << 2) is 4 (binary 0100). Their bitwise OR is 5 (binary 0101). So uxBits will be 5.
