鸿蒙HI3861模块中WiFi IoT智能家居套件 - UART2串口通信实现

系统
在HI3861模块中,UART2使用GPIO11和GPIO12管脚,串口通信一般是一个单片机最基本通信,我们一般调试的时候一个是点灯,另外一个就是打通串口。

[[353644]]

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com/#zz

在HI3861模块中,UART2使用GPIO11和GPIO12管脚,串口通信一般是一个单片机最基本通信,我们一般调试的时候一个是点灯,另外一个就是打通串口

虽然目前的开发板已经有串口输出了(UART0),但是这个串口是官方自带的,我们还是要熟悉一下串口的使用过程。

GPIO11->UART2_TX

GPIO12->UART2_RX

下面我们实现UART2的初始化,并将UART2接收到的数据从UART2发送出去。

硬件连接图

[[353645]]

串口相关的函数如下:

code\base\iot_hardware\frameworks\wifiiot_lite\src\wifiiot_uart.c

  1. //初始化函数 
  2. unsigned int UartInit(WifiIotUartIdx id, const WifiIotUartAttribute *param, const WifiIotUartExtraAttr *extraAttr) 
  3.  
  4. //读数据函数 
  5. int UartRead(WifiIotUartIdx id, unsigned char *data, unsigned int dataLen) 
  6.  
  7. //写数据函数 
  8. int UartWrite(WifiIotUartIdx id, const unsigned char *data, unsigned int dataLen) 

开发过程:

1. 使能UART2

在 vendor\hisi\hi3861\hi3861\build\config\usr_config.mk 修改 CONFIG_UART2_SUPPORT=y

2. 初始化GPIO为UART2(注意,如果已经执行步骤1,则不需要本过程)

如果已经配置 CONFIG_UART2_SUPPORT=y,则以下文件中的GPIO11,12会被自动初始化为UART2

vendor\hisi\hi3861\hi3861\app\wifiiot_app\init\app_io_init.c

  1. #ifdef CONFIG_UART2_SUPPORT 
  2.     /* uart2 sigma认证使用串口 */ 
  3.     hi_io_set_func(HI_IO_NAME_GPIO_11, HI_IO_FUNC_GPIO_11_UART2_TXD); /* uart2 tx */ 
  4.     hi_io_set_func(HI_IO_NAME_GPIO_12, HI_IO_FUNC_GPIO_12_UART2_RXD); /* uart2 rx */ 
  5. #endif 

3. 初始化UART2配置

  1. int usr_uart2_config(void) 
  2.     int ret; 
  3.  
  4.     //初始化UART2配置,115200,数据bit为8,停止位1,奇偶校验为NONE,流控为NONE 
  5.     WifiIotUartAttribute g_uart2_cfg = {115200, 8, 1, WIFI_IOT_UART_PARITY_NONE, 0}; 
  6.     ret = UartInit(WIFI_IOT_UART_IDX_2, &g_uart2_cfg,NULL); 
  7.  
  8.     if (ret != 0)  
  9.     { 
  10.         printf("uart2 init fail\r\n"); 
  11.     } 
  12.     return ret; 

 4. 调用UART读写函数

  1. unsigned char buff[50] = {0}; 
  2.    unsigned int len = 0; 
  3.    unsigned int ui = 0; 
  4.    len = UartRead(WIFI_IOT_UART_IDX_2, buff, 50);//接收串口2数据 
  5.    if(len > 0) 
  6.    { 
  7.        printf("UART2 recv len=%d\r\n", len); 
  8.        for(ui = 0 ; ui < len; ui++) 
  9.        { 
  10.            printf("0x%x\r\n", buff[ui]); 
  11.        } 
  12.        UartWrite(WIFI_IOT_UART_IDX_2, buff, len);//数据发送给串口2 
  13.    } 

 5. 完整的代码 uart2_demo.c

路径:code\applications\sample\wifi-iot\app\hello_world\uart2_demo.c

  1. #include <stdio.h> 
  2. #include <unistd.h> 
  3. #include "ohos_init.h" 
  4. #include "cmsis_os2.h" 
  5. #include "wifiiot_gpio.h" 
  6. #include "wifiiot_gpio_ex.h" 
  7. #include "wifiiot_uart.h" 
  8. #include "wifiiot_uart_ex.h" 
  9.  
  10. /* 
  11. 2020年11月19日  
  12. 作者:hcl0317 
  13. 链接: 
  14. 实现功能: 
  15. GPIO11->UART2_TX  
  16. GPIO12->UART2_RX  
  17.  
  18. 1. 在 vendor\hisi\hi3861\hi3861\build\config\usr_config.mk 修改 CONFIG_UART2_SUPPORT=y 
  19. 2. 打开上面的宏定义后,在 vendor\hisi\hi3861\hi3861\app\wifiiot_app\init\app_io_init.c中会自动对 GPIO11和GPIO12进行初始化 
  20.  
  21. //#ifdef CONFIG_UART2_SUPPORT 
  22. //    //uart2 sigma认证使用串口 
  23. //    hi_io_set_func(HI_IO_NAME_GPIO_11, HI_IO_FUNC_GPIO_11_UART2_TXD); // uart2 tx  
  24. //    hi_io_set_func(HI_IO_NAME_GPIO_12, HI_IO_FUNC_GPIO_12_UART2_RXD); // uart2 rx  
  25. //#endif 
  26.  
  27. //3.初始化UART2配置,115200,数据bit为8,停止位1,奇偶校验为NONE,流控为NONE 
  28.  
  29. //4.在任务中调用串口读写函数,收到什么数据,发送什么数据 
  30.  
  31. */ 
  32.  
  33. int usr_uart2_config(void) 
  34.     int ret; 
  35.  
  36.     //初始化UART2配置,115200,数据bit为8,停止位1,奇偶校验为NONE,流控为NONE 
  37.     WifiIotUartAttribute g_uart2_cfg = {115200, 8, 1, WIFI_IOT_UART_PARITY_NONE, 0}; 
  38.     ret = UartInit(WIFI_IOT_UART_IDX_2, &g_uart2_cfg,NULL); 
  39.  
  40.     if (ret != 0)  
  41.     { 
  42.         printf("uart2 init fail\r\n"); 
  43.     } 
  44.     return ret; 
  45.  
  46.  
  47. //1.任务处理函数 
  48. static void* Uart2Demo_Task(const char* arg) 
  49.     unsigned char buff[50] = {0}; 
  50.     unsigned int len = 0; 
  51.  
  52.     (void)arg; 
  53.     printf("[Uart2Demo] Uart2Demo_Task()\n"); 
  54.  
  55.     GpioInit();//使用GPIO,都需要调用该接口 
  56.  
  57.     printf("UART2 init...\r\n"); 
  58.     usr_uart2_config(); 
  59.  
  60.  
  61.     while(1) 
  62.     { 
  63.         //logic code for task 
  64.         unsigned int ui = 0; 
  65.         len = UartRead(WIFI_IOT_UART_IDX_2, buff, 50);//接收串口2数据 
  66.         if(len > 0) 
  67.         { 
  68.             printf("UART2 recv len=%d\r\n", len); 
  69.             for(ui = 0 ; ui < len; ui++) 
  70.             { 
  71.                 printf("0x%x\r\n", buff[ui]); 
  72.             } 
  73.             UartWrite(WIFI_IOT_UART_IDX_2, buff, len);//数据发送给串口2 
  74.         } 
  75.  
  76.         //usleep(500000); 
  77.         usleep(100000); 
  78.     } 
  79.   
  80.     return NULL
  81.  
  82. //2.任务入口函数 
  83. static void Uart2Demo_Entry(void) 
  84.     osThreadAttr_t attr = {0}; 
  85.  
  86.     printf("[Uart2Demo] Uart2Demo_Entry()\n"); 
  87.  
  88.     attr.name = "Uart2Demo_Task"
  89.     attr.attr_bits = 0U; 
  90.     attr.cb_mem = NULL
  91.     attr.cb_size = 0U; 
  92.     attr.stack_mem = NULL
  93.     attr.stack_size = 1024;//堆栈大小 
  94.     attr.priority = osPriorityNormal;//优先级 
  95.  
  96.     if (osThreadNew((osThreadFunc_t)Uart2Demo_Task, NULL, &attr) == NULL
  97.     { 
  98.         printf("[Uart2Demo] Falied to create LedTask!\n"); 
  99.     } 
  100.  
  101. //3.注册模块 
  102. SYS_RUN(Uart2Demo_Entry); 

6. 业务代码的编译脚本 BUILD.gn

路径:code\applications\sample\wifi-iot\app\uart2_demo\BUILD.gn

  1. static_library("uart2_demo_app") { 
  2.     sources = [ 
  3.         "uart2_demo.c" 
  4.     ] 
  5.  
  6.     include_dirs = [ 
  7.         "//utils/native/lite/include"
  8.         "//kernel/liteos_m/components/cmsis/2.0"
  9.         "//base/iot_hardware/interfaces/kits/wifiiot_lite"
  10.     ] 

 7. 模块的编译脚本BUILD.gn

路径:code\applications\sample\wifi-iot\app\BUILD.gn

  1. import("//build/lite/config/component/lite_component.gni"
  2.  
  3. lite_component("app") { 
  4.     features = [ 
  5.         "startup"
  6.         #"hello_world:hello_world_app"
  7.         "uart2_demo:uart2_demo_app" 
  8.     ] 

 执行结果,左侧是开发板原来的串口0,右侧是新增的串口2

 

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区 

https://harmonyos.51cto.com/#zz

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2020-11-13 11:53:52

4.WiFi IoT

2020-11-10 12:08:25

开发框架

2021-04-29 09:52:25

鸿蒙HarmonyOS应用

2020-10-16 09:50:37

Hi3861WiFi热点

2023-04-07 09:14:31

硬件通信串口通信实验

2021-02-02 15:52:17

鸿蒙HarmonyOS应用开发

2020-10-28 10:00:09

海思Hi3861CentOS鸿蒙LiteOS

2020-12-21 09:57:52

OLED温湿度计hi3861

2020-11-03 11:39:22

wifi小车

2015-08-26 10:14:28

2020-11-24 10:05:54

人工智能

2020-10-30 09:41:44

鸿蒙Hi3861WiFi小车

2021-01-15 15:36:32

人工智能智能家居机器人

2021-03-09 09:56:42

物联网安全物联网IOT

2022-05-30 15:21:27

Hi3861TCP通信

2022-02-24 16:39:41

OpenHarmonNiobe开发鸿蒙

2020-12-30 11:02:34

鸿蒙HarmonyOS智能家居

2020-11-02 12:07:11

鸿蒙 GPIO

2020-11-06 10:15:16

HiBurn

2022-03-15 15:00:59

Hi3861Pin接口鸿蒙
点赞
收藏

51CTO技术栈公众号