STM32 NVIC Priority Calculator

STM32 NVIC Priority Calculator

Configure STM32 NVIC interrupt priority grouping. Calculate preemption and sub-priority levels.

Key Formulas

Group config: NVIC_PriorityGroupConfig() sets split

Priority = (Preemption << SubBits) | Sub-priority

Frequently Asked Questions

What does the STM32 NVIC Priority Calculator compute?

This tool calculates how STM32’s Nested Vectored Interrupt Controller (NVIC) interprets interrupt priority values based on the selected priority grouping (PRIGROUP). It decomposes a given priority value into preemption and sub-priority levels, displays the corresponding binary bit allocation, and shows which interrupts can preempt each other.

Why is NVIC priority grouping important in STM32 firmware development?

Priority grouping determines how many bits are allocated to preemption priority (controlling interrupt nesting) versus sub-priority (resolving same-preemption-level conflicts). Choosing the wrong grouping can cause unexpected interrupt behavior—such as missed deadlines or unresponsive ISRs—especially in real-time applications like motor control or communication stacks.

What do “Total Priority Bits” and “Group Config” represent?

“Total Priority Bits” reflects the number of implemented priority bits in your STM32 device (typically 4 for most Cortex-M3/M4 MCUs, up to 8 for newer parts). “Group Config” (0–7) selects the PRIGROUP setting, defining how those bits are split between preemption and sub-priority fields per the ARMv7-M specification.

What are valid ranges for Interrupt Priority and Sub-priority inputs?

Interrupt Priority (0–15) is the raw 4-bit priority value written to the NVIC_IPR register. Sub-priority is only meaningful when derived *from* that value and the current grouping—it’s not an independent input. The calculator validates inputs against the selected grouping; e.g., with Group = 5 (2 preemption + 2 sub bits), valid priorities range from 0–15, but only 4 unique sub-priority values (0–3) exist.

How do I determine the correct Group Config for my application?

Start with Group Config = 4 (3 preemption + 1 sub-priority bit) for balanced nesting and simple prioritization. Use higher Group values (e.g., 5–7) if you need fine-grained preemption control (more preemption levels), or lower values (0–3) if you require more sub-priority resolution within the same preemption level. Always verify against your RTOS or HAL library defaults.

Why does changing Group Config affect which interrupts can preempt each other?

Preemption occurs only when an incoming interrupt has a *higher* preemption priority (numerically lower value) than the currently executing one. Changing Group Config reshuffles how bits map to preemption vs. sub-priority—so the same raw priority value may yield different preemption ranks across groupings, altering interrupt nesting behavior.

Can this calculator be used for all STM32 families?

Yes—for all Cortex-M-based STM32 lines (F0/F1/F3/F4/F7/H7/L0/L1/L4/MP1/G0/G4). However, confirm your device’s actual priority bit width (e.g., some G0 devices implement only 2 bits) via its reference manual, and adjust “Total Priority Bits” accordingly. The calculator respects ARM-defined PRIGROUP encoding regardless of series.

What happens if I enter an invalid priority value for the selected Group Config?

The calculator accepts inputs in the full 0–15 range for compatibility, but highlights inconsistencies—e.g., entering sub-priority = 5 when only 2 bits are allocated yields a value beyond the valid range (0–3). Results will still compute correctly using bitwise truncation, but such inputs indicate misconfiguration and should be corrected.

How does this relate to HAL_NVIC_SetPriority() and CMSIS functions?

HAL_NVIC_SetPriority() internally applies the same PRIGROUP-aware bit-shifting logic this calculator demonstrates. Understanding the output helps debug why HAL_NVIC_SetPriority(IRQn, 2, 1) behaves differently under Group Config = 4 vs. Group Config = 6—and ensures your HAL or custom NVIC setup matches hardware expectations.

Is there a way to reverse-calculate the raw priority value from preemption and sub-priority?

Yes—the calculator performs this bidirectionally. When you input preemption and sub-priority values (derived from Group Config), it computes the exact 4-bit raw priority byte written to NVIC_IPR. This is essential for manual register programming or verifying auto-generated priority tables in complex interrupt-driven systems.