0
0
Cnc-programmingConceptBeginner · 3 min read

Priority Grouping in ARM NVIC: Explanation and Usage

Priority grouping in ARM NVIC is a way to divide interrupt priority into two parts: group priority and subpriority. This helps control which interrupts can preempt others and which share the same priority level, allowing flexible and fine-grained interrupt management.
⚙️

How It Works

Imagine you have many tasks that need attention, but some are more urgent than others. The ARM Nested Vectored Interrupt Controller (NVIC) uses priority grouping to organize these tasks by splitting their priority into two parts: group priority and subpriority.

The group priority decides which interrupt can interrupt another (preempt), while the subpriority decides the order of interrupts that have the same group priority. Think of group priority as the main level of urgency, and subpriority as a tie-breaker when two interrupts have the same main level.

This system lets you control how strict or flexible the interrupt handling is. You can choose to have more group levels (more preemption options) or more subpriority levels (more fine ordering within the same group), depending on your application needs.

💻

Example

This example shows how to set priority grouping and assign priorities to two interrupts using CMSIS functions in C.

c
#include "stm32f4xx.h"  // Example MCU header

int main(void) {
    // Set priority grouping: 3 bits for group priority, 1 bit for subpriority
    NVIC_SetPriorityGrouping(3);

    // Set interrupt 1 with group priority 2 and subpriority 0
    NVIC_SetPriority((IRQn_Type)1, NVIC_EncodePriority(3, 2, 0));

    // Set interrupt 2 with group priority 2 and subpriority 1
    NVIC_SetPriority((IRQn_Type)2, NVIC_EncodePriority(3, 2, 1));

    while(1) {
        // Main loop
    }
}
Output
No runtime output; sets NVIC priority grouping and priorities for interrupts.
🎯

When to Use

Use priority grouping when you need precise control over interrupt handling in embedded systems. For example, in real-time applications like motor control or communication protocols, some interrupts must preempt others to meet timing requirements.

If you want certain interrupts to always interrupt others, assign them higher group priority. If you want to control the order of interrupts that have the same urgency, use subpriority.

This helps avoid missed deadlines and ensures critical tasks get CPU time first.

Key Points

  • Priority grouping splits interrupt priority into group priority and subpriority.
  • Group priority controls which interrupts can preempt others.
  • Subpriority orders interrupts with the same group priority.
  • Adjust grouping to balance preemption and ordering based on application needs.
  • Configured using NVIC_SetPriorityGrouping and NVIC_SetPriority functions.

Key Takeaways

Priority grouping divides interrupt priority into group priority and subpriority for flexible control.
Group priority determines interrupt preemption; subpriority sets order within the same group.
Use priority grouping to ensure critical interrupts run first in real-time systems.
Configure priority grouping with NVIC_SetPriorityGrouping and assign priorities with NVIC_SetPriority.
Balancing group and subpriority bits helps optimize interrupt handling for your application.