STM32-调用 vTaskStartScheduler API 后出现 HardFault

STM32 移植 FreeRTOS 后调用 vTaskStartScheduler() 后出现 HardFault 异常。

原因分析:

FreeRTOS 配置头文件 FreeRTOSConfig.h 中与中断有关的配置和通过系统接口 void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup) 设置的中断分组冲突。

c 复制代码
/* 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) )
c 复制代码
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

FreeRTOSConfig.h 中设置的中断最低优先级为 15 ,说明 可编程中断优先级的范围为 0 ~ 15 ,也即需要 4 bits 来表示抢占优先级。而通过 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0) API 设置为 0 bit 用于抢占优先级3 bits 用于子优先级 ,与 FreeRTOSConfig.h 中的中断设置冲突导致。

c 复制代码
/** @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

c 复制代码
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);

题外:

这里顺便也记录下 STM32 (Cortex-M3) 结合 FreeRTOS 对中断的设置。

STM32 (Cortex-M3)SCB_AIRCR 寄存器中的 Bits 10:9 设置中断优先级分组。

FreeRTOS 在配置文件 FreeRTOSConfig.h 中通过宏 configKERNEL_INTERRUPT_PRIORITYconfigMAX_SYSCALL_INTERRUPT_PRIORITY 来设置 FreeRTOS 中的中断设置。

  • configKERNEL_INTERRUPT_PRIORITY 用于设置中断最低优先级。这个宏必须结合 void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup) 来设置。
  • configMAX_SYSCALL_INTERRUPT_PRIORITY 用于 FreeRTOS 中的一些中断功能。典型应用就是 FreeRTOS 中以 FromISR 结尾的 API 可以安全的在 configMAX_SYSCALL_INTERRUPT_PRIORITY ~ configKERNEL_INTERRUPT_PRIORITY 之间的中断回调中使用。对于这部分描述,可以参考下图或者 FreeRTOS 官方文档中对 FreeRTOSConfig.h 中宏的说明
相关推荐
王光环19 分钟前
STM32H743IIT6_ADC采集误差分析与ADC_DMA
stm32·单片机
芯眼1 小时前
STM32启动文件详解(重点)
java·开发语言·c++·stm32·单片机·mybatis
长流小哥2 小时前
STM32 ADC+DMA+TIM触发采样实战:避坑指南与源码解析
stm32·单片机·嵌入式硬件·keil5
道亦无名2 小时前
STM32控制电机
stm32·单片机·嵌入式硬件
独行soc2 小时前
2025年渗透测试面试题总结-阿里云[实习]阿里云安全-安全工程师(题目+回答)
linux·经验分享·安全·阿里云·面试·职场和发展·云计算
happygrilclh3 小时前
STM32的ADC模块中,**采样时机(Sampling Time)**和**转换时机(Conversion Time),获取数据的时机详解
stm32·单片机·嵌入式硬件
子朔不言3 小时前
迅龙3号基于兆讯MH22D3适配CST328多点触摸驱动开发笔记
mcu·显示·mh22d3·新龙微·cst328
真的想上岸啊3 小时前
学习51单片机02
嵌入式硬件·学习·51单片机
草莓熊Lotso3 小时前
【C语言字符函数和字符串函数(一)】--字符分类函数,字符转换函数,strlen,strcpy,strcat函数的使用和模拟实现
c语言·开发语言·经验分享·笔记·其他
sword devil9004 小时前
STM32F407VET6实战:CRC校验
stm32·单片机·嵌入式硬件