資源描述:
《stm32設(shè)置systick中斷搶占優(yōu)先級(jí)》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、STM32設(shè)置SysTick中斷搶占優(yōu)先級(jí)最近使用STM32時(shí)希望將systick的中斷優(yōu)先級(jí)降低,但是CMSIS里給出的例子都是類似NVIC_InitStructure.NVIC_IRQChannel=TIM1_UP_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE
2、;NVIC_Init(&NVIC_InitStructure);還是沒有配置systick。后來閱讀CM3的技術(shù)參考手冊(cè),講解了NVIC的配置。中斷分為內(nèi)核中斷和芯片的中斷,配置的寄存器位置不同。芯片的中斷配置在NVIC的IP內(nèi),內(nèi)核的中斷配置在SCB內(nèi)??梢灾苯诱{(diào)用core_cm3.h里的函數(shù)static__INLINEvoidNVIC_SetPriority(IRQn_TypeIRQn,uint32_tpriority){if(IRQn<0){SCB->SHP[((uint32_t)(IRQn)&0xF)-4]=(
3、(priority<<(8-__NVIC_PRIO_BITS))&0xff);}/*setPriorityforCortex-M3SystemInterrupts*/else{NVIC->IP[(uint32_t)(IRQn)]=((priority<<(8-__NVIC_PRIO_BITS))&0xff);}/*setPriorityfordevicespecificInterrupts*/}這個(gè)函數(shù)實(shí)現(xiàn)了兩類中斷優(yōu)先級(jí)的配置。但是直觀上來看看不到搶先式優(yōu)先級(jí)與子優(yōu)先級(jí)。首先,配置優(yōu)先級(jí)組,可以使用固件庫的程序,也可
4、以用core_cm3.h的函數(shù)。voidNVIC_PriorityGroupConfig(uint32_tNVIC_PriorityGroup){/*SetthePRIGROUP[10:8]bitsaccordingtoNVIC_PriorityGroupvalue*/SCB->AIRCR=AIRCR_VECTKEY_MASK
5、NVIC_PriorityGroup;}或static__INLINEvoidNVIC_SetPriorityGrouping(uint32_tPriorityGroup){uint32_tre
6、g_value;uint32_tPriorityGroupTmp=(PriorityGroup&0x07);/*onlyvalues0..7areused*/reg_value=SCB->AIRCR;/*readoldregisterconfiguration*/reg_value&=~(SCB_AIRCR_VECTKEY_Msk
7、SCB_AIRCR_PRIGROUP_Msk);/*clearbitstochange*/reg_value=(reg_value
8、(0x5FA<9、)
10、(PriorityGroupTmp<<8));/*Insertwritekeyandpriortygroup*/SCB->AIRCR=reg_value;}參數(shù)范圍為0~7,具體如下隨后配置各個(gè)中斷的優(yōu)先級(jí),對(duì)于外設(shè)的中斷可以使用固件庫的程序,比較直觀。如最前面所給出的例子,但是根據(jù)其實(shí)現(xiàn),是不能夠用來配置內(nèi)核中斷的,例如NVIC_InitStructure.NVIC_IRQChannel=SysTick_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriori
11、ty=0;NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;NVIC_Init(&NVIC_InitStructure);這是錯(cuò)誤的,因?yàn)閮?nèi)部使用IRQn尋址,而SysTick_IRQn為-1.所以對(duì)于Sysick使用core_cm3.h的函數(shù)NVIC_SetPriority(SysTick_IRQn,4);這時(shí)不能顯式的看出搶先式優(yōu)先級(jí)與子優(yōu)先級(jí),寫入的優(yōu)先級(jí)需要根據(jù)優(yōu)先級(jí)組的配置來
12、選擇。NVIC_SetPriority(SysTick_IRQn,n);n=0x00~0x03設(shè)置Systick為搶占優(yōu)先級(jí)0n=0x04~0x07設(shè)置Systick為搶占優(yōu)先級(jí)1n=0x08~0x0B設(shè)置Systick為搶占優(yōu)先級(jí)2n=0x0C~0x0F設(shè)置Systick為搶占優(yōu)先級(jí)3NVIC_SetPriority函數(shù)指定中斷