{"docs":[{"id":145,"title":"Interrupts Done Right: Latency, Priorities and Shared Data","heading":"Interrupts Done Right: Latency, Priorities and Shared Data","subheading":null,"category":"Firmware","body":{"root":{"type":"root","children":[{"type":"heading","tag":"h3","version":1,"children":[{"type":"text","text":"Priorities and latency","version":1}]},{"type":"paragraph","version":1,"children":[{"type":"text","text":"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.","version":1}]},{"type":"paragraph","version":1,"children":[{"type":"text","text":"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.","version":1}]}],"direction":"ltr","format":"","indent":0,"version":1}},"tags":"Firmware, Interrupts, Real-Time","cardCta":{"label":null,"link":null},"pageCta":{"label":"Talk to AAVICO engineering","link":"/contact"},"excerpt":"Interrupts let firmware respond within microseconds, and they cause some of the hardest bugs to find. Short handlers, deliberate priorities and protected shared data keep them under control.","featuredImage":null,"type":"Blog","readTime":"3 min read","publishedAt":"2026-11-15T20:12:00.000Z","heroImage":null,"introText":"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.","metrics":[{"id":"interrupts-done-right-latency-priorities-and-sha-metric-1","value":"12 cycles","label":"Arm’s quoted interrupt entry latency for Cortex-M3 and Cortex-M4 with zero-wait-state memory.","icon":null},{"id":"interrupts-done-right-latency-priorities-and-sha-metric-2","value":"Lower first","label":"On Cortex-M, a lower priority number means a more urgent interrupt.","icon":null},{"id":"interrupts-done-right-latency-priorities-and-sha-metric-3","value":"volatile","label":"Stops the compiler caching a shared variable. It does not make access atomic.","icon":null},{"id":"interrupts-done-right-latency-priorities-and-sha-metric-4","value":"Defer","label":"Do the minimum in the handler and pass the rest of the work to a task or the main loop.","icon":null}],"gallery":[],"challenge":{"root":{"type":"root","children":[{"type":"paragraph","version":1,"children":[{"type":"text","text":"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.","version":1}]},{"type":"paragraph","version":1,"children":[{"type":"text","text":"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.","version":1}]}],"direction":"ltr","format":"","indent":0,"version":1}},"blockquote":{"quote":"Every variable touched by both a handler and the main code needs a stated protection strategy. Hope is not one.","author":"","role":"AAVICO Engineering"},"supportImage1":null,"deliverables":{"root":{"type":"root","children":[{"type":"paragraph","version":1,"children":[{"type":"text","text":"List every interrupt with its deadline and handler duration, and assign priorities in order of deadline.","version":1}]},{"type":"paragraph","version":1,"children":[{"type":"text","text":"Move all non-urgent work out of handlers: capture the data, set a flag or queue an event, and return.","version":1}]},{"type":"paragraph","version":1,"children":[{"type":"text","text":"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.","version":1}]}],"direction":"ltr","format":"","indent":0,"version":1}},"results":[{"id":"interrupts-done-right-latency-priorities-and-sha-result-1","title":"Bounded Latency","description":"Worst-case response times are known and checked against requirements."},{"id":"interrupts-done-right-latency-priorities-and-sha-result-2","title":"No Lost Events","description":"Short handlers keep the processor available for the next interrupt."},{"id":"interrupts-done-right-latency-priorities-and-sha-result-3","title":"Data Integrity","description":"Shared values are never read half-updated."},{"id":"interrupts-done-right-latency-priorities-and-sha-result-4","title":"Reproducibility","description":"Fewer timing-dependent bugs, and those that remain are easier to isolate."}],"lookingAhead":{"root":{"type":"root","children":[{"type":"paragraph","version":1,"children":[{"type":"text","text":"Measure real handler timing by toggling a spare pin at entry and exit and watching it on an oscilloscope or logic analyser.","version":1}]},{"type":"paragraph","version":1,"children":[{"type":"text","text":"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.","version":1}]}],"direction":"ltr","format":"","indent":0,"version":1}},"supportImage2":null,"testimonial":{"quote":null,"author":null,"role":null,"photo":null},"challengesSolved":[{"id":"interrupts-done-right-latency-priorities-and-sha-challenge-1","title":"Handlers that block other interrupts for too long","description":null,"icon":null},{"id":"interrupts-done-right-latency-priorities-and-sha-challenge-2","title":"Values read half-updated from a handler","description":null,"icon":null},{"id":"interrupts-done-right-latency-priorities-and-sha-challenge-3","title":"Priorities assigned by guesswork","description":null,"icon":null},{"id":"interrupts-done-right-latency-priorities-and-sha-challenge-4","title":"Bugs that vanish under the debugger","description":null,"icon":null}],"relatedPosts":[],"contactUs":{"show":true},"updatedAt":"2026-11-15T17:32:32.682Z","createdAt":"2026-11-15T17:32:32.682Z"}],"hasNextPage":false,"hasPrevPage":false,"limit":10,"nextPage":null,"page":1,"pagingCounter":1,"prevPage":null,"totalDocs":1,"totalPages":1}