# STM32F4 How are Preemption Priorities and Sub-Priorities used?

I have started working with ARM-based microcontrollers.

I not understand subpriority and preeemptionpriority. For example:

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;

Why would we use these?

• Looks like you're using an eternal library. Next time, please mention that you're doing this. I managed to find the library here on GitHub. Consider referring to the datasheet for the STM32. You can find information on interrupts there. – user103380 Jul 11 '18 at 22:08
• Joseph Yiu cortex M3 book is there. Please read. – User323693 Jul 11 '18 at 22:08

The Preemption Priority allows an ISR to be preempted (interrupted) by another interrupt of higher priority. When the higher-priority interrupt is completed, the lower-priority interrupt continues from where it left off.

Subpriority, on the other hand, has nothing to do with preemption. Say that you have two interrupts of the same priority which are both pending. The interrupt handler will choose which one to service first, based on their subpriority. Once the first one is completed, the second one will begin.

It may seem unlikely that two interrupts can happen at the same time. One common situation where this could happen is if you have an interrupt service routine that takes a long time. During this routine, multiple other interrupt triggers may have taken place. If so, the subpriority will determine which one is handled next.

Finally, keep in mind that as the actual priority increases, the priority value decreases. That is, Priority=1 will be serviced before Priority=2.

M3 or M4 core supports up to 240 interrupts.

You will need priority to be assigned to interrupts. Rather than defining 240 priority levels architecture groups a bunch of them together.

So, an intuition will be

12 main groups (group A,B,C,...) and each group has say 10 interrupt sources..

So, if there is already an interrupt being serviced in group A, then, another interrupt in same group A will not be serviced I til first interrupt is serviced completely. On the contrary, while serving first interrupt, if another interrupt occurs from a group of higher priority, then current interrupt service will be paused..

Sub priority helps when two interrupt occurs from the same group at the same time.. Then the sub priority will come into play to choose which ISR to execute.

From stm32f4-discovery.net

NVIC_IRQChannelPreemptionPriority
With this parameter you set interrupt priority, number from 0x00 to 0x0F. Let’s say we have to use USART receive interrupt and ADC conversion finished interrupt. USART is more important than ADC, so USART will have lower number than ADC.

NVIC_IRQChannelSubPriority
With this parameter you set interrupt priority, number from 0x00 to 0x0F. Let’s say you have 2 USARTs enabled, but USART1 is more important than USART2. So USART1 will have lower number than USART2.

NVIC_IRQChannelPreemptionPriority is used to determine if an interrupt that occurs after can overtake previous interrupt that is being serviced.

NVIC_IRQChannelSubPriority is used to determine priority if two interrupts occur at the same time. (If NVIC_IRQChannelSubPriority is not determined, the position in the NVIC table is used to determine the priority.)