Interrupt Handling, Vector Tables, and Nested Interrupts
What You'll Learn
- 1Explain how a vector table dispatches exceptions
- 2Configure priorities and nesting safely
- 3Minimize interrupt latency in handlers
- 4Share data between ISRs and the main loop
Interrupts let hardware events reach software without polling, which is what makes real time response possible. A vector table maps exception numbers to handler addresses, and a priority controller decides which pending request runs first and whether it may preempt the running handler. Poor design shows up as missed deadlines, priority inversion, and race conditions. Sound interrupt engineering sets priorities from deadlines, keeps handlers short, and moves bulk work to the main loop or an RTOS task.
Vector Tables and Dispatch
On reset, the core loads the initial stack pointer and the reset handler address from a vector table in fixed memory. Peripheral and fault interrupts have their own entries, and modern cores use a table offset register so the table can live in flash or RAM. Some architectures pack handler addresses with a thumb bit that must be preserved.
Fault handlers for hard faults, bus errors, and memory management faults are the last line of diagnosis. Capturing stacked registers in a hard fault handler turns a mysterious reset into a usable crash report.
Priorities, Masking, and Nesting
Priority numbers decide preemption: a higher priority interrupt may preempt a lower one when nesting is enabled, while equal priorities queue. Masking by priority threshold or global disable protects short critical sections without blocking all interrupts for long.
Nesting multiplies reentrancy risk, because globals touched by one handler may be interrupted by another. Every shared variable needs atomic access or a lock, and every handler needs its own stack accounting for worst case depth.
Latency, Bottom Halves, and Patterns
Interrupt latency runs from hardware assertion to the first instruction of the handler and includes startup, context save, and any higher priority work. Keep the ISR to acknowledge the source, copy data, and signal a consumer; defer filtering, protocol parsing, and control math to a deferred context.
Deferred work patterns include flag polling in the main loop, RTOS queues with task notification, and software timer callbacks. Measuring worst case response with a timer and a toggle pin gives an honest latency number for the design review.
Interrupt Design Decisions
| Concern | Approach | Failure If Ignored |
|---|---|---|
| Dispatch | Vector table with reset values | Boot failure or wrong handler |
| Priority | Set from deadline criticality | Missed real time deadlines |
| Sharing | Atomic access or short locks | Torn data and race conditions |
| Workload | Acknowledge in ISR, defer rest | Long blocked higher priority work |
| Latency | Measure worst case on hardware | Optimistic timing claims |
Real-World Examples
- ADC conversion ready ISR â The handler reads the result register, stores a sample, and wakes a processing task through a queue.
- Priority based motor control â Current loop interrupt preempts a slower communication interrupt to hold loop timing at twenty kilohertz.
- Fault capture handler â A hard fault handler stores stacked registers to a noinit section before a controlled reset.
- Encoder counter ISR â Edge interrupts accumulate a quadrature count while the control loop runs in a lower priority task.
â ī¸Watch Critical Sections
Common Mistakes
Doing heavy work inside the ISR
Fix: Long handlers delay higher priority events; defer processing to a task or main loop.
Sharing data without atomic access
Fix: Multi byte values can be read half updated when an interrupt lands mid copy.
Ignoring priority inversion
Fix: A low priority task holding a lock can block a high priority ISR; use priority inheritance or short sections.
Key Takeaways
- The vector table maps exception sources to handlers at defined addresses.
- Priorities and nesting are set from deadline analysis, not convenience.
- Short handlers with deferred work keep latency predictable.
- Measured worst case latency is the number that matters in review.
Frequently Asked Questions
What is interrupt latency?
Time from a hardware event to the first instruction of its handler, including context save and any preemption.
Why is polling sometimes better than interrupts?
For very high rates or simple devices, polling avoids context overhead and gives steady timing.
How do ISR and main loop share a variable safely?
Use atomic types, a short critical section, or hand data through an RTOS queue with clear ownership.
Interview Questions
Q1: How do you decide interrupt priorities?
Rank sources by deadline and jitter tolerance, then reserve headroom so worst case nesting still meets every deadline.
Q2: Describe a good interrupt handler structure.
Acknowledge and clear the source, capture minimal data, signal the consumer, and return; everything else runs elsewhere.
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type â Always start by categorizing the problem
- Incorrect formula application â Verify the formula applies to your specific case
- Unit errors â Always check units are consistent throughout
- Sign errors â Pay careful attention to positive and negative values
- Rounding too early â Keep full precision until the final answer
- Forgetting constraints â Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic â Build on the foundation laid here
- Next Topic â See how this concept extends to more advanced material
- Practice Problems â Additional exercises with solutions
- Video Explanation â Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions