STM32 CubeIDE 使用RT-Thread Nano


  在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

在这里插入图片描述

回到 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
在这里插入图片描述
适配 RT-Thread Nano
中断与异常处理
RT-Thread 操作系统重定义 HardFault_HandlerPendSV_HandlerSysTick_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文件里面。
在这里插入图片描述
在这里插入图片描述

/* USER CODE BEGIN 4 */
char rt_hw_console_getchar(void)
{
	int ch = -1;
	if (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_RXNE) != RESET)
	{
		ch = huart1.Instance->DR & 0xff;
	}
	else
	{
		if (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_ORE) != RESET)
		{
			__HAL_UART_CLEAR_OREFLAG(&huart1);
		}
		rt_thread_mdelay(10);
	}
	return ch;
}
void rt_hw_console_output(const char *str)
{
	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')
		{
			ITM_SendChar(a);
			HAL_UART_Transmit(&huart1, (uint8_t*) &a, 1, 1);
		}
		HAL_UART_Transmit(&huart1, (uint8_t*) (str + i), 1, 1);
	}
}

/* USER CODE END 4 */

3.3 、编写线程文件

创建一个app_rt_thread.c文件用于保存线程代码
在这里插入图片描述
app_rt_thread.c文件内容:

#include "rtthread.h"
#include "main.h"
#include "stdio.h"
#include <finsh.h>	


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

int MX_RT_Thread_Init(void)
{
	//初始化线程
	rt_err_t rst;
	rst = rt_thread_init(&led_thread,
						(const char *)"ledshine",  /* 线程名字 */
						led_thread_entry,  /* 线程入口函数 */
						RT_NULL,           /* 线程入口函数参数 */
						&led_thread_stack[0],
						sizeof(led_thread_stack),   /* 线程栈大小 */
						RT_THREAD_PRIORITY_MAX-2,  /* 线程的优先级 */
						20); /* 线程时间片 */
	if(rst == RT_EOK)
	{///* 启动线程,开启调度 */
		rt_thread_startup(&led_thread);
	}

}


/*
*************************************************************************
* 线程定义
*************************************************************************
*/
static void led_thread_entry(void *parameter)
{
	while(1)
	{
		rt_kprintf("led1_thread running,LED1_ON\r\n");
		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);
		rt_thread_mdelay(500);
		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);
		rt_thread_mdelay(500);
	}
}

MSH_CMD_EXPORT(led_thread_entry,thread running);

3.4 、main.c 修改

在这里插入图片描述

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

extern int MX_RT_Thread_Init(void);

在这里插入图片描述

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

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

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

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
  MX_RT_Thread_Init();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	  HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_2);
	  rt_kprintf("led1_thread TEST\r\n");
	  rt_thread_mdelay(100);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

串口输出:
在这里插入图片描述

  • 8
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
STM32CubeIDE使用 RT-Thread,可以按照以下步骤进行: 1. 首先,你需要从 RT-Thread 官网下载 STM32CubeIDE 的插件。插件包含了 RT-Thread 的配置文件和驱动代码,可以让你在 STM32CubeIDE 中直接使用 RT-Thread。 2. 下载并安装 STM32CubeIDE,确保你已经安装了合适版本的 STM32CubeMX。 3. 打开 STM32CubeIDE,在 "Help" 菜单下找到 "Eclipse Marketplace"。 4. 在 "Eclipse Marketplace" 中搜索 "RT-Thread" 插件,安装它。 5. 安装完成后,重新启动 STM32CubeIDE。 6. 创建一个新的 STM32 项目或者打开一个已有的项目。 7. 在 "Project Explorer" 视图中,右击项目名称,选择 "Properties"。 8. 在弹出的对话框中,选择 "C/C++ Build" -> "Settings" -> "Tool Settings"。 9. 在 "Tool Settings" 中,选择 "MCU GCC Compiler" -> "Preprocessor"。 10. 在 "Defined symbols (-D)" 中添加 RT-Thread 的宏定义,例如:`RT_USING_RTTHREAD`,这些宏定义可以在 RT-Thread 的配置文件中找到。 11. 在 "Tool Settings" 中,选择 "MCU GCC Compiler" -> "Include paths"。 12. 添加 RT-Thread 的头文件路径,这些路径可以在 RT-Thread 的配置文件中找到。 13. 在 "Project Explorer" 视图中,找到 RT-Thread 的配置文件,通常是 rtconfig.h 或者 rtconfig.py。 14. 根据你的需求,配置 RT-Thread 的内核选项、驱动选项和组件选项。 15. 编写你的应用程序代码,并且按照 RT-Thread 的编程规范来使用 RT-Thread 的 API。 16. 编译、烧录和运行你的应用程序。 以上是在 STM32CubeIDE使用 RT-Thread 的基本步骤。根据你的具体需求,可能还需要进行一些额外的配置和修改。记得参考 RT-Thread 官方文档和示例代码来帮助你进行开发。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值