HaaS100串口UART使用介绍

简介: HaaS100串口UART使用介绍

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

串口去初始化接口。


3. 使用案例

下面以UART 2使用方式为例,介绍一下UART口的使用方法。

1)实现功能

通过UART 2连接电脑,电脑上的串口工具输入一个字符串,按回车后,HaaS100通过UART2往电脑发送"recv="加相同的字符串。

2)硬件连接

UART 2为TTL接口,需要通过一个TTL转USB的转接设备连接。如下图所示:

3)软件代码

  • 串口初始化:

  1. uart_dev_t uart_ttl = {0};

  2. void uart_test_init()

  3. {

  4.    uart_ttl.port = 2;  /*ttl use uart 2*/

  5.    uart_ttl.config.baud_rate = 115200;

  6.    uart_ttl.config.data_width = DATA_WIDTH_8BIT;

  7.    uart_ttl.config.flow_control = FLOW_CONTROL_DISABLED;

  8.    uart_ttl.config.mode = MODE_TX_RX;

  9.    uart_ttl.config.parity = NO_PARITY;

  10.    uart_ttl.config.stop_bits = STOP_BITS_1;

  11.    hal_uart_init(&uart_ttl);

  12. }

  • 数据收送

  1. void uart_test()

  2. {

  3. int ret       = 0;

  4. int recv_size = 0;

  5. int index     = 5;

  6.    char buf[128] = {0};

  7.    buf[0] = 'r';

  8.    buf[1] = 'e';

  9.    buf[2] = 'c';

  10.    buf[3] = 'v';

  11.    buf[4] = '=';

  12. while (1) {

  13.        ret = -1;

  14.        recv_size = 0;

  15.        ret = hal_uart_recv_II(&uart_ttl, &buf[index], 1, &recv_size, 2000);

  16. if ((ret == 0) && (recv_size == 1)) {

  17. if (buf[index] == '\n') {

  18.                hal_uart_send(&uart_ttl, buf, index + 1, AOS_WAIT_FOREVER);

  19. index = 5;

  20.                aos_msleep(10);

  21. continue;

  22.            }

  23. index ++;

  24. if(index >= 128) {

  25.                hal_uart_send(&uart_ttl, buf, index, AOS_WAIT_FOREVER);

  26. index = 5;

  27.            }

  28.        }

  29.        aos_msleep(10);

  30.    }

  31. }

4)测试方法

第一步:

在solutions/helloworld_demo/helloworld.c中,增加头文件#include "aos/hal/uart.h",增加上面串口初始化及传输数据收发的代码。

请参考下面文档下载代码、编译及烧录

HaaS100快速开始

代码详见:

  1. /*

  2. * Copyright (C) 2015-2021 Alibaba Group Holding Limited

  3. */

  4. #include

  5. #include

  6. #include

  7. #include

  8. #include "aos/init.h"

  9. #include "board.h"

  10. #include

  11. #include "aos/hal/uart.h"

  12. uart_dev_t uart_ttl = {0};

  13. void uart_test_init()

  14. {

  15.    uart_ttl.port = 2;  /*ttl use uart 2*/

  16.    uart_ttl.config.baud_rate = 115200;

  17.    uart_ttl.config.data_width = DATA_WIDTH_8BIT;

  18.    uart_ttl.config.flow_control = FLOW_CONTROL_DISABLED;

  19.    uart_ttl.config.mode = MODE_TX_RX;

  20.    uart_ttl.config.parity = NO_PARITY;

  21.    uart_ttl.config.stop_bits = STOP_BITS_1;

  22.    hal_uart_init(&uart_ttl);

  23. }

  24. void uart_test()

  25. {

  26. int ret       = 0;

  27. int recv_size = 0;

  28. int index     = 5;

  29. char buf[128] = {0};

  30.    buf[0] = 'r';

  31.    buf[1] = 'e';

  32.    buf[2] = 'c';

  33.    buf[3] = 'v';

  34.    buf[4] = '=';

  35. while (1) {

  36.        ret = -1;

  37.        recv_size = 0;

  38.        ret = hal_uart_recv_II(&uart_ttl, &buf[index], 1, &recv_size, 2000);

  39. if ((ret == 0) && (recv_size == 1)) {

  40. if (buf[index] == '\n') {

  41.                hal_uart_send(&uart_ttl, buf, index + 1, AOS_WAIT_FOREVER);

  42.                index = 5;

  43.                aos_msleep(10);

  44. continue;

  45.            }

  46.            index ++;

  47. if(index >= 128) {

  48.                hal_uart_send(&uart_ttl, buf, index, AOS_WAIT_FOREVER);

  49.                index = 5;

  50.            }

  51.        }

  52.        aos_msleep(10);

  53.    }

  54. }

  55. int application_start(int argc, char *argv[])

  56. {

  57. int count = 0;

  58. printf("uart test entry here!\r\n");

  59.    uart_test_init();

  60.    uart_test();

  61. }

第二步:

将编译出来的版本烧录到HaaS100中,按照硬件连接章节图示连接HaaS100和电脑。在电脑上,使用串口工具打开UART2对应的串口,输入字符串后,按回车。串口会打印出recv=加输入的字符串。如下图:

相关文章
|
C语言 C++
STM32F103C8 串口的使用
STM32F103C8 串口的使用
247 0
|
5月前
|
存储 传感器
【STM32基础 CubeMX】uart串口通信
【STM32基础 CubeMX】uart串口通信
302 0
|
XML 测试技术 网络安全
开发工具:USB转IIC/I2C/SPI/UART适配器模块可编程开发板
总的思路是通过USB或者UART接口发送一些协议字符串,由模块转换成上面几种接口的硬件时序电信号,实现与这几种接口芯片、设备的快速测试。 首先声明一下,大家都是搞硬件开发的,这几种接口当然是很简单的事,但有些时候对于一个新的设备或者芯片的测试,有个现成的工具当然更顺手,节省时间,也更可靠嘛。
|
Linux 定位技术 芯片
|
移动开发
STM32F1案例 串口USART使用
STM32F1案例 串口USART使用
181 0
|
存储 芯片 异构计算
|
C语言
51单片机串口使用
51单片机串口使用
220 0
|
存储 芯片
可编程 USB 转串口适配器开发板 UART 转 I2C
AT24Cxx 是可擦写 EEPROM 存储芯片,xx 表示容量,单位为 Kbits。USB2S 板上已有 1 片 AT24Cxx (默认为 AT24C02),设备地址为 0xA0。
可编程 USB 转串口适配器开发板 UART 转 I2C
可编程 USB 转串口适配器开发板 USB 转 UART和 I2C
USB2S 内置了 USB 转UART 芯片,可使用CH340/CH341 驱动程序。驱动安装步骤如下: 双击运行“CH341SER\SETUP.exe”,打开驱动安装窗口。
可编程 USB 转串口适配器开发板 USB 转 UART和 I2C
查看串口
查看串口
101 0
下一篇
无影云桌面