关于openharmony:Hi3861-通过UART串口协议与其它开发板进行通信

30次阅读

共计 4071 个字符,预计需要花费 11 分钟才能阅读完成。

一、搭建编译环境

1、下载虚拟机 VMware 和 Ubuntu20.0.14
下载 VMware Workstation Pro | CN
https://www.vmware.com/cn/pro…
Ubuntu 零碎下载 | Ubuntu
https://cn.ubuntu.com/download
2、装置 vm 实现后关上 vm,点击创立新的虚拟机

3、抉择典型点击下一步,抉择下载的 Ubuntu,点击下一步,创立实现后虚拟机主动装置 Ubuntu

4、装置实现后获取 OpenHarmony 源码可参考https://docs.openharmony.cn/p…
5、装置编译工具
(1)装置 Node.js 关上 Ubuntu 终端输出命令装置:

sudo apt-get install nodejs
sudo apt-get install npm
node --version   // 查看 nodejs 版本
npm --version    // 查看 npm 版本

(2)装置 Python 编译环境

sudo apt-get install python3.8
sudo apt-get install python3-pip
sudo pip3 install setuptools
sudo pip3 install kconfiglib 
sudo pip3 install pycryptodome
sudo pip3 install six --upgrade --ignore-installed six
sudo pip3 install ecdsa

(3)装置 SCons

python3 -m pip install scons
scons -v   // 查看版本

如图:

(4)装置 hb 工具代码测试

python3 -m pip install --user ohos-build
vim ~/.bashrc                      // 设置环境变量
export PATH=~/.local/bin:$PATH        // 将以下命令拷贝到.bashrc 文件的最初一行,保留并退出
source ~/.bashrc            // 更新环境变量

执行 ”hb -h”,有打印以下信息即示意装置胜利。

(5)装置 gcc_riscv32
下载 gcc_riscv32 镜像
https://gitee.com/link?target…
设置环境变量
将压缩包解压到根目录

tar -xvf gcc_riscv32-linux-7.3.0.tar.gz -C ~    // 文件名须要与下载的文件相匹配
设置环境变量。vim ~/.bashrc                      // 设置环境变量
export PATH=~/gcc_riscv32/bin:$PATH    // 将以下命令拷贝到.bashrc 文件的最初一行,保留并退出
source ~/.bashrc            // 更新环境变量
riscv32-unknown-elf-gcc -v        // 显示版本号,则装置胜利

6、批改 usr_config.mk 文件
文件在 OpenHarmony 源码目录下
device/hisilicon/hispark_pegasus/sdk_liteos/build/config/usr_config.mk

CONFIG_I2C_SUPPORT=y
CONFIG_PWM_SUPPORT=y

7、批改 wifiservice 文件夹
文件在 OpenHarmony 源码目录下
device/hisilicon/hispark_pegasus/hi3861_adapter/hals/communication/wifi_lite/wifiservice/source/wifi_hotspot.c

EnableHotspot 函数中屏蔽如下字段
     //if (SetHotspotIpConfig() != WIFI_SUCCESS) {
     //    return ERROR_WIFI_UNKNOWN;
     //}
地址:device/hisilicon/hispark_pegasus/hi3861_adapter/hals/communication/wifi_lite/wifiservice/source/wifi_device.c
DispatchConnectEvent 函数下 屏蔽 StaSetWifiNetConfig 相干代码行
      //StaSetWifiNetConfig(HI_WIFI_EVT_CONNECTED);
      //StaSetWifiNetConfig(HI_WIFI_EVT_DISCONNECTED);

二、创立我的项目文件夹

1、在 OpenHarmony1.01 版本创立一个我的项目 demo
在源码目录下的 vendor/team_x 创立 smart_demo
在 scr 外面增加咱们写的代码:

2、初始化 uart 串口
首先咱们须要创立一个初始化 uart 串口的程序
将 GPIO0 初始化为 tx 端,将 GPIO1 初始化为 rx 端

void UartInit(void){RaiseLog(LOG_LEVEL_INFO,"[2022012x01] entry into UartInit");
    IoTGpioInit(HAL_WIFI_IOT_IO_NAME_GPIO_0);
    HalIoSetFunc(HAL_WIFI_IOT_IO_NAME_GPIO_0, WIFI_IOT_IO_FUNC_GPIO_0_UART1_TXD);
    IoTGpioInit(HAL_WIFI_IOT_IO_NAME_GPIO_1);
    HalIoSetFunc(HAL_WIFI_IOT_IO_NAME_GPIO_1, WIFI_IOT_IO_FUNC_GPIO_1_UART1_RXD); 
    hi_uart_attribute uart_attr = {
        .baud_rate = UART_BAUD_RATE,     /* baud_rate: 9600 */
        .data_bits = UART_DATA_BITS,      /* data_bits: 8bits */
        .stop_bits = UART_STOP_BITS,
        .parity = 0,
    }
    RaiseLog(LOG_LEVEL_INFO,"[2022012x01] uart_init success");
    /* Initialize uart driver */
    hi_u32 ret = hi_uart_init(HI_UART_IDX_1, &uart_attr, HI_NULL);
    if (ret != HI_ERR_SUCCESS)
    {printf("[Dustbin_tes3]Failed to init uart! Err code = %d\n", ret);
        return;
    }
}

3、创立线程工作

static void *uart_demo_task(void)
{
    static uint16_t countSendTimes = 0;
    static uint8_t countReceiveTimes = 0;
    uartController.isReadBusy = false;
    printf("[Initialize uart successfully\n");
    UartInit();
    while (1)
    {osDelay(50); 
        UartReceiveMessage();//Collecting Serial Port Data
        hi_sleep(SMART_BIN_SLEEP_2500MS);  
    }
    return 0;
}
static void IotMainEntry(void)
{
    osThreadAttr_t attr;
    RaiseLog(LOG_LEVEL_INFO, "DATA:%s Time:%s \r\n", __FUNCTION__, __DATE__, __TIME__);

    // Create the IoT Main task
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL
    attr.stack_size = CONFIG_TASK_MAIN_STACKSIZE;
    attr.priority = CONFIG_TASK_MAIN_PRIOR;
    attr.name = "IoTMain";
    (void) osThreadNew((osThreadFunc_t)uart_demo_task, NULL, (const osThreadAttr_t *)&attr);
    return;
}
APP_FEATURE_INIT(IotMainEntry);

4、接管串口数据

static void UartReceiveMessage(void)
{
    char *recData;
    printf("----Listening----\n");
    RaiseLog(LOG_LEVEL_INFO,"Start Listening serial port");
    if (UartIsBufEmpty())
        {return;}
        if (uartController.isReadBusy)
        {return;}
        uartController.isReadBusy = true;
        g_ReceivedDatalen = hi_uart_read(UART_NUM, g_uart_buff, UART_BUFF_SIZE);
        if (g_ReceivedDatalen > 0)
        {printf("handleUartReceiveMessage rcvData len:%d,msg:%s.\n", g_ReceivedDatalen, g_uart_buff);     
            setVoiceCommand();//Setting voice Commands
            memset(g_uart_buff, 0, sizeof(g_uart_buff));
            g_ReceivedDatalen = 0;
        }
        uartController.isReadBusy = false;
}

当 hi3861 开发板接管到其余开发板传输的数据后,能够通过串口打印进去,对此能够写一个解析命令,对发送的字符串,执行相应的命令

注:在接线时,要将 tx 与另一个开发板的 rx 连贯,因为在 UART 串口协定中,通过 TX(发送引脚)和 RX(接管引脚)进行数据传输与接管,须要将 TX 发送引脚与另一开发板的 RX 接管引脚相连接,故须要穿插连贯以保障两块开发板的失常通信。

正文完
 0