AnsweredAssumed Answered

STM32F4 interrupts clarification

Question asked by grauman.joshua on Sep 21, 2016
Hello all,

I am using a STM32F411 on a custom board I built and am programming it using mbed with gcc under Linux. I am trying to understand how all the interrupt priorities work and think I have it figured out, but would like someone to confirm/deny my understanding and answer a couple questions:

1) Arm cortex m4 has two kinds of interrupt priorities: preemption priorities and sub priorities. If an interrupt has a higher (lower value) preemption it will interrupt another interrupt. If interrupts have the same preemption value, the one with the higher (lower value) sub priority will go first, but neither will interrupt the other.

2) We call NVIC_SetPriorityGrouping() to set how many bits are allocated to each kind of interrupt (of 4 total bits. Priority group 0 gives all bits to sub-priority, and Priority group 4 gives them all to preempt priority).

3) We call NVIC_SetPriority() to set the priority of a given interrupt. The value given to the interrupt 'respects' the priority grouping. So, for example, if I have the PriorityGroup set to 2 (2 bits each for preempt and sub-priority), and set the priority of an interrupt like NVIC_SetPriority(USART1_IRQn, 0xC), I just set the preempt priority to 3 (lowest) for usart 1, and the sub-priority to 0 (highest).

Now my questions:

1) Is NVIC_Init() and NVIC_SetPriority()/NVIC_EnableIRQ() redundant? It looks like using NVIC_Init() just lets me set the preempt and sub-priority and enable the interrupt which can also be done with NVIC_SetPriority() and NVIC_EnableIRQ().

2) The docs I read on NVIC_Init() say that you have 4 bits for preempt priority and 4 for sub-priority. But elsewhere I read that there are only 4 bits total, determined by the Priority Grouping? So do the values of preempt priority and sub-priority in NVIC_Init() also depend on the Priority Grouping?

3) If I want to set the priority of one of my interrupts above the rest of the my interrupts, is it ok to loop over all the interrupt values and set them all to 2 for example, and then set a single interrupt to value 1. Like this:
for(int i=0; i<86; i++)
  NVIC_SetPriority((IRQn_Type)i, 2);

4) If anyone knows mbed2: Are there any mbed specific interrupts I should be aware of? Ie. how does mbed use the systick interrupt and does it use other interrupts?