STM32 移植 FreeRTOS 后調(diào)用 vTaskStartScheduler() 后出現(xiàn) HardFault 異常。
原因分析:
FreeRTOS 配置頭文件 FreeRTOSConfig.h
中與中斷有關的配置和通過系統(tǒng)接口 void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)
設置的中斷分組沖突。
/* The lowest interrupt priority that can be used in a call to a "set priority"
function. */
#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY 15
/* The highest interrupt priority that can be used by any interrupt service
routine that makes calls to interrupt safe FreeRTOS API functions. DO NOT CALL
INTERRUPT SAFE FREERTOS API FUNCTIONS FROM ANY INTERRUPT THAT HAS A HIGHER
PRIORITY THAN THIS! (higher priorities are lower numeric values. */
#define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY 5
/* Interrupt priorities used by the kernel port layer itself. These are generic
to all Cortex-M ports, and do not rely on any particular library functions. */
#define configKERNEL_INTERRUPT_PRIORITY ( configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
/* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!!
See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */
#define configMAX_SYSCALL_INTERRUPT_PRIORITY ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
在 FreeRTOSConfig.h
中設置的中斷最低優(yōu)先級為 15,說明 可編程中斷優(yōu)先級的范圍為 0 ~ 15,也即需要 4 bits 來表示搶占優(yōu)先級。而通過 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0)
API 設置為 0 bit 用于搶占優(yōu)先級,3 bits 用于子優(yōu)先級,與 FreeRTOSConfig.h
中的中斷設置沖突導致。
/** @defgroup Preemption_Priority_Group
* @{
*/
#define NVIC_PriorityGroup_0 ((uint32_t)0x700) /*!< 0 bits for pre-emption priority
4 bits for subpriority */
#define NVIC_PriorityGroup_1 ((uint32_t)0x600) /*!< 1 bits for pre-emption priority
3 bits for subpriority */
#define NVIC_PriorityGroup_2 ((uint32_t)0x500) /*!< 2 bits for pre-emption priority
2 bits for subpriority */
#define NVIC_PriorityGroup_3 ((uint32_t)0x400) /*!< 3 bits for pre-emption priority
1 bits for subpriority */
#define NVIC_PriorityGroup_4 ((uint32_t)0x300) /*!< 4 bits for pre-emption priority
0 bits for subpriority */
解決方案:
將中斷分組設置為 NVIC_PriorityGroup_4
即可。FreeRTOS 官方文檔 也建議對于 STM32 (Cortex-M3),建議設置為 NVIC_PriorityGroup_4
。
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
題外:
這里順便也記錄下 STM32 (Cortex-M3) 結合 FreeRTOS 對中斷的設置。
STM32 (Cortex-M3) 在 SCB_AIRCR 寄存器中的 Bits 10:9 設置中斷優(yōu)先級分組。
FreeRTOS 在配置文件 FreeRTOSConfig.h
中通過宏 configKERNEL_INTERRUPT_PRIORITY
和 configMAX_SYSCALL_INTERRUPT_PRIORITY
來設置 FreeRTOS 中的中斷設置。文章來源:http://www.zghlxwxcb.cn/news/detail-816275.html
- 宏
configKERNEL_INTERRUPT_PRIORITY
用于設置中斷最低優(yōu)先級。這個宏必須結合void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)
來設置。 - 宏
configMAX_SYSCALL_INTERRUPT_PRIORITY
用于 FreeRTOS 中的一些中斷功能。典型應用就是 FreeRTOS 中以FromISR
結尾的 API 可以安全的在configMAX_SYSCALL_INTERRUPT_PRIORITY
~configKERNEL_INTERRUPT_PRIORITY
之間的中斷回調(diào)中使用。對于這部分描述,可以參考下圖或者 FreeRTOS 官方文檔中對 FreeRTOSConfig.h 中宏的說明。
文章來源地址http://www.zghlxwxcb.cn/news/detail-816275.html
到了這里,關于STM32-調(diào)用 vTaskStartScheduler API 后出現(xiàn) HardFault的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!