【开发板试用】5. WiFi IoT智能家居套件 - UART2串口通信实现 精华

hcl0317
发布于 2020-11-19 14:58
浏览
4收藏

 

 

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

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

GPIO11->UART2_TX 
GPIO12->UART2_RX 

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

硬件连接图

 

【开发板试用】5. WiFi IoT智能家居套件 - UART2串口通信实现-鸿蒙开发者社区

串口相关的函数如下:

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

//初始化函数
unsigned int UartInit(WifiIotUartIdx id, const WifiIotUartAttribute *param, const WifiIotUartExtraAttr *extraAttr)

//读数据函数
int UartRead(WifiIotUartIdx id, unsigned char *data, unsigned int dataLen)

//写数据函数
int UartWrite(WifiIotUartIdx id, const unsigned char *data, unsigned int dataLen)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

开发过程:

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

#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
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

3. 初始化UART2配置

int usr_uart2_config(void)
{
    int ret;

    //初始化UART2配置,115200,数据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.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

4. 调用UART读写函数

        unsigned char buff[50] = {0};
        unsigned int len = 0;
        unsigned int ui = 0;
        len = UartRead(WIFI_IOT_UART_IDX_2, buff, 50);//接收串口2数据
        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);//数据发送给串口2
        }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

 

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
链接:
实现功能:
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,数据bit为8,停止位1,奇偶校验为NONE,流控为NONE

//4.在任务中调用串口读写函数,收到什么数据,发送什么数据

*/

int usr_uart2_config(void)
{
    int ret;

    //初始化UART2配置,115200,数据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.任务处理函数
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,都需要调用该接口

    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数据
        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);//数据发送给串口2
        }

        //usleep(500000);
        usleep(100000);
    }
 
    return NULL;
}

//2.任务入口函数
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;//优先级

    if (osThreadNew((osThreadFunc_t)Uart2Demo_Task, NULL, &attr) == NULL)
    {
        printf("[Uart2Demo] Falied to create LedTask!\n");
    }
}

//3.注册模块
SYS_RUN(Uart2Demo_Entry);
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.

6.业务代码的编译脚本 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",
    ]
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

 

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"
    ]
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

 

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

【开发板试用】5. WiFi IoT智能家居套件 - UART2串口通信实现-鸿蒙开发者社区

 

 

 

uart2_demo.zip 2.25K 145次下载
已于2020-11-30 14:18:48修改
1
收藏 4
回复
举报
1
3
4
3条回复
按时间正序
/
按时间倒序
鲜橙加冰
鲜橙加冰

这位兄弟,你已经获得专栏使用资格了。

地址:https://harmonyos.51cto.com/column

已于2020-11-30 14:20:22修改
回复
2020-11-30 14:19:35
hcl0317
hcl0317

感谢,最近有点忙,没更新了,后面争取持续更新

回复
2020-12-5 22:21:35
黑大蒜啊
黑大蒜啊

请问它这个板子有两组gpio11跟12 为什么两个传感器同时接,总有一个uartinit -1?求解

回复
2023-6-15 20:29:50


回复
    相关推荐