找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2480|回复: 2
收起左侧

STM32 CubeIDE 使用RT-Thread Nano

[复制链接]
ID:606388 发表于 2020-9-24 21:44 | 显示全部楼层 |阅读模式
本帖最后由 qingyemurong 于 2020-9-24 22:13 编辑

STM32 CubeIDE 使用RT-Thread Nano

使用的 STM32 CubeIDE 版本 1.4
  在STM32 CubeIDE中已经集成了RT-Thread Nano,可以直接在 IDE 中进行下载添加。工程文件附件自己下载。

  • # 1、RT-Thread Nano pack 安装
打开 STM32 CubeIDE   --------->**Software Packs**     ------------>**Manager Software Packs**界面(

  获取 RT-Thread Nano 软件包,需要在 STM32CubeIDE 中添加 [https://www.rt-thread.org/download/cube/RealThread.RT-Thread.pdsc](https://www.rt-thread.org/download/cube/RealThread.RT-Thread.pdsc)


回到 Manage software packages 界面,就会发现 RT-Thread Nano 3.1.3 软件包,选择该软件包,点击 Install Now,如下图所示(颜色填充表示已安装)(


(
  • # 2、创建工程添加 RT-Thread Nano
## 2.1 、创建一个基本工程
创建一个基本的工程文件,包含2个LED灯和USART1。





(

#
  • # 2.2、配置 Nano


勾选 RT-Thread
(?x-oss
  • **适配 RT-Thread Nano**
中断与异常处理
RT-Thread 操作系统重定义 **HardFault_Handler**、**PendSV_Handler**、**SysTick_Handler** 中断函数,为了避免重复定义的问题,在生成工程之前,需要在中断配置中,代码生成的选项中,取消选择三个中断函数(对应注释选项是 Hard fault interrupt, Pendable request, Time base :System tick timer),最后点击生成代码,具体操作如下图中

(

# 3、工程代码修改
  • ## 3.1 需要修改的部分
1 、修改启动文件  startup_stm32f103rctx.s
**bl main**  修改为  **bl  entry**     
(

  • ## 3.2 、配置rt_kprintf端口输出
端口映射,函数可以放在main.c文件里面。
(


(

  1. /* USER CODE BEGIN 4 */
  2. char rt_hw_console_getchar(void)
  3. {
  4.         int ch = -1;
  5.         if (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_RXNE) != RESET)
  6.         {
  7.                 ch = huart1.Instance->DR & 0xff;
  8.         }
  9.         else
  10.         {
  11.                 if (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_ORE) != RESET)
  12.                 {
  13.                         __HAL_UART_CLEAR_OREFLAG(&huart1);
  14.                 }
  15.                 rt_thread_mdelay(10);
  16.         }
  17.         return ch;
  18. }
  19. void rt_hw_console_output(const char *str)
  20. {
  21.         rt_size_t i = 0, size = 0;
  22.         char a = '\r';
  23.         __HAL_UNLOCK(&huart1);
  24.         size = rt_strlen(str);
  25.         for (i = 0; i < size; i++)
  26.         {
  27.                 if (*(str + i) == '\n')
  28.                 {
  29.                         ITM_SendChar(a);
  30.                         HAL_UART_Transmit(&huart1, (uint8_t*) &a, 1, 1);
  31.                 }
  32.                 HAL_UART_Transmit(&huart1, (uint8_t*) (str + i), 1, 1);
  33.         }
  34. }

  35. /* USER CODE END 4 */
复制代码


## 3.3 、编写线程文件
创建一个**app_rt_thread.c**文件用于保存线程代码
(


app_rt_thread.c文件内容:

  1. #include "rtthread.h"
  2. #include "main.h"
  3. #include "stdio.h"
  4. #include <finsh.h>        


  5. /* 定义线程控制块 */
  6. //添加LED闪烁线程
  7. static struct rt_thread led_thread;
  8. static char led_thread_stack[256];
  9. static void led_thread_entry(void *parameter);
  10. int MX_RT_Thread_Init(void);

  11. int MX_RT_Thread_Init(void)
  12. {
  13.         //初始化线程
  14.         rt_err_t rst;
  15.         rst = rt_thread_init(&led_thread,
  16.                                                 (const char *)"ledshine",  /* 线程名字 */
  17.                                                 led_thread_entry,  /* 线程入口函数 */
  18.                                                 RT_NULL,           /* 线程入口函数参数 */
  19.                                                 &led_thread_stack[0],
  20.                                                 sizeof(led_thread_stack),   /* 线程栈大小 */
  21.                                                 RT_THREAD_PRIORITY_MAX-2,  /* 线程的优先级 */
  22.                                                 20); /* 线程时间片 */
  23.         if(rst == RT_EOK)
  24.         {///* 启动线程,开启调度 */
  25.                 rt_thread_startup(&led_thread);
  26.         }

  27. }


  28. /*
  29. *************************************************************************
  30. * 线程定义
  31. *************************************************************************
  32. */
  33. static void led_thread_entry(void *parameter)
  34. {
  35.         while(1)
  36.         {
  37.                 rt_kprintf("led1_thread running,LED1_ON\r\n");
  38.                 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);
  39.                 rt_thread_mdelay(500);
  40.                 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);
  41.                 rt_thread_mdelay(500);
  42.         }
  43. }

  44. MSH_CMD_EXPORT(led_thread_entry,thread running);
复制代码


  • ## 3.4 、main.c 修改
  • (

  1. /* USER CODE BEGIN Includes */
  2. #include "rtthread.h"

  3. extern int MX_RT_Thread_Init(void);
复制代码





(

  1. int main(void)
  2. {
  3.   /* USER CODE BEGIN 1 */

  4.   /* USER CODE END 1 */

  5.   /* MCU Configuration--------------------------------------------------------*/

  6.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  7.   HAL_Init();

  8.   /* USER CODE BEGIN Init */

  9.   /* USER CODE END Init */

  10.   /* Configure the system clock */
  11.   SystemClock_Config();

  12.   /* USER CODE BEGIN SysInit */

  13.   /* USER CODE END SysInit */

  14.   /* Initialize all configured peripherals */
  15.   MX_GPIO_Init();
  16.   MX_USART1_UART_Init();
  17.   /* USER CODE BEGIN 2 */
  18.   MX_RT_Thread_Init();
  19.   /* USER CODE END 2 */

  20.   /* Infinite loop */
  21.   /* USER CODE BEGIN WHILE */
  22.   while (1)
  23.   {
  24.           HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_2);
  25.           rt_kprintf("led1_thread TEST\r\n");
  26.           rt_thread_mdelay(100);
  27.     /* USER CODE END WHILE */

  28.     /* USER CODE BEGIN 3 */
  29.   }
  30.   /* USER CODE END 3 */
  31. }
复制代码


串口输出:
(






RT_Thread.7z

1.61 MB, 下载次数: 15, 下载积分: 黑币 -5

工程文件

回复

使用道具 举报

ID:538806 发表于 2021-6-21 16:56 | 显示全部楼层
好用吗?有没有bug啊
回复

使用道具 举报

ID:688008 发表于 2021-8-2 08:29 | 显示全部楼层
学习一下,后续开始搞rt
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表