stm32 設(shè)置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的技術(shù)參考手冊,講解了NVIC的配置。
中斷分為內(nèi)核中斷和芯片的中斷,配置的寄存器位置不同。芯片的中斷配置在NVIC的IP內(nèi),內(nèi)核的中斷配置在SCB內(nèi)。
可以直接調(diào)用core_cm3.h里的函數(shù)
[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的函數(shù)。
[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)先級,對于外設(shè)的中斷可以使用固件庫的程序,比較直觀。如最前面所給出的例子,但是根據(jù)其實(shí)現(xiàn),是不能夠用來配置內(nèi)核中斷的,例如
[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的函數(shù)
[cpp] view plaincopy
- NVIC_SetPriority(SysTick_IRQn,
4);
[cpp] view plaincopy
- NVIC_SetPriority(SysTick_IRQn,
n); - n=0x00~0x03
設(shè)置Systick為搶占優(yōu)先級0 - n=0x04~0x07
設(shè)置Systick為搶占優(yōu)先級1 - n=0x08~0x0B
設(shè)置Systick為搶占優(yōu)先級2 - n=0x0C~0x0F
設(shè)置Systick為搶占優(yōu)先級3
NVIC_SetPriority函數(shù)指定中斷優(yōu)先級的寄存器位(STM32只用4位來表示優(yōu)先級)的數(shù)據(jù),例如中斷優(yōu)先級組設(shè)置為了2,即高2位用于指定搶占式優(yōu)先級,低2位用于指定響應(yīng)優(yōu)先級,0x00~0x03高2位為0,所以搶占優(yōu)先級為0;0x04~0x07高2位為1,所以搶占優(yōu)先級為1,以此類推。
評論