stm32 设置systick中断抢先式优先级
[plain]
view plaincopy
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPrio rity = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);
后来阅读CM3的技术参考手册,讲解了NVIC的配置。
中断分为内核中断和芯片的中断,配置的寄存器位置不同。芯片的中断配置在NVIC的IP内,内核的中断配置在SCB内。
可以直接调用core_cm3.h里的函数
[cpp]
view plaincopy
- static
__INLINE void NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority) - {
if(IRQn < 0) { SCB->SHP[((uint32_t)(IRQn) & 0xF)-4] = ((priority << (8 - __NVIC_PRIO_BITS)) & 0xff); } else { NVIC->IP[(uint32_t)(IRQn)] = ((priority << (8 - __NVIC_PRIO_BITS)) & 0xff); } - }
但是直观上来看看不到抢先式优先级与子优先级。
首先,配置优先级组,可以使用固件库的程序,也可以用core_cm3.h的函数。
[cpp]
view plaincopy
- void
NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup) - {
SCB->AIRCR = AIRCR_VECTKEY_MASK | NVIC_PriorityGroup; - }
或
[cpp]
view plaincopy
- static
__INLINE void NVIC_SetPriorityGrouping(uint32_t PriorityGroup) - {
uint32_t reg_value; uint32_t PriorityGroupTmp = (PriorityGroup & 0x07); reg_value = SCB->AIRCR; reg_value &= ~(SCB_AIRCR_VECTKEY_Msk | SCB_AIRCR_PRIGROUP_Msk); reg_value = (reg_value | (0x5FA << SCB_AIRCR_VECTKEY_Pos) | (PriorityGroupTmp << 8)); SCB->AIRCR = reg_value; - }
![](http://m.amcfsurvey.com/editerupload/fetch/20161101/322685_1_0.jpg)
随后配置各个中断的优先级,对于外设的中断可以使用固件库的程序,比较直观。如最前面所给出的例子,但是根据其实现,是不能够用来配置内核中断的,例如
[cpp]
view plaincopy
- NVIC_InitStructure.NVIC_IRQChannel
= SysTick_IRQn; - NVIC_InitStructure.NVIC_IRQChannelPreemptionPrio
rity = 0; - NVIC_InitStructure.NVIC_IRQChannelSubPriority
= 0; - NVIC_InitStructure.NVIC_IRQChannelCmd
= ENABLE; - NVIC_Init(&NVIC_InitStructure);
所以对于Sysick使用core_cm3.h的函数
[cpp]
view plaincopy
- NVIC_SetPriority(SysTick_IRQn,
4);
[cpp]
view plaincopy
- NVIC_SetPriority(SysTick_IRQn,
n); - n=0x00~0x03
设置Systick为抢占优先级0 - n=0x04~0x07
设置Systick为抢占优先级1 - n=0x08~0x0B
设置Systick为抢占优先级2 - n=0x0C~0x0F
设置Systick为抢占优先级3
NVIC_SetPriority函数指定中断优先级的寄存器位(STM32只用4位来表示优先级)的数据,例如中断优先级组设置为了2,即高2位用于指定抢占式优先级,低2位用于指定响应优先级,0x00~0x03高2位为0,所以抢占优先级为0;0x04~0x07高2位为1,所以抢占优先级为1,以此类推。
评论