12

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?

bitsmack
  • 16,747
  • 9
  • 52
  • 108
ziyad
  • 121
  • 1
  • 1
  • 3
  • 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](https://github.com/Laurenceb/STM32-Logger/blob/master/interrupts.c~). Consider referring to the datasheet for the STM32. You can find information on interrupts there. –  Jul 11 '18 at 22:08
  • Joseph Yiu cortex M3 book is there. Please read. – User323693 Jul 11 '18 at 22:08

3 Answers3

23

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.

bitsmack
  • 16,747
  • 9
  • 52
  • 108
4

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.

or STM32 Interrupt Service Routine Priority

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.)

Blair Fonville
  • 3,837
  • 4
  • 19
  • 45
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.

User323693
  • 9,151
  • 4
  • 21
  • 50