開發與維運

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

串口去初始化接口。

具體接口使用說明,可以參考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)軟件代碼

  • 串口初始化:

  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 <stdio.h>
  5. #include <stdlib.h>
  6. #include <aos/errno.h>
  7. #include <aos/kernel.h>
  8. #include "aos/init.h"
  9. #include "board.h"
  10. #include <k_api.h>
  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=加輸入的字符串。如下圖:

Leave a Reply

Your email address will not be published. Required fields are marked *