• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
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 <stdlib.h>
17 #include <stdio.h>
18 #include "string.h"
19 #include "securec.h"
20 #include "test_suite_console.h"
21 #include "test_suite_task.h"
22 #include "test_suite_uart.h"
23 #ifdef CONFIG_DRIVERS_USB_SERIAL_GADGET
24 #include "gadget/usbd_acm.h"
25 #endif
26 
27 #ifdef SUPPORT_AUDIO_LIEYIN_TOOL
28 #define TEST_SUITE_UART_RX_BUFFER_SIZE  512
29 #else
30 #define TEST_SUITE_UART_RX_BUFFER_SIZE  128
31 #endif
32 #define TEST_SUITE_UART_LEN_MAX         512
33 #define TEST_SUITE_UART_QUEUE_SIZE      1024
34 
35 static uart_bus_t g_test_suite_uart = TEST_SUITE_UART_BUS;
36 static uint8_t g_test_suite_uart_rx_buffer_test[TEST_SUITE_UART_RX_BUFFER_SIZE];
37 
38 #ifdef CONFIG_DRIVERS_USB_SERIAL_GADGET
39 static test_uart_mode_t g_test_uart_mode = TEST_CHIP_UART;
40 #endif
41 
test_suite_uart_init(void)42 void test_suite_uart_init(void)
43 {
44     uart_buffer_config_t uart_buffer_config;
45 
46     uart_pin_config_t uart_pin_config = {
47         .tx_pin = TEST_SUITE_UART_TX_PIN,
48         .rx_pin = TEST_SUITE_UART_RX_PIN,
49         .cts_pin = PIN_NONE,
50         .rts_pin = PIN_NONE
51     };
52 
53     uart_attr_t uart_line_config = {
54         .baud_rate = TEST_SUITE_UART_BAUD_RATE,
55         .data_bits = UART_DATA_BIT_8,
56         .stop_bits = UART_STOP_BIT_1,
57         .parity = UART_PARITY_NONE
58     };
59 
60     uart_buffer_config.rx_buffer_size = TEST_SUITE_UART_RX_BUFFER_SIZE;
61     uart_buffer_config.rx_buffer = g_test_suite_uart_rx_buffer_test;
62     (void)uapi_uart_init(TEST_SUITE_UART_BUS, &uart_pin_config, &uart_line_config, NULL, &uart_buffer_config);
63 
64 #ifdef SUPPORT_AUDIO_LIEYIN_TOOL
65     uapi_uart_register_rx_callback(g_test_suite_uart, UART_RX_CONDITION_MASK_IDLE, 1, \
66                                    test_suite_uart_rx_callback);
67 #else
68     uapi_uart_register_rx_callback(g_test_suite_uart, UART_RX_CONDITION_FULL_OR_SUFFICIENT_DATA_OR_IDLE, 1, \
69                                    test_suite_uart_rx_callback);
70 #endif
71 }
72 
test_suite_uart_deinit(void)73 void test_suite_uart_deinit(void)
74 {
75     if (g_test_suite_uart == TEST_SUITE_UART_BUS) {
76         uapi_uart_deinit(g_test_suite_uart);
77     }
78 }
79 
test_suite_uart_reset_baud_rate(void)80 void test_suite_uart_reset_baud_rate(void)
81 {
82     uart_attr_t uart_line_config = {
83         .baud_rate = TEST_SUITE_UART_BAUD_RATE,
84         .data_bits = UART_DATA_BIT_8,
85         .stop_bits = UART_STOP_BIT_1,
86         .parity = UART_PARITY_NONE
87     };
88     while (uapi_uart_has_pending_transmissions(TEST_SUITE_UART_BUS)) {};
89     uapi_uart_set_attr(TEST_SUITE_UART_BUS, &uart_line_config);
90 }
91 
test_suite_uart_write(const char * buffer,uint16_t length)92 static void test_suite_uart_write(const char *buffer, uint16_t length)
93 {
94 #ifdef CONFIG_DRIVERS_USB_SERIAL_GADGET
95     if (g_test_uart_mode == TEST_USB_SERIAL) {
96         usb_serial_write(0, buffer, length);
97     } else {
98         uapi_uart_write(g_test_suite_uart, (uint8_t *)buffer, (uint32_t)length, 0);
99     }
100 #else
101     uapi_uart_write(g_test_suite_uart, (uint8_t *)buffer, (uint32_t)length, 0);
102 #endif
103 }
104 
test_suite_uart_send_char(char c)105 void test_suite_uart_send_char(char c)
106 {
107     test_suite_uart_write(&c, 1);
108 }
109 
110 #ifdef CONFIG_DRIVERS_USB_SERIAL_GADGET
test_suite_switch_serial_mode(test_uart_mode_t mode)111 void test_suite_switch_serial_mode(test_uart_mode_t mode)
112 {
113     g_test_uart_mode = mode;
114 }
115 #endif
116 
test_suite_uart_send(const char * str)117 void test_suite_uart_send(const char *str)
118 {
119     test_suite_uart_write(str, (uint16_t)strlen(str));
120 }
121 
test_suite_uart_sendf(const char * str,...)122 void test_suite_uart_sendf(const char *str, ...)
123 {
124     static char s[TEST_SUITE_UART_LEN_MAX];  /* This needs to be large enough to store the string */
125     int32_t str_len;
126 
127     va_list args;
128     va_start(args, str);
129     str_len = vsprintf_s(s, sizeof(s), str, args);
130     va_end(args);
131 
132     if (str_len < 0) {
133         return;
134     }
135 
136     test_suite_uart_send(s);
137 }
138 
test_suite_uart_send_line(const char * str)139 void test_suite_uart_send_line(const char *str)
140 {
141     test_suite_uart_send(str);
142     test_suite_uart_send("\r\n");
143 }
144 
test_suite_uart_funcs_get(void)145 test_suite_channel_funcs_t *test_suite_uart_funcs_get(void)
146 {
147     static test_suite_channel_funcs_t test_suite_uart_funcs = {
148         .init = test_suite_uart_init,
149         .deinit = test_suite_uart_deinit,
150         .send_char = test_suite_uart_send_char,
151         .send = test_suite_uart_send,
152         .sendf = test_suite_uart_sendf,
153         .send_line = test_suite_uart_send_line,
154     };
155     return &test_suite_uart_funcs;
156 }
157 
test_suite_uart_rx_callback(const void * buffer,uint16_t length,bool error)158 void test_suite_uart_rx_callback(const void *buffer, uint16_t length, bool error)
159 {
160     if (length == 0 || error) {
161         return;
162     }
163 
164     if (test_suite_console_is_enabled()) {
165         test_suite_write_msgqueue(buffer, length);
166     }
167 }
168