開發(fā)板使用的是gd32f450zk,env工具使用的版本是1.3.5,rtthread版本是5.0.0。
添加串口外設(shè)的方法,串口2是打印口,串口0是數(shù)據(jù)收發(fā)口。
串口2的引腳是PB10和PD9,串口0的引腳是PA9和PA10.
使用env工具,menuconfig進行配置,修改調(diào)試打印口的名稱為uart2,如下圖:
串口的配置,如下圖:
保存之后,生成工程。打開工程進行查看,如下圖:
對串口2的代碼進行修改,修改之后如下:
#ifdef BSP_USING_UART2
{
USART2, // uart peripheral index
USART2_IRQn, // uart iqrn
RCU_USART2, RCU_GPIOB, RCU_GPIOD, // periph clock, tx gpio clock, rt gpio clock
#if defined SOC_SERIES_GD32F4xx
GPIOB, GPIO_AF_7, GPIO_PIN_10, // tx port, tx alternate, tx pin
//GPIOB, GPIO_AF_7, GPIO_PIN_11, // rx port, rx alternate, rx pin
GPIOD, GPIO_AF_7, GPIO_PIN_9, // rx port, rx alternate, rx pin
//GPIOC, GPIO_AF_7, GPIO_PIN_11, // rx port, rx alternate, rx pin
#else
GPIOB, GPIO_PIN_10, // tx port, tx pin
GPIOB, GPIO_PIN_11, // rx port, rx pin
#endif
&serial2,
"uart2",
},
#endif
對工程進行編譯,下載到開發(fā)板運行,可以看到串口2打印的信息,如下圖,
串口0數(shù)據(jù)收發(fā)口的測試,直接復(fù)制官方文檔的測試demo,進行修改,改成串口0,代碼如下:
/*
程序清單:這是一個 串口 設(shè)備使用例程
例程導(dǎo)出了 uart_sample 命令到控制終端
命令調(diào)用格式:uart_sample uart2
命令解釋:命令第二個參數(shù)是要使用的串口設(shè)備名稱,為空則使用默認的串口設(shè)備
程序功能:通過串口輸出字符串"hello RT-Thread!",然后錯位輸出輸入的字符
/
#include
#define SAMPLE_UART_NAME "uart0"
/ 用于接收消息的信號量 /
static struct rt_semaphore rx_sem;
static rt_device_t serial;
/ 接收數(shù)據(jù)回調(diào)函數(shù) /
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
/ 串口接收到數(shù)據(jù)后產(chǎn)生中斷,調(diào)用此回調(diào)函數(shù),然后發(fā)送接收信號量 */
rt_sem_release(&rx_sem);
return RT_EOK;
}
static void serial_thread_entry(void parameter)
{
char ch;
while (1)
{
/ 從串口讀取一個字節(jié)的數(shù)據(jù),沒有讀取到則等待接收信號量 /
while (rt_device_read(serial, -1, &ch, 1) != 1)
{
/ 阻塞等待接收信號量,等到信號量后再次讀取數(shù)據(jù) /
rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
}
/ 讀取到的數(shù)據(jù)通過串口錯位輸出 */
ch = ch + 1;
rt_device_write(serial, 0, &ch, 1);
}
}
static int uart_sample(int argc, char argv[])
{
rt_err_t ret = RT_EOK;
char uart_name[RT_NAME_MAX];
char str[] = "hello RT-Thread!rn";
if (argc == 2)
{
rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
}
else
{
rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);
}
/ 查找系統(tǒng)中的串口設(shè)備 /
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);
/ 以中斷接收及輪詢發(fā)送模式打開串口設(shè)備 /
rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
/ 設(shè)置接收回調(diào)函數(shù) /
rt_device_set_rx_indicate(serial, uart_input);
/ 發(fā)送字符串 /
rt_device_write(serial, 0, str, (sizeof(str) - 1));
/ 創(chuàng)建 serial 線程 /
rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10);
/ 創(chuàng)建成功則啟動線程 /
if (thread != RT_NULL)
{
rt_thread_startup(thread);
}
else
{
ret = RT_ERROR;
}
return ret;
}
/ 導(dǎo)出到 msh 命令列表中 */
MSH_CMD_EXPORT(uart_sample, uart device sample);
測試,可以看到串口0發(fā)送了信息”hello RT-Thread!”,用串口調(diào)試助手進行數(shù)據(jù)發(fā)送,可以看到串口0接收到數(shù)據(jù)并且返回數(shù)據(jù)。
在此基礎(chǔ)上進行修改,使用3個信號量,設(shè)計一種帶超時的數(shù)據(jù)接收處理方式,修改后的代碼如下:
/*
Copyright (c) 2006-2021, RT-Thread Development Team
SPDX-License-Identifier: Apache-2.0
Change Logs:
Date Author Notes
2023-02-03 XYZ the first version
/
#include
#include
#include
#include
#define SAMPLE_UART_NAME "uart0"
#define LRNGTH 256
/ 用于接收消息的信號量 */
static struct rt_semaphore rx_sem;
static struct rt_semaphore rx_semRx;
static struct rt_semaphore rx_semRxTimeOut;
static rt_device_t serial;
static uint8_t bufTemp[LRNGTH]={0};
static uint8_t bufTempNum=0;
//發(fā)送函數(shù)
static int Bsp_Tx(uint8_t *buf,int size)
{
rt_device_write(serial, 0, buf, size);
// for(int k=0;k < size;k++)
// {
// rt_kprintf("send_buf[%d]=%02xrn",k,buf[k]);
// }
return 0;
}
//調(diào)用此函數(shù),獲取接收數(shù)據(jù),帶超時
static int Bsp_Rx(uint8_t *buf,int size)
{
rt_err_t ret;
// struct timeval tv = { 0 };
// struct timezone tz = { 0 };
// gettimeofday(&tv, &tz);
// rt_kprintf("time1:%drn",tv.tv_sec);
ret=rt_sem_take(&rx_semRxTimeOut, 1000);//1秒超時
// gettimeofday(&tv, &tz);
// rt_kprintf("time2:%drn",tv.tv_sec);
if( ret == RT_EOK)
{
rt_memcpy(buf,bufTemp,bufTempNum);
size=bufTempNum;
bufTempNum=0;
return size;
}
else if(ret == -RT_ETIMEOUT)
{
bufTempNum=0;
return 0;
}
bufTempNum=0;
return -1;
}
/* 接收數(shù)據(jù)回調(diào)函數(shù) */
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
/* 串口接收到數(shù)據(jù)后產(chǎn)生中斷,調(diào)用此回調(diào)函數(shù),然后發(fā)送接收信號量 */
rt_sem_release(&rx_sem);
return RT_EOK;
}
static void serial_thread_entry(void *parameter)
{
int len=0;
while (1)
{
rt_sem_take(&rx_semRx, RT_WAITING_FOREVER);
if( (bufTemp[0] == 0x01) && (bufTempNum==5) )//這里需要根據(jù)實際修改,判斷是否接收到了一包數(shù)據(jù)
{
rt_sem_release(&rx_semRxTimeOut);//數(shù)據(jù)接收成功
}
}
}
static void serial_threadRx_entry(void *parameter)
{
char ch[LRNGTH]={0};
int len=0;
while (1)
{
/* 從串口讀取一個字節(jié)的數(shù)據(jù),沒有讀取到則等待接收信號量 */
rt_memset(ch,0,sizeof(ch));
while ( ( len = rt_device_read(serial, -1, ch, sizeof(ch)) )== 0)
{
/* 阻塞等待接收信號量,等到信號量后再次讀取數(shù)據(jù) */
rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
}
//rt_kprintf("len=%d,ch=%srn",len,ch);
rt_memcpy(&bufTemp[bufTempNum],ch,len);
bufTempNum += len;
rt_sem_release(&rx_semRx);
}
}
static int Bsp_init(void)
{
rt_err_t ret = RT_EOK;
//char str[] = "hello RT-Thread!rn";
/* 查找系統(tǒng)中的串口設(shè)備 */
serial = rt_device_find(SAMPLE_UART_NAME);
if (!serial)
{
rt_kprintf("find %s failed!n", SAMPLE_UART_NAME);
return RT_ERROR;
}
//這里進行串口配置
/* 初始化信號量 */
rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
rt_sem_init(&rx_semRx, "rx_semRx", 0, RT_IPC_FLAG_FIFO);
rt_sem_init(&rx_semRxTimeOut, "rx_semRxTimeOut", 0, RT_IPC_FLAG_FIFO);
/* 以中斷接收及輪詢發(fā)送模式打開串口設(shè)備 */
rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
/* 設(shè)置接收回調(diào)函數(shù) */
rt_device_set_rx_indicate(serial, uart_input);
/* 發(fā)送字符串 */
//rt_device_write(serial, 0, str, (sizeof(str) - 1));
/* 創(chuàng)建 serial 線程 */
rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10);
/* 創(chuàng)建成功則啟動線程 */
if (thread != RT_NULL)
{
rt_thread_startup(thread);
}
else
{
ret = RT_ERROR;
}
/* 創(chuàng)建 serial 線程 */
thread = rt_thread_create("serialRx", serial_threadRx_entry, RT_NULL, 1024, 25, 10);
/* 創(chuàng)建成功則啟動線程 */
if (thread != RT_NULL)
{
rt_thread_startup(thread);
}
else
{
ret = RT_ERROR;
}
return ret;
return 0;
}
-
串口
+關(guān)注
關(guān)注
14文章
1557瀏覽量
77035 -
GPIO
+關(guān)注
關(guān)注
16文章
1216瀏覽量
52377 -
回調(diào)函數(shù)
+關(guān)注
關(guān)注
0文章
87瀏覽量
11621 -
串口中斷
+關(guān)注
關(guān)注
0文章
67瀏覽量
14007 -
RT-Thread
+關(guān)注
關(guān)注
31文章
1305瀏覽量
40383 -
GD32F450
+關(guān)注
關(guān)注
1文章
11瀏覽量
6673 -
串口傳輸
+關(guān)注
關(guān)注
0文章
33瀏覽量
1845
發(fā)布評論請先 登錄
相關(guān)推薦
評論