• 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  * Description: serial
15  *
16  * Create: 2021-03-09
17  */
18 
19 #include <stdbool.h>
20 #include <string.h>
21 #include "boot_def.h"
22 #include "boot_delay.h"
23 #include "boot_serial.h"
24 
25 #define UART_GETC_DELAY_TIME                    2
26 #define UART_GETS_DELAY_TIME                    2000
27 #define UART_DELAY_TIME                         (8 * 1000000)
28 
29 #define HIBURN_UART_RX_BUFFER_SIZE              1
30 
31 uart_bus_t g_hiburn_uart = UART_BUS_NONE;
32 bool g_uart_mute = false;
33 bool g_hiburn_uart_enabled = false;
34 
serial_set_mute(void)35 void serial_set_mute(void)
36 {
37     g_uart_mute = true;
38 }
39 
check_uart_mute(void)40 static bool check_uart_mute(void)
41 {
42     return g_uart_mute;
43 }
44 
45 /* 取消禁言 */
serial_cancel_mute(void)46 void serial_cancel_mute(void)
47 {
48     g_uart_mute = false;
49 }
50 
serial_putc(const char c)51 void serial_putc(const char c)
52 {
53     bool is_mute = check_uart_mute();
54     if (is_mute == true) {
55         return;
56     }
57     uapi_uart_write(HIBURN_CODELOADER_UART, (uint8_t *)&c, sizeof(int8_t), UART_GETC_DELAY_TIME);
58 }
59 
serial_puts(const char * s)60 void serial_puts(const char *s)
61 {
62     bool is_mute = check_uart_mute();
63     if (is_mute == true) {
64         return;
65     }
66     uapi_uart_write(HIBURN_CODELOADER_UART, (uint8_t *)s, strlen(s), UART_GETS_DELAY_TIME);
67 }
68 
serial_put_buf(const char * buffer,int32_t length)69 void serial_put_buf(const char *buffer, int32_t length)
70 {
71     bool is_mute;
72 
73     is_mute = check_uart_mute();
74     if (is_mute == true) {
75         return;
76     }
77 
78     uapi_uart_write(HIBURN_CODELOADER_UART, (uint8_t *)buffer, (uint32_t)length, UART_GETS_DELAY_TIME);
79 }
80 
serial_getc(void)81 uint8_t serial_getc(void)
82 {
83     uint8_t getc = 0;
84     uapi_uart_read(HIBURN_CODELOADER_UART, (uint8_t *)&getc, sizeof(getc), UART_GETC_DELAY_TIME);
85     return getc;
86 }
87 
88 
serial_puthex(uint32_t h,bool print_all)89 void serial_puthex(uint32_t h, bool print_all)
90 {
91     int32_t i;
92     uint8_t c;
93     char mark = 0;
94     uint32_t temp = h;
95 
96     bool is_mute = check_uart_mute();
97     if (is_mute == true) {
98         return;
99     }
100 
101     serial_puts("0x");
102 
103     for (i = 0; i < BITS_PER_BYTE; i++) {
104         c = (temp >> 28) & 0x0F; /* u32 right shift 28 */
105 
106         if (c >= DECIMAL) {
107             c = (c - DECIMAL) + 'A';
108         } else {
109             c = c + '0';
110         }
111 
112         if (print_all) {
113             serial_putc(c);
114             temp = temp << 4; /* u32 left shift 4 */
115             continue;
116         }
117 
118         /* 如果不是最后一个数且之前数字都为0 */
119         if ((mark == 0) && (i != BITS_PER_BYTE - 1)) {
120             if (c != '0') {
121                 mark = 1;
122                 serial_putc(c);
123             }
124         } else {
125             serial_putc(c);
126         }
127 
128         temp = temp << 4; /* u32 left shift 4 */
129     }
130 }
131 
serial_getc_timeout(uint32_t timeout_us,uint8_t * ch)132 uint32_t serial_getc_timeout(uint32_t timeout_us, uint8_t *ch)
133 {
134     uint32_t cnt = 0;
135 
136     while (cnt < timeout_us) {
137         if (uapi_uart_rx_fifo_is_empty(HIBURN_CODELOADER_UART) == false) {
138             *ch = serial_getc();
139             return ERRCODE_SUCC;
140         }
141         cnt++;
142     }
143     return ERRCODE_FAIL;
144 }
145 #ifndef ATE_CLOSE_DEBUG
boot_put_errno(uint32_t print_errno)146 void boot_put_errno(uint32_t print_errno)
147 {
148     serial_puts("\nerrno=");
149     serial_puthex(print_errno, 0);
150     serial_puts("\n");
151     mdelay(RESET_DELAY_MS);
152 }
153 
boot_puthex(uint32_t h,bool print0)154 void boot_puthex(uint32_t h, bool print0)
155 {
156     serial_puts(" ");
157     serial_puthex(h, print0);
158 }
159 
boot_msg0(const char * s)160 void boot_msg0(const char *s)
161 {
162     serial_puts(s);
163     serial_puts("\r\n");
164 }
165 
boot_msg1(const char * s,uint32_t h)166 void boot_msg1(const char *s, uint32_t h)
167 {
168     serial_puts(s);
169     serial_puthex(h, 0);
170     serial_puts("\r\n");
171 }
172 
boot_msg2(const char * s,uint32_t h1,uint32_t h2)173 void boot_msg2(const char *s, uint32_t h1, uint32_t h2)
174 {
175     serial_puts(s);
176     serial_puthex(h1, 0);
177     serial_puts(" ");
178     serial_puthex(h2, 0);
179     serial_puts("\r\n");
180 }
181 
boot_msg4(const char * s,uint32_t h1,uint32_t h2,uint32_t h3,uint32_t h4)182 void boot_msg4(const char *s, uint32_t h1, uint32_t h2, uint32_t h3, uint32_t h4)
183 {
184     serial_puts(s);
185     serial_puthex(h1, 0);
186     serial_puts(" ");
187     serial_puthex(h2, 0);
188     serial_puts(" ");
189     serial_puthex(h3, 0);
190     serial_puts(" ");
191     serial_puthex(h4, 0);
192     serial_puts("\r\n");
193 }
194 
hiburn_uart_init(uart_param_stru uart_param,uart_bus_t uart_bus)195 void hiburn_uart_init(uart_param_stru uart_param, uart_bus_t uart_bus)
196 {
197     uart_pin_config_t uart_pins;
198     uart_attr_t uart_line_config;
199     uart_buffer_config_t uart_buffer_config;
200     uint8_t g_hiburn_rx_buffer[HIBURN_UART_RX_BUFFER_SIZE] = { 0 };
201     UNUSED(uart_bus);
202 
203     g_hiburn_uart = HIBURN_CODELOADER_UART;
204     // TX configuration
205     uart_pins.tx_pin = HIBURN_UART_TX;
206     uart_pins.rts_pin = PIN_NONE;
207 
208     // RX configuration
209     uart_pins.rx_pin = HIBURN_UART_RX;
210     uart_pins.cts_pin = PIN_NONE;
211 
212     uart_line_config.baud_rate = uart_param.baudrate;
213     uart_line_config.data_bits = uart_param.databit - 5; // 映射需要减5
214     uart_line_config.parity = uart_param.parity;
215     if (uart_line_config.parity == 1) { // 1代表奇校验
216         uart_line_config.parity = UART_PARITY_ODD;
217     } else if (uart_line_config.parity == 2) { // 2代表偶校验
218         uart_line_config.parity = UART_PARITY_EVEN;
219     } else {
220         uart_line_config.parity = UART_PARITY_NONE;
221     }
222     uart_line_config.stop_bits = uart_param.stopbit - 1;
223 
224     uart_buffer_config.rx_buffer_size = HIBURN_UART_RX_BUFFER_SIZE;
225     uart_buffer_config.rx_buffer = g_hiburn_rx_buffer;
226 
227     (void)uapi_uart_init(HIBURN_CODELOADER_UART, &uart_pins, &uart_line_config, NULL, &uart_buffer_config);
228     uart_port_unregister_irq(HIBURN_CODELOADER_UART);
229     g_hiburn_uart_enabled = true;
230 }
231 
hiburn_uart_deinit(void)232 void hiburn_uart_deinit(void)
233 {
234     uapi_uart_deinit(g_hiburn_uart);
235     g_hiburn_uart_enabled = false;
236 }
237 #endif
238