# How to understand STM32 priority group

Blog Post created by William Chang on May 13, 2018

In my case, I am using STM32F107

1. Why need Priority group and how it works?

To increase priority control in systems with interrupts, the NVIC supports priority grouping.

This divides each interrupt priority register entry into two fields:

• An upper field that defines the group priority
• A lower field that defines a subpriority within the group.

Only the group priority determines preemption of interrupt exceptions. When the processor

is executing an interrupt exception handler, another interrupt with the same group priority as

the interrupt being handled does not preempt the handler,

If multiple pending interrupts have the same group priority, the subpriority field determines

the order in which they are processed. If multiple pending interrupts have the same group

priority and subpriority, the interrupt with the lowest IRQ number is processed first.

//quote from PM0056.PDF

2. Priority Group  (general)

PRIGROUP occupy 3 digits, which can stands for 8 different values. So we can define 8 groups in total below.

When the PRIGROUP is set to 0, in binary is 0b000. We can say, we are using Priority group 0.

See?!,  this is what we call priority group. we have 8 of them.

Priority group 0 -> PRIGROUP = 0b000 = 0

Priority group 1 -> PRIGROUP = 0b001 = 1

Priority group 2 -> PRIGROUP = 0b010 = 2

Priority group 3 -> PRIGROUP = 0b011 = 3

Priority group 4 -> PRIGROUP = 0b100 = 4

Priority group 5 -> PRIGROUP = 0b101 = 5

Priority group 6 -> PRIGROUP = 0b110 = 6

Priority group 7 -> PRIGROUP = 0b111 = 7

In Priority group 0, indicates the points that splits the PRI_n fields in the Interrupt Priority Registers into separate group priority(7th bit - 1st bit) and subpriority(0 bit) fields. So the Numbers of Group priorities is 2 the power of 7, and the Numbers of Subpriorities is 2 the power of 1.

# Let's take a look of Group priorities in keil.

//the red colors stands for which PRIGROUP

//the green colors stands for the bits for Group priority

//the blue colors stands for the bits for Subpriorities

3. priority Group (STM32F1)

the STM32F1 only use 4 bits in PRI_n fields of the Interrupt Priority Registers, not all 8 bits like general one does.

the 4 bits begin at most significant bit, which is bit 7, bit 6, bit 5 and bit 4. The numbers of the priority is 2 the power of 4, which is 16.

Let's take a look at CUBE.

So we know, STM32F1 only use 5 priority group.

Priority group 3  // = NVIC_PRIORITYGROUP_4

Priority group 4  // = NVIC_PRIORITYGROUP_3

Priority group 5  // = NVIC_PRIORITYGROUP_2

Priority group 6  // = NVIC_PRIORITYGROUP_1

Priority group 7  // = NVIC_PRIORITYGROUP_0

This picture will look better.

Let's do some experiment!

I select using the "4 bits for pre-emption priority 0 bits for subpriority"

and build the CUBEMX! Then build the KEIL project and enter the debug mode.

after executing the

We see the PRIGROUP change to

So what that means? Can you remember?

3 : means we are using Priority group 3.

What is Priority group 3.

in Priority group 3, we are using 4 Upper bits for Group priority, and other 4 bits for subpriority.

But in STM32F1 family, we only use 4 Upper bits.

So we none bits for subpriority, all the bits are used by Group priority.

Which means, 4 bits for pre-emption priority, 0 bits for subpriority.

Why priority = 64 ?

Because the value of the priority is stored in 8 bit container.

We need to convert the Group priority and subpriority into an single uint_8 value.

How to get that?

priority value = Group priority << (bits of subpriority+__NVIC_PRIO_BITS) + subpriority<<__NVIC_PRIO_BITS

What is #define __NVIC_PRIO_BITS ?

> #define __NVIC_PRIO_BITS           4U       /*!< STM32 uses 4 Bits for the Priority Levels    */

In my case, 4<<(0+4) + 0<<4 = 64.