Interrupts Done Right: Latency, Priorities and Shared Data
An interrupt stops whatever the processor is doing and runs a handler, which is how firmware meets tight timing without polling. The same mechanism makes code run at moments nobody planned for, and the resulting bugs are intermittent, timing-dependent and hard to reproduce. Getting interrupts right comes down to three things: knowing the latency, assigning priorities deliberately and protecting data shared with handlers. This article covers each.
At a glance
- 12 cycles
- Arm’s quoted interrupt entry latency for Cortex-M3 and Cortex-M4 with zero-wait-state memory.
- Lower first
- On Cortex-M, a lower priority number means a more urgent interrupt.
- volatile
- Stops the compiler caching a shared variable. It does not make access atomic.
- Defer
- Do the minimum in the handler and pass the rest of the work to a task or the main loop.
The problem
Long handlers are the first problem. A handler that formats a log message or waits on a peripheral blocks every interrupt of equal or lower priority for the whole time, so another event is serviced late or lost altogether.
Shared data is the second. A main loop reading a 32-bit counter on an 8-bit processor, or updating a structure that a handler also writes, can be interrupted halfway through and see a value that never existed. These faults may appear once a week and vanish under the debugger.
Every variable touched by both a handler and the main code needs a stated protection strategy. Hope is not one.
Priorities and latency
Assign priorities by deadline, not by importance: the interrupt that must be serviced soonest gets the highest priority. The worst-case latency of any interrupt is its hardware entry time plus the longest time it can be held off by higher-priority handlers and by any code that disables interrupts.
Keep sections that disable interrupts to a few instructions. On Cortex-M cores with the BASEPRI register, mask only the priorities that share the data, leaving more urgent interrupts free to run. If an RTOS is used, follow its rules on which priorities may call its functions.
What good looks like
List every interrupt with its deadline and handler duration, and assign priorities in order of deadline.
Move all non-urgent work out of handlers: capture the data, set a flag or queue an event, and return.
Identify every variable shared between a handler and other code, and protect each with a critical section, an atomic access or a single-producer queue.
| cycles | |
|---|---|
| Cortex-M0 | 16 |
| Cortex-M0+ | 15 |
| Cortex-M3 | 12 |
| Cortex-M4 | 12 |
| Hazard | Example | Fix |
|---|---|---|
| Torn read | 32-bit counter read on an 8-bit core | Read with interrupts briefly disabled |
| Lost update | Read-modify-write of a shared flag word | Critical section or atomic instruction |
| Stale value | Compiler keeps a variable in a register | Declare it volatile |
| Inconsistent structure | Several fields updated by a handler | Copy under a critical section, or use a queue |
What it changes
Bounded Latency
Worst-case response times are known and checked against requirements.
No Lost Events
Short handlers keep the processor available for the next interrupt.
Data Integrity
Shared values are never read half-updated.
Reproducibility
Fewer timing-dependent bugs, and those that remain are easier to isolate.
Where to go next
Measure real handler timing by toggling a spare pin at entry and exit and watching it on an oscilloscope or logic analyser.
For streams of data, a ring buffer can avoid disabling interrupts when its indices are word-sized and each is written by only one side.
Problems this prevents
- Handlers that block other interrupts for too long
- Values read half-updated from a handler
- Priorities assigned by guesswork
- Bugs that vanish under the debugger
Discuss your project
Email support@aavico.com or use Get in Touch when the website finishes loading.