HarmonyOS小熊派 | HarmonyOS基础外设开发--UART数据读写
BearPi-HM_Nano开发板基础外设开发——UART数据读写
本示例将演示如何在BearPi-HM_Nano开发板上使用UART进行数据的收发
UART API分析
本示例主要使用了以下API完成UART数据读写
1、IoTUartInit()
/**
* @brief Configures a UART device with the port number specified by <b>id</b>
* based on the basic and extended attributes.
*
*
*
* @param id Indicates the port number of the UART device.
* @param param Indicates the pointer to the UART attributes.
* @return Returns {@link IOT_SUCCESS} if the UART device is configured successfully;
* returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
* @since 2.2
* @version 2.2
*/
unsigned int IoTUartInit(unsigned int id, const IotUartAttribute *param);
描述:
配置一个UART设备。 参数:
名字 | 描述 |
---|---|
id | UART端口号. |
param | 表示基本UART属性 |
2、IoTUartWrite()
/**
* @brief Writes a specified length of data to a UART device with the port number specified by <b>id</b>.
*
*
*
* @param id Indicates the port number of the UART device.
* @param data Indicates the pointer to the start address of the data to write.
* @param dataLen Indicates the number of bytes to write.
* @return Returns the number of bytes written if the operation is successful; returns <b>-1</b> otherwise.
* @since 2.2
* @version 2.2
*/
int IoTUartWrite(unsigned int id, const unsigned char *data, unsigned int dataLen);
描述: 将数据写入UART设备
参数:
名字 | 描述 |
---|---|
id | UART端口号. |
data | 表示指向要写入数据的起始地址的指针 |
dataLen | 表示读取数据的长度 |
3、IoTUartRead()
/**
* @brief Reads a specified length of data from a UART device with the port number specified by <b>id</b>.
*
*
*
* @param id Indicates the port number of the UART device.
* @param data Indicates the pointer to the start address of the data to read.
* @param dataLen Indicates the number of bytes to read.
* @return Returns the number of bytes read if the operation is successful; returns <b>-1</b> otherwise.
* @since 2.2
* @version 2.2
*/
int IoTUartRead(unsigned int id, unsigned char *data, unsigned int dataLen);
描述:
从UART设备读取数据。
参数:
名字 | 描述 |
---|---|
id | UART端口号. |
data | 表示指向要读取数据的起始地址的指针 |
dataLen | 表示读取数据的长度 |
硬件设计
本案例将用 BearPi-HM_Nano 开发板 E53 接口的 UART 作为测试,如原理图所示第 18 和 19 脚分别为 TXD 和 RXD ,连接了主控芯片的 GPIO_6 和 GPIO_5 ,所以在编写软件的时候需要将 GPIO_6 和 GPIO_5 分别复用为 TXD 和 RXD 。
软件设计
主要代码分析
这部分代码为UART初始化的代码,首先要在 uart_attr
结构体这配置波特率、数据位、停止位、奇偶检验位,然后通过 IoTUartInit()
函数对串口1进行配置。
这部分的代码主要实现通过 IoTUartWrite()
函数在串口1发送一串数据,然后通过 IoTUartRead()
函数将数据都回来,并通过 debug
串口打印出来。
yuchuan_basic_uart.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_uart.h"
#include "iot_errno.h"
#define THREAD_CB_MEM_SIZE 0U
#define THREAD_STACK_MEM_SIZE 1024 * 4
#define THREAD_PRIORITY 25
#define IOT_UART_IDX_1 1
#define UART_BUFF_SIZE 1000
static const char *data = "Yuchuan Huaying HarmonyOS Hello!!!\n";
static void yuchuanUartTask(void *argument)
{
(void)argument;
uint32_t ret;
uint8_t uart_buff[UART_BUFF_SIZE] = {0};
uint8_t *uart_buff_p = uart_buff;
int i = 0;
IotUartAttribute uart_attr = {
.baudRate = 9600,
.dataBits = 8,
.stopBits =1,
.parity = 0,
};
//初始化UART driver
ret = IoTUartInit(IOT_UART_IDX_1,&uart_attr);
if (ret != IOT_SUCCESS)
{
/* code */
printf("Init Uart Falied Error No : %d\n",ret);
return;
}
printf("Uart Test Start!!!\n");
while (1)
{
i++;
if (i > 5)
{
/* code */
return;
}
/* code */
printf("====================================================\r\n");
printf("*************Yuchuan UART_Example Task**************\r\n");
printf("====================================================\r\n");
//通过串口1发送数据
IoTUartWrite(IOT_UART_IDX_1,(unsigned char*)data,strlen(data));
//通过串口1接收数据
IoTUartRead(IOT_UART_IDX_1,uart_buff_p,UART_BUFF_SIZE);
printf("Yuchuan Read Uart Data : %s %d\n",uart_buff_p,i);
usleep(3000000);
}
i = 0;
}
static void yuchuanBasicUARTEntry(void)
{
osThreadAttr_t threadAttr;
threadAttr.attr_bits = 0U;
threadAttr.cb_mem = NULL;
threadAttr.cb_size = THREAD_CB_MEM_SIZE;
threadAttr.stack_mem = NULL;
threadAttr.stack_size = THREAD_STACK_MEM_SIZE;
threadAttr.priority = THREAD_PRIORITY;
threadAttr.name = "BasicUARTThread";
if (osThreadNew((osThreadFunc_t)yuchuanUartTask, NULL, &threadAttr) == NULL)
{
/* code */
printf("Falied to Create Basic UART Task!!!\n");
}
}
APP_FEATURE_INIT(yuchuanBasicUARTEntry);
编译调试
//applications/sample/BearPi/BearPi-HM_Nano/sample/B6_YuchuanBasicUART
static_library("YuchuanBasicUART"){
sources = [
"yuchuan_basic_uart.c",
]
include_dirs = [
"//utils/native/lite/include",
"//kernel/liteos_m/kal/cmsis",
"//base/iot_hardware/peripheral/interfaces/kits",
]
}
修改 BUILD.gn 文件
修改 applications\BearPi\BearPi-HM_Nano\sample
路径下 BUILD.gn 文件,指定 YuchuanBasicUART 参与编译。
# Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology 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.
import("//build/lite/config/component/lite_component.gni")
lite_component("sample") {
features = [
#"A1_kernal_thread:thread_example",
#"A2_kernel_timer:timer_example",
#"A3_kernel_event:event_example",
#"A4_kernel_mutex:mutex_example",
#"A5_kernel_semaphore:semaphore_example",
#"A6_kernel_message:message_example",
#"B1_basic_led_blink:led_example",
#"B2_basic_button:button_example",
#"B3_basic_pwm_led:pwm_example",
#"B4_basic_adc:adc_example",
#"B5_basic_i2c_nfc:i2c_example",
#"B6_basic_uart:uart_example",
#"C1_e53_sf1_mq2:e53_sf1_example",
#"C2_e53_ia1_temp_humi_pls:e53_ia1_example",
#"C3_e53_sc1_pls:e53_sc1_example",
#"C4_e53_sc2_axis:e53_sc2_example",
#"C5_e53_is1_infrared:e53_is1_example",
#"D1_iot_wifi_ap:wifi_ap",
#"D2_iot_wifi_sta_connect:wifi_sta_connect",
#"D3_iot_udp_client:udp_client",
#"D4_iot_tcp_server:tcp_server",
#"D5_iot_mqtt:iot_mqtt",
#"D6_iot_cloud_oc:oc_mqtt",
#"D7_iot_cloud_onenet:onenet_mqtt",
#"D8_iot_cloud_oc_smoke:cloud_oc_smoke",
#"D9_iot_cloud_oc_light:cloud_oc_light",
#"D10_iot_cloud_oc_manhole_cover:cloud_oc_manhole_cover",
#"D11_iot_cloud_oc_infrared:cloud_oc_infrared",
#"D12_iot_cloud_oc_agriculture:cloud_oc_agriculture",
#"D13_iot_cloud_oc_gps:cloud_oc_gps",
#"A1_YuchuanThread:yuchuanThread",
#"A2_YuchuanTimer:yuchuanTimer",
#"A4_YuchuanMutex:yuchuanMutex",
#"A5_YuchuanSemaphore:yuchuanSemaphore",
#"A3_YuchuanEvent:yuchuanEvent",
#"A6_YuchuanMessage:yuchuanMessageQueue",
#"B3_YuchuanBasicPWM:yuchuanPWM",
#"B4_YuchuanBasicADC:yuchuanBasicADC",
#"B5_YuchuanNFCI2C:yuchuanNFCI2C",
"B6_YuchuanBasicUART:YuchuanBasicUART",
]
}
运行结果
示例代码编译烧录代码后,按下开发板的RESET按键, 未将开发板上E53接口的UART_TX和UART_RX用跳线短接
通过串口助手查看日志,串口1实现自发自收。
ready to OS start
sdk ver:Hi3861V100R001C00SPC025 2020-09-03 18:10:00
FileSystem mount ok.
wifi init success!
hiview init success.Uart Test Start!!!
====================================================
*************Yuchuan UART_Example Task**************
====================================================
Yuchuan Read Uart Data : 1
00 00:00:00 0 132 D 0/HIVIEW: log limit init success.
00 00:00:00 0 132 I 1/SAMGR: Bootstrap core services(count:3).
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b0388 TaskPool:0xe4b3c
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b0394 TaskPool:0xe4b5c
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b0860 TaskPool:0xe4b7c
00 00:00:00 0 164 I 1/SAMGR: Init service 0x4b0394 <time: 0ms> success!
00 00:00:00 0 64 I 1/SAMGR: Init service 0x4b0388 <time: 10ms> success!
00 00:00:00 0 8 I 1/SAMGR: Init service 0x4b0860 <time: 10ms> success!
00 00:00:00 0 8 I 1/SAMGR: Initialized all core system services!
00 00:00:00 0 64 I 1/SAMGR: Bootstrap system and application services(count:0).
00 00:00:00 0 64 I 1/SAMGR: Initialized all system and application services!
00 00:00:00 0 64 I 1/SAMGR: Bootstrap dynamic registered services(count:0).
====================================================
*************Yuchuan UART_Example Task**************
====================================================
Yuchuan Read Uart Data : 2
====================================================
*************Yuchuan UART_Example Task**************
====================================================
Yuchuan Read Uart Data : 3
====================================================
*************Yuchuan UART_Example Task**************
====================================================
Yuchuan Read Uart Data : 4
====================================================
*************Yuchuan UART_Example Task**************
====================================================
Yuchuan Read Uart Data : 5
示例代码编译烧录代码后,按下开发板的RESET按键, 将开发板上E53接口的UART_TX和UART_RX用跳线短接
通过串口助手查看日志,串口1实现自发自收。
ready to OS start
sdk ver:Hi3861V100R001C00SPC025 2020-09-03 18:10:00
formatting spiffs...
FileSystem mount ok.
wifi init success!
hiview init success.Uart Test Start!!!
====================================================
*************Yuchuan UART_Example Task**************
====================================================
Yuchuan Read Uart Data : 1
00 00:00:00 0 132 D 0/HIVIEW: log limit init success.
00 00:00:00 0 132 I 1/SAMGR: Bootstrap core services(count:3).
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b0388 TaskPool:0xe4b3c
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b0394 TaskPool:0xe4b5c
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b0860 TaskPool:0xe4b7c
00 00:00:00 0 164 I 1/SAMGR: Init service 0x4b0394 <time: 10ms> success!
00 00:00:00 0 64 I 1/SAMGR: Init service 0x4b0388 <time: 10ms> success!
00 00:00:00 0 8 I 1/SAMGR: Init service 0x4b0860 <time: 10ms> success!
00 00:00:00 0 8 I 1/SAMGR: Initialized all core system services!
00 00:00:00 0 64 I 1/SAMGR: Bootstrap system and application services(count:0).
00 00:00:00 0 64 I 1/SAMGR: Initialized all system and application services!
00 00:00:00 0 64 I 1/SAMGR: Bootstrap dynamic registered services(count:0).
====================================================
*************Yuchuan UART_Example Task**************
====================================================
Yuchuan Read Uart Data : Yuchuan Huaying HarmonyOS Hello!!!
2
====================================================
*************Yuchuan UART_Example Task**************
====================================================
Yuchuan Read Uart Data : Yuchuan Huaying HarmonyOS Hello!!!
3
====================================================
*************Yuchuan UART_Example Task**************
====================================================
Yuchuan Read Uart Data : Yuchuan Huaying HarmonyOS Hello!!!
4
====================================================
*************Yuchuan UART_Example Task**************
====================================================
Yuchuan Read Uart Data : Yuchuan Huaying HarmonyOS Hello!!!
5
- 点赞
- 收藏
- 关注作者
评论(0)