你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32 CubeIDE 使用RT-Thread Nano

[复制链接]
STMCU 发布时间:2020-9-24 12:54
1、RT-Thread Nano pack 安装

打开 STM32 CubeIDE --------->Software Packs ------------>Manager Software Packs界面


1_meitu_1.jpg




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


2_meitu_2.jpg


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


3_meitu_3.jpg


4_meitu_4.jpg



2、创建工程添加 RT-Thread Nano

2.1 、创建一个基本工程

创建一个基本的工程文件,包含2个LED灯和USART1。



5_meitu_5.jpg


6_meitu_6.jpg


7_meitu_7.jpg



2.2、配置 Nano

勾选 RT-Thread


8_meitu_8.jpg


适配 RT-Thread Nano


中断与异常处理


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


9_meitu_9.jpg



3、工程代码修改

3.1 需要修改的部分

1 、修改启动文件 startup_stm32f103rctx.s
bl main 修改为 bl entry


10_meitu_10.jpg



3.2 、配置rt_kprintf端口输出

端口映射,函数可以放在main.c文件里面。

11_meitu_11.jpg


12_meitu_12.jpg






  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文件用于保存线程代码


13_meitu_13.jpg


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 修改

14_meitu_14.jpg



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

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


15_meitu_15.jpg


  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. }
复制代码

串口输出:


16_meitu_16.jpg





收藏 评论2 发布时间:2020-9-24 12:54

举报

2个回答
乐天乐 回答时间:2020-9-24 15:22:40
rtthread
乐天乐 回答时间:2020-9-24 16:12:30
cubev5.61不支持吗?

所属标签

STM32团队

意法半导体微控制器和微处理器拥有广泛的产品线,包含低成本的8位单片机和基于ARM® Cortex®-M0、M0+、M3、M4、M33、M7及A7内核并具备丰富外设选择的32位微控制器及微处理器


最新内容

相似技术帖

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
微信公众号二维码 微信公众号
手机版二维码 手机版