0
0
Power-electronicsConceptBeginner · 4 min read

Nested Interrupt in Embedded C: Explanation and Example

A nested interrupt in Embedded C is when an interrupt occurs while another interrupt is already being handled, allowing higher priority interrupts to be serviced immediately. This helps the system respond faster to urgent events by temporarily pausing the current interrupt routine.
⚙️

How It Works

Imagine you are reading a book (main program), and suddenly your phone rings (an interrupt). You stop reading and answer the call (interrupt service routine). While on the call, another important call comes in (higher priority interrupt). Instead of ignoring it, you put the first call on hold and answer the new call immediately. This is how nested interrupts work in embedded systems.

In technical terms, when an interrupt occurs, the processor stops the main program and runs the interrupt service routine (ISR). If another interrupt with higher priority happens during this ISR, the processor pauses the current ISR and runs the new one. After finishing the higher priority ISR, it returns to the previous ISR and then back to the main program.

💻

Example

This example shows two interrupts: a low priority and a high priority. The high priority interrupt can interrupt the low priority ISR, demonstrating nested interrupts.

c
#include <stdio.h>
#include <signal.h>
#include <unistd.h>

volatile int low_priority_flag = 0;
volatile int high_priority_flag = 0;

void low_priority_ISR(int signum) {
    printf("Low priority ISR start\n");
    sleep(2); // Simulate work
    if (high_priority_flag) {
        printf("High priority interrupt detected during low priority ISR\n");
    }
    printf("Low priority ISR end\n");
    low_priority_flag = 0;
}

void high_priority_ISR(int signum) {
    printf("High priority ISR start\n");
    sleep(1); // Simulate work
    printf("High priority ISR end\n");
    high_priority_flag = 0;
}

int main() {
    signal(SIGUSR1, low_priority_ISR);  // Low priority interrupt
    signal(SIGUSR2, high_priority_ISR); // High priority interrupt

    printf("Send SIGUSR1 for low priority interrupt\n");
    printf("Send SIGUSR2 for high priority interrupt\n");

    low_priority_flag = 1;
    raise(SIGUSR1); // Trigger low priority interrupt

    sleep(1); // Wait 1 second before high priority interrupt

    high_priority_flag = 1;
    raise(SIGUSR2); // Trigger high priority interrupt

    // Wait to finish ISRs
    sleep(4);
    return 0;
}
Output
Send SIGUSR1 for low priority interrupt Send SIGUSR2 for high priority interrupt Low priority ISR start High priority ISR start High priority ISR end High priority interrupt detected during low priority ISR Low priority ISR end
🎯

When to Use

Nested interrupts are useful when your system must handle urgent tasks immediately, even if it is busy with other interrupts. For example, in a medical device, a critical sensor alert (high priority) should interrupt a routine data logging task (low priority) to respond quickly.

Use nested interrupts when you have multiple interrupt sources with different priorities and need fast response times for the most important events. However, be careful because nested interrupts can make your program more complex and harder to debug.

Key Points

  • Nested interrupts allow higher priority interrupts to interrupt lower priority ISRs.
  • This improves system responsiveness to urgent events.
  • They require careful design to avoid complexity and errors.
  • Priority levels must be configured in the microcontroller.

Key Takeaways

Nested interrupts let urgent interrupts pause current interrupt routines for faster response.
They are essential in systems with multiple interrupt priorities and time-critical tasks.
Proper priority configuration and careful coding are needed to avoid bugs.
Nested interrupts increase system complexity but improve real-time performance.
Use nested interrupts when immediate attention to high priority events is required.