Understanding STM32 Interrupt Priority Grouping and Preemption in 10 Minutes

Click the blue text above to follow us

Embedded Training – Choose Jufeng Smart Link

STM32 interrupt priority mechanism is a concept that often confuses beginners and newcomers to projects. What is preemption? What is response priority? What is the purpose of priority grouping? Why does the priority you set not take effect at all?Don’t worry, this article will help you clarify the workings of STM32 interrupt priorities step by step from concept -> configuration -> practical application in just 10 minutes.

1. Overview of STM32 Interrupt Priority Mechanism

The interrupt control of STM32 is managed by the NVIC (Nested Vectored Interrupt Controller), which supports interrupt nesting, meaning “interrupts within interrupts”.

The interrupt priority of NVIC includes two core concepts:

  • Preemption Priority: Who can interrupt whom.

  • Response Priority / Sub Priority: In the case of equal preemption levels, who gets responded to first.

Analogy for understanding:

Preemption Priority = Hierarchical positionResponse Priority = Who has been waiting in line longer

If two interrupts have different preemption levels, the one with the higher preemption level can interrupt the lower one.If the preemption levels are the same, then the response priority is compared.

2. What is Priority Grouping?

STM32 uses the CMSIS standard to represent interrupt priorities as a 4-bit value (0~15), but how this 4-bit value is divided between preemption and response is configured by you:

<span>NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);</span>

STM32’s library functions allow you to choose between 5 “priority groups”:

Grouping Mode Preemption Priority Bits Response Priority Bits
Group 0 0 4
Group 1 1 3
Group 2 2 2
Group 3 3 1
Group 4 4 0

For example: If you set it to Group 2, then it has 2 bits for preemption priority + 2 bits for response priority.

Note: The default grouping of STM32 may not be what you expect! You must explicitly call <span>NVIC_PriorityGroupConfig()</span> to set it; otherwise, the priority you set may not take effect at all.

3. How to Use the Priority Configuration Function?

<span>NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;</span>

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1; NVIC_InitStructure.NVIC_IRQChannelSubPriority=2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

Key reminder: The priority you set (0~15) is actually mapped to a hardware priority value, which is directly related to <span>NVIC_PriorityGroupConfig()</span><span>. Not setting the group will cause the priority behavior to be completely ineffective.</span>

4. Practical Example: Which Interrupt Responds First?

We set up two interrupts:

  • <span>EXTI0_IRQn</span>: External button interrupt

  • <span>USART1_IRQn</span>: Serial port interrupt

Set as follows:

<span>NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_IRQChannelPreemptionPriority = 0;</span>

NVIC_IRQChannelSubPriority=1;// For USART NVIC_IRQChannelPreemptionPriority = 1;

NVIC_IRQChannelSubPriority = 0; // For EXTI

The result: Even if EXTI is triggered first, it will be “cut in line” by the USART interrupt.

Because USART has a higher preemption priority (0 < 1), it can interrupt the currently executing EXTI.

Conclusion: As long as the preemption priorities are different, regardless of who triggers first, the interrupt will definitely be interrupted!

5. The Three Most Common Misconceptions for Beginners

Mistake 1: Only set the priority without setting the group

The interrupt priority configuration of STM32 is dual:Priority value + Group configuration are both essential.

Mistake 2: Thinking that the sum of preemption priority and response priority cannot exceed 4

It is not “the sum”, but the number of bits defined separately by NVIC_PriorityGroup!

Mistake 3: Not knowing that certain interrupts (SysTick, PendSV) have fixed priorities

The priorities of system interrupts (like <span>SysTick</span>) are set using special registers, and cannot be configured arbitrarily with NVIC_Init!

6. Practical Advice for Debuggers

  • When debugging interrupt priorities, it is highly recommended to use a logic analyzer or serial print to mark time points.

  • It is advisable to consistently use NVIC_PriorityGroup_2, as it provides sufficient preemption priority and allows flexible allocation of response priority.

  • If you need to ensure that a certain interrupt has absolute priority, set it to Preemption = 0, and try to avoid complex processing logic to prevent interrupt blocking.

Summary

STM32 interrupt priorities may seem complex, but as long as you remember these three points, you can master them:

  1. Preemption Priority = Who can interrupt whom

  2. Response Priority = Who responds first when preemption levels are the same

  3. Priority grouping determines the “bit width allocation” of priorities

Understanding STM32 Interrupt Priority Grouping and Preemption in 10 MinutesOriginal link: https://blog.csdn.net/jk5518852/article/details/147168601

Leave a Comment