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.

Interrupt entry latency by core
cycles
Cortex-M016
Cortex-M0+15
Cortex-M312
Cortex-M412
Arm’s figures in clock cycles to the first handler instruction, with zero-wait-state memory. Flash wait states and bus activity add more.
Shared data hazards and fixes
HazardExampleFix
Torn read32-bit counter read on an 8-bit coreRead with interrupts briefly disabled
Lost updateRead-modify-write of a shared flag wordCritical section or atomic instruction
Stale valueCompiler keeps a variable in a registerDeclare it volatile
Inconsistent structureSeveral fields updated by a handlerCopy under a critical section, or use a queue
General techniques. The right choice depends on the core, the data size and the timing.

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.