- #ifndef APP_CFG_MODULE_PRESENT
- #define APP_CFG_MODULE_PRESENT
- /*
- *********************************************************************************************************
- * MODULE ENABLE / DISABLE
- *********************************************************************************************************
- */
- #define APP_CFG_SERIAL_EN DEF_ENABLED
- /*
- *********************************************************************************************************
- * TASK PRIORITIES
- *********************************************************************************************************
- */
- #define APP_CFG_TASK_START_PRIO 2u
- #define APP_CFG_TASK_OBJ_PRIO 3u
- #define APP_HIGH_PRIORITY (APP_CFG_TASK_START_PRIO+1)
- /*
- *********************************************************************************************************
- * TASK STACK SIZES
- * Size of the task stacks (# of OS_STK entries)
- *********************************************************************************************************
- */
- #define APP_CFG_TASK_START_STK_SIZE 512u
- #define APP_CFG_TASK_OBJ_STK_SIZE 512u
- /*
- *********************************************************************************************************
- * TRACE / DEBUG CONFIGURATION
- *********************************************************************************************************
- */
- #ifndef TRACE_LEVEL_OFF
- #define TRACE_LEVEL_OFF 0u
- #endif
- #ifndef TRACE_LEVEL_INFO
- #define TRACE_LEVEL_INFO 1u
- #endif
- #ifndef TRACE_LEVEL_DBG
- #define TRACE_LEVEL_DBG 2u
- #endif
- #if (APP_CFG_SERIAL_EN == DEF_ENABLED)
- #define APP_TRACE_LEVEL TRACE_LEVEL_DBG
- #else
- #define APP_TRACE_LEVEL TRACE_LEVEL_OFF
- #endif
- #define APP_TRACE printf
- #define APP_TRACE_INFO(x) ((APP_TRACE_LEVEL >= TRACE_LEVEL_INFO) ? (void)(APP_TRACE x) : (void)0)
- #define APP_TRACE_DBG(x) ((APP_TRACE_LEVEL >= TRACE_LEVEL_DBG) ? (void)(APP_TRACE x) : (void)0)
- #endif
4.6 第六步:修改启动文件修改 startup_apm32f402.s将PendSV_Handler 和 SysTick_Handler 分别改为OS_CPU_PendSVHandler 和OS_CPU_SysTickHandler
4.7 第七步:编写应用程序main.c 示例代码
- /**
- * [url=home.php?mod=space&uid=288409]@file[/url] main.c
- *
- * [url=home.php?mod=space&uid=247401]@brief[/url] Main program body
- *
- * [url=home.php?mod=space&uid=895143]@version[/url] V1.0.0
- *
- * [url=home.php?mod=space&uid=212281]@date[/url] 2024-12-01
- *
- * @attention
- *
- * Copyright (C) 2024-2025 Geehy Semiconductor
- *
- * You may not use this file except in compliance with the
- * GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
- *
- * The program is only for reference, which is distributed in the hope
- * that it will be useful and instructional for customers to develop
- * their software. Unless required by applicable law or agreed to in
- * writing, the program is distributed on an "AS IS" BASIS, WITHOUT
- * ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
- * See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
- * and limitations under the License.
- */
- /* Includes ***************************************************************/
- #include "main.h"
- #include "board_apm32f402_403_tiny.h"
- #include <stdio.h>
- #include "os.h"
- #include "app_cfg.h"
- /* Private includes *******************************************************/
- /* Private macro **********************************************************/
- /* Private typedef ********************************************************/
- #define DEBUG_USART USART1
- /* Private variables ******************************************************/
- // 任务控制块
- static OS_TCB AppTaskStartTCB;
- static OS_TCB AppTask1TCB;
- static OS_TCB AppTask2TCB;
- // 任务堆栈
- static CPU_STK AppTaskStartStk[128];
- static CPU_STK AppTask1Stk[128];
- static CPU_STK AppTask2Stk[128];
- // 任务函数声明
- static void AppTaskStart(void *p_arg);
- static void AppTask1(void *p_arg);
- static void AppTask2(void *p_arg);
- /* Private function prototypes ********************************************/
- /* External variables *****************************************************/
- /* External functions *****************************************************/
- /**
- * [url=home.php?mod=space&uid=247401]@brief[/url] Main program
- *
- * @param None
- *
- * @retval None
- */
- int main(void)
- {
- USART_Config_T usartConfigStruct;
- usartConfigStruct.baudRate = 115200;
- usartConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_NONE;
- usartConfigStruct.mode = USART_MODE_TX;
- usartConfigStruct.parity = USART_PARITY_NONE;
- usartConfigStruct.stopBits = USART_STOP_BIT_1;
- usartConfigStruct.wordLength = USART_WORD_LEN_8B;
- BOARD_COM_Config(COM1, &usartConfigStruct);
-
- BOARD_LED_Config(LED2);
- BOARD_LED_Config(LED3);
- SysTick_Config(RCM_ReadSYSCLKFreq() / 1000);
-
- printf("ucOSIII RTOS on APM32F402R Micro-EVB Board\r\n");
-
- OS_ERR err;
-
- // 初始化μC/OS-III
- OSInit(&err);
-
- // 创建启动任务
- OSTaskCreate((OS_TCB *)&AppTaskStartTCB,
- (CPU_CHAR *)"App Task Start",
- (OS_TASK_PTR )AppTaskStart,
- (void *)0,
- (OS_PRIO )4,
- (CPU_STK *)&AppTaskStartStk[0],
- (CPU_STK_SIZE )128/10,
- (CPU_STK_SIZE )128,
- (OS_MSG_QTY )0,
- (OS_TICK )0,
- (void *)0,
- (OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR),
- (OS_ERR *)&err);
-
- // 启动多任务调度
- OSStart(&err);
-
- /* Infinite loop */
- while (1)
- {
-
- }
- }
- static void AppTaskStart(void *p_arg)
- {
- OS_ERR err;
-
- // 创建其他任务
- OSTaskCreate((OS_TCB *)&AppTask1TCB,
- (CPU_CHAR *)"App Task 1",
- (OS_TASK_PTR )AppTask1,
- (void *)0,
- (OS_PRIO )5,
- (CPU_STK *)&AppTask1Stk[0],
- (CPU_STK_SIZE )128/10,
- (CPU_STK_SIZE )128,
- (OS_MSG_QTY )0,
- (OS_TICK )0,
- (void *)0,
- (OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR),
- (OS_ERR *)&err);
-
- OSTaskCreate((OS_TCB *)&AppTask2TCB,
- (CPU_CHAR *)"App Task 2",
- (OS_TASK_PTR )AppTask2,
- (void *)0,
- (OS_PRIO )6,
- (CPU_STK *)&AppTask2Stk[0],
- (CPU_STK_SIZE )128/10,
- (CPU_STK_SIZE )128,
- (OS_MSG_QTY )0,
- (OS_TICK )0,
- (void *)0,
- (OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR),
- (OS_ERR *)&err);
-
- while(1)
- {
- // 启动任务主循环
- OSTimeDlyHMSM(0, 0, 1, 0, OS_OPT_TIME_HMSM_STRICT, &err);
- }
- }
- static void AppTask1(void *p_arg)
- {
- OS_ERR err;
-
- while(1)
- {
- // 任务1的功能代码
- // LED2闪烁
- BOARD_LED_Toggle(LED2);
- OSTimeDlyHMSM(0, 0, 0, 800, OS_OPT_TIME_HMSM_STRICT, &err);
- printf("LED2 toggled\r\n");
- }
- }
- static void AppTask2(void *p_arg)
- {
- OS_ERR err;
-
- while(1)
- {
- // 任务2的功能代码
- // LED3闪烁
- BOARD_LED_Toggle(LED3);
- OSTimeDlyHMSM(0, 0, 0, 500, OS_OPT_TIME_HMSM_STRICT, &err);
- printf("LED3 toggled\r\n");
- }
- }
- /*!
- * Redirect C Library function printf to serial port.
- * After Redirection, you can use printf function.
- *
- * @param ch: The characters that need to be send.
- *
- * @param *f: pointer to a FILE that can recording all information
- * needed to control a stream
- *
- * @retval The characters that need to be send.
- */
- int fputc(int ch, FILE *f)
- {
- /** send a byte of data to the serial port */
- USART_TxData(DEBUG_USART, (uint8_t)ch);
- /** wait for the data to be send */
- while (USART_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == RESET);
- return (ch);
- }
串口输出及LED灯交替闪烁
5. 常见问题及解决方案5.1 编译错误处理
- 包含路径错误: 确保所有必要的头文件路径都已添加到工程中
- 宏定义冲突: 检查os_cfg.h和os_cfg_app.h中的配置是否正确
- 链接错误: 确保所有必要的源文件都已添加到工程中
5.2 运行时问题- 系统无法启动: 检查堆栈大小是否足够,建议初始堆栈至少128字节
- 任务切换异常: 确保PendSV和SysTick中断优先级设置正确
- 系统卡死: 检查是否有任务没有延时或阻塞,导致低优先级任务无法运行
5.3 FPU支持(如果使用浮点运算)APM32F402支持硬件浮点单元,如果应用中需要使用浮点运算:
- 在编译选项中启用FPU支持
- 修改os_cpu_a.s文件中的上下文切换代码,保存和恢复浮点寄存器
- 在任务堆栈计算中考虑浮点寄存器的空间需求
6. 性能优化建议6.1 任务优先级分配- 中断服务任务:优先级0-3
- 实时性要求高的任务:优先级4-15
- 一般应用任务:优先级16-31
6.2 堆栈大小配置- 简单任务:128-256字节
- 复杂任务:512-1024字节
- 中断堆栈:256-512字节
6.3 系统配置优化- 根据实际需求配置OS_CFG_PRIO_MAX,减少内存占用
- 合理配置消息池大小
- 启用必要的功能选项,关闭不需要的功能
7. 调试技巧7.1 使用μC/OS-III内置调试功能- #define OS_CFG_DBG_EN 1u // 使能调试功能
7.2 任务运行状态监控使用OSTaskStkChk()函数检查任务堆栈使用情况:
- OS_STK_DATA stk_data;
- OSTaskStkChk(&AppTask1TCB, &stk_data, &err);
7.3 系统统计信息启用系统统计功能查看CPU使用率:
- #define OS_CFG_STAT_TASK_EN 1u
8. 总结通过本教程,您应该能够成功将μC/OS-III移植到APM32F402平台。关键要点包括:
- 正确配置编译环境和包含路径
- 添加必要的源文件和移植文件
- 修改配置文件以适应具体应用需求
- 正确处理中断向量表
- 合理分配任务优先级和堆栈大小
移植完成后,建议从简单的多任务程序开始测试,逐步增加复杂功能,确保系统稳定运行。
9. 参考资料- μC/OS-III官方文档
- APM32F402用户手册
- ARM Cortex-M4技术参考手册
- 《μC/OS-III内核实现与应用开发实战指南》