1 /*
2 * Copyright (c) 2021 GOODIX.
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 #include "gpio_if.h"
16 #include "driver_test.h"
17 #include "uart_if.h"
18
19 static DevHandle uart_handle = NULL;
20 #define baudrate_115200 115200
21 enum case_test_index {
22 SET_BAUD,
23 GET_BAUD,
24 UART_CONFIG,
25 UART_WRITE,
26 UART_READ
27 };
28
29
uart_test_case_read_write(int uart_state)30 int uart_test_case_read_write(int uart_state)
31 {
32 int state_temp = uart_state;
33 int32_t ret;
34 uint8_t send_data[] = "Hello OpenHarmony\r\n";
35 uint8_t recv_data[16];
36
37 switch (state_temp) {
38 case UART_WRITE:
39 ret = UartWrite(uart_handle, send_data, strlen(send_data));
40 if (ret != 0) {
41 LOG_E("%s, state[%d] failed", __func__, state_temp);
42 return;
43 }
44 break;
45 case UART_READ:
46 ret = UartRead(uart_handle, recv_data, sizeof(recv_data) - 1);
47 if (ret > 0) {
48 recv_data[ret] = 0;
49 LOG_I("%s, state[%d] recv:%s", __func__, state_temp, recv_data);
50 } else {
51 LOG_I("%s, state[%d] no data received", __func__, state_temp);
52 }
53 state_temp = UART_WRITE;
54 break;
55 default:
56 LOG_I("%s, state[%d] is not support", __func__, state_temp);
57 break;
58 }
59
60 return state_temp;
61 }
62
uart_test_case(DevHandle uart_handle)63 static void uart_test_case(DevHandle uart_handle)
64 {
65 static int state = 0;
66 int32_t ret;
67 uint32_t baudrate;
68 struct UartAttribute attr;
69 uint8_t recv_data[16];
70
71 ret = UartSetBaud(uart_handle, baudrate_115200);
72 if (ret != 0) {
73 LOG_E("%s, state[%d] failed", __func__, state);
74 return;
75 }
76
77 ret = UartGetBaud(uart_handle, &baudrate);
78 if (ret != 0) {
79 LOG_E("%s, state[%d] failed", __func__, state);
80 return;
81 }
82
83 attr.dataBits = UART_ATTR_DATABIT_8;
84 attr.parity = UART_ATTR_PARITY_NONE;
85 attr.stopBits = UART_ATTR_STOPBIT_1;
86 ret = UartSetAttribute(uart_handle, &attr);
87 if (ret != 0) {
88 LOG_E("%s, state[%d] failed", __func__, state);
89 return;
90 }
91
92 ret = UartRead(uart_handle, recv_data, sizeof(recv_data) - 1);
93 if (ret > 0) {
94 recv_data[ret] = 0;
95 LOG_I("%s, state[%d] recv:%s", __func__, state, recv_data);
96 } else {
97 LOG_I("%s, state[%d] no data received", __func__, state);
98 }
99
100 state = UART_WRITE;
101 state = uart_test_case_read_write(state);
102 }
103
uart_test(void)104 void uart_test(void)
105 {
106 static int uart_state = 0;
107
108 switch (uart_state) {
109 case 0:
110 uart_handle = UartOpen(1);
111 if (uart_handle == NULL) {
112 LOG_E (">>>uart open failed");
113 } else {
114 LOG_I (">>>uart open success");
115 uart_state++;
116 }
117 break;
118 case 1:
119 if (uart_handle) {
120 uart_test_case(uart_handle);
121 }
122 break;
123 default:
124 break;
125 }
126 }
127