STM32CubeMX_FreeRTOS Configuration

本文详细解析了RTOS内核配置参数,包括抢占与合作调度模式、CPU时钟频率、tick中断频率、最大优先级数、栈空间大小、线程名长度、互斥锁、信号量计数等功能选项。同时介绍了内存管理、中断嵌套行为、任务通知等关键配置。

Config parameters

Kernel settings

  • USE_PREEMPTION 抢占
    Set to 1 to use the preemptive RTOS scheduler, or 0 to use the cooperative RTOS scheduler
    使用抢占调度模式,使用合作调度模式

  • CPU_CLOCK_HZ CPU时钟频率

  • TICK_RATE_HZ
    Sets the tick interrupt frequency, The value is specified in Hz
    设置tick中断频率

  • MAX_PRIOPITIES
    Sets the maximum priority that can be assigned to a task.
    Tasks can be assigned a priority from zero, which is the lowest priority, to (MAX_PRIORITIES-1), which is the highest priority.
    设置线程可分配的最大的优先级数

  • MINIMAL_STACK_SIZE
    Sets the size of the stack allocated to the idle task. The value is specified in words (here, of 32 bits), not bytes.
    设置线程可使用的栈空间大小

  • MAX_TASK_NAME_LEN
    Sets the maximum number of characters that can be used for the name of a task. The NULL terminator is included in the count of characters.
    设置线程名字字符大小,终止符和NULL包括在内

  • USE_16_BIT_TICKS
    The tick count is held in a variable of type portTickType.

  • IDLE_SHOULD_YIELD
    IDLE_SHOULD_YIELD controls the behavior of the idle task if there are application tasks that also run at the idle priority
    It only has an effect if the preemptive scheduler is being used.
    使能空闲运行行程

  • USE_MUTEXES
    Set to 1 to include mutex functionality in the build, or 0 to omit mutex functionality from the build.
    1在编译时包含互斥功能,0忽略互斥

  • USE_RECURSIVE_MUTEXES
    Diagnostic: Both values allowed when configUSE_MUTEXS equals to 1.
    Sets to 1 to include recursive mutex functionality in the build, or 0 to omit recursive mutex functionality from the build.
    设置递归互斥

  • USE_COUNTING _SEMAPHORES
    Set to 1 to include counting semaphore functionality in the build, or 0 to omit counting semaphore functionality from the build
    设置信号量计数功能

  • QUEUE_REGISTRY_SIZE
    The queue registry has two purposes, both of which are associated with RTOS kernel aware debugging:
    1.It allows a textual name to be associated with a queue for easy queue identification within a debugging GUI.
    2.It contains the information required by a debugger to locate each registered queue and semaphore.
    The queue registry has no purpose unless you are using a RTOS kernel aware debugger.
    设置队列寄存器大小

  • USE_APPLICATION_TASK_TAG
    USE_APPLICATION_TASK_TAG must be defined as 1 for the vTaskSetApplicationTaskTag function to be available.
    This function is intended for advanced users only.
    A ‘tag’ value can be assigned to each task. This value is for the use of the application only - the RTOS kernel itself does not make use of it in any way.
    The FreeRTOS trace macros documentation provides a good example of how an application might make use of this feature.

  • ENABLE_BACKWARD_COMPATIBILITY
    开启向下兼容模式

  • USE_PORT_OPTIMISED_TASK_SELECTION
    优化线程选择

  • USE_TICKLESS_IDLE
    By setting the configUSE_TICKLESS_IDLE, the idle task suppresses ticks and the processor stays in a low power mode for as long as possible.
    利用滴答时钟空闲
  • USE_TASK_NOTIFICATIONS
    Each RTOS task has a 32-bit notification value . An RTOS task notification is an event sent directly to a task that can unblock the receiving task, and optionally update the receiving task’s notification vaule.
    使用线程通知

Memory management settings

  • Memory Allocation
    内存分配方式,动态,静态,动/静态
  • TOTAL_HEAP_SIZE
    总内存堆大小设置
  • Memory Management scheme
    内存管理方案 heap_1.2.3.4
- USE_IDLE_HOOK
- USE_TICK_HOOK
- USE_MALLOC_FAILED_HOOK
- USE_DAEMON_TASK_STARTUP_HOOK
- CHECK_FOR_STACK_OVERFLOW          
- GENERATE_RUN_TIME_STATS
- USE_TRACE_FACILITY
- USE_STATS_FORMATTING_FUNCTIONS
- USE_CO_ROUTINES
- MAX_CO_ROUTINE_PRIORITIES

Software timer definitons

- USE_TIMERS

Interrupt nesting behaviour configuration

- LIBRARY_LOWEST_INTERRUPT_PRIORITY
- LIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY
### STM32CubeMX 中配置和使用 FreeRTOS 的教程 #### 1. 开始前的准备 为了在 STM32CubeMX 中成功配置并使用 FreeRTOS,需要先安装好 STM32CubeMX 和支持的目标开发环境(如 STM32CubeIDE 或 Keil)。确保已下载最新的 STM32CubeFirmwarePackage 并导入到 CubeMX 工具中[^1]。 #### 2. 创建新项目 启动 STM32CubeMX 软件,在初始界面选择目标微控制器型号,并点击“Start Project”。完成硬件外设的基础设置后,进入下一步配置阶段[^2]。 #### 3. 启用 FreeRTOS 支持 - 在左侧导航栏找到 “Middleware” 类别下的 “FreeRTOS”,勾选启用该选项。 - 设置 RTOS Tick Interrupt Period 值,默认为 `1000Hz` 即可满足大多数应用场景需求。如果对功耗敏感,则可以根据实际调整此参数。 #### 4. 配置任务优先级及其他参数 - 进入 Configuration -> Middleware -> FreeRTOS 页面,可以看到多个子项用于定义堆内存分配方式、栈大小以及默认的任务调度策略等重要属性。 - 对于初学者来说,建议保持大部分默认设定不变;仅需关注 Stack Configurator 板块来指定各个线程所需的最小堆空间尺寸[^3]。 #### 5. 解决可能遇到的问题 当使用特定 IDE 如 STM32CubeIDE 或 CLion 构建工程时可能会碰到编译错误的情况。此时应回到 STM32CubeMX 主程序里重新确认所有相关中间件已被正确加载完毕后再导出更新后的源文件集合。 #### 6. 编写第一个 FreeRTOS 应用实例 以下是创建简单 LED 控制循环的一个基本例子: ```c #include "main.h" #include "cmsis_os.h" void StartDefaultTask(void const * argument); int main(void){ HAL_Init(); SystemClock_Config(); osThreadDef(default_task, StartDefaultTask ,osPriorityNormal, 0, configMINIMAL_STACK_SIZE); osThreadCreate(osThread(default_task), NULL); vTaskStartScheduler(); while (1){} } void StartDefaultTask(void const * argument){ while(1){ HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); osDelay(500); } } ``` 通过以上步骤即可顺利完成基于 STM32CubeMXFreeRTOS 环境搭建工作流程。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值