cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F2xx_StdPeriph_Lib NVIC_Init(): preemption priority, issue with PRIGROUP

reich
Associate II
Posted on June 07, 2013 at 11:56

STM32F2xx_StdPeriph_Lib_V1.1.0

NVIC_Init()

 inmisc.c has problems with AIRCR.PRIGROUP< 0b011.

After reset, SCB->AIRCR.PRIGROUP ==0b000

Calculation is then asfollows:

tmppriority

 (

7 - PRIGROUP)  ==> 7- 0b00  ==> 7

 tmppre = 4- tmppriority

 ==> (uint_8)(-3)  ==> 0xFD

 tmpsub = 0x0F >> tmppriority  ==> 0x0F >> 7  ==>0x00

 tmppriority =

PreemptionPriority

<<tmppre  ==> (0..F) << 0xFD  ==> 0x00  // always zero, independentlyof

PreemptionPriority

tmppriority |=

SubPriority

& tmpsub

  ==> (0..F) & 0x00  ==> 0x00  // alwayszero, independently of

SubPriority

 tmppriority <<= 4

  ==> 0x00  // always zero, independently of

PreemptionPriority

or

SubPriority

 

NVIC->

IP

[

IRQChannel

]

= tmppriority

That is: given Preemption- orSubPriority has no effect, all interrupts get programmed with IP==0x00,thus having the same group priority(0).

Result: nopreemption.

WithPRIGROUP == 0b011:

tmppriority

 (

7 - PRIGROUP)  ==> 7 - 0b011 ==>4

 tmppre = 4- tmppriority

 ==> 0

 tmpsub = 0x0F >> tmppriority  ==> 0x0F >> 0  ==>0x0F

 tmppriority =

PreemptionPriority

<< tmppre  ==> (0..F)<< 0  ==>

PreemptionPriority

(0..F)

tmppriority |=

SubPriority

&tmpsub

  ==> (0..F) & 0x0F  ==> 

PreemptionPriority | SubPriority

(0..F) // this makes no sense, IMHO

 tmppriority <<= 4  ==>0x00..0xF0

 NVIC->IP[IRQChannel]

= tmppriority

Is this behaviour by design?

What is the recommended procedure to set IRQ preemption priority using NVIC_Init()?

#stm32-nvic-peripheral-library
6 REPLIES 6
Posted on June 07, 2013 at 12:11

misc.c:

 How to configure Interrupts using driver

1. Configure the NVIC Priority Grouping using NVIC_PriorityGroupConfig()

JW

reich
Associate II
Posted on June 07, 2013 at 12:18

This sets only SCR->AIRCR.PRIGROUP, but does not solve the issue mentioned.

zzdz2
Associate II
Posted on June 07, 2013 at 12:47

Is this behaviour by design?

 

 

What is the recommended procedure to set IRQ preemption priority using NVIC_Init()?

 

I think you are right, NVIC_Init() is likely to be buggy.

Note that you don't really need the stm32 library, you are free to set priority registers as you like.

You can include core_cm3.h alone, it contains everything you need to access those registers and it also has some nice functions to set priorities and groups.

 

Posted on June 07, 2013 at 14:04

> This sets only SCR->AIRCR.PRIGROUP, but does not solve the issue mentioned.

Yes, it does, if you follow the instructions, i.e. use first NVIC_PriorityGroupConfig() with one of the NVIC_PriorityGroup_x - note how they are defined in misc.h.

I don't say it can't be improved, but as Knik said above, you might be better off not using it, if it does not fit your style.

JW

reich
Associate II
Posted on June 07, 2013 at 18:29

Sure, actually I'd even prefer to not use the STM32 peripheral library at all, but there are forces beyond my control that drive me to use it 😉

Anyway, programming SCR->AIRCR.PRIGROUP either directly or via NVIC_PriorityGroupConfig() still does not solve the problem that NVIC_Init() 

a) fails for PRIGROUP < 0b011, and

b) with PRIGROUP == 0b011 ORs PreemptionPriority and SubPriority together, leading to unexpected results.

Because of b), NVIC_Init() only works as expected if PreemptionPriority == SubPriority, or SubPriority == 0.

zzdz2
Associate II
Posted on June 07, 2013 at 18:47

I think I know what's the actual problem, stm32 only has 16 priority levels so setting grouping below 0b011 doesn't make sense.

Edit:

If you set grouping to 0b011 subpriority has to be zero.