您的位置:首页 > 健康 > 养生 > RT-Thread(Nano版本)的快速移植(基于NUCLEO-F446RE)

RT-Thread(Nano版本)的快速移植(基于NUCLEO-F446RE)

2024/10/6 1:01:37 来源:https://blog.csdn.net/mftang/article/details/141980933  浏览:    关键词:RT-Thread(Nano版本)的快速移植(基于NUCLEO-F446RE)

目录

概述

1 RT-Thread

1.1 RT-Thread的版本

 1.2 认识Nano版本

2 STM32F446U上移植RT-Thread 

2.1 STM32Cube创建工程

2.2 移植RT-Thread

2.2.1 安装RT-Thread Packet

 2.2.2 加载RT-Thread

2.2.3 匹配相关接口

2.2.3.1 初次编译代码

 2.2.3.2 匹配端口

 2.2.4 移植FinSH 接口

2.2.5 Tick函数调用

3 测试

3.1 使用STM32Cube重新生成Project

3.2 RT-Thread时钟参数配置

 3.3 编写测试函数

4 运行代码的问题


概述

本文主要介绍RT-Thread Nano版本在NUCLEO-F446RE上的移植方法,包括RT-Thread的版本信息,各个版本的差异,Keil下RT-Thread的安装,以及结合STM32Cube创建工程的方法。文中还详细记录了修改代码的内容和修改方法。还编写具体的案例验证代码是否能正常工作。

1 RT-Thread

1.1 RT-Thread的版本

打开RT-Thread的文档地址,可以看见,RT-Thread提供了3个版本可供开发者使用,其包括;: 标准版本, Nano版本, Smart 版本

标准版本:

RT-Thread不仅仅是一个实时内核,还具备丰富的中间层组件

Nano版本

RT-Thread Nano 是一个极简版的硬实时内核,它是由 C 语言开发,采用面向对象的编程思维,具有良好的代码风格,是一款可裁剪的、抢占式实时多任务的 RTOS。其内存资源占用极小,功能包括任务处理、软件定时器、信号量、邮箱和实时调度等相对完整的实时操作系统特性。适用于家电、消费电子、医疗设备、工控等领域大量使用的 32 位 ARM 入门级 MCU 的场合。

 Smart版本

RT-Thread Smart 是基于 RT-Thread 操作系统上的混合操作系统,简称为 rt-smart,它把应用从内核中独立出来,形成独立的用户态应用程序,并具备独立的地址空间(32 位系统上是 4G 的独立地址空间)。

 1.2 认识Nano版本

RT-Thread Nano 是一个简洁的版本,对于MCU资源不太丰富的系统,非常实用。该版本也已经集成到Keil和STM32Cube软件上,对开发者非常友好。

  • 易裁剪:Nano 的配置文件为 rtconfig.h,该文件中列出了内核中的所有宏定义,有些默认没有打开,如需使用,打开即可。
  • 易添加 FinSH 组件:FinSH 组件 可以很方便的在 Nano 上进行移植,而不再依赖 device 框架,只需修改相关的函数接口内容,就可以支持FinSH功能。
  • 自选驱动库:可以使用厂商提供的固件驱动库,如 ST 的 STD 库、HAL 库、LL 库等,可以自行选择。
  • 完善的文档:包含 内核基础、线程管理 (例程)、时钟管理 (例程)、线程间同步 (例程)、线程间通信 (例程)、内存管理 (例程)、中断管理,以及 Nano 版块的移植教程。

2 STM32F446U上移植RT-Thread 

2.1 STM32Cube创建工程

打开STM2Cube,选择NUCLEO-F446RE创建项目

点击板卡信息,STM32Cube会自动配置外围资源

 创建项目完成后,使用Keil打开项目文件,其文件架构如下:

2.2 移植RT-Thread

2.2.1 安装RT-Thread Packet

在Keil上安装RT-Thread的packet,建议手动安装。

手动安装RT-thead 方法:

step -1:   登录该网址

https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-nano/nano-port-keil/an0039-nano-port-keil

step -2:   下载packet

在Keil上自动安装pcket的方法,安装过程会很慢(一般很难成功,建议手动安装) 

 2.2.2 加载RT-Thread

添加下面图标,加载RT-Thread的packet

加载完成软件包后,会在代码架构中看见如下内容

2.2.3 匹配相关接口

2.2.3.1 初次编译代码

初次编译代码,编译并不能成功,如下提示告诉开发者,这些介接口需要被编写。

 2.2.3.2 匹配端口

ERROR -1:

RTE/RTOS/board.c(47): error:  #35: #error directive: "TODO 1: OS Tick Configuration."
  #error "TODO 1: OS Tick Configuration."
RTE/RTOS/board.c: 0 warnings, 1 error

修正方法:

该error提示,在该函数中添加MCU时钟的初始化函数,其主要用于RT-Thread的TICK

修正方法:在void rt_hw_board_init(void)函数中添加如下函数:

void rt_hw_board_init(void)
{
// #error "TODO 1: OS Tick Configuration."/* * TODO 1: OS Tick Configuration* Enable the hardware timer and call the rt_os_tick_callback function* periodically with the frequency RT_TICK_PER_SECOND. *//* 1、系统、时钟初始化 */HAL_Init(); // 初始化 HAL 库SystemClock_Config(); // 配置系统时钟SystemCoreClockUpdate(); // 对系统时钟进行更新/* 2、OS Tick 频率配置,RT_TICK_PER_SECOND = 1000 表示 1ms 触发一次中断 */SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);/* Call components board initial (use INIT_BOARD_EXPORT()) */
#ifdef RT_USING_COMPONENTS_INITrt_components_board_init();
#endif#if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)rt_system_heap_init(rt_heap_begin_get(), rt_heap_end_get());
#endif
}

具体代码位置如下:

 ERROR -2: 

compiling finsh_port.c...
RTE/RTOS/finsh_port.c(14): error:  #35: #error directive: Please uncomment the line <#include "finsh_config.h"> in the rtconfig.h 
  #error Please uncomment the line <#include "finsh_config.h"> in the rtconfig.h 
RTE/RTOS/finsh_port.c: 0 warnings, 1 error

修正方法:

在rtconfig.h文件中使能finsh_config.h头文件

 ERROR -3:  

RTE/RTOS/finsh_port.c(24): error:  #35: #error directive: "TODO 4: Read a char from the uart and assign it to 'ch'."
  #error "TODO 4: Read a char from the uart and assign it to 'ch'."
RTE/RTOS/finsh_port.c: 0 warnings, 1 error

修正方法:在finshport.c中添加串口读取函数,具体代码如下:

RT_WEAK char rt_hw_console_getchar(void)
{/* Note: the initial value of ch must < 0 */int ch = -1;//#error "TODO 4: Read a char from the uart and assign it to 'ch'."if (__HAL_UART_GET_FLAG(&huart2, UART_FLAG_RXNE) != RESET){ch = huart2.Instance->DR & 0xff;}else{if(__HAL_UART_GET_FLAG(&huart2, UART_FLAG_ORE) != RESET){__HAL_UART_CLEAR_OREFLAG(&huart2);}rt_thread_mdelay(10);}return ch;
}

 函数具体位置:

  ERROR -4:  

RT_Thread_F446RU_Proj\RT_Thread_F446RU_Proj.axf: Error: L6200E: Symbol HardFault_Handler multiply defined (by context_rvds.o and stm32f4xx_it.o).
RT_Thread_F446RU_Proj\RT_Thread_F446RU_Proj.axf: Error: L6200E: Symbol PendSV_Handler multiply defined (by context_rvds.o and stm32f4xx_it.o).

Not enough information to list image symbols.
Not enough information to list load addresses in the image map.

 修正方法:取消如下两项,不使其生成函数

 2.2.4 移植FinSH 接口

初始状态下FinSH接口是被disable

 在rtconfig.h文件中能如下选项

 在board.c函数完善如下函数

源代码如下:

#ifdef RT_USING_CONSOLEstatic int uart_init(void)
{
//#error "TODO 2: Enable the hardware uart and config baudrate."MX_USART2_UART_Init();return 0;
}
INIT_BOARD_EXPORT(uart_init);void rt_hw_console_output(const char *str)
{
//#error "TODO 3: Output the string 'str' through the uart."rt_size_t i = 0, size = 0;char a = '\r';__HAL_UNLOCK(&huart2);size = rt_strlen(str);for (i = 0; i < size; i++){if (*(str + i) == '\n'){HAL_UART_Transmit(&huart2, (uint8_t *)&a, 1, 1);}HAL_UART_Transmit(&huart2, (uint8_t *)(str + i), 1, 1);}
}#endif

2.2.5 Tick函数调用

在stm32的SysTick_Handler()中调用rt_os_tick_callback();

函数rt_os_tick_callback()实现的功能:

void rt_os_tick_callback(void)
{rt_interrupt_enter();rt_tick_increase();rt_interrupt_leave();
}

3 测试

3.1 使用STM32Cube重新生成Project

使用STM32Cube生成Project,此时不用生产main函数,生成项目后,在代码中重写main()

重写的main函数如下:

3.2 RT-Thread时钟参数配置

 系统时钟配置,MCU的工作频率配置为180M Hz

在如下文件中配置Tick计数

 3.3 编写测试函数

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
int main(void)
{MX_GPIO_Init();while (1){rt_thread_mdelay(1000);HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);}/* USER CODE END 3 */
}
/* USER CODE END 0 */

添加断点,快速运行代码

4 运行代码的问题

Issue -1: 打印的log不全

解决方法:延长发送数据的Timeout时间

#ifdef RT_USING_CONSOLEstatic int uart_init(void)
{//#error "TODO 2: Enable the hardware uart and config baudrate."MX_USART2_UART_Init();MX_USART1_UART_Init();return 0;
}
INIT_BOARD_EXPORT(uart_init);void rt_hw_console_output(const char *str)
{//#error "TODO 3: Output the string 'str' through the uart."rt_size_t i = 0, size = 0;char a = '\r';__HAL_UNLOCK(&huart1);size = rt_strlen(str);for (i = 0; i < size; i++){if (*(str + i) == '\n'){HAL_UART_Transmit(&huart1, (uint8_t *)&a, 1, 1000);}HAL_UART_Transmit(&huart1, (uint8_t *)(str + i), 1, 1000);}
}#endif

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com