STM32 CubeIDE utilise RT-Thread Nano
RT-Thread Nano a été intégré dans STM32 CubeIDE, qui peut être téléchargé et ajouté directement dans l'IDE.
1. Installation du pack RT-Thread Nano
Ouvrez STM32 CubeIDE ---------> Software Packs ------------> Interface Manager Software Packs
Pour obtenir le progiciel RT-Thread Nano, vous devez ajouter https://www.rt-thread.org/download/cube/RealThread.RT-Thread.pdsc dans STM32CubeIDE
De retour à l'interface de gestion des packages logiciels, vous trouverez le package logiciel RT-Thread Nano 3.1.3, sélectionnez le package logiciel et cliquez sur Installer maintenant, comme illustré dans la figure suivante (le remplissage de couleur indique qu'il a été installé):
2. Créez un projet pour ajouter RT-Thread Nano
2.1. Créer un projet de base
Créez un fichier de projet de base contenant 2 voyants LED et USART1.
2.2, placement Nano
Cochez RT-Thread pour
adapter la
gestion des interruptions et des exceptions RT-Thread Nano . Le
système d'exploitation RT-Thread redéfinit les fonctions d'interruption HardFault_Handler , PendSV_Handler , SysTick_Handler . Afin d'éviter le problème de duplication des définitions, vous devez générer du code dans la configuration d'interruption avant de générer le projet. Dans les options, désélectionnez les trois fonctions d'interruption (les options de commentaire correspondantes sont Interruption sur défaut dur, Demande en attente, Base de temps: Minuterie de ticks système), et enfin cliquez pour générer le code, l'opération spécifique est comme indiqué dans la figure ci-dessous
3. Modification du code technique
3.1 Pièces à modifier
1. Modifiez le fichier de démarrage startup_stm32f103rctx.s
bl main en entrée bl
3.2. Configurer la sortie du port rt_kprintf
Mappage de port, les fonctions peuvent être placées dans le fichier 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, écrire un fichier de thread
Créez un fichier app_rt_thread.c pour enregistrer le
contenu du fichier de code de thread 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, modification 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 */
}
Sortie série: