stm32 設置systick中斷搶先式優(yōu)先級
[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); } - }
但是直觀上來看看不到搶先式優(yōu)先級與子優(yōu)先級。
首先,配置優(yōu)先級組,可以使用固件庫的程序,也可以用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; - }
隨后配置各個中斷的優(yōu)先級,對于外設的中斷可以使用固件庫的程序,比較直觀。如最前面所給出的例子,但是根據其實現(xiàn),是不能夠用來配置內核中斷的,例如
[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為搶占優(yōu)先級0 - n=0x04~0x07
設置Systick為搶占優(yōu)先級1 - n=0x08~0x0B
設置Systick為搶占優(yōu)先級2 - n=0x0C~0x0F
設置Systick為搶占優(yōu)先級3
NVIC_SetPriority函數指定中斷優(yōu)先級的寄存器位(STM32只用4位來表示優(yōu)先級)的數據,例如中斷優(yōu)先級組設置為了2,即高2位用于指定搶占式優(yōu)先級,低2位用于指定響應優(yōu)先級,0x00~0x03高2位為0,所以搶占優(yōu)先級為0;0x04~0x07高2位為1,所以搶占優(yōu)先級為1,以此類推。
評論