🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
NEWSLIVESearch All Content
đŸ’ŧ Servicesâ„šī¸ Aboutâœ‰ī¸ ContactView Pricing Plansfrom $10

NVIC: Interrupt Priorities, Nesting, and Pending Bits

Microcontrollers & ARMGPIO and TimersđŸŸĸ Free Lesson

NVIC: Interrupt Priorities, Nesting, and Pending Bits

What You'll Learn

  • 1Enable and disable interrupts through NVIC registers
  • 2Explain priority grouping and masking
  • 3Describe nesting, tail chaining, and late arrival
  • 4Debug stuck or retriggered interrupts

The nested vector interrupt controller sits inside the ARM core and gives every Cortex-M part the same interrupt behavior. It manages enable, pending, and active state per interrupt, plus a priority field that decides which request wins. Priorities split into a preemption field and a subpriority field, and the grouping choice determines how many hardware priority levels you actually have. Understanding the exception entry and exit mechanics is the difference between a responsive system and one that mysteriously hangs in an interrupt handler.

Enables, Pending Bits, and Priority

Each interrupt has a set enable bit, a clear enable bit, a set pending bit, a clear pending bit, an active bit, and a priority byte. Software can force an interrupt to pending to test a handler without hardware, and the active bit tells you whether the handler is currently executing. Because separate registers exist for set and clear, no read modify write is needed.

Priority values are implemented with the least significant bits truncated, so a part that implements only four priority bits offers sixteen effective levels written as multiples of sixteen. Writing a priority that is finer than the hardware resolution silently rounds, which explains many surprise priority inversions.

Priority Grouping and Masking

The priority grouping register divides the priority byte into the preemption group and the subpriority. Higher preemption value wins and can interrupt an active handler; equal preemption falls back to subpriority, where the numerically lowest value wins. A global priority mask register can block all interrupts below a threshold, which is how critical sections protect shared data.

Choosing the grouping is a system decision. Too few groups and unrelated high rate interrupts preempt critical code, too many and latency grows because urgent work waits behind preemption checks. Document the mapping from task and interrupt to priority early, because later changes ripple through the whole scheduler.

Nesting, Tail Chaining, and Late Arrival

When an interrupt arrives and its preemption priority beats the active handler, the hardware stacks the return state and vectors to the new handler, which is nesting. On exit, if another interrupt is already pending, the core skips the unstacking and jumps straight in, a mechanism called tail chaining that saves a dozen or more cycles. Late arrival happens when an interrupt pends during the entry sequence of another and gets folded in cleanly.

These mechanisms explain why measured interrupt latency differs from the sum of handler times. They also explain a classic bug: clearing an interrupt source after enabling it in the NVIC causes an immediate retrigger, so the handler runs forever and the main loop starves.

NVIC Registers and Their Job

Register BlockPurposeNotes
Set enableEnable interrupts one bit at a timeWrite to set, no read modify write
Clear enableDisable interruptsUsed to mask a source safely
Set and clear pendingForce or clear a pending requestUseful for software triggered events
Active bitShow which handlers are runningReads as one while executing
Interrupt priorityHold preemption and subpriorityTrailing bits may be unimplemented
Priority groupingSplit preemption from subpriorityConfigured in the application control register

Real-World Examples

  • Enable a peripheral interrupt — Clear the pending bit, set the priority, clear the source flag, then set the enable bit in that order.
  • Build a critical section — Raise the priority mask above your interrupt priorities, touch the shared data, then restore the mask.
  • Test a handler offline — Set the pending bit for an unused interrupt to prove the vector, priority, and handler dispatch work.
  • Diagnose a runaway interrupt — Read the active bit and the peripheral status flag to see which source never clears.

âš ī¸Clear the Source Before Enabling

If the peripheral flag is still set when you enable its NVIC channel, the interrupt pends immediately and the handler can loop forever.

Common Mistakes

Writing priorities without checking resolution

Fix: Unimplemented low bits are dropped, so two interrupts can share the same effective priority.

Doing read modify write on enable registers

Fix: Interrupts arriving during the read modify write window are lost; use the dedicated set and clear registers.

Forgetting the global priority mask

Fix: A critical section that only disables one interrupt still loses data to every other source.

Key Takeaways

  • NVIC channels have separate enable, pending, and active state.
  • Priority grouping decides which interrupts preempt and which wait.
  • Tail chaining and late arrival remove redundant unstack and stack work.
  • Interrupt sources must be cleared before the NVIC channel is enabled.

Frequently Asked Questions

What is the difference between pending and active?

Pending means a request is waiting to run, while active means the handler has been entered and has not yet returned.

Why does my interrupt fire twice?

The source flag was still set on entry, or the flag is level sensitive and the peripheral keeps reasserting it.

How many priority levels do I really have?

Check how many high order bits the part implements; the rest read as zero and write with no effect.

Interview Questions

Q1: Explain tail chaining.

If another interrupt is pending when a handler returns, the core skips the unstack and restack sequence and enters the next handler directly, saving latency.

Q2: How would you assign priorities in a motor controller?

The current trip and PWM break run at the highest preemption priority, the control loop next, communications below that, and housekeeping lowest.


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

  1. Misidentifying the problem type — Always start by categorizing the problem
  2. Incorrect formula application — Verify the formula applies to your specific case
  3. Unit errors — Always check units are consistent throughout
  4. Sign errors — Pay careful attention to positive and negative values
  5. Rounding too early — Keep full precision until the final answer
  6. 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

  1. Basic: Solve 5 problems of increasing difficulty using the core concept.
  2. Intermediate: Apply the concept to word problems with multiple steps.
  3. Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
  4. 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

Quick Reference Card

ConceptFormula/RuleKey InsightCommon Use
Core DefinitionSee main textUnderstand the intuitionFoundation for all applications
Key FormulaSee main textMemorize the derivationDirect problem solving
Special CaseSee main textRecognize when it appliesSimplifies complex problems
Edge CaseSee main textKnow the limitationsAvoids 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
CW

ChatWhole Learn Editorial Team

Written by experienced data scientists, software engineers, and educators. All content is reviewed by industry professionals to ensure accuracy and relevance.

Last reviewed: 2026-11-14

—
☆☆☆☆☆
0 ratings

Rate & Feedback

Need Expert Microcontrollers & ARM Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement