ISR best practices (keep it short) in Embedded C - Time & Space Complexity
We look at how the time an Interrupt Service Routine (ISR) takes grows with input or events.
We want to know how fast the ISR runs as more interrupts happen.
Analyze the time complexity of this simple ISR example.
void ISR_Handler(void) {
static int count = 0;
count++;
if (count >= 100) {
count = 0;
// Do a quick task
}
}
This ISR increments a counter each time it runs and does a quick task every 100 interrupts.
Look for repeated actions inside the ISR.
- Primary operation: Incrementing the counter and checking its value.
- How many times: Once per interrupt event.
The ISR runs once for each interrupt, doing a small fixed task.
| Interrupts (n) | Approx. Operations |
|---|---|
| 10 | 10 increments and checks |
| 100 | 100 increments and checks, 1 quick task |
| 1000 | 1000 increments and checks, 10 quick tasks |
Pattern observation: The work grows directly with the number of interrupts.
Time Complexity: O(n)
This means the total ISR work grows linearly with the number of interrupts.
[X] Wrong: "The ISR time stays the same no matter how many interrupts happen."
[OK] Correct: Each interrupt triggers the ISR, so total work adds up with more interrupts.
Understanding ISR time helps you write fast, reliable embedded code that handles events smoothly.
"What if the ISR called a function that loops over an array of size n? How would the time complexity change?"
Practice
Solution
Step 1: Understand ISR impact on system
ISRs temporarily stop main code and other interrupts, so long ISRs delay system response.Step 2: Reason why short ISR is important
Short ISR avoids blocking other interrupts and keeps system fast and responsive.Final Answer:
To avoid delaying other interrupts and keep system responsive -> Option DQuick Check:
Keep ISR short = avoid delays [OK]
- Thinking ISR can run long calculations
- Using blocking calls inside ISR
- Assuming ISR uses more memory for variables
Solution
Step 1: Identify safe communication from ISR
Heavy processing or blocking calls inside ISR are unsafe; printing can cause delays.Step 2: Use flag to communicate
Setting a flag variable is fast and lets main code handle processing safely.Final Answer:
Set a flag variable that main code checks -> Option CQuick Check:
Use flags to signal main code [OK]
- Calling complex functions inside ISR
- Using delays or blocking calls in ISR
- Printing inside ISR causing slowdowns
flag after the interrupt triggers once?
volatile int flag = 0;
void ISR() {
flag = 1;
}Solution
Step 1: Understand ISR effect on flag
ISR sets flag to 1 when triggered.Step 2: Check initial and final flag value
Initially flag is 0, after ISR runs once, flag becomes 1.Final Answer:
1 -> Option AQuick Check:
ISR sets flag = 1 [OK]
- Assuming flag stays 0
- Thinking ISR causes error
- Confusing volatile keyword effect
void ISR() {
while(!data_ready) {
// wait for data
}
process_data();
}Solution
Step 1: Analyze ISR code behavior
ISR contains a while loop waiting for data_ready, which blocks ISR execution.Step 2: Understand why blocking is bad in ISR
Blocking inside ISR delays other interrupts and main code, causing system issues.Final Answer:
Using a blocking loop inside ISR -> Option BQuick Check:
Blocking loops inside ISR cause problems [OK]
- Ignoring blocking loops in ISR
- Thinking ISR needs return statement
- Confusing volatile usage
Options: A) Read and process data fully inside ISR B) Use delay inside ISR to wait for sensor C) Set a flag in ISR and process data in main loop D) Print sensor data inside ISR for debugging
Solution
Step 1: Evaluate ISR workload options
Processing data fully or using delay inside ISR blocks system; printing slows ISR.Step 2: Choose safe communication method
Setting a flag in ISR and processing in main loop keeps ISR short and system responsive.Final Answer:
Set a flag in ISR and process data in main loop -> Option AQuick Check:
Flag in ISR, process later = best practice [OK]
- Doing heavy work inside ISR
- Using delays or prints in ISR
- Ignoring main loop processing
Ready to prove it?
Pass the test and this pattern moves unknown
