0
0
FreeRTOSprogramming~10 mins

ISR-to-task notification pattern in FreeRTOS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ISR-to-task notification pattern
Interrupt Occurs
ISR Executes
Send Notification to Task
Task Receives Notification
Task Processes Event
Task Waits for Next Notification
When an interrupt happens, the ISR sends a notification to a task, which then wakes up and processes the event.
Execution Sample
FreeRTOS
void ISR_Handler() {
  BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  vTaskNotifyGiveFromISR(taskHandle, &xHigherPriorityTaskWoken);
  portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}

void TaskFunction(void *params) {
  for(;;) {
    ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
    // Process event
  }
}
ISR sends notification to a task; task waits for notification and processes event when notified.
Execution Table
StepContextActionNotification StateTask StateOutput/Result
1Interrupt OccursISR_Handler startsNotification = 0Task blocked waitingNo output
2ISRvTaskNotifyGiveFromISR calledNotification incremented to 1Task still blockedNo output
3ISRportYIELD_FROM_ISR checks xHigherPriorityTaskWokenNotification = 1Task unblocked if higher priorityContext switch if needed
4TaskulTaskNotifyTake calledNotification decremented to 0Task runningTask wakes and processes event
5TaskTask processes eventNotification = 0Task runningEvent handled
6TaskTask loops back to waitNotification = 0Task blocked waitingWaiting for next notification
7No new interruptNo notification sentNotification = 0Task blockedNo output
💡 Execution waits indefinitely at ulTaskNotifyTake until next ISR notification.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6
Notification Count0100
Task StateBlockedBlockedRunningBlocked
Key Moments - 3 Insights
Why does the task unblock after the ISR sends a notification?
Because in step 3, portYIELD_FROM_ISR triggers a context switch if the task has higher priority and a notification was sent, unblocking the task as shown in the execution_table.
What happens if the task does not call ulTaskNotifyTake after notification?
The notification count remains at 1 (step 2), but the task stays blocked until it calls ulTaskNotifyTake to consume the notification, as seen in steps 2 and 4.
Can notifications be lost if multiple interrupts occur before the task processes them?
No, notifications accumulate as counts (step 2 increments notification), so multiple ISR calls increase the notification count until the task processes them.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the notification count after vTaskNotifyGiveFromISR is called?
A1
B0
C2
DUndefined
💡 Hint
Check the 'Notification State' column at step 2 in the execution_table.
At which step does the task change from blocked to running?
AStep 1
BStep 3
CStep 4
DStep 6
💡 Hint
Look at the 'Task State' column in the execution_table to see when it changes to running.
If the ISR is called twice before the task runs, how would the notification count change after step 2?
ANotification count would be 1
BNotification count would be 2
CNotification count would be 0
DNotification count would reset to 0
💡 Hint
Refer to the variable_tracker and key moment about notification accumulation.
Concept Snapshot
ISR-to-task notification pattern in FreeRTOS:
- ISR sends notification using vTaskNotifyGiveFromISR.
- Task waits with ulTaskNotifyTake.
- Notification increments unblock the task.
- portYIELD_FROM_ISR triggers context switch if needed.
- Notifications accumulate if multiple ISRs occur before task runs.
Full Transcript
This visual execution trace shows how an interrupt service routine (ISR) communicates with a FreeRTOS task using the ISR-to-task notification pattern. When an interrupt occurs, the ISR runs and calls vTaskNotifyGiveFromISR to send a notification to the task. This increments the notification count. If the task has higher priority, portYIELD_FROM_ISR triggers a context switch to unblock the task. The task, which waits on ulTaskNotifyTake, then wakes up, consumes the notification (decrementing the count), and processes the event. After processing, the task waits again for the next notification. Notifications accumulate if multiple interrupts happen before the task processes them, ensuring no events are lost. The execution table and variable tracker show step-by-step changes in notification count and task state, helping beginners understand the flow and synchronization between ISR and task.