Overview
Curated: · Written: · Reviewed:
Interrupts & Interrupt Service Routines
In one line
An interrupt is a hardware-initiated call that preempts foreground code through a vector table entry. Reach for it when an event needs bounded latency and polling would waste the core. Move to DMA when the event rate is too high for a per-item handler.
Key definitions
- Exception: the umbrella term for reset, NMI, faults, SVCall, PendSV, SysTick and IRQs.
- VTOR: the register holding the vector table base. Entry N sits at VTOR + 4N.
- EXC_RETURN: the value the core loads into LR on entry (0xFFFFFFF9, 0xFFFFFFFD or 0xFFFFFFF1 on ARMv7-M). The return instruction uses it to unstack.
- PRIMASK / BASEPRI: PRIMASK masks every configurable-priority exception. BASEPRI masks exceptions at or below a threshold. Neither masks NMI or HardFault.
- Tail-chaining: an exit that starts a pending handler without unstacking and restacking the frame.
- Level-sensitive: a line that stays asserted until the device clears its cause.
Formulas and complexity
Entry takes 12 cycles with zero-wait-state memory, stacking eight words. Exit takes 12 cycles, or 6 when tail-chaining.
Latency is a sum, not the entry figure alone. Illustrative worst case at 168 MHz: 244 cycles, about 1.45 µs.
Rough CPU budget: keep the interrupt load below about 30% of the core.
How it works
- The core finishes the current instruction and pushes R0-R3, R12, LR, PC and xPSR, 32 bytes in all.
- It reads the handler address at VTOR + 4N, loads LR with EXC_RETURN and sets IPSR.
- The handler's prologue saves R4-R11 if the handler uses them. The interrupt attribute makes the compiler emit that prologue and an EXC_RETURN-preserving return.
- On return the core unstacks. If another exception is pending, it tail-chains instead.
When to use and when not to
| Situation | Interrupt | Polling | DMA |
|---|---|---|---|
| Rare event, bounded latency needed | Yes | Wastes CPU | Overkill |
| Wait of a few µs | Entry and exit cost more than the wait | Yes, timing stays obvious | No |
| Very high rate, e.g. 10 MB/s SPI | Cannot service per byte | No | One interrupt per buffer |
| Only the newest value matters | Overwrites a shared variable | Read at a fixed rate | No |
Traps interviewers probe
- Clearing NVIC pending but not the device flag. On a level line the request re-pends at once. A UART ISR that leaves RXNE set re-enters about every 16 cycles and the watchdog fires. Fix: read DR to clear RXNE. The line then deasserts and the pending bit clears with it.
- Trusting volatile for a shared counter. It stops register caching but does not make a read-modify-write atomic, and a multi-word read can tear. Fix:
_Atomicor__atomicoperations, a single writer, or a short critical section around the access. - Calling printf, malloc, a mutex or a blocking call from an ISR. A lock held by a task deadlocks the ISR. printf is unbounded, around 8 µs. Calling a FromISR API from an ISR more urgent than configMAX_SYSCALL_INTERRUPT_PRIORITY is also invalid. Fix: enqueue bounded data and do the rest in the foreground.
- Judging an ISR by its mean. A 6 µs path that occasionally runs 700 µs misses a 400 µs deadline. Fix: budget the worst-case path and move rare work such as log flushing out of the ISR.
- Assuming the table is wherever the bootloader left it. The core reads VTOR, not address 0. Fix: set VTOR to the application's table, aligned to its size, and check it in the debugger.
Interview tips
Opening: "An interrupt is a hardware call through the vector table. I check the entry sequence, what is masked, the ISR contract, how the source is acknowledged, and what state is shared."
Follow-ups to expect:
- PRIMASK versus BASEPRI, and when to use each.
- Why volatile is not enough for a counter shared with an ISR.
- What tail-chaining does to the stacked frame. The second handler sees the original context.
- Why a lower NVIC priority number is more urgent.
- How to tell a spurious interrupt from a shared line owned by another device. Check the device's own flag, and return without clearing anything you did not set.
- How to measure latency on a release image. Toggle a GPIO at entry and exit.
30-second outline:
- The core stacks eight words and fetches the handler from VTOR + 4N: 12 cycles, about 71 ns at 168 MHz.
- The ISR clears the device cause first. The NVIC pending bit then clears when the line deasserts.
- Work stays bounded: enqueue to a ring buffer with one writer per index, and defer the rest.
- Latency is the sum of the current instruction, masked sections, higher-priority handlers and entry. I measure it on the release build.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.