Skip to content
Tech Interview Prep
Technical interview guide

Interrupts & Interrupt Service Routines

How hardware interrupts preempt normal execution, and the discipline required to write an ISR that doesn't corrupt shared state or blow the interrupt budget.

Minutes to read
4
Practice MCQs
25
Interview Q&A
25
Edition
v4
Editorial status
Reviewed

Scope: CMSIS-Core 6.2 interrupt APIs; current FreeRTOS Cortex-M port guidance; current GCC interrupt attributes and Arm ABI reviewed 2026-09-04.

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.

tentry=12168 MHz≈71 nst_{\text{entry}} = \frac{12}{168\text{ MHz}} \approx 71\text{ ns}

Latency is a sum, not the entry figure alone. Illustrative worst case at 168 MHz: 244 cycles, about 1.45 µs.

tlatency=tcurrent instr+tmasked+thigher ISR+tentryt_{\text{latency}} = t_{\text{current instr}} + t_{\text{masked}} + t_{\text{higher ISR}} + t_{\text{entry}}

Rough CPU budget: keep the interrupt load below about 30% of the core.

rate×(tentry+thandler+texit)≤0.3\text{rate} \times (t_{\text{entry}} + t_{\text{handler}} + t_{\text{exit}}) \le 0.3

How it works

  1. The core finishes the current instruction and pushes R0-R3, R12, LR, PC and xPSR, 32 bytes in all.
  2. It reads the handler address at VTOR + 4N, loads LR with EXC_RETURN and sets IPSR.
  3. 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.
  4. On return the core unstacks. If another exception is pending, it tail-chains instead.
yes no Device asserts line NVIC sets pending Higher priority and unmasked Stack 8 words and fetch VTOR plus 4N ISR clears device cause Enqueue bounded data Return via EXC_RETURN Stays pending

When to use and when not to

SituationInterruptPollingDMA
Rare event, bounded latency neededYesWastes CPUOverkill
Wait of a few µsEntry and exit cost more than the waitYes, timing stays obviousNo
Very high rate, e.g. 10 MB/s SPICannot service per byteNoOne interrupt per buffer
Only the newest value mattersOverwrites a shared variableRead at a fixed rateNo

Traps interviewers probe

  1. 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.
  2. 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: _Atomic or __atomic operations, a single writer, or a short critical section around the access.
  3. 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.
  4. 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.
  5. 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:

  1. The core stacks eight words and fetches the handler from VTOR + 4N: 12 cycles, about 71 ns at 168 MHz.
  2. The ISR clears the device cause first. The NVIC pending bit then clears when the line deasserts.
  3. Work stays bounded: enqueue to a ring buffer with one writer per index, and defer the rest.
  4. 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.