0
0
FreeRTOSprogramming~10 mins

Event-driven architecture in FreeRTOS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Event-driven architecture
Event Occurs
Event Detected by ISR
Send Event to Queue
Task Waits on Queue
Task Receives Event
Task Processes Event
Task Waits for Next Event
Events happen and are caught by an interrupt. The event is sent to a queue. A task waits for events and processes them one by one.
Execution Sample
FreeRTOS
void ISR_Handler() {
  EventType event;
  BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  xQueueSendFromISR(eventQueue, &event, &xHigherPriorityTaskWoken);
  portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}

void EventTask(void *pvParameters) {
  EventType event;
  while(1) {
    if(xQueueReceive(eventQueue, &event, portMAX_DELAY)) {
      processEvent(event);
    }
  }
}
An ISR sends events to a queue, and a task waits on the queue to process events as they arrive.
Execution Table
StepActionEvent Queue StateTask StateOutput
1ISR_Handler triggered by eventEvent added to queueWaiting on queueNo output yet
2EventTask unblocked by eventEvent removed from queueProcessing eventprocessEvent(event) called
3EventTask finishes processingQueue emptyWaiting on queueReady for next event
4No new eventsQueue emptyWaiting on queueIdle, waiting
💡 Execution waits indefinitely for new events; no exit unless system stops
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
eventQueueempty1 eventemptyemptyempty
eventundefinedset by ISRreceived by taskprocessedready for next
xHigherPriorityTaskWokenpdFALSEpdTRUEN/AN/ApdFALSE
Key Moments - 3 Insights
Why does the task wait on the queue instead of polling?
The task uses xQueueReceive with portMAX_DELAY to block and wait efficiently, saving CPU instead of polling continuously. See execution_table step 4 where task waits idle.
What ensures the ISR does not block when sending to the queue?
ISR uses xQueueSendFromISR which is safe and non-blocking in interrupt context, as shown in execution_table step 1.
How does the system handle multiple events arriving quickly?
Events queue up in eventQueue. The task processes them one by one as it receives them from the queue, shown by eventQueue state changes in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what happens to the eventQueue?
AThe event is removed from the queue
BThe queue is cleared
CAn event is added to the queue
DThe queue is unchanged
💡 Hint
Check the 'Event Queue State' column at step 2 in execution_table
According to variable_tracker, what is the value of xHigherPriorityTaskWoken after step 1?
ApdFALSE
Bundefined
CpdTRUE
Devent
💡 Hint
Look at the 'xHigherPriorityTaskWoken' row after Step 1 in variable_tracker
If no new events occur, what is the task state at step 4 in execution_table?
AProcessing event
BWaiting on queue
CBlocked indefinitely
DTerminated
💡 Hint
See the 'Task State' column at step 4 in execution_table
Concept Snapshot
Event-driven architecture in FreeRTOS:
- ISR detects event and sends it to a queue
- Task waits (blocks) on queue for events
- Task processes events one by one
- Uses xQueueSendFromISR and xQueueReceive
- Efficient CPU use by blocking, not polling
Full Transcript
In FreeRTOS event-driven architecture, when an event happens, an interrupt service routine (ISR) catches it and sends the event to a queue using xQueueSendFromISR. A dedicated task waits on this queue using xQueueReceive with an indefinite timeout, so it blocks and saves CPU. When the task receives an event, it processes it and then waits again for the next event. This design avoids polling and allows responsive, efficient handling of asynchronous events.