Event-driven architecture in FreeRTOS - Time & Space Complexity
When using event-driven architecture in FreeRTOS, tasks wait for events to happen before running. We want to understand how the time to handle events changes as more events occur.
How does the system's work grow when more events are sent and processed?
Analyze the time complexity of the following code snippet.
// Task waits for events and processes them
void vEventTask(void *pvParameters) {
EventBits_t uxBits;
for (;;) {
uxBits = xEventGroupWaitBits(xEventGroup, EVENT_1 | EVENT_2, pdTRUE, pdFALSE, portMAX_DELAY);
if (uxBits & EVENT_1) {
// Handle EVENT_1
}
if (uxBits & EVENT_2) {
// Handle EVENT_2
}
}
}
This task waits for one or more events and then processes each event that occurred.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The infinite loop that waits for events and processes them.
- How many times: The loop runs forever, processing events as they come.
Each time an event occurs, the task wakes and processes it. If more events happen at once, the task processes each one in turn.
| Input Size (number of events) | Approx. Operations |
|---|---|
| 1 | 1 event processed |
| 5 | 5 events processed |
| 10 | 10 events processed |
Pattern observation: The work grows directly with the number of events to handle.
Time Complexity: O(n)
This means the time to process events grows linearly with the number of events received.
[X] Wrong: "The task processes all events instantly, so time does not grow with more events."
[OK] Correct: Each event needs some time to be handled, so more events mean more total processing time.
Understanding how event-driven tasks scale helps you design responsive and efficient embedded systems. This skill shows you can think about system behavior as workload changes.
"What if the task handled events using multiple smaller tasks instead of one? How would the time complexity change?"
Practice
Solution
Step 1: Understand event-driven architecture basics
Event-driven architecture means the program waits and reacts only when events happen, not running tasks all the time.Step 2: Connect to FreeRTOS event groups
FreeRTOS event groups let tasks wait efficiently for events, saving CPU time by not running unnecessarily.Final Answer:
The program reacts only when an event occurs, saving CPU time. -> Option CQuick Check:
Event-driven = React on event, save CPU [OK]
- Thinking event-driven runs all tasks continuously
- Believing it uses more CPU by checking constantly
- Confusing event-driven with disabling multitasking
Solution
Step 1: Recall FreeRTOS event group creation syntax
The official FreeRTOS function to create an event group is xEventGroupCreate(), returning EventGroupHandle_t.Step 2: Match the correct syntax
EventGroupHandle_t xEventGroup = xEventGroupCreate(); uses the correct function name and type: EventGroupHandle_t xEventGroup = xEventGroupCreate();Final Answer:
EventGroupHandle_t xEventGroup = xEventGroupCreate(); -> Option BQuick Check:
Correct function and type = EventGroupHandle_t xEventGroup = xEventGroupCreate(); [OK]
- Using wrong function names like createEventGroup()
- Missing the correct handle type EventGroupHandle_t
- Incorrect capitalization or function syntax
<pre>EventGroupHandle_t xEventGroup = xEventGroupCreate(); // Task A waits for bit 0 and bit 1 EventBits_t uxBits = xEventGroupWaitBits(xEventGroup, (1 << 0) | (1 << 1), pdTRUE, pdTRUE, portMAX_DELAY); // Task B sets bit 0 xEventGroupSetBits(xEventGroup, (1 << 0)); // Task C sets bit 1 xEventGroupSetBits(xEventGroup, (1 << 1)); // What is the value of uxBits after Task A unblocks?
Solution
Step 1: Understand xEventGroupWaitBits parameters
Task A waits for bits 0 and 1 together (bitmask 0x03), clears bits on exit (pdTRUE), waits for all bits (pdTRUE), and waits indefinitely.Step 2: Analyze bit setting by Task B and Task C
Task B sets bit 0 (0x01), Task C sets bit 1 (0x02). When both bits are set, Task A unblocks and uxBits contains both bits set (0x03).Final Answer:
0x03 (both bits 0 and 1 set) -> Option AQuick Check:
Wait for bits 0 & 1 set = 0x03 [OK]
- Assuming only one bit is set when unblocked
- Ignoring that wait is for all bits (pdTRUE)
- Confusing bit masks with decimal values
EventGroupHandle_t xEventGroup; xEventGroupSetBits(xEventGroup, (1 << 0));
Solution
Step 1: Check event group initialization
xEventGroup is declared but never created with xEventGroupCreate(), so it is uninitialized (likely NULL).Step 2: Understand consequences of uninitialized handle
Calling xEventGroupSetBits with an uninitialized handle causes undefined behavior or crash.Final Answer:
xEventGroup is not created before use. -> Option DQuick Check:
Create event group before use [OK]
- Forgetting to call xEventGroupCreate()
- Thinking bit mask is invalid
- Misunderstanding handle vs pointer usage
Solution
Step 1: Understand the parameters for xEventGroupWaitBits
The third parameter clears bits on exit (pdTRUE), the fourth parameter controls if waiting for all bits (pdTRUE) or any bit (pdFALSE).Step 2: Match requirement to wait for either bit 2 or bit 3, but not both
Waiting for any bit means pdFALSE for the fourth parameter. Using bitmask (1 << 2) | (1 << 3) waits for either bit 2 or bit 3.Final Answer:
xEventGroupWaitBits(xEventGroup, (1 << 2) | (1 << 3), pdTRUE, pdFALSE, portMAX_DELAY); -> Option AQuick Check:
Wait for any bit = pdFALSE, bitmask ORed = xEventGroupWaitBits(xEventGroup, (1 << 2) | (1 << 3), pdTRUE, pdFALSE, portMAX_DELAY); [OK]
- Using pdTRUE to wait for all bits instead of any
- Using bitwise AND instead of OR for bitmask
- Not clearing bits on exit when needed
Ready to prove it?
Pass the test and this pattern moves unknown
