鴻蒙HI3861模塊中WiFi IoT智能家居套件 - UART2串口通信實現(xiàn)
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)
https://harmonyos.51cto.com/#zz
在HI3861模塊中,UART2使用GPIO11和GPIO12管腳,串口通信一般是一個單片機最基本通信,我們一般調(diào)試的時候一個是點燈,另外一個就是打通串口
雖然目前的開發(fā)板已經(jīng)有串口輸出了(UART0),但是這個串口是官方自帶的,我們還是要熟悉一下串口的使用過程。
GPIO11->UART2_TX
GPIO12->UART2_RX
下面我們實現(xiàn)UART2的初始化,并將UART2接收到的數(shù)據(jù)從UART2發(fā)送出去。
硬件連接圖
串口相關(guān)的函數(shù)如下:
code\base\iot_hardware\frameworks\wifiiot_lite\src\wifiiot_uart.c
- //初始化函數(shù)
- unsigned int UartInit(WifiIotUartIdx id, const WifiIotUartAttribute *param, const WifiIotUartExtraAttr *extraAttr)
- //讀數(shù)據(jù)函數(shù)
- int UartRead(WifiIotUartIdx id, unsigned char *data, unsigned int dataLen)
- //寫數(shù)據(jù)函數(shù)
- int UartWrite(WifiIotUartIdx id, const unsigned char *data, unsigned int dataLen)
開發(fā)過程:
1. 使能UART2
在 vendor\hisi\hi3861\hi3861\build\config\usr_config.mk 修改 CONFIG_UART2_SUPPORT=y
2. 初始化GPIO為UART2(注意,如果已經(jīng)執(zhí)行步驟1,則不需要本過程)
如果已經(jīng)配置 CONFIG_UART2_SUPPORT=y,則以下文件中的GPIO11,12會被自動初始化為UART2
vendor\hisi\hi3861\hi3861\app\wifiiot_app\init\app_io_init.c
- #ifdef CONFIG_UART2_SUPPORT
- /* uart2 sigma認證使用串口 */
- hi_io_set_func(HI_IO_NAME_GPIO_11, HI_IO_FUNC_GPIO_11_UART2_TXD); /* uart2 tx */
- hi_io_set_func(HI_IO_NAME_GPIO_12, HI_IO_FUNC_GPIO_12_UART2_RXD); /* uart2 rx */
- #endif
3. 初始化UART2配置
- int usr_uart2_config(void)
- {
- int ret;
- //初始化UART2配置,115200,數(shù)據(jù)bit為8,停止位1,奇偶校驗為NONE,流控為NONE
- WifiIotUartAttribute g_uart2_cfg = {115200, 8, 1, WIFI_IOT_UART_PARITY_NONE, 0};
- ret = UartInit(WIFI_IOT_UART_IDX_2, &g_uart2_cfg,NULL);
- if (ret != 0)
- {
- printf("uart2 init fail\r\n");
- }
- return ret;
- }
4. 調(diào)用UART讀寫函數(shù)
- unsigned char buff[50] = {0};
- unsigned int len = 0;
- unsigned int ui = 0;
- len = UartRead(WIFI_IOT_UART_IDX_2, buff, 50);//接收串口2數(shù)據(jù)
- if(len > 0)
- {
- printf("UART2 recv len=%d\r\n", len);
- for(ui = 0 ; ui < len; ui++)
- {
- printf("0x%x\r\n", buff[ui]);
- }
- UartWrite(WIFI_IOT_UART_IDX_2, buff, len);//數(shù)據(jù)發(fā)送給串口2
- }
5. 完整的代碼 uart2_demo.c
路徑:code\applications\sample\wifi-iot\app\hello_world\uart2_demo.c
- #include <stdio.h>
- #include <unistd.h>
- #include "ohos_init.h"
- #include "cmsis_os2.h"
- #include "wifiiot_gpio.h"
- #include "wifiiot_gpio_ex.h"
- #include "wifiiot_uart.h"
- #include "wifiiot_uart_ex.h"
- /*
- 2020年11月19日
- 作者:hcl0317
- 鏈接:
- 實現(xiàn)功能:
- GPIO11->UART2_TX
- GPIO12->UART2_RX
- 1. 在 vendor\hisi\hi3861\hi3861\build\config\usr_config.mk 修改 CONFIG_UART2_SUPPORT=y
- 2. 打開上面的宏定義后,在 vendor\hisi\hi3861\hi3861\app\wifiiot_app\init\app_io_init.c中會自動對 GPIO11和GPIO12進行初始化
- //#ifdef CONFIG_UART2_SUPPORT
- // //uart2 sigma認證使用串口
- // hi_io_set_func(HI_IO_NAME_GPIO_11, HI_IO_FUNC_GPIO_11_UART2_TXD); // uart2 tx
- // hi_io_set_func(HI_IO_NAME_GPIO_12, HI_IO_FUNC_GPIO_12_UART2_RXD); // uart2 rx
- //#endif
- //3.初始化UART2配置,115200,數(shù)據(jù)bit為8,停止位1,奇偶校驗為NONE,流控為NONE
- //4.在任務(wù)中調(diào)用串口讀寫函數(shù),收到什么數(shù)據(jù),發(fā)送什么數(shù)據(jù)
- */
- int usr_uart2_config(void)
- {
- int ret;
- //初始化UART2配置,115200,數(shù)據(jù)bit為8,停止位1,奇偶校驗為NONE,流控為NONE
- WifiIotUartAttribute g_uart2_cfg = {115200, 8, 1, WIFI_IOT_UART_PARITY_NONE, 0};
- ret = UartInit(WIFI_IOT_UART_IDX_2, &g_uart2_cfg,NULL);
- if (ret != 0)
- {
- printf("uart2 init fail\r\n");
- }
- return ret;
- }
- //1.任務(wù)處理函數(shù)
- static void* Uart2Demo_Task(const char* arg)
- {
- unsigned char buff[50] = {0};
- unsigned int len = 0;
- (void)arg;
- printf("[Uart2Demo] Uart2Demo_Task()\n");
- GpioInit();//使用GPIO,都需要調(diào)用該接口
- printf("UART2 init...\r\n");
- usr_uart2_config();
- while(1)
- {
- //logic code for task
- unsigned int ui = 0;
- len = UartRead(WIFI_IOT_UART_IDX_2, buff, 50);//接收串口2數(shù)據(jù)
- if(len > 0)
- {
- printf("UART2 recv len=%d\r\n", len);
- for(ui = 0 ; ui < len; ui++)
- {
- printf("0x%x\r\n", buff[ui]);
- }
- UartWrite(WIFI_IOT_UART_IDX_2, buff, len);//數(shù)據(jù)發(fā)送給串口2
- }
- //usleep(500000);
- usleep(100000);
- }
- return NULL;
- }
- //2.任務(wù)入口函數(shù)
- static void Uart2Demo_Entry(void)
- {
- osThreadAttr_t attr = {0};
- printf("[Uart2Demo] Uart2Demo_Entry()\n");
- attr.name = "Uart2Demo_Task";
- attr.attr_bits = 0U;
- attr.cb_mem = NULL;
- attr.cb_size = 0U;
- attr.stack_mem = NULL;
- attr.stack_size = 1024;//堆棧大小
- attr.priority = osPriorityNormal;//優(yōu)先級
- if (osThreadNew((osThreadFunc_t)Uart2Demo_Task, NULL, &attr) == NULL)
- {
- printf("[Uart2Demo] Falied to create LedTask!\n");
- }
- }
- //3.注冊模塊
- SYS_RUN(Uart2Demo_Entry);
6. 業(yè)務(wù)代碼的編譯腳本 BUILD.gn
路徑:code\applications\sample\wifi-iot\app\uart2_demo\BUILD.gn
- static_library("uart2_demo_app") {
- sources = [
- "uart2_demo.c"
- ]
- include_dirs = [
- "//utils/native/lite/include",
- "//kernel/liteos_m/components/cmsis/2.0",
- "//base/iot_hardware/interfaces/kits/wifiiot_lite",
- ]
- }
7. 模塊的編譯腳本BUILD.gn
路徑:code\applications\sample\wifi-iot\app\BUILD.gn
- import("//build/lite/config/component/lite_component.gni")
- lite_component("app") {
- features = [
- "startup",
- #"hello_world:hello_world_app",
- "uart2_demo:uart2_demo_app"
- ]
- }
執(zhí)行結(jié)果,左側(cè)是開發(fā)板原來的串口0,右側(cè)是新增的串口2

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)
https://harmonyos.51cto.com/#zz