Nested interrupt handling lets higher priority interrupts pause lower priority ones. This helps your system respond faster to urgent events.
Nested interrupt handling in FreeRTOS
void ISR_Handler(void) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
// Handle interrupt
// Clear interrupt flag
// If a higher priority task was woken by ISR
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}Use portYIELD_FROM_ISR() to request a context switch if needed.
Configure interrupt priorities properly to enable nesting.
void HighPriorityISR(void) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
// Handle high priority interrupt
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}void LowPriorityISR(void) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
// Handle low priority interrupt
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}This program simulates two interrupts: low and high priority. The high priority interrupt can preempt the low priority one, showing nested interrupt handling.
#include "FreeRTOS.h" #include "task.h" #include "semphr.h" volatile int highPriorityFlag = 0; volatile int lowPriorityFlag = 0; void HighPriorityISR(void) { BaseType_t xHigherPriorityTaskWoken = pdFALSE; highPriorityFlag = 1; // Set flag portYIELD_FROM_ISR(xHigherPriorityTaskWoken); } void LowPriorityISR(void) { BaseType_t xHigherPriorityTaskWoken = pdFALSE; lowPriorityFlag = 1; // Set flag // Simulate longer processing for (volatile int i = 0; i < 100000; i++) {} portYIELD_FROM_ISR(xHigherPriorityTaskWoken); } int main(void) { // Simulate interrupts firing LowPriorityISR(); if (lowPriorityFlag) { printf("Low priority interrupt handled\n"); } HighPriorityISR(); if (highPriorityFlag) { printf("High priority interrupt handled\n"); } return 0; }
Make sure interrupt priorities are set correctly in your microcontroller to allow nesting.
Keep ISRs short to avoid long blocking of other interrupts.
Use portYIELD_FROM_ISR() to switch tasks safely after ISR.
Nested interrupts let urgent tasks interrupt less urgent ones.
Use proper priority settings and portYIELD_FROM_ISR() in FreeRTOS ISRs.
Keep interrupt code short and efficient for best results.
Practice
What is the main purpose of nested interrupt handling in FreeRTOS?
Solution
Step 1: Understand interrupt priorities
Nested interrupts allow interrupts with higher priority to interrupt those with lower priority.Step 2: Relate to FreeRTOS behavior
FreeRTOS supports nested interrupts to ensure urgent tasks get immediate attention.Final Answer:
Allow higher priority interrupts to interrupt lower priority ones -> Option AQuick Check:
Nested interrupts = higher priority interrupts interrupting lower priority [OK]
- Thinking all interrupts are disabled during task switching
- Believing interrupts run only sequentially without nesting
- Confusing nested interrupts with disabling interrupts
Which of the following is the correct way to yield from an ISR in FreeRTOS?
void ISR_Handler(void) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
// Some ISR code
// ...
// Yield if needed
?
}Solution
Step 1: Identify correct ISR yield function
In FreeRTOS, portYIELD_FROM_ISR is used to request a context switch from an ISR safely.Step 2: Check other options
taskYIELD is for tasks, vTaskDelay is for delaying tasks, portEND_SWITCHING_ISR is not a FreeRTOS API.Final Answer:
portYIELD_FROM_ISR(xHigherPriorityTaskWoken); -> Option BQuick Check:
Yield from ISR = portYIELD_FROM_ISR [OK]
- Using taskYIELD inside ISR causing errors
- Calling vTaskDelay inside ISR which is invalid
- Confusing portEND_SWITCHING_ISR with portYIELD_FROM_ISR
Given the following FreeRTOS ISR code snippet, what will be the value of xHigherPriorityTaskWoken after execution?
void ISR_Timer(void) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xSemaphoreGiveFromISR(xSemaphore, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}Assume the semaphore unblocks a higher priority task waiting.
Solution
Step 1: Understand xSemaphoreGiveFromISR behavior
This function sets xHigherPriorityTaskWoken to pdTRUE if a higher priority task is unblocked.Step 2: Given higher priority task is unblocked
Since the semaphore unblocks a higher priority task, xHigherPriorityTaskWoken becomes pdTRUE.Final Answer:
pdTRUE -> Option CQuick Check:
Higher priority task unblocked = xHigherPriorityTaskWoken pdTRUE [OK]
- Assuming xHigherPriorityTaskWoken stays pdFALSE always
- Confusing semaphore handle with boolean flag
- Ignoring the effect of unblocking higher priority tasks
Identify the error in this nested interrupt handling code snippet:
void ISR_UART(void) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xQueueSendFromISR(xQueue, &data, NULL);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}Solution
Step 1: Check xQueueSendFromISR parameters
The function requires a pointer to xHigherPriorityTaskWoken to update it if a higher priority task is woken.Step 2: Identify missing argument
Passing NULL means xHigherPriorityTaskWoken is never updated, so portYIELD_FROM_ISR may not yield correctly.Final Answer:
xHigherPriorityTaskWoken should be passed to xQueueSendFromISR -> Option DQuick Check:
Pass xHigherPriorityTaskWoken pointer to FromISR APIs [OK]
- Passing NULL instead of &xHigherPriorityTaskWoken
- Calling portYIELD_FROM_ISR without updating flag
- Using wrong variable types for task woken flag
You want to implement nested interrupt handling where a high priority interrupt can preempt a low priority one and safely switch tasks if needed. Which combination of steps is correct?
- Configure interrupt priorities properly
- Use
portYIELD_FROM_ISR()inside ISRs - Disable all interrupts during ISR execution
- Use FromISR API versions for FreeRTOS calls inside ISRs
Choose the best combination.
Solution
Step 1: Configure interrupt priorities
Proper priority setup allows nested interrupts to work correctly.Step 2: Use portYIELD_FROM_ISR and FromISR APIs
These ensure safe task switching and API calls inside ISRs.Step 3: Avoid disabling all interrupts during ISR
Disabling all interrupts prevents nesting, so step 3 is incorrect.Final Answer:
1, 2, and 4 only -> Option AQuick Check:
Nested interrupts need priorities + FromISR APIs + portYIELD_FROM_ISR [OK]
- Disabling all interrupts prevents nesting
- Forgetting to use FromISR API versions
- Ignoring interrupt priority configuration
Ready to prove it?
Pass the test and this pattern moves unknown
