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);
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代码增加串口中断接收的处理部分,代码如下:
#include "main.h"
#include "rtthread.h"
extern void MX_RT_Thread_Init(void);
uint8_t StatusFlag = 0;
uint8_t Uart2_RxBuff[8];
UART_HandleTypeDef huart2;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
int main(void)
{
#if 0
HAL_Init();
SystemClock_Config();
#endif
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_RT_Thread_Init();
HAL_UART_Receive_IT(&huart2, Uart2_RxBuff, 1);
while (1)
{
rt_thread_mdelay(1000);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
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();
}
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();
}
}
static void MX_USART2_UART_Init(void)
{
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();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
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);
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
{
if(UartHandle==&huart2)
{
StatusFlag = 1;
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (htim->Instance == TIM2) {
HAL_IncTick();
}
}
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
}
#endif
实验效果
下载版本后,通过串口工具,可以观察打印输出,并在输入后看到打印变化:
工程资源占用
资源占用情况如下(存储空间节省的编译方式):
例程下载
STM32F401CCU6_RTT_LED_UART
STM32C011J4M3_RTT_LED_UART
–End–
|