STM32 CubeIDE utiliza RT-Thread Nano
RT-Thread Nano se ha integrado en STM32 CubeIDE, que se puede descargar y agregar directamente en el IDE.
1. Instalación del paquete RT-Thread Nano
Abra STM32 CubeIDE ---------> Paquetes de software ------------> Interfaz de paquetes de software de administrador
Para obtener el paquete de software RT-Thread Nano, debe agregar https://www.rt-thread.org/download/cube/RealThread.RT-Thread.pdsc en STM32CubeIDE
De vuelta a la interfaz Administrar paquetes de software, encontrará el paquete de software RT-Thread Nano 3.1.3, seleccione el paquete de software y haga clic en Instalar ahora, como se muestra en la figura siguiente (el relleno de color significa que se ha instalado):
2. Cree un proyecto para agregar RT-Thread Nano
2.1. Crear un proyecto básico
Cree un archivo de proyecto básico que contenga 2 luces LED y USART1.
2.2, colocación Nano
Marque RT-Thread para
adaptar RT-Thread Nano
interrupción y manejo de excepciones. El
sistema operativo RT-Thread redefine las funciones de interrupción HardFault_Handler , PendSV_Handler , SysTick_Handler . Para evitar el problema de la duplicación de definiciones, es necesario generar código en la configuración de interrupciones antes de generar el proyecto. En las opciones, deseleccione las tres funciones de interrupción (las opciones de comentario correspondientes son Interrupción por falla severa, Solicitud pendiente, Base de tiempo: Temporizador de tick del sistema), y finalmente haga clic para generar el código, la operación específica es como se muestra en la siguiente figura
3. Modificación del código de ingeniería
3.1 Partes que necesitan ser modificadas
1. Modifique el archivo de inicio startup_stm32f103rctx.s
bl main a la entrada bl
3.2. Configurar la salida del puerto rt_kprintf
Mapeo de puertos, las funciones se pueden colocar en el archivo 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, escribir archivo de hilo
Cree un archivo app_rt_thread.c para guardar el
contenido del archivo app_rt_thread.c del código del hilo :
#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, modificación de 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 */
}
Salida serial: