Electronic – STM32F4 How are Preemption Priorities and Sub-Priorities used

arminterruptsmicrocontrollerstm32stm32f4-discovery

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?

Best Answer

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.