• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Talkweb 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 <hdf_log.h>
18 #include <uart_if.h>
19 #include "cmsis_os2.h"
20 #include "samgr_lite.h"
21 #include "ohos_run.h"
22 
23 #define HDF_USART_STACK_SIZE 0x1000
24 #define HDF_USART_TASK_NAME "hdf_usart_test_task"
25 #define HDF_USART_TASK_PRIORITY 25
26 
27 uint8_t txbuf[80] = "this is usart test function\n";
28 uint8_t rxbuf[80] = {0};
29 uint8_t len = 0;
30 uint8_t tmp;
31 
HdfUsartTestEntry(void * arg)32 static void* HdfUsartTestEntry(void* arg)
33 {
34     (void *)arg;
35     uint32_t port = 5;
36     DevHandle handle = UartOpen(port);
37     if (handle == NULL) {
38         HDF_LOGE("UartOpen %u: failed!\n", port);
39         return NULL;
40     }
41     int32_t ret;
42     uint32_t baudRate;
43     ret = UartGetBaud(handle, &baudRate);
44     if (ret != 0) {
45         HDF_LOGE("UartGetBaud: failed, ret %d\n", ret);
46     }
47     baudRate = 115200;
48     ret = UartSetBaud(handle, baudRate);
49     if (ret != 0) {
50         HDF_LOGE("UartGetBaud: failed, ret %d\n", ret);
51     }
52     struct UartAttribute attribute = {0};
53     ret = UartGetAttribute(handle, &attribute);
54     if (ret != 0) {
55         HDF_LOGE("UartGetAttribute: failed, ret %d\n", ret);
56     }
57     attribute.dataBits = UART_ATTR_DATABIT_8;
58     attribute.fifoTxEn = 1;
59     attribute.fifoRxEn = 1;
60     attribute.parity = UART_ATTR_PARITY_NONE;
61     attribute.stopBits = UART_ATTR_STOPBIT_1;
62     attribute.cts = 0;
63     attribute.rts = 0;
64     ret = UartSetAttribute(handle, &attribute);
65     if (ret != 0) {
66         HDF_LOGE("UartSetAttribute: failed, ret %d\n", ret);
67     }
68     len = strlen((char *)txbuf);
69     ret = UartWrite(handle, txbuf, len);
70     if (ret != 0) {
71         HDF_LOGE("UartWrite: failed, ret %d\n", ret);
72         goto _ERR;
73     }
74     while (1) {
75         ret = UartRead(handle, rxbuf, len);
76         if (ret < 0) {
77             HDF_LOGE("UartRead: failed, ret %d\n", ret);
78             goto _ERR;
79         } else if (ret > 0) {
80             HDF_LOGI("UartRead: content length is %d is :%s\n", ret, rxbuf);
81         }
82         osDelay(100);
83     }
84 
85 _ERR:
86     UartClose(handle);
87     return NULL;
88 }
89 
StartHdfUsartTest(void)90 void StartHdfUsartTest(void)
91 {
92     osThreadAttr_t attr;
93 
94     attr.name = HDF_USART_TASK_NAME;
95     attr.attr_bits = 0U;
96     attr.cb_mem = NULL;
97     attr.cb_size = 0U;
98     attr.stack_mem = NULL;
99     attr.stack_size = HDF_USART_STACK_SIZE;
100     attr.priority = HDF_USART_TASK_PRIORITY;
101 
102     if (osThreadNew((osThreadFunc_t)HdfUsartTestEntry, NULL, &attr) == NULL) {
103         printf("Failed to create thread1!\n");
104     }
105 }
106 
107 OHOS_APP_RUN(StartHdfUsartTest);
108 
109