ISR-safe API functions (FromISR suffix) in FreeRTOS - Time & Space Complexity
When using ISR-safe API functions in FreeRTOS, it's important to know how their execution time changes as input grows.
We want to see how the cost of these functions scales when called from interrupts.
Analyze the time complexity of this ISR-safe queue send function.
void vExampleISR(void) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
int data = 42;
xQueueSendFromISR(xQueue, &data, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
This code sends data to a queue safely from an interrupt service routine.
Look for loops or repeated steps inside the ISR-safe function.
- Primary operation: The queue send checks and updates queue state.
- How many times: It runs a fixed number of steps, no loops over input size.
The function's work stays about the same no matter how many items are in the queue.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Constant steps |
| 100 | Constant steps |
| 1000 | Constant steps |
Pattern observation: The execution time stays constant regardless of input size.
Time Complexity: O(1)
This means the function takes about the same time no matter how big the queue is.
[X] Wrong: "The ISR-safe queue send takes longer if the queue has more items."
[OK] Correct: The function only updates pointers and flags, not scanning all items, so time stays constant.
Understanding how ISR-safe functions behave helps you write reliable real-time code and explain your choices clearly.
"What if the ISR-safe function had to copy multiple items instead of one? How would the time complexity change?"
Practice
What is the main reason to use FreeRTOS API functions with the FromISR suffix inside an interrupt service routine (ISR)?
Solution
Step 1: Understand ISR context constraints
Inside an ISR, normal FreeRTOS API functions are unsafe because they may block or cause race conditions.Step 2: Role of FromISR functions
Functions with theFromISRsuffix are designed to be safe in interrupt context by avoiding blocking and managing critical sections properly.Final Answer:
They are safe to call from ISRs and handle interrupt context correctly. -> Option BQuick Check:
FromISR functions = ISR safe calls [OK]
- Calling normal API functions inside ISR causing crashes
- Assuming FromISR functions disable interrupts
- Thinking FromISR creates tasks automatically
Which of the following is the correct way to declare a variable to track if a higher priority task was woken inside an ISR?
?Solution
Step 1: Identify FreeRTOS type for ISR task wake flag
FreeRTOS usesBaseType_tfor portability and definespdFALSEandpdTRUEfor boolean values.Step 2: Correct initialization
The variable must start aspdFALSEto indicate no task was woken yet.Final Answer:
BaseType_t higherPriorityTaskWoken = pdFALSE; -> Option CQuick Check:
Use BaseType_t and pdFALSE for ISR flags [OK]
- Using plain int instead of BaseType_t
- Initializing flag to 1 instead of pdFALSE
- Using bool which may not be portable
Consider this ISR code snippet:
BaseType_t higherPriorityTaskWoken = pdFALSE;
xQueueSendFromISR(queue, &data, &higherPriorityTaskWoken);
if (higherPriorityTaskWoken) {
portYIELD_FROM_ISR();
}What happens if higherPriorityTaskWoken is set to pdTRUE after xQueueSendFromISR?
Solution
Step 1: Understand higherPriorityTaskWoken flag
This flag indicates if a task with higher priority than the current one was made ready by the ISR.Step 2: Role of portYIELD_FROM_ISR()
If the flag is true, callingportYIELD_FROM_ISR()requests a context switch to that higher priority task immediately after ISR exit.Final Answer:
The ISR requests a context switch to the higher priority task immediately after ISR. -> Option AQuick Check:
higherPriorityTaskWoken true triggers context switch [OK]
- Thinking interrupts are disabled by portYIELD_FROM_ISR()
- Assuming queue send fails if flag is true
- Believing higher priority task waits for ISR to finish
Identify the error in this ISR code snippet:
void ISR_Handler(void) {
BaseType_t higherPriorityTaskWoken = pdFALSE;
xSemaphoreGive(semaphore);
if (higherPriorityTaskWoken) {
portYIELD_FROM_ISR();
}
}What is the problem here?
Solution
Step 1: Check function used for semaphore give
The code usesxSemaphoreGive, which is not safe inside ISR context.Step 2: Correct ISR-safe function usage
Inside ISR,xSemaphoreGiveFromISRmust be used withhigherPriorityTaskWokenpassed by reference to track task wake.Final Answer:
The higherPriorityTaskWoken variable is not passed to xSemaphoreGiveFromISR. -> Option DQuick Check:
Use FromISR functions with higherPriorityTaskWoken [OK]
- Using normal API functions inside ISR
- Not passing higherPriorityTaskWoken pointer
- Declaring higherPriorityTaskWoken as global unnecessarily
You want to send data from an ISR to a task using a queue. Which sequence correctly uses FreeRTOS ISR-safe API functions to do this and ensure the higher priority task runs immediately if woken?
?Solution
Step 1: Use ISR-safe queue send function
Inside ISR,xQueueSendFromISRmust be used with a pointer tohigherPriorityTaskWoken.Step 2: Check higherPriorityTaskWoken and yield
IfhigherPriorityTaskWokenis set topdTRUE, callportYIELD_FROM_ISR()to request immediate context switch.Final Answer:
BaseType_t higherPriorityTaskWoken = pdFALSE; xQueueSendFromISR(queue, &data, &higherPriorityTaskWoken); if (higherPriorityTaskWoken) portYIELD_FROM_ISR(); -> Option AQuick Check:
Use FromISR + check flag + yield [OK]
- Using normal xQueueSend inside ISR
- Not passing higherPriorityTaskWoken pointer
- Calling portYIELD_FROM_ISR unconditionally
Ready to prove it?
Pass the test and this pattern moves unknown
