Pandora loT使用uart3进行数据收发
1 引脚定义
潘多拉开发板预留了一些IO口供我们使用,其中便有PB10和PB11,其引脚功能查看数据手册如下:
使用USB转TTL将开发板连上电脑,下载口uart1也要连接
2 新建工程
打开RT-Studio,新建基于芯片STM32L475VETx的工程,下载口选择SWD.
3 修改配置
-
修改applications->main.c #include <rtthread.h>
#include <board.h>
#include <rtdevice.h>
#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
#define LED0_PIN GET_PIN(E,8)
int main(void)
{
int count = 1;
rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);
LOG_D("System Clock information");
LOG_D("SYSCLK_Frequency = %d", HAL_RCC_GetSysClockFreq());
LOG_D("HCLK_Frequency = %d", HAL_RCC_GetHCLKFreq());
LOG_D("PCLK1_Frequency = %d", HAL_RCC_GetPCLK1Freq());
LOG_D("PCLK2_Frequency = %d", HAL_RCC_GetPCLK2Freq());
while (count++)
{
rt_pin_write(LED0_PIN, count % 2);
// LOG_D("Hello RT-Thread!");
rt_thread_mdelay(1000);
}
return RT_EOK;
}
这一步修改完之后,保存编译下载,可以看到绿灯闪烁 -
打开drivers->board.h ,在其中添加uart3的引脚配置,并使能usat3,如下: #define BSP_USING_UART3
#define BSP_UART3_TX_PIN "PB10"
#define BSP_UART3_RX_PIN "PB11"
修改时钟为外部时钟源
#define BSP_CLOCK_SOURCE ("HSE")
#define BSP_CLOCK_SOURCE_FREQ_MHZ ((int32_t)8)
#define BSP_CLOCK_SYSTEM_FREQ_MHZ ((int32_t)80)
这一步修改完之后,保存编译下载程序,在finsh中输入list_device ,可以看到以及添加了uart3设备
- 添加
applications->myuart3.c ,代码如下:
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-03-06 F_188 the first version
*/
#include <rtthread.h>
#define UART3_NAME "uart3" /* 定义串口名称 */
#define RX_DATA_LEN 11 /* 定义接收数据长度 */
static rt_device_t ser;
static struct rt_semaphore rx_sem;
static char rx_data[RX_DATA_LEN];
/* 串口接收中断 */
static rt_err_t uart3_rx(rt_device_t dev, rt_size_t size)
{
rt_sem_release(&rx_sem);
return RT_EOK;
}
/* 串口线程入口 */
static void ser3_tid_entry(void *p)
{
rt_uint8_t i = 0, j;
char ch;
rt_uint8_t START_RX_FLAG = 0;
while(1)
{
while(rt_device_read(ser, -1, &ch, 1) != 1) /* 每接收一个字符则循环一次 */
{
rt_sem_take(&rx_sem, RT_WAITING_FOREVER); /* 等待获取信号量 */
if(i < RX_DATA_LEN)
{
if(ch == '$' || START_RX_FLAG) /* 自定义数据头为"$" */
{
START_RX_FLAG = 1;
rx_data[i++] = ch;
}
if(i == RX_DATA_LEN - 1) /* 打印输出 */
{
i = 0;
START_RX_FLAG = 0;
for(j = 0; j < RX_DATA_LEN; j++)
{
rt_kprintf("%c", rx_data[j]);
}
rt_kprintf("\n");
}
}
}
}
}
static int uart3_init(void)
{
rt_err_t ret = RT_EOK;
// 初始化串口3
ser = rt_device_find(UART3_NAME); /* 寻找设备 */
if(!ser)
{
rt_kprintf("find %s failed\n", UART3_NAME);
return -RT_ERROR;
}
ret = rt_device_open(ser, RT_DEVICE_FLAG_INT_RX); /* 打开设备 */
if(ret != RT_EOK)
{
rt_kprintf("open %s device failed!\n", UART3_NAME);
return -RT_ERROR;
}
rt_device_set_rx_indicate(ser, uart3_rx); /* 设置串口接收中断 */
// 初始化线程和信号量
rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
rt_thread_t tid = rt_thread_create("ser3", ser3_tid_entry, RT_NULL, 1024, 25, 10);
if(tid != RT_NULL)
{
rt_thread_startup(tid);
}
else
{
rt_kprintf("uart3 thread init failed!\n");
ret = -RT_ERROR;
}
rt_kprintf("uart3 init success\n");
return ret;
}
INIT_APP_EXPORT(uart3_init);
? 保存编译下载,在uart3的串口终端中输入$1234567890 ,在finsh中可以看到输出
4 参考
- RT-Thread Studio快速上手
- UART设备
|