
1、HaaS100串口UART硬體說明
HaaS100開發板上有3個物理串口(UART),
1.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口無法使用,
1.2、UART 1
UART 1擴展成RS485介面,如下圖紅框所示:
上圖中標準的A、B、G表示RS485協議中的A、B和GND,
1.3、UART 2
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口的使用方法,
3.1、實作功能
通過UART 2連接電腦,電腦上的串口工具輸入一個字串,按回車后,HaaS100通過UART2往電腦發送"recv="加相同的字串,
3.2、硬體連接
UART 2為TTL介面,需要通過一個TTL轉USB的轉接設備連接,如下圖所示:
3.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);
}
}
3.4、測驗方法
第一步:
在application/example/helloworld_demo/appdemo.c中,增加頭檔案#include "aos/hal/uart.h",增加上面串口初始化及傳輸資料收發的代碼,然后執行命令aos make helloworld_demo@haas100 -c config; aos make命令編譯,
代碼詳見:
/*
* Copyright (C) 2015-2021 Alibaba Group Holding Limited
*/
#include <stdio.h>
#include <stdlib.h>
#include <aos/errno.h>
#include <aos/kernel.h>
#include "aos/init.h"
#include "board.h"
#include <k_api.h>
#include "aos/hal/uart.h"
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快速開始,
按照硬體連接章節圖示連接HaaS100和電腦,在電腦上,使用串口工具打開UART2對應的串口,輸入字串后,按回車,串口會列印出recv=加輸入的字串,如下圖:
4、開發者技術支持
如需更多技術支持,可加入釘釘開發者群,或者關注微信公眾號

更多技術與解決方案介紹,請訪問阿里云AIoT首頁https://iot.aliyun.com/
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/250743.html
標籤:其他
