鸿蒙硬件HI3861开发环境搭建-串口2测试

鸿蒙硬件HI3861开发环境搭建-串口2测试

https://www.bilibili.com/read/cv8208920

鸿蒙其他教程请看https://blog.csdn.net/qq_33259323/category_10520249.html

1.接线

注意引脚复用,如果用串口2,2号板子是不能接上去的

使用USB转串口连接串口2

 2.开启串口2

打开Y:\harmony\code\code-1.0\vendor\hisi\hi3861\hi3861\build\config\usr_config.mk文件,在文件中加入

CONFIG_UART2_SUPPORT=y

3.编写测试代码

下面的代码都是官方测试代码

可以在Y:\harmony\code\code-1.0\vendor\hisi\hi3861\hi3861\app\demo\src\app_demo_uart.c里面找到

因为我这代码是用来搞M5311的,有需要请修改文件名和.h文件

m5311.c

#include "m5311.h"
#include "cmsis_os2.h"

hi_u32 g_uart_demo_task_id = 0;

static void *m5311Yask(const char *arg){

    hi_u8 uart_buff[UART_BUFF_SIZE] = {0};
    hi_u8 *uart_buff_ptr = uart_buff;
    hi_unref_param(arg);
    printf("Initialize uart demo successfully, please enter some datas via DEMO_UART_NUM port...\n");
    
    while(1){
        printf("Input Now\n");
        hi_s32 len = hi_uart_read(DEMO_UART_NUM, uart_buff_ptr, UART_BUFF_SIZE);
        if (len > 0) {
            printf("len > 0\n");
#ifdef WRITE_BY_INT
            hi_uart_write(DEMO_UART_NUM, uart_buff_ptr, len);
#else
            hi_uart_write_immediately(DEMO_UART_NUM, uart_buff_ptr, len);
#endif
        } else {
            printf("Read nothing!\n");
            hi_sleep(1000); /* sleep 1000ms */
        }

        //hi_sleep(1000);
    }

    hi_task_delete(g_uart_demo_task_id);
    g_uart_demo_task_id = 0;

    return NULL;
}

void M5311_Init(void){
    osThreadAttr_t attr;

    attr.name = "m5311Task";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 10240;
    attr.priority = 27;

    hi_u32 ret;
    hi_uart_attribute uart_attr = {
        .baud_rate = 115200, /* baud_rate: 115200 */
        .data_bits = 8,      /* data_bits: 8bits */
        .stop_bits = 1,
        .parity = 0,
    };

    /* Initialize uart driver */
    ret = hi_uart_init(DEMO_UART_NUM, &uart_attr, HI_NULL);
    if (ret != HI_ERR_SUCCESS) {
        printf("Failed to init uart! Err code = %d\n", ret);
        return;
    }

    if (osThreadNew((osThreadFunc_t)m5311Yask, NULL, &attr) == NULL) {
        printf("[M5311] Falied to create M5311Task!\n");
    }
}

m5311.h

#ifndef M5311_H
#define M5311_H

#include <stdio.h>
#include <hi_types_base.h>
#include <hi_early_debug.h>
#include <hi_task.h>
#include <hi_uart.h>

#define WRITE_BY_INT
#define UART_DEMO_TASK_STAK_SIZE 2048
#define UART_DEMO_TASK_PRIORITY  25
#define DEMO_UART_NUM            HI_UART_IDX_2
#define UART_BUFF_SIZE           32

void M5311_Init(void);

#endif

 BUILD.gn

# Copyright (c) 2020 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

static_library("m5311") {
    sources = [
        "m5311.c"
    ]
    include_dirs = [
        "//kernel/liteos_m/components/cmsis/2.0",
    ]
}

4.测试代码

打开Y:\harmony\code\code-1.0\applications\sample\wifi-iot\app\BUILD.gn文件

把刚才写的BUILD.gn文件里面的features加进去,比如"//XX/XX/XX/M5311:m5311"

然后在my_first_app文件夹下面的BUILD.gn文件include_dirs里面加入"//XX/XX/XX/M5311",

然后在hello_word.c文件里面加入头文件#include "m5311.h"

然后在HelloWorld函数里面加入M5311_Init();

5.验证测试

打开串口工具设置波特率

如果在过程中遇到什么问题可以来联系我B站

猜你喜欢

转载自blog.csdn.net/qq_33259323/article/details/109481764