鸿蒙硬件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:harmonycodecode-1.0vendorhisihi3861hi3861buildconfigusr_config.mk文件,在文件中加入
CONFIG_UART2_SUPPORT=y
3.编写测试代码下面的代码都是官方测试代码
可以在Y:harmonycodecode-1.0vendorhisihi3861hi3861appdemosrcapp_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 Nown");
- hi_s32 len = hi_uart_read(DEMO_UART_NUM, uart_buff_ptr, UART_BUFF_SIZE);
- if (len > 0) {
- printf("len > 0n");
- #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 = %dn", 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
- static_library("m5311") {
- sources = [
- "m5311.c"
- ]
- include_dirs = [
- "//kernel/liteos_m/components/cmsis/2.0",
- ]
- }
复制代码 4.测试代码打开Y:harmonycodecode-1.0applicationssamplewifi-iotappBUILD.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站