1、在board目录下Kconfig文件中增加如下内容
打开CubeMX_Config下的CubeMX_Config.ioc的cubeMx工程
a;使能串口1;
b;使能外部时钟,设置时钟树;
1工程名字必须是CubeMX_Config
下图是新增串口的配置
代码生成后会出现编译不过的情况
解决办法是添加一个 stm32f1xx_hal_conf.h
参看博客 https://blog.csdn.net/u010261063/article/details/103988752
测试代码:
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-11-06 SummerGift first version
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
/* defined the LED0 pin: PB1 */
#define LED0_PIN GET_PIN(B, 5)
#define SAMPLE_UART_NAME "uart3"
/* ? ? ? ? ? ? ? ? ? ? */
static struct rt_semaphore rx_sem;
static rt_device_t serial;
/* ? ? ? ? ? ? ? ? */
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
/* ? ? ? ? ? ? ? ? ? ? ? ?, ? ? ? ? ? ? ?, ? ? ? ? ? ? ? ? ? */
rt_sem_release(&rx_sem);
return RT_EOK;
}
static void serial_thread_entry(void *parameter)
{
char ch='a';
while (1)
{
/* ? ? ? ? ? ? ? ? ? ? ? ?, ? ? ? ? ? ? ? ? ? ? ? ? ? */
while (rt_device_read(serial, -1, &ch, 1) != 1)
{
/* ? ? ? ? ? ? ? ? ?, ? ? ? ? ? ? ? ? ? ? ? ? */
rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
}
/* ? ? ? ? ? ? ? ? ? ? ? ? ? ? */
// ch = ch + 1;
rt_device_write(serial, 0, &ch, 1);
// rt_thread_sleep(100);
}
}
static int uart_sample()
{
rt_err_t ret = RT_EOK;
char uart_name[RT_NAME_MAX] ;
char str[] = "hello RT-Thread!\r\n";
rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);
/* ? ? ? ? ? ? ? ? ? ? */
serial = rt_device_find(uart_name);
if (!serial)
{
rt_kprintf("find %s failed!\n", uart_name);
return RT_ERROR;
}
/* ? ? ? ? ? ? */
rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
/* ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? */
rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
/* ? ? ? ? ? ? ? ? */
rt_device_set_rx_indicate(serial, uart_input);
/* ? ? ? ? ? */
rt_device_write(serial, 0, str, (sizeof(str) - 1));
/* ? ? serial ? ? */
rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL,
1024, 25, 10);
/* ? ? ? ? ? ? ? ? ? */
if (thread != RT_NULL)
{
rt_thread_startup(thread);
}
else
{
ret = RT_ERROR;
}
return ret;
}
int main(void)
{
int count = 1;
/* set LED0 pin mode to output */
rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);
uart_sample();
while (count++)
{
rt_pin_write(LED0_PIN, PIN_HIGH);
rt_thread_mdelay(500);
rt_pin_write(LED0_PIN, PIN_LOW);
rt_thread_mdelay(500);
}
return RT_EOK;
}
后记:
更改cubeMx后导致stm32f1xx_hal_conf.h文件中的宏定义发生变化