現(xiàn)在意法半導(dǎo)體的配套軟件做得很全面了,簡直可以說是保姆式的服務(wù)。從芯片選型,引腳定義,到代碼模板生成,一條龍服務(wù),很方便。但是方便也有方便的壞處,那就是有些細(xì)節(jié)的規(guī)則會造成天然的bug。
比如,在stm32cubeIDE里新建工程的時候,如果勾選了FREERTOS
那么,程序后臺會默認(rèn)把RTOS可控的優(yōu)先級范圍設(shè)定成5-15(目前還看不見,生成后在FreeRTOSConfig.h中可以查到):
接下來繼續(xù)配置串口。
可以看到,串口中斷的優(yōu)先級已經(jīng)被鎖定成5,且不可更改。
實(shí)際上這樣暫時還沒有什么問題,生成代碼后,做基本的串口收發(fā)一時半會兒也看不出有什么問題。
但是,一旦讓程序多跑跑,就會發(fā)現(xiàn),串口中斷早晚會卡死,而其他程序還可以正常跑。
這是為什么呢?
在串口中斷接收的函數(shù)中,有LOCK這樣的功能。如果在LOCK期間程序被打斷,那么就再也不能解鎖,導(dǎo)致卡死。
而由于串口中斷的優(yōu)先級被默認(rèn)限制成了5,RTOS就有了打斷的可能。
所以一切問題的根源是stm32cubeIDE。
要解決這個問題也很簡單,去stm32f1xx_hal_msp.c修改一下串口中斷的優(yōu)先級就好了。
默認(rèn)是5,只要把優(yōu)先級修改到0-4之間即可。這個范圍內(nèi)RTOS是不會管理的,也就是不會打斷。
用到這個函數(shù)說明如下:
/**
* @brief Sets the priority of an interrupt.
* @param IRQn: External interrupt number.
* This parameter can be an enumerator of IRQn_Type enumeration
* (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32f10xx.h))
* @param PreemptPriority: The preemption priority for the IRQn channel.
* This parameter can be a value between 0 and 15
* A lower priority value indicates a higher priority
* @param SubPriority: the subpriority level for the IRQ channel.
* This parameter can be a value between 0 and 15
* A lower priority value indicates a higher priority.
* @retval None
*/
void HAL_NVIC_SetPriority(IRQn_Type IRQn, uint32_t PreemptPriority, uint32_t SubPriority)
{
uint32_t prioritygroup = 0x00U;
/* Check the parameters */
assert_param(IS_NVIC_SUB_PRIORITY(SubPriority));
assert_param(IS_NVIC_PREEMPTION_PRIORITY(PreemptPriority));
prioritygroup = NVIC_GetPriorityGrouping();
NVIC_SetPriority(IRQn, NVIC_EncodePriority(prioritygroup, PreemptPriority, SubPriority));
}
關(guān)鍵是這句注釋:文章來源:http://www.zghlxwxcb.cn/news/detail-606252.html
-
This parameter can be a value between 0 and 15 A lower priority value indicates a higher priority
所以,保姆雖然事無巨細(xì),但還是無法做到面面俱到呀。文章來源地址http://www.zghlxwxcb.cn/news/detail-606252.html
到了這里,關(guān)于STM32用FreeRTOS串口中斷接收卡死問題的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!