type: concept title: FreeRTOS 移植指南 tags: [freertos, rtos, porting, stm32, hal, config, concept]
FreeRTOS/
├── Demo/ 演示例程(多芯片架构)
├── License/ 许可
├── Source/ 核心源码
│ ├── include/ 头文件(通用)
│ ├── portable/ 移植文件(编译器相关)
│ │ ├── Keil/ → 指向 RVDS
│ │ ├── RVDS/ 不同内核芯片移植文件
│ │ │ └── ARM_CM3/ port.c + portmacro.h
│ │ └── MemMang/heap_1~5.c
│ ├── tasks.c 任务管理
│ ├── queue.c 队列
│ ├── timers.c 软件定时器
│ ├── event_groups.c
│ ├── stream_buffer.c
│ └── list.c 列表
└── Test/ 测试代码
port.c:上下文切换等核心汇编代码,由 FreeRTOS 官方为各内核编写。portmacro.h:数据类型和宏定义。
Project/
├── FreeRTOS/
│ ├── portable/ (Keil + RVDS + MemMang)
│ └── source/ (7 个 .c 文件)
└── Core/Inc/FreeRTOSConfig.h
FreeRTOS/Source 和 FreeRTOS/Portableport.c + heap_4.cinclude/ 和 RVDS/ARM_CM3/#define xPortPendSVHandler PendSV_Handler
#define vPortSVCHandler SVC_Handler
#define INCLUDE_xTaskGetSchedulerState 1
"FreeRTOS.h" 和 "task.h"SVC_Handler() 和 PendSV_Handler()添加 SysTick 中断服务函数:
extern void xPortSysTickHandler(void);
void SysTick_Handler(void) {
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
xPortSysTickHandler();
}
}
注意:HAL 和 FreeRTOS 都默认依赖 SysTick,建议在 CubeMX SYS 配置中将 HAL 时基换成其他定时器。
配置项分三类:
| 类别 | 格式 | 说明 |
|---|---|---|
| 函数使能 | INCLUDE_函数名 |
1=可用,0=禁用 |
| 功能配置 | configXXX |
基本/内存/钩子/中断配置 |
| 其他 | 宏定义 | PendSV/SVC 映射 |
#define configUSE_PREEMPTION 1 // 抢占式调度
#define configCPU_CLOCK_HZ SystemCoreClock
#define configTICK_RATE_HZ 1000 // 系统节拍频率
#define configMAX_PRIORITIES 32
#define configMINIMAL_STACK_SIZE 128
#define configTOTAL_HEAP_SIZE ((size_t)(10 * 1024))
#define configUSE_MUTEXES 1
#define configUSE_COUNTING_SEMAPHORES 1
#define configUSE_TIMERS 1
#define configUSE_TICKLESS_IDLE 0