Tag: Nested Interrupts

  • Top Interview Questions on Interrupt Service Routine (ISR) | Master Embedded Interview Guide (2026)

    Interview Questions on Interrupt Service Routine : When preparing for embedded systems interviews, one of the most important topics is the Interrupt Service Routine (ISR). Recruiters often ask questions on ISR because it is a core concept in microcontrollers, operating systems, and real-time systems. In this guide, we’ll go through commonly asked ISR interview questions, explained in a simple way for beginners.

    What is an Interrupt Service Routine (ISR)?

    An Interrupt Service Routine (ISR) is a special block of code that executes when a hardware or software interrupt occurs. Instead of continuously checking a condition (polling), the system pauses its normal execution and jumps to the ISR to handle the event immediately.

    Example: When you press a button on a microcontroller, an interrupt occurs, and the ISR handles the button press.

    Interview Questions on Interrupt Service Routine
    Top Interview Questions on Interrupt Service Routine (ISR) Master Embedded Interview Guide (2025)

    Common Interview Questions on Interrupt Service Routine (ISR)

    1. What is an ISR in embedded systems?

    Answer: An ISR is a function that handles interrupts. It is triggered automatically when an interrupt occurs, allowing the CPU to respond quickly to external or internal events.

    2. What are the characteristics of an ISR?

    Answer:

    • Fast execution : Should be short and efficient.
    • No return values : ISR does not return values.
    • No blocking calls : Avoid using functions like delay() inside ISR.
    • Context saving : CPU saves the current state before jumping to ISR.

    3. Can an ISR call another function?

    Answer: Yes, but it should only call lightweight functions. Heavy operations inside ISR may slow down the system and block other interrupts.

    4. What are nested interrupts?

    Answer: Nested interrupts occur when a higher-priority interrupt occurs while another ISR is still running. In such cases, the CPU pauses the current ISR and executes the higher-priority one.

    5. Why should we keep ISRs short?

    Answer: Long ISRs delay the processing of other interrupts. In real-time systems, this can cause missed events and system instability.

    6. What is the difference between ISR and a normal function?

    Answer:

    • ISR executes automatically when an interrupt occurs, while a normal function is called manually.
    • ISR does not return values, while functions can.
    • ISR runs in a special CPU context and must be as short as possible.

    7. Can we use global variables inside ISR?

    Answer: Yes, but they must be declared as volatile so that the compiler does not optimize them. This ensures real-time updates between the ISR and the main program.

    8. What is interrupt latency?

    Answer: Interrupt latency is the time delay between an interrupt request and the start of ISR execution. Low latency is important for real-time systems.

    9. How do you handle multiple interrupts?

    Answer:

    • Assign priority levels to interrupts.
    • Use interrupt vectors to map different ISRs.
    • Ensure critical ISRs are shorter and higher priority.

    10. What are some best practices for writing ISRs?

    Answer:

    • Keep ISRs short and simple.
    • Use global variables carefully (mark them as volatile).
    • Avoid using printf, malloc, or delay inside ISR.
    • Handle only critical tasks inside ISR and defer others to the main loop or task scheduler.

    List of Interview Questions on Interrupt Service Routine (ISR)

    Basic Questions of Interrupt Service Routine interview questions

    1. What is an Interrupt Service Routine (ISR)?
    2. Why do we need ISR in embedded systems?
    3. What are the main features of an ISR?
    4. What is the difference between polling and ISR?
    5. What happens when an interrupt occurs?

    Intermediate Questions of ISR in embedded systems

    1. What are the rules for writing an ISR?
    2. Why should an ISR be short and fast?
    3. Can we use global variables inside ISR?
    4. What is the role of the volatile keyword in ISR?
    5. What is interrupt latency?
    6. Can an ISR return a value? Why or why not?
    7. What is the difference between ISR and a normal function?
    8. Can ISRs be nested? What are nested interrupts?
    9. How does the CPU know which ISR to execute?
    10. What is an interrupt vector table?

    Advanced Questions of common ISR interview questions

    1. How are multiple interrupts handled in a system?
    2. What are interrupt priorities?
    3. What is the difference between maskable and non-maskable interrupts?
    4. What are reentrant and non-reentrant ISRs?
    5. Can we use dynamic memory allocation (malloc/free) inside ISR?
    6. Why is it not recommended to use printf() inside ISR?
    7. What are some common mistakes while writing ISRs?
    8. How do you handle critical tasks inside ISR?
    9. What happens if an ISR takes too long to execute?
    10. How do modern microcontrollers reduce interrupt latency?

    Practical/Implementation Questions of interrupt handling in embedded systems

    1. How do you declare an ISR in C/C++?
    2. How do you enable and disable interrupts in a microcontroller?
    3. What is the difference between hardware and software interrupts?
    4. Can an ISR call another ISR?
    5. How do you share data between ISR and the main program?
    6. What are best practices for writing ISRs?
    7. What tools or debuggers can be used to test ISR execution?
    8. Can an ISR cause a system crash? How?
    9. How does an operating system handle interrupts differently from bare-metal programming?
    10. What is the difference between vectored and non-vectored interrupts?

    1.What is an Interrupt Service Routine (ISR)?

    Ans: An ISR is a special function that executes automatically when an interrupt occurs, allowing the CPU to handle events quickly without polling.

    2.Why do we use ISR in embedded systems?

    Ans: ISR reduces CPU load, improves response time, and makes systems more efficient by handling events only when they occur.

    3. Can an ISR return a value?

    Ans: No, ISRs cannot return values because they are executed by hardware/OS, not called like regular functions.

    4. Why should ISR be short and fast?

    Ans: Long ISRs block other interrupts and can cause system delays or missed events.

    5. Can we use global variables in ISR?

    Ans: Yes, but they should be declared as volatile to ensure real-time updates between ISR and the main program.

    6. What is interrupt latency?

    Ans: Interrupt latency is the time delay between an interrupt request and the start of ISR execution.

    7. Can we use printf or delay inside ISR?

    Ans: No, functions like printf, malloc, or delay should be avoided inside ISR as they are slow and may cause system instability.

    8. What is the difference between ISR and normal function?

    Ans:
    a) ISR executes automatically when an interrupt occurs, while a normal function is called explicitly.
    b) ISR does not return values, while functions can.
    c) ISR runs in a special CPU context.

    9. What are nested interrupts?

    Ans: Nested interrupts occur when a higher-priority interrupt interrupts the execution of a lower-priority ISR.

    10. What are best practices for writing ISRs?

    Ans:
    a) Keep ISRs short and efficient.
    b) Use volatile for shared variables.
    c) Avoid blocking functions.
    d) Handle only critical tasks inside ISR, defer the rest to main loop or task scheduler.
    Interrupt Service Routine
    Interrupt Service Routine (ISR) Definition, Examples, Best Practices Embedded Interview Guide (2025)
  • Interrupt Service Routine (ISR): Definition, Examples, Best Practices | Embedded Interview Guide

    Learn what an Interrupt Service Routine (ISR) is, how it works, with C examples, NVIC priorities, latency tips, Linux top/bottom halves, and common pitfalls.An Interrupt Service Routine (ISR) is a short, high-priority function that runs automatically when hardware or software triggers an interrupt. Its job is to respond fast, clear the source, and defer heavy work, so the system stays responsive and deterministic.

    What Is an Interrupt Service Routine?

    An Interrupt Service Routine (ISR) is a special function invoked by the CPU when an interrupt occurs—an asynchronous event like a timer tick, GPIO edge, UART byte received, or a fault condition. The CPU pauses the main thread, saves minimal context, jumps to the ISR address from the interrupt vector table, executes the routine, then returns to the pre-empted code.

    Why ISRs Exist

    • Responsiveness: Process time-critical events immediately.
    • Efficiency: Avoid constant polling loops.
    • Determinism: Bound the delay (latency) to meet real-time deadlines.

    Core Concepts You Should Know

    Interrupt Vector Table (IVT)

    A table of addresses that map each interrupt source to its ISR entry point. On ARM Cortex-M, the IVT (vector table) starts at a fixed address (often 0x00000000 or relocated) and includes reset and exception vectors.

    Maskable vs Non-Maskable

    • Maskable interrupts: Can be disabled (masked) by the CPU or software.
    • Non-Maskable Interrupt (NMI): Highest priority, cannot be masked, used for critical faults.

    Priority and Preemption (NVIC on ARM Cortex-M)

    The Nested Vectored Interrupt Controller (NVIC) assigns priorities. A higher-priority interrupt can preempt a lower one (nested interrupts). Proper priority design reduces worst-case latency for critical sources.

    Latency vs Response vs Service Time

    • Interrupt latency: Time from event to ISR start.
    • Response time: Latency plus any hardware pipeline delays.
    • Service time: Time spent inside the ISR.
      Goal: Minimize latency and service time to protect real-time behavior.

    Golden Rules for Writing ISRs

    1. Keep ISRs short and deterministic.
    2. Clear the interrupt source early (status/flag register).
    3. Never block: no delays, no busy-waits, avoid printf.
    4. Avoid dynamic allocation and heavy computations.
    5. Use volatile for shared variables modified in ISR and main/threads.
    6. Defer work to a main loop, RTOS task, or bottom half.
    7. Minimize critical sections (time with interrupts disabled).
    8. Make ISRs reentrant-safe or explicitly non-reentrant via hardware.
    9. Respect priority scheme; test worst-case nesting.
    10. Instrument and measure latency/jitter with GPIO toggles + scope/logic analyzer.

    Minimal C Example (Bare-Metal, Cortex-M Style)

    #include <stdint.h>
    #include "stm32f4xx.h" // device header (example)
    
    volatile uint8_t button_event = 0;
    
    void EXTI0_IRQHandler(void) {
        // Check pending flag for EXTI line 0
        if (EXTI->PR & EXTI_PR_PR0) {
            EXTI->PR = EXTI_PR_PR0;   // 1) Clear interrupt source early
            button_event = 1;         // 2) Set a small flag (volatile)
            // 3) Do not debounce here; defer heavy work to main/task
        }
    }
    
    int main(void) {
        // ... GPIO/EXTI/NVIC init: configure PA0 as input, EXTI0 on rising edge
        while (1) {
            if (button_event) {
                button_event = 0;
                // Handle button: debounce in main context or schedule a task
            }
            // other non-blocking work
        }
    }
    

    Why this is good: short ISR, clears the flag, uses a volatile flag, defers the work.

    With an RTOS (FreeRTOS) — Deferring Work Correctly

    // Assume a task waits on a notification or queue
    extern TaskHandle_t buttonTaskHandle;
    
    void EXTI0_IRQHandler(void) {
        BaseType_t xHigherPriorityTaskWoken = pdFALSE;
    
        if (EXTI->PR & EXTI_PR_PR0) {
            EXTI->PR = EXTI_PR_PR0;
            vTaskNotifyGiveFromISR(buttonTaskHandle, &xHigherPriorityTaskWoken);
            portYIELD_FROM_ISR(xHigherPriorityTaskWoken); // request context switch if needed
        }
    }
    

    Notes: Use the FromISR variants only. They are designed to be ISR-safe and avoid locking issues.

    Linux Driver Perspective : Top Half vs Bottom Half

    In the Linux kernel, the top half is the fast interrupt handler (ISR) that acknowledges the device and schedules a bottom half (e.g., tasklet or workqueue) to complete longer processing in process context.

    static irqreturn_t my_irq_handler(int irq, void *dev_id)
    {
        struct mydev *d = dev_id;
        u32 status = readl(d->mmio + STATUS);
    
        if (!(status & IRQ_OCCURRED))
            return IRQ_NONE;
    
        writel(status, d->mmio + STATUS); // Ack/clear quickly
        schedule_work(&d->work);          // Defer heavy work
        return IRQ_HANDLED;
    }
    

    This pattern mirrors the embedded “set a flag and get out quickly” philosophy.

    Designing Priorities and Handling Nested Interrupts

    • Put hard real-time sources (e.g., motor control, high-rate ADC, safety signals) at higher priority.
    • Keep their ISRs ultra-short; move computations to lower priority tasks.
    • Test nesting: Use synthetic interrupt storms to verify the system remains stable and meets deadlines.
    • Beware of priority inversion with shared resources — use lock-free queues or ISR-safe ring buffers.

    Reducing Interrupt Latency and Jitter

    • Disable interrupts for the shortest possible time.
    • Avoid large critical sections (__disable_irq() / __enable_irq() sparingly).
    • Use branch-free, cache-friendly code in hot paths.
    • Ensure vector table is in fast memory (when relocatable).
    • Tune compiler optimization for ISR sections (inline, -O2/-O3 judiciously).
    • Prefer DMA + ISR for completion events instead of byte-wise ISRs.

    Common Mistakes (and Fixes)

    • Forgetting volatile: Variables changed in ISR must be volatile to avoid compiler reordering/optimization issues.
    • Not clearing the interrupt flag: Causes immediate retrigger or lockup.
    • Work inside ISR too heavy: Leads to missed deadlines; always defer.
    • Calling non-reentrant APIs (like printf, malloc) in ISR: avoid or provide ISR-safe alternatives.
    • Priority misconfiguration: A low-priority critical ISR getting delayed.
    • Debouncing in ISR: Use timers or software debouncing in main/task context instead.

    Testing & Debugging ISRs

    • Oscilloscope method: Toggle a GPIO at ISR entry/exit to measure latency and service time.
    • Logic analyzer: Correlate multiple events (e.g., RX line vs ISR start).
    • Fault handlers: Implement HardFault/NMI handlers with minimal logging hooks.
    • Stress tests: Burst interrupts, nested paths, and DMA completion storms.
    • Static analysis: Check for race conditions, missing volatile, and reentrancy issues.

    Quick ISR Checklist (Pin or print)

    • ISR is short and clears the source early
    • Uses volatile for shared flags/counters
    • No blocking calls, no dynamic allocation
    • Work deferred to main/task/bottom half
    • Priorities documented and tested for nesting
    • Latency/jitter measured with GPIO or tracing
    • Minimal time with interrupts disabled
    • Safe access to shared peripherals/buffers

    Top Interview Questions on Interrupt Service Routine (ISR)

    Now that you have a comprehensive understanding of Interrupt Service Routine (ISR) , you are well-equipped to confidently solve related problems and excel in interviews in one shot. Focus on mastering questions such as:

    1. What is an Interrupt Service Routine (ISR) in embedded systems, and why is it important?
    2. Explain how an ISR differs from a normal function call in embedded programming.
    3. What are the key steps involved in writing an effective ISR?
    4. How do you ensure minimal latency in an ISR execution?
    5. What are the best practices for ISR design in embedded systems?
    6. How is ISR priority determined in a microcontroller or processor?
    7. Explain the role of interrupt vectors in ISR execution.
    8. How do you handle nested interrupts in an embedded application?
    9. What are the common mistakes to avoid when writing an ISR?
    10. How do ISRs interact with the main program or RTOS tasks?
    11. Explain the impact of interrupt latency on real-time performance.
    12. How do you debug and test ISR functions in embedded systems?
    13. What tools and techniques are commonly used for ISR profiling?
    14. How can ISR execution affect system stability and reliability?
    15. Share an example where ISR optimization significantly improved performance in your embedded project.

    FAQ: Interrupt Service Routine

    Q1. What exactly is an Interrupt Service Routine?
    An ISR is a high-priority function that automatically runs when an interrupt occurs, handles the event quickly, and returns control to normal code.

    Q2. What should never be inside an ISR?
    Blocking calls, long loops, printf, dynamic memory allocation, or any heavy computation. Defer to a task/bottom half.

    Q3. How do I share data between ISR and main code safely?
    Use volatile for simple flags/counters. For larger data, use lock-free ring buffers or RTOS queues with FromISR APIs.

    Q4. What is interrupt latency and how do I reduce it?
    Latency is the delay from the event to ISR start. Reduce by shortening disabled-interrupt regions, optimizing priorities, and keeping ISRs minimal.

    Q5. Are nested interrupts good or bad?
    They’re essential for critical events but must be used carefully. Keep higher-priority ISRs ultra-short and validate worst-case timing.

    Conclusion

    An Interrupt Service Routine is the backbone of a responsive embedded and real-time system. Design for speed, simplicity, and determinism: acknowledge the event, clear the source, and defer the heavy work. With disciplined priorities, careful sharing of data, and solid testing, your ISRs will meet deadlines and keep the whole system stable.

    Interrupt Service Routine
    Interrupt Service Routine (ISR) Definition, Examples, Best Practices Embedded Interview Guide (2025)