STM32 CubeIDE uses RT-Thread Nano


  RT-Thread Nano has been integrated in STM32 CubeIDE, which can be downloaded and added directly in the IDE.

1. Installation of RT-Thread Nano pack

Open STM32 CubeIDE ---------> Software Packs ------------> Manager Software Packs interface
Insert picture description here

  To obtain the RT-Thread Nano software package, you need to add https://www.rt-thread.org/download/cube/RealThread.RT-Thread.pdsc in STM32CubeIDE

Insert picture description here

Back to the Manage software packages interface, you will find the RT-Thread Nano 3.1.3 software package, select the software package, and click Install Now, as shown in the figure below (color filling means it has been installed):
Insert picture description here
Insert picture description here

2. Create a project to add RT-Thread Nano

2.1. Create a basic project

Create a basic project file that contains 2 LED lights and USART1.

Insert picture description here
Insert picture description here
Insert picture description here

2.2, placement Nano

Tick ​​RT-Thread to
Insert picture description here
adapt RT-Thread Nano
interrupt and exception handling.
RT-Thread operating system redefines HardFault_Handler , PendSV_Handler , SysTick_Handler interrupt functions. In order to avoid the problem of duplication of definitions, you need to generate code in the interrupt configuration before generating the project. In the options, deselect the three interrupt functions (the corresponding comment options are Hard fault interrupt, Pendable request, Time base: System tick timer), and finally click to generate the code, the specific operation is as shown in the figure below
Insert picture description here

3. Engineering code modification

3.1 Parts that need to be modified

1. Modify the startup file startup_stm32f103rctx.s
bl main to bl entry
Insert picture description here

3.2. Configure rt_kprintf port output

Port mapping, functions can be placed in the main.c file.
Insert picture description here
Insert picture description here

/* 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, write thread file

Create an app_rt_thread.c file to save the thread code
Insert picture description here
app_rt_thread.c file content:

#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 modification

Insert picture description here

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

extern int MX_RT_Thread_Init(void);

Insert picture description here

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 */
}

Serial output:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45488643/article/details/108761487