1. HaaS100串口UART硬件說明
HaaS100上有3個物理串口(UART)。
1) UART 0
UART 0是調試串口,已用USB轉UART芯片轉為USB口,可通過Micro USB線與電腦連接,用於串口打印及shell命令輸入。同時,可以通過板子上硬件電阻切換成RS232。
UART 0調試串口在板子上的位置如下圖紅框所示:
UART 0作為調試口時,使用Micro USB數據線連接串口即可。
UART 0作為RS232使用時,在板子上的位置如下圖所示:
RS232由於與調試串口共用UART 0,使用時,需要修改板子上的硬件,把UART 0切換成RS232,修改方法如下:
如上圖,將圖示中紅框中的2個焊點短接,UART0切換成RS232接口,此時USB口無法使用。
2)UART 1
UART 1擴展成RS485接口,如下圖紅框所示:
上圖中標準的A、B、G表示RS485協議中的A、B和GND。
2)UART 2 (HaaS100常用)
UART 2為TTL,在42PIN排針上,具體位置如下圖所示:
2. 接口說明
UART接口在頭文件include/aos/hal/uart.h中,主要接口如下表:
接口 |
說明 |
hal_uart_init |
UART初始化,接口中設置UART的端口號、波特率、數據位寬等 |
hal_uart_send |
通過指定UART發送數據接口 |
hal_uart_send_poll |
該接口HaaS100上未實現 |
hal_uart_recv |
該接口HaaS100上未實現 |
hal_uart_recv_poll |
該接口HaaS100上未實現 |
hal_uart_recv_II |
通過指定UART接收指定長度的串口數據 |
hal_uart_finalize |
串口去初始化接口。 |
具體接口使用說明,可以參考AliOS Things接口幫助文檔 https://help.aliyun.com/document_detail/161062.html?spm=a2c4g.11186623.6.574.64c67c26w3auXW
3. 使用案例
下面以UART 2使用方式為例,介紹一下UART口的使用方法。
1)實現功能
通過UART 2連接電腦,電腦上的串口工具輸入一個字符串,按回車後,HaaS100通過UART2往電腦發送"recv="加相同的字符串。
2)硬件連接
UART 2為TTL接口,需要通過一個TTL轉USB的轉接設備連接。如下圖所示:
3)軟件代碼
- 串口初始化:
-
uart_dev_t uart_ttl = {0};
-
-
void uart_test_init()
-
{
-
uart_ttl.port = 2; /*ttl use uart 2*/
-
uart_ttl.config.baud_rate = 115200;
-
uart_ttl.config.data_width = DATA_WIDTH_8BIT;
-
uart_ttl.config.flow_control = FLOW_CONTROL_DISABLED;
-
uart_ttl.config.mode = MODE_TX_RX;
-
uart_ttl.config.parity = NO_PARITY;
-
uart_ttl.config.stop_bits = STOP_BITS_1;
-
-
hal_uart_init(&uart_ttl);
-
}
- 數據收送
-
void uart_test()
-
{
-
int ret = 0;
-
int recv_size = 0;
-
int index = 5;
-
char buf[128] = {0};
-
-
buf[0] = 'r';
-
buf[1] = 'e';
-
buf[2] = 'c';
-
buf[3] = 'v';
-
buf[4] = '=';
-
while (1) {
-
ret = -1;
-
recv_size = 0;
-
ret = hal_uart_recv_II(&uart_ttl, &buf[index], 1, &recv_size, 2000);
-
if ((ret == 0) && (recv_size == 1)) {
-
if (buf[index] == '\n') {
-
hal_uart_send(&uart_ttl, buf, index + 1, AOS_WAIT_FOREVER);
-
index = 5;
-
aos_msleep(10);
-
continue;
-
}
-
index ++;
-
if(index >= 128) {
-
hal_uart_send(&uart_ttl, buf, index, AOS_WAIT_FOREVER);
-
index = 5;
-
}
-
}
-
aos_msleep(10);
-
}
-
}
4)測試方法
第一步:
在solutions/helloworld_demo/helloworld.c中,增加頭文件#include "aos/hal/uart.h",增加上面串口初始化及傳輸數據收發的代碼。
請參考下面文檔下載代碼、編譯及燒錄
代碼詳見:
-
/*
-
* Copyright (C) 2015-2021 Alibaba Group Holding Limited
-
*/
-
-
-
-
-
-
-
-
-
-
-
uart_dev_t uart_ttl = {0};
-
-
void uart_test_init()
-
{
-
uart_ttl.port = 2; /*ttl use uart 2*/
-
uart_ttl.config.baud_rate = 115200;
-
uart_ttl.config.data_width = DATA_WIDTH_8BIT;
-
uart_ttl.config.flow_control = FLOW_CONTROL_DISABLED;
-
uart_ttl.config.mode = MODE_TX_RX;
-
uart_ttl.config.parity = NO_PARITY;
-
uart_ttl.config.stop_bits = STOP_BITS_1;
-
-
hal_uart_init(&uart_ttl);
-
}
-
-
void uart_test()
-
{
-
int ret = 0;
-
int recv_size = 0;
-
int index = 5;
-
char buf[128] = {0};
-
-
buf[0] = 'r';
-
buf[1] = 'e';
-
buf[2] = 'c';
-
buf[3] = 'v';
-
buf[4] = '=';
-
while (1) {
-
ret = -1;
-
recv_size = 0;
-
ret = hal_uart_recv_II(&uart_ttl, &buf[index], 1, &recv_size, 2000);
-
if ((ret == 0) && (recv_size == 1)) {
-
if (buf[index] == '\n') {
-
hal_uart_send(&uart_ttl, buf, index + 1, AOS_WAIT_FOREVER);
-
index = 5;
-
aos_msleep(10);
-
continue;
-
}
-
index ++;
-
if(index >= 128) {
-
hal_uart_send(&uart_ttl, buf, index, AOS_WAIT_FOREVER);
-
index = 5;
-
}
-
}
-
aos_msleep(10);
-
}
-
}
-
-
int application_start(int argc, char *argv[])
-
{
-
int count = 0;
-
-
printf("uart test entry here!\r\n");
-
-
uart_test_init();
-
uart_test();
-
}
第二步:
將編譯出來的版本燒錄到HaaS100中,按照硬件連接章節圖示連接HaaS100和電腦。在電腦上,使用串口工具打開UART2對應的串口,輸入字符串後,按回車。串口會打印出recv=加輸入的字符串。如下圖: