0
0
Embedded Cprogramming~7 mins

Nested interrupts in Embedded C

Choose your learning style9 modes available
Introduction

Nested interrupts let a higher priority interrupt stop a lower priority one. This helps the system respond faster to important events.

When a critical sensor needs immediate attention even if another interrupt is running.
In real-time systems where some tasks must preempt others quickly.
When handling multiple devices with different urgency levels.
To improve responsiveness in embedded control systems.
When you want to avoid missing important interrupts during long interrupt processing.
Syntax
Embedded C
void ISR_LowPriority(void) {
    // Enable global interrupts to allow nesting
    __enable_irq();
    // Low priority interrupt code
    __disable_irq();
}

void ISR_HighPriority(void) {
    // High priority interrupt code
}

Enabling global interrupts inside an ISR allows higher priority interrupts to interrupt the current ISR.

Make sure to re-disable interrupts before exiting the ISR to avoid unwanted nesting.

Examples
This example shows enabling interrupts inside a low priority ISR to allow higher priority interrupts to occur.
Embedded C
void ISR_LowPriority(void) {
    __enable_irq(); // Allow nesting
    // Handle low priority interrupt
    __disable_irq();
}
High priority ISR runs immediately if nested interrupts are enabled.
Embedded C
void ISR_HighPriority(void) {
    // Handle high priority interrupt
}
Sample Program

This program simulates nested interrupts by calling a high priority ISR inside a low priority ISR after enabling interrupts. It prints the order of execution to show nesting.

Embedded C
#include <stdio.h>
#include <stdbool.h>

volatile bool high_interrupt_triggered = false;
volatile bool low_interrupt_triggered = false;

void __enable_irq() {
    // Simulate enabling interrupts
}

void __disable_irq() {
    // Simulate disabling interrupts
}

void ISR_HighPriority(void) {
    printf("High priority interrupt started\n");
    high_interrupt_triggered = true;
    printf("High priority interrupt finished\n");
}

void ISR_LowPriority(void) {
    printf("Low priority interrupt started\n");
    __enable_irq(); // Allow nesting
    if (!high_interrupt_triggered) {
        ISR_HighPriority(); // Simulate nested high priority interrupt
    }
    __disable_irq();
    low_interrupt_triggered = true;
    printf("Low priority interrupt finished\n");
}

int main() {
    ISR_LowPriority();
    if (high_interrupt_triggered && low_interrupt_triggered) {
        printf("Both interrupts handled with nesting.\n");
    } else {
        printf("Interrupts not handled as expected.\n");
    }
    return 0;
}
OutputSuccess
Important Notes

Nested interrupts can cause complexity; use them carefully to avoid bugs.

Always save and restore processor state properly in real embedded systems.

Check your microcontroller's documentation for how to enable nested interrupts.

Summary

Nested interrupts let higher priority interrupts interrupt lower priority ones.

Enable global interrupts inside an ISR to allow nesting.

Use nested interrupts to improve responsiveness in embedded systems.