• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Hunan OpenValley Digital Industry Development Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <stdio.h>
17 #include "uart_types.h"
18 #include "uart_if.h"
19 #include "cmsis_os2.h"
20 #include "hdf_log.h"
21 #include "ohos_run.h"
22 
23 #define SYS_DELAY_TICKS 200
24 #define TASK_STACK_SIZE 4096
25 #define TASK_PRIO 25
26 #define BUFF_MAX_SIZE   128
27 #define UART_BAUDRATE        115200
28 
29 #define UART_INDEX   0       // 串口号
30 
31 osThreadId_t g_taskID = NULL;
32 
uart_test(void)33 static void uart_test(void)
34 {
35     int32_t ret;
36     DevHandle hd = UartOpen(UART_INDEX);    // 打开串口
37     if (hd == NULL) {
38         printf("%s UartOpen failed!\n", __func__);
39     }
40 
41     uint32_t baudRate = UART_BAUDRATE;
42     ret = UartSetBaud(hd, baudRate);      // 设置波特率
43     if (ret < 0) {
44         printf("HDF UartSetBaud %d fail!\r\n", baudRate);
45     }
46 
47     baudRate = 0;   // 波特率参数清零
48     ret = UartGetBaud(hd, &baudRate);             // 获取波特率
49     if (ret >= 0) {
50         printf("HDF UartGetBaud %d success!\r\n", baudRate);
51     }
52 
53     ret = UartSetTransMode(hd, UART_MODE_RD_BLOCK);   // 设置串口为读取阻塞模式
54     if (ret >= 0) {
55         printf("UartSetTransMode UART_MODE_RD_BLOCK!\r\n");
56     }
57 
58     while (1) {
59         char buff[BUFF_MAX_SIZE] = { 0 };   // 用于接收串口数据的buffer
60         ret = UartRead(hd, buff, BUFF_MAX_SIZE);
61         if (ret > 0) {
62             if (strstr(buff, "quit")) { // 接收到包含"quit"的字符串则退出任务
63                 break;
64             } else {
65                 UartWrite(hd, buff, ret);   // 将接收到的数据写入串口
66             }
67         } else if (ret == 0) {
68             printf("No data arrived!\r\n");
69         } else {
70             printf("Read Uart data fail! ret = %d\r\n", ret);
71         }
72     }
73     UartClose(hd);
74     printf("---> Exit uart_test task!\r\n");
75     return;
76 }
77 
uart_example_task(void)78 static void uart_example_task(void)
79 {
80     HDF_LOGE("into uart hdf example!\n");
81     osThreadAttr_t attr;
82     attr.name = "uart_test";
83     attr.attr_bits = 0U;
84     attr.cb_mem = NULL;
85     attr.cb_size = 0U;
86     attr.stack_mem = NULL;
87     attr.stack_size = TASK_STACK_SIZE;
88     attr.priority = TASK_PRIO;
89     g_taskID = osThreadNew((osThreadFunc_t)uart_test, NULL, &attr);
90     if (g_taskID == NULL) {
91         HDF_LOGE("Failed to create Test Uart thread!\n");
92     }
93 }
94 
95 OHOS_APP_RUN(uart_example_task);
96