STMCUBEIDE RT-THREAD NANO工程搭建(LED闪灯+UART)
RT-THREAD提供了用于STM32CUBEIDE/STM32CUBEMX的支持包,从而可以搭建资源占用最小化的Nano系统。这里以STM32F401CCU6开发板和STM32CUBEIDE开发环境为例,搭建LED闪灯和UART通讯两个任务的工程。
本实验是在 STMCUBEIDE RT-THREAD NANO工程搭建(LED闪灯 ) 的基础上增加UART通讯,所以基本工程搭建过程一致,这里只介绍增加的部分。
UART配置
这里采用UART2作为通讯口,在工程的rtconfig.h文件里,就不需要注释掉RT_USING_CONSOLE。另外如果不使用UART2而使用如UART1,则仍然应注释掉RT_USING_CONSOLE。
保存后生成增加UART2配置后的代码。
任务代码升级
app_rt_thread.c里需要增加串口的任务,这里的任务设计为默认定时打印输出”You make it!“, 而当每次收到接收中断后,会插入一条"Well done!"的打印输出,这里的实验功能不对串口接收到的数据做识别,可自行增加及调整。
app_rt_thread.c的代码:
#include "rtthread.h"#include "main.h"#include "stdio.h"/* 定义线程控制块 */void MX_RT_Thread_Init(void);//添加LED闪烁线程static struct rt_thread led_thread;static char led_thread_stack[256];static void led_thread_entry(void *parameter);static struct rt_thread uart2_thread;static char uart2_thread_stack[256];static void uart2_thread_entry(void *parameter);void 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);}rst = rt_thread_init(&uart2_thread,(const char *)"uart2_console_output", /* 线程名字 */uart2_thread_entry, /* 线程入口函数 */RT_NULL, /* 线程入口函数参数 */&uart2_thread_stack[0],sizeof(uart2_thread_stack), /* 线程栈大小 */RT_THREAD_PRIORITY_MAX-3, /* 线程的优先级 */20); /* 线程时间片 */if(rst == RT_EOK){///* 启动线程,开启调度 */rt_thread_startup(&uart2_thread);}}/*************************************************************************** 线程定义**************************************************************************/static void led_thread_entry(void *parameter){while(1){HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);rt_thread_mdelay(500);}}static void uart2_thread_entry(void *parameter){extern UART_HandleTypeDef huart2;extern uint8_t StatusFlag ;extern uint8_t Uart2_RxBuff[8];char resp1[] = "You make it!\r\n";char resp2[] = "Well done!\r\n";while(1){if(StatusFlag==0){HAL_UART_Transmit(&huart2, resp1, sizeof(resp1), 2700);rt_thread_mdelay(500);}else{HAL_UART_Transmit(&huart2, resp2, sizeof(resp2), 2700);rt_thread_mdelay(500);StatusFlag = 0;__HAL_UART_CLEAR_FLAG(&huart2, UART_FLAG_RXNE);HAL_UART_Receive_IT(&huart2, Uart2_RxBuff, 1);}}}MSH_CMD_EXPORT(led_thread_entry,thread running);MSH_CMD_EXPORT(uart2_thread_entry,thread running);
main.c代码升级
main.c代码增加串口中断接收的处理部分,代码如下:
/* USER CODE BEGIN Header *//** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * Copyright (c) 2022 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** *//* USER CODE END Header *//* Includes ------------------------------------------------------------------*/#include "main.h"/* Private includes ----------------------------------------------------------*//* USER CODE BEGIN Includes */#include "rtthread.h"extern void MX_RT_Thread_Init(void);/* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*//* USER CODE BEGIN PTD */uint8_t StatusFlag = 0;uint8_t Uart2_RxBuff[8];/* USER CODE END PTD *//* Private define ------------------------------------------------------------*//* USER CODE BEGIN PD *//* USER CODE END PD *//* Private macro -------------------------------------------------------------*//* USER CODE BEGIN PM *//* USER CODE END PM *//* Private variables ---------------------------------------------------------*/UART_HandleTypeDef huart2;/* USER CODE BEGIN PV *//* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/void SystemClock_Config(void);static void MX_GPIO_Init(void);static void MX_USART2_UART_Init(void);/* USER CODE BEGIN PFP *//* USER CODE END PFP *//* Private user code ---------------------------------------------------------*//* USER CODE BEGIN 0 *//* USER CODE END 0 *//** * @brief The application entry point. * @retval int */int main(void){ /* USER CODE BEGIN 1 */#if 0 /* 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 */#endif /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_USART2_UART_Init(); /* USER CODE BEGIN 2 */ MX_RT_Thread_Init(); HAL_UART_Receive_IT(&huart2, Uart2_RxBuff, 1); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { rt_thread_mdelay(1000); /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */}/** * @brief System Clock Configuration * @retval None */void SystemClock_Config(void){ RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Configure the main internal regulator output voltage */ __HAL_RCC_PWR_CLK_ENABLE(); __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2); /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI; RCC_OscInitStruct.PLL.PLLM = 16; RCC_OscInitStruct.PLL.PLLN = 168; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = 4; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); }}/** * @brief USART2 Initialization Function * @param None * @retval None */static void MX_USART2_UART_Init(void){ /* USER CODE BEGIN USART2_Init 0 */ /* USER CODE END USART2_Init 0 */ /* USER CODE BEGIN USART2_Init 1 */ /* USER CODE END USART2_Init 1 */ huart2.Instance = USART2; huart2.Init.BaudRate = 115200; huart2.Init.WordLength = UART_WORDLENGTH_8B; huart2.Init.StopBits = UART_STOPBITS_1; huart2.Init.Parity = UART_PARITY_NONE; huart2.Init.Mode = UART_MODE_TX_RX; huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart2.Init.OverSampling = UART_OVERSAMPLING_16; if (HAL_UART_Init(&huart2) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN USART2_Init 2 */ /* USER CODE END USART2_Init 2 */}/** * @brief GPIO Initialization Function * @param None * @retval None */static void MX_GPIO_Init(void){ GPIO_InitTypeDef GPIO_InitStruct = {0}; /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET); /*Configure GPIO pin : PC13 */ GPIO_InitStruct.Pin = GPIO_PIN_13; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);}/* USER CODE BEGIN 4 */void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle){ if(UartHandle==&huart2) { StatusFlag = 1; }}/* USER CODE END 4 *//** * @brief Period elapsed callback in non blocking mode * @note This function is called when TIM2 interrupt took place, inside * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment * a global variable "uwTick" used as application time base. * @param htim : TIM handle * @retval None */void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){ /* USER CODE BEGIN Callback 0 */ /* USER CODE END Callback 0 */ if (htim->Instance == TIM2) { HAL_IncTick(); } /* USER CODE BEGIN Callback 1 */ /* USER CODE END Callback 1 */}/** * @brief This function is executed in case of error occurrence. * @retval None */void Error_Handler(void){ /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ __disable_irq(); while (1) { } /* USER CODE END Error_Handler_Debug */}#ifdef USE_FULL_ASSERT/** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */void assert_failed(uint8_t *file, uint32_t line){ /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */}#endif /* USE_FULL_ASSERT */
实验效果
下载版本后,通过串口工具,可以观察打印输出,并在输入后看到打印变化:
工程资源占用
资源占用情况如下(存储空间节省的编译方式):
例程下载
STM32F401CCU6_RTT_LED_UART
STM32C011J4M3_RTT_LED_UART
–End–