How do interrupt priorities work on Cortex-M? Lower number means?
Short answer: On Cortex-M a lower priority number means higher urgency. Reset (−3), NMI (−2) and HardFault (−1) have fixed negative priorities and are the most urgent; configurable exceptions and interrupts have priorities from 0 (most urgent) upward. A higher-priority (lower-number) interrupt can pre-empt a running lower-priority handler; equal priorities never pre-empt each other.
Implemented bits: the priority field is 8 bits, but chips implement only the top few bits (e.g. 4 bits on STM32 → 16 levels, written as level << 4). CMSIS NVIC_SetPriority(IRQn, 5) does the shift for you using __NVIC_PRIO_BITS; writing raw registers you must shift yourself. Unimplemented low bits read as zero.
Priority grouping (Armv7-M and Armv8-M Mainline)
The priority value can be split into group (pre-emption) priority and sub-priority with NVIC_SetPriorityGrouping() (the PRIGROUP field in SCB->AIRCR):
- Only the group priority decides whether one interrupt can pre-empt another.
- The sub-priority only decides the order in which pending interrupts with the same group priority are serviced.
- If both are equal, the lower exception number goes first.
Example with 4 implemented bits and grouping "2 bits group, 2 bits sub": priorities 0x40 and 0x50 have the same group (1) and different sub-priorities, so neither pre-empts the other.
Practical guidance
- Give the most time-critical, short ISRs the highest priority (lowest numbers).
- With FreeRTOS, ISRs that call RTOS APIs must have a numeric priority ≥
configMAX_SYSCALL_INTERRUPT_PRIORITY(i.e. less urgent); more urgent ISRs must never call RTOS functions. This trips up many people because of the inverted numbering. - PendSV should be the lowest priority (largest number), SysTick usually low as well.
- Keep the grouping simple (all bits as pre-emption priority is common, and what FreeRTOS recommends).
Armv6-M (M0/M0+): only 2 implemented bits (4 levels), no grouping, no BASEPRI.