很多朋友在調(diào)試的時候,都喜歡使用串口來調(diào)試。畢竟簡單嘛。
1 RT-Thread 的UART簡介
UART和其他設(shè)備一樣,應(yīng)用程序通過統(tǒng)一的設(shè)備管理接口來訪問串口硬件,相關(guān)接口如下所示:
函數(shù) | 描述 |
---|---|
rt_device_find() | 查找設(shè)備 |
rt_device_open() | 打開設(shè)備 |
rt_device_read() | 讀取數(shù)據(jù) |
rt_device_write() | 寫入數(shù)據(jù) |
rt_device_control() | 控制設(shè)備 |
rt_device_set_rx_indicate() | 設(shè)置接收回調(diào)函數(shù) |
rt_device_set_tx_complete() | 設(shè)置發(fā)送完成回調(diào)函數(shù) |
rt_device_close() | 關(guān)閉設(shè)備 |
關(guān)于API的詳細(xì)描述請參看官網(wǎng)手冊:
https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/device/uart/uart_v1/uart
UART的主要步驟如下所示:
1.首先查找串口設(shè)備獲取設(shè)備句柄。
2.配置串口參數(shù)。
3.初始化回調(diào)函數(shù)發(fā)送使用的信號量,然后以讀寫及中斷接收方式打開串口設(shè)備。
4.設(shè)置串口設(shè)備的接收回調(diào)函數(shù),之后發(fā)送字符串,并創(chuàng)建讀取數(shù)據(jù)線程。
運(yùn)行序列圖如下圖所示:
上述方式是基于中斷實(shí)現(xiàn)的,當(dāng)然也可使用DMA,目前UART驅(qū)動還不支持,待以后完善吧。
2 UART使用實(shí)例
筆者這里使用的是EK-RA6E2開發(fā)板,R7FA6E2BB3CFM的有2路串口,均通過引腳引出,SCI0已經(jīng)用作調(diào)試口,因此筆者這里使用SCI9演示。
2.1 硬件電路
首先看下電路圖:
Figure 2?1 UART9硬件電路
SCI9使用的是P109和P110。
2.2 RA Smart Configurator配置UART
打開RA Smart Configurator,在配置界面里面依次打開“Pins->Peripherals->Connectivity:SCI->SCI9”配置SCI模塊,配置為“Asynchronous UART”模式,并選擇開發(fā)板所用的串口引腳,這里TX和RX分別接的是P109和P110引腳。
[]()Figure 2?2 串口引腳設(shè)置
接下來就是添加串口stack。
[]()Figure 2?3 添加串口stack步驟
接下來需要配置串口的參數(shù)。
[]()Figure 2?4 串口參數(shù)設(shè)置
這里可以設(shè)置串口的參數(shù),我這里設(shè)置串口的變量名、通道以及相應(yīng)的回調(diào)函數(shù),SCI的編號和Channel編號是一一對應(yīng)的,因此需要設(shè)置為0,回調(diào)函數(shù)依據(jù)C語言命名規(guī)范任意編譯一個就行。
RT-Thread配置
只需要簡單配置就可使用,當(dāng)然也可使用其他串口。
2.4 代碼實(shí)現(xiàn)
首先通過uart9設(shè)備來實(shí)現(xiàn)簡單的發(fā)送。
#define LED1_PIN BSP_IO_PORT_02_PIN_07 /* Onboard LED1 pins */
#define LED2_PIN BSP_IO_PORT_04_PIN_00 /* Onboard LED2 pins */
#define SAMPLE_UART_NAME "uart9" /* 串口設(shè)備名稱 */
static rt_device_t serial; /* 串口設(shè)備句柄 */
char str[] = "hello RT-Thread!rn";
void hal_entry(void)
{
/* 查找串口設(shè)備 */
serial = rt_device_find(SAMPLE_UART_NAME);
rt_device_open(serial, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING); // 串口設(shè)備使用模式為 (發(fā)送阻塞 接收非阻塞) 模式
/* 發(fā)送字符串 */
rt_device_write(serial, 0, str, (sizeof(str) - 1));
while (1)
{
rt_pin_write(LED1_PIN, PIN_HIGH);
rt_pin_write(LED2_PIN, PIN_HIGH);
rt_thread_mdelay(500);
rt_pin_write(LED1_PIN, PIN_LOW);
rt_pin_write(LED2_PIN, PIN_LOW);
rt_thread_mdelay(500);
}
}
將UART9接到USB轉(zhuǎn)TTL上。編譯下載程序,打印信息如下。
接下將通過消息隊(duì)列不斷接收數(shù)據(jù)。
#include < rtthread.h >
#include < rtdevice.h >
#define SAMPLE_UART_NAME "uart9" /* 串口設(shè)備名稱 */
/* 串口接收消息結(jié)構(gòu) */
struct rx_msg
{
rt_device_t dev;
rt_size_t size;
};
/* 串口設(shè)備句柄 */
static rt_device_t serial;
/* 消息隊(duì)列控制塊 */
static struct rt_messagequeue rx_mq;
/* 接收數(shù)據(jù)回調(diào)函數(shù) */
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
struct rx_msg msg;
rt_err_t result;
msg.dev = dev;
msg.size = size;
result = rt_mq_send(&rx_mq, &msg, sizeof(msg));
if (result == -RT_EFULL)
{
/* 消息隊(duì)列滿 */
rt_kprintf("message queue full!n");
}
return result;
}
static void serial_thread_entry(void *parameter)
{
struct rx_msg msg;
rt_err_t result;
rt_uint32_t rx_length;
static char rx_buffer[1024 + 1];
while (1)
{
rt_memset(&msg, 0, sizeof(msg));
/* 從消息隊(duì)列中讀取消息 */
result = rt_mq_recv(&rx_mq, &msg, sizeof(msg), RT_WAITING_FOREVER);
rt_kprintf("recv result = %d rn",result);
if (result > 0)
{
/* 從串口讀取數(shù)據(jù) */
rx_length = rt_device_read(msg.dev, 0, rx_buffer, msg.size);
rx_buffer[rx_length] = '?';
/* 通過串口設(shè)備 serial 輸出讀取到的消息 */
rt_device_write(serial, 0, rx_buffer, rx_length);
/* 打印數(shù)據(jù) */
rt_kprintf("%sn",rx_buffer);
}
}
}
static int uart_sample(int argc, char *argv[])
{
rt_err_t ret = RT_EOK;
char uart_name[RT_NAME_MAX];
static char msg_pool[1024];
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);
}
/* 查找串口設(shè)備 */
serial = rt_device_find(uart_name);
if (!serial)
{
rt_kprintf("find %s failed!n", uart_name);
return RT_ERROR;
}
/* 初始化消息隊(duì)列 */
rt_mq_init(&rx_mq, "rx_mq",
msg_pool, /* 存放消息的緩沖區(qū) */
sizeof(struct rx_msg), /* 一條消息的最大長度 */
sizeof(msg_pool), /* 存放消息的緩沖區(qū)大小 */
RT_IPC_FLAG_FIFO); /* 如果有多個線程等待,按照先來先得到的方法分配消息 */
/* 接收及輪詢發(fā)送方式打開串口設(shè)備 */
rt_device_open(serial, RT_DEVICE_FLAG_RX_NON_BLOCKING | RT_DEVICE_FLAG_TX_BLOCKING);
/* 設(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);
編譯下載,調(diào)試信息如下:
從以上打印信息可以看出,串口9已經(jīng)使能,然后使用MSH命令‘uart_sample’即可使能串口線程。
使能串口線程后,串口9將打印‘hello RT-Thread’,用戶也可通過串口9發(fā)送數(shù)據(jù)到開發(fā)板,發(fā)送信息后,調(diào)試終端即可看到串口9發(fā)送的數(shù)據(jù)。
-
調(diào)試
+關(guān)注
關(guān)注
7文章
618瀏覽量
35195 -
串口
+關(guān)注
關(guān)注
15文章
1596瀏覽量
81341 -
uart
+關(guān)注
關(guān)注
22文章
1284瀏覽量
105465
發(fā)布評論請先 登錄
評論