From 5995cb307ed1f198f94c33a384441801a91a970e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=B8=B8=E4=B9=90=E5=9C=BA?= <121892803@qq.com>
Date: Thu, 28 Apr 2022 04:53:52 -0700
Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=B2=E5=8F=A3=E4=BE=8B?=
=?UTF-8?q?=E7=A8=8B2?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../sample/B7_basic_uart_2/BUILD.gn | 25 ++++
.../sample/B7_basic_uart_2/README.md | 139 ++++++++++++++++++
.../sample/B7_basic_uart_2/uart_example2.c | 109 ++++++++++++++
3 files changed, 273 insertions(+)
create mode 100755 applications/BearPi/BearPi-HM_Nano/sample/B7_basic_uart_2/BUILD.gn
create mode 100755 applications/BearPi/BearPi-HM_Nano/sample/B7_basic_uart_2/README.md
create mode 100755 applications/BearPi/BearPi-HM_Nano/sample/B7_basic_uart_2/uart_example2.c
diff --git a/applications/BearPi/BearPi-HM_Nano/sample/B7_basic_uart_2/BUILD.gn b/applications/BearPi/BearPi-HM_Nano/sample/B7_basic_uart_2/BUILD.gn
new file mode 100755
index 000000000..9f5e8e5b1
--- /dev/null
+++ b/applications/BearPi/BearPi-HM_Nano/sample/B7_basic_uart_2/BUILD.gn
@@ -0,0 +1,25 @@
+# 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.
+
+static_library("uart_example2") {
+ sources = [
+ "uart_example2.c"
+ ]
+
+ include_dirs = [
+ "//utils/native/lite/include",
+ "//kernel/liteos_m/components/cmsis/2.0",
+ "//base/iot_hardware/interfaces/kits/wifiiot_lite",
+ "//vendor/hisi/hi3861/hi3861/platform/include",
+ ]
+}
diff --git a/applications/BearPi/BearPi-HM_Nano/sample/B7_basic_uart_2/README.md b/applications/BearPi/BearPi-HM_Nano/sample/B7_basic_uart_2/README.md
new file mode 100755
index 000000000..a8b5589e0
--- /dev/null
+++ b/applications/BearPi/BearPi-HM_Nano/sample/B7_basic_uart_2/README.md
@@ -0,0 +1,139 @@
+# BearPi-HM_Nano开发板基础外设开发——UART数据读写
+本示例将演示如何在BearPi-HM_Nano开发板上使用UART进行数据的收发
+
+![BearPi-HM_Nano](/applications/BearPi/BearPi-HM_Nano/docs/figures/00_public/BearPi-HM_Nano.png)
+## 注意点
+本例使用的uart1,其txd脚也就是芯片的18脚为硬件配置字,上电时必须低电平,所以必须等烧录成功并完成重启之后再连接串口模块。
+## UART API分析
+本示例主要使用了以下API完成UART数据读写
+## hi_uart_init()
+```c
+hi_u32 hi_uart_init(hi_uart_idx id, const hi_uart_attribute *param, const hi_uart_extra_attr *extra_attr)
+```
+ **描述:**
+
+配置一个UART设备。
+**参数:**
+
+|名字|描述|
+|:--|:------|
+| id | UART端口号. |
+| param |表示基本UART属性|
+| extraAttr |表示扩展UART属性|
+
+## hi_uart_write()
+```c
+hi_s32 hi_uart_write(hi_uart_idx id, const hi_u8 *data, hi_u32 data_len)
+```
+ **描述:**
+将数据写入UART设备
+
+
+**参数:**
+
+|名字|描述|
+|:--|:------|
+| id | UART端口号. |
+| data |表示指向要写入数据的起始地址的指针|
+| dataLen |表示读取数据的长度|
+
+## hi_uart_read_timeout()
+```c
+hi_s32 hi_uart_read_timeout(hi_uart_idx id, hi_u8 *data, hi_u32 len, hi_u32 timeout_ms)
+```
+ **描述:**
+
+从UART设备读取数据。
+
+
+**参数:**
+
+|名字|描述|
+|:--|:------|
+| id | UART端口号. |
+| data |表示指向要读取数据的起始地址的指针|
+| dataLen |表示读取数据的长度|
+| timeout_ms |表示读取数据的超时时间|
+
+
+
+## 硬件设计
+本案例将用 BearPi-HM_Nano 开发板 E53 接口的 UART 作为测试,如原理图所示第 18 和 19 脚分别为 TXD 和 RXD ,连接了主控芯片的 GPIO_6 和 GPIO_5 ,所以在编写软件的时候需要将 GPIO_6 和 GPIO_5 分别复用为 TXD 和 RXD 。
+
+![](/applications/BearPi/BearPi-HM_Nano/docs/figures/B6_basic_uart/E53接口电路.png "E53接口电路")
+
+## 软件设计
+
+**主要代码分析**
+
+这部分代码为UART初始化的代码,首先要在 `hi_uart_attribute` 结构体这配置波特率、数据位、停止位、奇偶检验位,然后通过 `hi_uart_init()` 函数对串口1进行配置。
+
+```c
+ hi_uart_attribute uart_attr = {
+ .baud_rate = 115200,
+ .data_bits = 8,
+ .stop_bits = 1,
+ .parity = 0,
+ };
+
+ //Initialize uart driver
+ ret = hi_uart_init(HI_UART_IDX_1, &uart_attr, NULL);
+ if (ret != HI_ERR_SUCCESS)
+ {
+ printf("Failed to init uart! Err code = %d\n", ret);
+ return;
+ }
+```
+这部分的代码为接收线程,阻塞在uart1_recv进行串口接收,接收到数据之后调用处理函数进行处理
+```c
+ while (1)
+ {
+ //阻塞等待串口1数据
+ rxlen=uart1_recv(uart_buff_ptr, UART_BUFF_SIZE);
+ //处理数据
+ uart1_rxproc(uart_buff_ptr,rxlen);
+ osDelay(2);
+ }
+```
+这是串口接收函数,没有接收到数据时在while中死循环阻塞,收到数据且断帧达到UART_TIMEOUT_MS后返回接收长度
+```c
+uint16_t uart1_recv(uint8_t *buf,uint16_t len)
+{
+ uint16_t rxlen=0;
+ uint16_t ret=0;
+ while(len-rxlen>0){
+ ret=hi_uart_read_timeout(HI_UART_IDX_1, buf+rxlen, len-rxlen,UART_TIMEOUT_MS);
+ if(ret==0&&rxlen!=0){
+ return rxlen;
+ }
+ else{
+ rxlen+=ret;
+ }
+ }
+ return rxlen;
+}
+```
+
+
+## 编译调试
+
+### 修改 BUILD.gn 文件
+
+
+修改 `applications\BearPi\BearPi-HM_Nano\sample` 路径下 BUILD.gn 文件,指定 `uart_example` 参与编译。
+
+```r
+#"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",
+"B7_basic_uart_2:uart_example2",
+```
+
+
+### 运行结果
+
+示例代码编译烧录代码后,按下开发板的RESET按键, `将开发板上E53接口的UART_TX和UART_RX用杜邦线接到TTL转USB模块(自备)` 通过串口助手查看数据,串口1实现接收回显。
+
diff --git a/applications/BearPi/BearPi-HM_Nano/sample/B7_basic_uart_2/uart_example2.c b/applications/BearPi/BearPi-HM_Nano/sample/B7_basic_uart_2/uart_example2.c
new file mode 100755
index 000000000..a99713872
--- /dev/null
+++ b/applications/BearPi/BearPi-HM_Nano/sample/B7_basic_uart_2/uart_example2.c
@@ -0,0 +1,109 @@
+/*
+ * 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.
+ */
+
+#include
+#include
+#include
+
+#include "ohos_init.h"
+#include "cmsis_os2.h"
+#include "hi_uart.h"
+#include "hi_io.h"
+
+#define UART_BUFF_SIZE 1024 //接收缓冲大小
+#define UART_TIMEOUT_MS 20 //接收超时时间
+
+void uart1_send(uint8_t *buf,uint16_t len)
+{
+ hi_uart_write(HI_UART_IDX_1, (unsigned char *)buf, len);
+}
+
+void uart1_rxproc(uint8_t *buf,uint16_t len)
+{
+ uart1_send((unsigned char *)buf, len);
+ printf("Uart1 read data len:%d\r\n", len);
+}
+
+
+//串口接收,无数据时阻塞,有数据时收完一帧或者收到最大值返回
+uint16_t uart1_recv(uint8_t *buf,uint16_t len)
+{
+ uint16_t rxlen=0;
+ uint16_t ret=0;
+ while(len-rxlen>0){
+ ret=hi_uart_read_timeout(HI_UART_IDX_1, buf+rxlen, len-rxlen,UART_TIMEOUT_MS);
+ if(ret==0&&rxlen!=0){
+ return rxlen;
+ }
+ else{
+ rxlen+=ret;
+ }
+ }
+ return rxlen;
+}
+
+void uart_task(void)
+{
+ uint8_t uart_buff[UART_BUFF_SIZE] = {0};
+ uint8_t *uart_buff_ptr = uart_buff;
+ uint32_t ret;
+ uint16_t rxlen;
+
+ hi_uart_attribute uart_attr = {
+ .baud_rate = 115200,
+ .data_bits = 8,
+ .stop_bits = 1,
+ .parity = 0,
+ };
+
+ //Initialize uart driver
+ ret = hi_uart_init(HI_UART_IDX_1, &uart_attr, NULL);
+ if (ret != HI_ERR_SUCCESS)
+ {
+ printf("Failed to init uart! Err code = %d\n", ret);
+ return;
+ }
+ printf("UART Test Start\n");
+
+ while (1)
+ {
+ //阻塞等待串口1数据
+ rxlen=uart1_recv(uart_buff_ptr, UART_BUFF_SIZE);
+ //处理数据
+ uart1_rxproc(uart_buff_ptr,rxlen);
+ osDelay(2);
+ }
+}
+
+static void UART_ExampleEntry(void)
+{
+
+ osThreadAttr_t attr;
+
+ attr.name = "UART_Task";
+ attr.attr_bits = 0U;
+ attr.cb_mem = NULL;
+ attr.cb_size = 0U;
+ attr.stack_mem = NULL;
+ attr.stack_size = 2048;
+ attr.priority = 15;
+
+ if (osThreadNew((osThreadFunc_t)uart_task, NULL, &attr) == NULL)
+ {
+ printf("[Uart Example2] Falied to create UART_Task!\n");
+ }
+}
+
+APP_FEATURE_INIT(UART_ExampleEntry);
\ No newline at end of file
--
Gitee