Bird
Raised Fist0
Embedded Cprogramming~20 mins

Nested interrupts in Embedded C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Nested Interrupt Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of nested interrupt simulation
Consider the following embedded C code simulating nested interrupts. What will be the output printed to the console?
Embedded C
#include <stdio.h>

volatile int flag = 0;

void ISR1() {
    printf("ISR1 start\n");
    if (flag == 0) {
        flag = 1;
        ISR2();
    }
    printf("ISR1 end\n");
}

void ISR2() {
    printf("ISR2 start\n");
    printf("ISR2 end\n");
}

int main() {
    printf("Main start\n");
    ISR1();
    printf("Main end\n");
    return 0;
}
A
Main start
ISR2 start
ISR2 end
ISR1 start
ISR1 end
Main end
B
Main start
ISR1 start
ISR1 end
ISR2 start
ISR2 end
Main end
C
Main start
ISR1 start
ISR2 start
ISR2 end
ISR1 end
Main end
D
Main start
ISR1 start
ISR2 start
ISR1 end
ISR2 end
Main end
Attempts:
2 left
💡 Hint
Think about how ISR1 calls ISR2 before finishing.
🧠 Conceptual
intermediate
1:30remaining
Understanding interrupt priority in nested interrupts
In an embedded system with nested interrupts enabled, which statement about interrupt priority is true?
AInterrupts always execute in the order they are triggered regardless of priority.
BA higher priority interrupt can preempt a lower priority interrupt.
CA lower priority interrupt can preempt a higher priority interrupt.
DNested interrupts are disabled automatically when an interrupt occurs.
Attempts:
2 left
💡 Hint
Think about which interrupt can interrupt another.
🔧 Debug
advanced
2:30remaining
Identify the error causing nested interrupt failure
This code is intended to enable nested interrupts, but nested interrupts never occur. What is the main reason?
Embedded C
#include <stdio.h>
#include <signal.h>

void ISR1(int signum) {
    printf("ISR1 start\n");
    // Nested interrupts should be enabled here
    printf("ISR1 end\n");
}

void ISR2(int signum) {
    printf("ISR2 executed\n");
}

int main() {
    signal(SIGINT, ISR1);
    signal(SIGTERM, ISR2);
    while(1) {}
    return 0;
}
ANested interrupts are not enabled because the signal handlers do not re-enable signals inside ISR1.
BNested interrupts are disabled because the main loop is empty and blocks signals.
CNested interrupts fail because ISR2 is not called inside ISR1.
DNested interrupts are disabled because signal handlers are not registered correctly.
Attempts:
2 left
💡 Hint
Signals are blocked during their handler unless explicitly re-enabled.
📝 Syntax
advanced
1:30remaining
Identify the syntax error preventing nested interrupt simulation
Which option contains the correct syntax to enable nested interrupts in this embedded C snippet?
Embedded C
void ISR1() {
    // Enable nested interrupts here
    __enable_irq();
    // ISR code
}
A
void ISR1() {
    __enable_irq();
}
B
void ISR1() {
    enable_irq();
    // ISR code
}
C
void ISR1() {
    __enable_irq;
    // ISR code
}
D
void ISR1() {
    __enable_irq();
    // ISR code
}
Attempts:
2 left
💡 Hint
Check function call syntax carefully.
🚀 Application
expert
2:00remaining
Calculate the number of nested interrupt levels possible
An embedded system supports 4 levels of interrupt priority (0 highest, 3 lowest). Nested interrupts are enabled so that higher priority interrupts can preempt lower ones. If an interrupt of priority 3 is running, how many more nested interrupts can occur before the CPU stops nesting?
A3
B4
C2
D1
Attempts:
2 left
💡 Hint
Count how many higher priority levels exist above priority 3.

Practice

(1/5)
1.

What is the main purpose of enabling nested interrupts in embedded C?

easy
A. Allow higher priority interrupts to interrupt lower priority ISRs
B. Disable all interrupts during ISR execution
C. Prevent any interrupt from occurring
D. Increase the execution time of all ISRs

Solution

  1. Step 1: Understand interrupt priority

    Interrupts have priorities; higher priority interrupts can preempt lower priority ones if nesting is enabled.
  2. Step 2: Role of nested interrupts

    Nested interrupts allow a higher priority interrupt to interrupt an ISR of a lower priority interrupt, improving responsiveness.
  3. Final Answer:

    Allow higher priority interrupts to interrupt lower priority ISRs -> Option A
  4. Quick Check:

    Nested interrupts = higher priority preempts lower priority [OK]
Hint: Nested interrupts let urgent tasks interrupt less urgent ones [OK]
Common Mistakes:
  • Thinking nested interrupts disable all interrupts
  • Assuming nested interrupts increase ISR execution time
  • Believing nested interrupts prevent interrupts
2.

Which of the following code snippets correctly enables nested interrupts inside an ISR in embedded C?

void ISR_Handler(void) {
    // Enable global interrupts
    __enable_irq();
    // ISR code
}
easy
A. __disable_irq(); // disables global interrupts inside ISR
B. sei(); // disables interrupts globally
C. cli(); // disables interrupts globally
D. __enable_irq(); // enables global interrupts inside ISR

Solution

  1. Step 1: Identify function to enable interrupts

    In embedded C, __enable_irq() enables global interrupts, allowing nesting.
  2. Step 2: Confirm correct usage inside ISR

    Calling __enable_irq() inside ISR allows higher priority interrupts to nest.
  3. Final Answer:

    __enable_irq(); // enables global interrupts inside ISR -> Option D
  4. Quick Check:

    __enable_irq() enables interrupts inside ISR [OK]
Hint: Use __enable_irq() inside ISR to allow nesting [OK]
Common Mistakes:
  • Using __disable_irq() which disables interrupts
  • Confusing cli() and sei() functions
  • Not enabling interrupts inside ISR
3.

Consider the following code snippet in embedded C where nested interrupts are enabled:

volatile int count = 0;

void ISR_LowPriority(void) {
    __enable_irq();
    for(int i = 0; i < 5; i++) {
        count++;
    }
}

void ISR_HighPriority(void) {
    count += 10;
}

int main() {
    // Assume interrupts are configured and enabled
    while(1) {}
}

If ISR_HighPriority interrupts ISR_LowPriority exactly once during the loop, what is the final value of count after ISR_LowPriority finishes?

medium
A. 10
B. 5
C. 15
D. 20

Solution

  1. Step 1: Analyze ISR_LowPriority increments

    ISR_LowPriority increments count 5 times, so count increases by 5.
  2. Step 2: Account for ISR_HighPriority interrupt

    ISR_HighPriority adds 10 to count during ISR_LowPriority execution.
  3. Step 3: Calculate final count

    Count = 5 (from low priority) + 10 (from high priority) = 15.
  4. Final Answer:

    15 -> Option C
  5. Quick Check:

    5 + 10 = 15 [OK]
Hint: Add increments from both ISRs considering nesting [OK]
Common Mistakes:
  • Ignoring the interrupt during the loop
  • Counting only one ISR's increments
  • Assuming interrupts don't nest
4.

Given the following ISR code, identify the error that prevents nested interrupts from working correctly:

void ISR_Handler(void) {
    // ISR code
    __disable_irq();
    // Critical section
    __enable_irq();
}
medium
A. __disable_irq() disables interrupts inside ISR, preventing nesting
B. Missing __enable_irq() call to enable interrupts inside ISR
C. ISR_Handler should not contain any interrupt control calls
D. __enable_irq() disables interrupts instead of enabling

Solution

  1. Step 1: Understand effect of __disable_irq()

    Calling __disable_irq() inside ISR disables global interrupts, blocking nesting.
  2. Step 2: Check interrupt enabling

    Although __enable_irq() is called later, interrupts remain disabled during critical section, so nesting is blocked.
  3. Final Answer:

    __disable_irq() disables interrupts inside ISR, preventing nesting -> Option A
  4. Quick Check:

    Disabling interrupts inside ISR blocks nesting [OK]
Hint: Avoid disabling interrupts inside ISR to allow nesting [OK]
Common Mistakes:
  • Thinking __disable_irq() enables interrupts
  • Assuming __enable_irq() disables interrupts
  • Believing ISR should never control interrupts
5.

You have two interrupts: ISR_A with priority 1 and ISR_B with priority 2 (2 is higher). You want ISR_B to interrupt ISR_A when it occurs. Which of the following is the correct approach to implement nested interrupts in embedded C?

void ISR_A(void) {
    // Enable nested interrupts
    __enable_irq();
    // ISR_A code
}

void ISR_B(void) {
    // ISR_B code
}
hard
A. Set ISR_A priority higher than ISR_B to allow nesting
B. Enable global interrupts inside ISR_A using __enable_irq() to allow ISR_B nesting
C. Call ISR_B manually inside ISR_A to simulate nesting
D. Disable global interrupts inside ISR_A to prevent ISR_B nesting

Solution

  1. Step 1: Understand priority and nesting

    Higher priority ISR_B (priority 2) should interrupt lower priority ISR_A (priority 1).
  2. Step 2: Enable nested interrupts inside ISR_A

    Calling __enable_irq() inside ISR_A allows ISR_B to interrupt ISR_A.
  3. Step 3: Avoid disabling interrupts or wrong priority settings

    Disabling interrupts or reversing priorities prevents correct nesting.
  4. Final Answer:

    Enable global interrupts inside ISR_A using __enable_irq() to allow ISR_B nesting -> Option B
  5. Quick Check:

    Enable interrupts inside lower priority ISR for nesting [OK]
Hint: Enable interrupts inside lower priority ISR to allow higher priority nesting [OK]
Common Mistakes:
  • Disabling interrupts inside ISR_A
  • Manually calling ISR_B instead of nesting
  • Setting wrong priority order
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard