1 // Copyright (C) 2022 Beken Corporation
2 //
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 <common/bk_include.h>
16 #include <common/bk_compiler.h>
17 #include <os/mem.h>
18 #include "uart_driver.h"
19 #include "uart_hal.h"
20 #include <driver/uart.h>
21 #include "bk_uart.h"
22 #include "gpio_driver.h"
23 #include <driver/gpio.h>
24 #include "bk_fifo.h"
25 #include <os/os.h>
26 #include "uart_statis.h"
27 #include <driver/int.h>
28 #include "icu_driver.h"
29 #include "power_driver.h"
30 #include "clock_driver.h"
31 #include <os/os.h>
32 #include "bk_arch.h"
33 #include <components/system.h>
34 #if (CONFIG_SYSTEM_CTRL)
35 #include "sys_driver.h"
36 #endif
37
38 static void uart_isr_common(uart_id_t id) __BK_SECTION(".itcm");
39 static uint32_t uart_id_read_fifo_frame(uart_id_t id, const kfifo_ptr_t rx_ptr) __BK_SECTION(".itcm");
40
41 typedef struct {
42 uart_hal_t hal;
43 uint8_t id_init_bits;
44 uint8_t id_sw_fifo_enable_bits;
45 } uart_driver_t;
46
47 typedef struct {
48 bool rx_blocked;
49 beken_semaphore_t rx_int_sema;
50 } uart_sema_t;
51
52 typedef struct
53 {
54 uart_isr_t callback;
55 void *param;
56 } uart_callback_t;
57
58 #define CONFIG_UART_MIN_BAUD_RATE (UART_CLOCK / (0x1fff + 1))
59 #define CONFIG_UART_MAX_BAUD_RATE (UART_CLOCK / (4 + 1))
60 #ifndef CONFIG_PRINTF_BUF_SIZE
61 #define CONFIG_PRINTF_BUF_SIZE (128)
62 #endif
63
64 static uart_driver_t s_uart[SOC_UART_ID_NUM_PER_UNIT] = {0};
65 static bool s_uart_driver_is_init = false;
66 static uart_callback_t s_uart_rx_isr[SOC_UART_ID_NUM_PER_UNIT] = {NULL};
67 static uart_callback_t s_uart_tx_isr[SOC_UART_ID_NUM_PER_UNIT] = {NULL};
68 static kfifo_ptr_t s_uart_rx_kfifo[SOC_UART_ID_NUM_PER_UNIT] = {NULL};
69 static uart_sema_t s_uart_sema[SOC_UART_ID_NUM_PER_UNIT] = {0};
70
71 #define UART_RETURN_ON_NOT_INIT() do {\
72 if (!s_uart_driver_is_init) {\
73 UART_LOGE("UART driver not init\r\n");\
74 return BK_ERR_UART_NOT_INIT;\
75 }\
76 } while(0)
77
78 #define UART_RETURN_ON_INVALID_ID(id) do {\
79 if ((id) >= SOC_UART_ID_NUM_PER_UNIT) {\
80 UART_LOGE("UART id number(%d) is invalid\r\n", (id));\
81 return BK_ERR_UART_INVALID_ID;\
82 }\
83 } while(0)
84
85 #define UART_RETURN_ON_ID_NOT_INIT(id) do {\
86 if (!(s_uart[id].id_init_bits & BIT((id)))) {\
87 return BK_ERR_UART_ID_NOT_INIT;\
88 }\
89 } while(0)
90
91 #define UART_RETURN_ON_BAUD_RATE_NOT_SUPPORT(baud_rate) do {\
92 if ((baud_rate) < CONFIG_UART_MIN_BAUD_RATE ||\
93 (baud_rate) > CONFIG_UART_MAX_BAUD_RATE) {\
94 UART_LOGE("UART baud rate(%d) not support\r\n", (baud_rate));\
95 return BK_ERR_UART_BAUD_RATE_NOT_SUPPORT;\
96 }\
97 } while(0)
98
99 #if (CONFIG_DEBUG_FIRMWARE)
100 #define DEAD_WHILE() do{\
101 while(1);\
102 } while(0)
103 #else
104 #define DEAD_WHILE() do{\
105 os_printf("dead\r\n");\
106 } while(0)
107 #endif
108
109 #if (CONFIG_SYSTEM_CTRL)
uart_clock_enable(uart_id_t id)110 static void uart_clock_enable(uart_id_t id)
111 {
112 switch(id)
113 {
114 case UART_ID_0:
115 sys_drv_dev_clk_pwr_up(CLK_PWR_ID_UART1, CLK_PWR_CTRL_PWR_UP);
116 break;
117 case UART_ID_1:
118 sys_drv_dev_clk_pwr_up(CLK_PWR_ID_UART2, CLK_PWR_CTRL_PWR_UP);
119 break;
120 case UART_ID_2:
121 sys_drv_dev_clk_pwr_up(CLK_PWR_ID_UART3, CLK_PWR_CTRL_PWR_UP);
122 break;
123 default:
124 break;
125 }
126 }
127
uart_clock_disable(uart_id_t id)128 static void uart_clock_disable(uart_id_t id)
129 {
130 switch(id)
131 {
132 case UART_ID_0:
133 sys_drv_dev_clk_pwr_up(CLK_PWR_ID_UART1, CLK_PWR_CTRL_PWR_DOWN);
134 break;
135 case UART_ID_1:
136 sys_drv_dev_clk_pwr_up(CLK_PWR_ID_UART2, CLK_PWR_CTRL_PWR_DOWN);
137 break;
138 case UART_ID_2:
139 sys_drv_dev_clk_pwr_up(CLK_PWR_ID_UART3, CLK_PWR_CTRL_PWR_DOWN);
140 break;
141 default:
142 break;
143 }
144 }
145
uart_interrupt_enable(uart_id_t id)146 static void uart_interrupt_enable(uart_id_t id)
147 {
148 switch(id)
149 {
150 case UART_ID_0:
151 sys_drv_int_enable(UART0_INTERRUPT_CTRL_BIT);
152 break;
153 case UART_ID_1:
154 sys_drv_int_enable(UART1_INTERRUPT_CTRL_BIT);
155 break;
156 case UART_ID_2:
157 sys_drv_int_enable(UART2_INTERRUPT_CTRL_BIT);
158 break;
159 default:
160 break;
161 }
162 }
163
uart_interrupt_disable(uart_id_t id)164 static void uart_interrupt_disable(uart_id_t id)
165 {
166 switch(id)
167 {
168 case UART_ID_0:
169 sys_drv_int_disable(UART0_INTERRUPT_CTRL_BIT);
170 break;
171 case UART_ID_1:
172 sys_drv_int_disable(UART1_INTERRUPT_CTRL_BIT);
173 break;
174 case UART_ID_2:
175 sys_drv_int_disable(UART2_INTERRUPT_CTRL_BIT);
176 break;
177 default:
178 break;
179 }
180 }
181 #endif
182
uart_init_gpio(uart_id_t id)183 static void uart_init_gpio(uart_id_t id)
184 {
185 switch (id)
186 {
187 case UART_ID_0:
188 {
189 gpio_dev_map(uart_hal_get_tx_pin(id), GPIO_DEV_UART1_TXD);
190 gpio_dev_map(uart_hal_get_rx_pin(id), GPIO_DEV_UART1_RXD);
191 bk_gpio_pull_up(uart_hal_get_tx_pin(id));
192 bk_gpio_pull_up(uart_hal_get_rx_pin(id));
193 #if CONFIG_UART1_FLOW_CTRL
194 //NOTICE:BEKEN ASIC CTS PIN really function is RTS.
195 gpio_dev_map(uart_hal_get_cts_pin(id), GPIO_DEV_UART1_CTS);
196 bk_gpio_enable_output(uart_hal_get_cts_pin(id));
197 bk_gpio_pull_down(uart_hal_get_cts_pin(id));
198
199 gpio_dev_map(uart_hal_get_rts_pin(id), GPIO_DEV_UART1_RTS);
200 bk_gpio_enable_input(uart_hal_get_rts_pin(id));
201 bk_gpio_pull_down(uart_hal_get_rts_pin(id));
202 bk_uart_set_hw_flow_ctrl(id, UART1_FLOW_CTRL_CNT);
203 #endif
204 break;
205 }
206 case UART_ID_1:
207 {
208 gpio_dev_map(uart_hal_get_tx_pin(id), GPIO_DEV_UART2_TXD);
209 gpio_dev_map(uart_hal_get_rx_pin(id), GPIO_DEV_UART2_RXD);
210 bk_gpio_pull_up(uart_hal_get_tx_pin(id));
211 bk_gpio_pull_up(uart_hal_get_rx_pin(id));
212 break;
213 }
214 case UART_ID_2:
215 {
216 gpio_dev_map(uart_hal_get_tx_pin(id), GPIO_DEV_UART3_TXD);
217 gpio_dev_map(uart_hal_get_rx_pin(id), GPIO_DEV_UART3_RXD);
218 bk_gpio_pull_up(uart_hal_get_tx_pin(id));
219 bk_gpio_pull_up(uart_hal_get_rx_pin(id));
220 break;
221 }
222 default:
223 break;
224 }
225 }
226
uart_deinit_tx_gpio(uart_id_t id)227 static void uart_deinit_tx_gpio(uart_id_t id)
228 {
229 switch (id)
230 {
231 case UART_ID_0:
232 {
233 gpio_dev_unmap(uart_hal_get_tx_pin(id));
234 bk_gpio_pull_up(uart_hal_get_tx_pin(id));
235 break;
236 }
237 case UART_ID_1:
238 {
239 gpio_dev_unmap(uart_hal_get_tx_pin(id));
240 bk_gpio_pull_up(uart_hal_get_tx_pin(id));
241 break;
242 }
243 case UART_ID_2:
244 {
245 gpio_dev_unmap(uart_hal_get_tx_pin(id));
246 bk_gpio_pull_up(uart_hal_get_tx_pin(id));
247 break;
248 }
249 default:
250 break;
251 }
252 }
253
uart_deinit_rx_gpio(uart_id_t id)254 static void uart_deinit_rx_gpio(uart_id_t id)
255 {
256 switch (id)
257 {
258 case UART_ID_0:
259 {
260 gpio_dev_unmap(uart_hal_get_rx_pin(id));
261 bk_gpio_pull_up(uart_hal_get_rx_pin(id));
262 break;
263 }
264 case UART_ID_1:
265 {
266 gpio_dev_unmap(uart_hal_get_rx_pin(id));
267 bk_gpio_pull_up(uart_hal_get_rx_pin(id));
268 break;
269 }
270 case UART_ID_2:
271 {
272 gpio_dev_unmap(uart_hal_get_rx_pin(id));
273 bk_gpio_pull_up(uart_hal_get_rx_pin(id));
274 break;
275 }
276 default:
277 break;
278 }
279 }
280
uart_id_init_kfifo(uart_id_t id)281 static bk_err_t uart_id_init_kfifo(uart_id_t id)
282 {
283 if (!s_uart_rx_kfifo[id]) {
284 s_uart_rx_kfifo[id] = kfifo_alloc(CONFIG_KFIFO_SIZE);
285 if (!s_uart_rx_kfifo[id]) {
286 UART_LOGE("uart(%d) rx kfifo alloc failed\n", id);
287 return BK_ERR_NULL_PARAM;
288 }
289 }
290 return BK_OK;
291 }
292
uart_id_deinit_kfifo(uart_id_t id)293 static void uart_id_deinit_kfifo(uart_id_t id)
294 {
295 if (s_uart_rx_kfifo[id]) {
296 kfifo_free(s_uart_rx_kfifo[id]);
297 }
298 s_uart_rx_kfifo[id] = NULL;
299 }
300
301 /* 1. power up uart
302 * 2. set clock
303 * 3. set gpio as uart
304 */
uart_id_init_common(uart_id_t id)305 static bk_err_t uart_id_init_common(uart_id_t id)
306 {
307 bk_err_t ret = 0;
308
309 #if (CONFIG_SYSTEM_CTRL)
310 uart_clock_enable(id);
311 sys_drv_uart_select_clock(id, UART_SCLK_XTAL_26M);
312 #else
313 power_uart_pwr_up(id);
314 clk_set_uart_clk_26m(id);
315 #endif
316
317 uart_init_gpio(id);
318 ret = uart_id_init_kfifo(id);
319 uart_statis_id_init(id);
320
321 s_uart_sema[id].rx_blocked = false;
322 if (s_uart_sema[id].rx_int_sema == NULL) {
323 ret = rtos_init_semaphore(&(s_uart_sema[id].rx_int_sema), 1);
324 BK_ASSERT(kNoErr == ret);
325 }
326 s_uart[id].id_init_bits |= BIT(id);
327 s_uart[id].id_sw_fifo_enable_bits |= BIT(id);
328
329 return ret;
330 }
331
uart_id_deinit_common(uart_id_t id)332 static void uart_id_deinit_common(uart_id_t id)
333 {
334 s_uart[id].id_init_bits &= ~BIT(id);
335 uart_hal_stop_common(&s_uart[id].hal, id);
336 uart_hal_reset_config_to_default(&s_uart[id].hal, id);
337 #if (CONFIG_SYSTEM_CTRL)
338 uart_interrupt_disable(id);
339 uart_clock_disable(id);
340 #else
341 icu_disable_uart_interrupt(id);
342 power_uart_pwr_down(id);
343 #endif
344 uart_id_deinit_kfifo(id);
345 rtos_deinit_semaphore(&(s_uart_sema[id].rx_int_sema));
346 }
347
uart_id_is_sw_fifo_enabled(uart_id_t id)348 static inline bool uart_id_is_sw_fifo_enabled(uart_id_t id)
349 {
350 return !!(s_uart[id].id_sw_fifo_enable_bits & BIT(id));
351 }
352
uart_id_read_fifo_frame(uart_id_t id,const kfifo_ptr_t rx_ptr)353 static uint32_t uart_id_read_fifo_frame(uart_id_t id, const kfifo_ptr_t rx_ptr)
354 {
355 uint8_t read_val = 0;
356 uint32_t rx_count = sizeof(read_val);
357 uint32_t unused = kfifo_unused(rx_ptr);
358 uint32_t kfifo_put_cnt = 0;
359 __attribute__((__unused__)) uart_statis_t *uart_statis = uart_statis_get_statis(id);
360
361 //TODO: optimize flow ctrl
362 while (uart_hal_is_fifo_read_ready(&s_uart[id].hal, id)) {
363 /* must read when fifo read ready, otherwise will loop forever */
364 read_val = uart_hal_read_byte(&s_uart[id].hal, id);
365 UART_LOGD("read val:0x%x, rx_count/unused: %d/%d\n", read_val, rx_count, unused);
366 if (rx_count > unused) {
367 if (!uart_hal_is_flow_control_enabled(&s_uart[id].hal, id)) {
368 UART_LOGW("rx kfifo is full, out/in:%d/%d, unused:%d\n", rx_ptr->out, rx_ptr->in, unused);
369 UART_STATIS_INC(uart_statis->kfifo_status.full_cnt);
370 #if CFG_CLI_DEBUG
371 extern void cli_show_running_command(void);
372 cli_show_running_command();
373 #endif
374 }
375 } else {
376 kfifo_put_cnt += kfifo_put(rx_ptr, &read_val, sizeof(read_val));
377 UART_STATIS_INC(uart_statis->kfifo_status.put_cnt);
378 UART_STATIS_SET(uart_statis->kfifo_status.last_value, read_val);
379 }
380 unused = kfifo_unused(rx_ptr);
381 }
382 UART_STATIS_SET(uart_statis->kfifo_status.in, rx_ptr->in);
383 UART_STATIS_SET(uart_statis->kfifo_status.out, rx_ptr->out);
384
385 return kfifo_put_cnt;
386 }
387
print_hex_dump(const char * prefix,const void * buf,int len)388 void print_hex_dump(const char *prefix, const void *buf, int len)
389 {
390 int i;
391 const u8 *b = buf;
392
393 if (prefix)
394 BK_LOG_RAW("%s", prefix);
395 for (i = 0; i < len; i++)
396 BK_LOG_RAW("%02X ", b[i]);
397 BK_LOG_RAW("\n");
398 }
399
uart_write_byte(uart_id_t id,uint8_t data)400 bk_err_t uart_write_byte(uart_id_t id, uint8_t data)
401 {
402 /* wait for fifo write ready
403 * optimize it when write very fast
404 * wait for fifo write ready will waste CPU performance
405 */
406 BK_WHILE (!uart_hal_is_fifo_write_ready(&s_uart[id].hal, id));
407 uart_hal_write_byte(&s_uart[id].hal, id, data);
408 return BK_OK;
409 }
410
uart_write_byte_for_ate(uart_id_t id,uint8_t * data,uint8_t cnt)411 void uart_write_byte_for_ate(uart_id_t id, uint8_t *data, uint8_t cnt)
412 {
413 int i;
414 for(i = 0; i < cnt; i ++)
415 {
416 BK_WHILE (!uart_hal_is_fifo_write_ready(&s_uart[id].hal, id));
417 uart_hal_write_byte(&s_uart[id].hal, id, data[i]);
418 }
419 }
420
uart_write_ready(uart_id_t id)421 bk_err_t uart_write_ready(uart_id_t id)
422 {
423 /* wait for fifo write ready
424 * optimize it when write very fast
425 * wait for fifo write ready will waste CPU performance
426 */
427 if (!uart_hal_is_fifo_write_ready(&s_uart[id].hal, id)) {
428 return BK_FAIL;
429 }
430
431 return BK_OK;
432 }
433
uart_write_string(uart_id_t id,const char * string)434 bk_err_t uart_write_string(uart_id_t id, const char *string)
435 {
436 const char *p = string;
437
438 while (*string) {
439 if (*string == '\n') {
440 if (p == string || *(string - 1) != '\r')
441 uart_write_byte(id, '\r'); /* append '\r' */
442 }
443 uart_write_byte(id, *string++);
444 }
445
446 return BK_OK;
447 }
448
uart_read_ready(uart_id_t id)449 bk_err_t uart_read_ready(uart_id_t id)
450 {
451 if (!uart_hal_is_fifo_read_ready(&s_uart[id].hal, id)) {
452 return BK_FAIL;
453 }
454
455 return BK_OK;
456 }
457
uart_read_byte(uart_id_t id)458 int uart_read_byte(uart_id_t id)
459 {
460 int val = -1;
461 if (uart_hal_is_fifo_read_ready(&s_uart[id].hal, id)) {
462 val = uart_hal_read_byte(&s_uart[id].hal, id);
463 }
464 return val;
465 }
466
uart_read_byte_ex(uart_id_t id,uint8_t * ch)467 int uart_read_byte_ex(uart_id_t id, uint8_t *ch)
468 {
469 int val = -1;
470 if (uart_hal_is_fifo_read_ready(&s_uart[id].hal, id)) {
471 *ch = uart_hal_read_byte(&s_uart[id].hal, id);
472 val = 0;
473 }
474 return val;
475 }
476
uart_get_length_in_buffer(uart_id_t id)477 uint32_t uart_get_length_in_buffer(uart_id_t id)
478 {
479 return kfifo_data_size(s_uart_rx_kfifo[id]);
480 }
481
uart_isr_register_functions(uart_id_t id)482 static void uart_isr_register_functions(uart_id_t id)
483 {
484 switch(id)
485 {
486 case UART_ID_0:
487 bk_int_isr_register(INT_SRC_UART1, uart0_isr, NULL);
488 break;
489 case UART_ID_1:
490 bk_int_isr_register(INT_SRC_UART2, uart1_isr, NULL);
491 break;
492 case UART_ID_2:
493 bk_int_isr_register(INT_SRC_UART3, uart2_isr, NULL);
494 break;
495 default:
496 break;
497 }
498 }
499
bk_uart_driver_init(void)500 bk_err_t bk_uart_driver_init(void)
501 {
502 if (s_uart_driver_is_init) {
503 return BK_OK;
504 }
505
506 os_memset(&s_uart, 0, sizeof(s_uart));
507 os_memset(&s_uart_rx_isr, 0, sizeof(s_uart_rx_isr));
508 os_memset(&s_uart_tx_isr, 0, sizeof(s_uart_tx_isr));
509
510 for(uart_id_t id = UART_ID_0; id < SOC_UART_ID_NUM_PER_UNIT; id++) {
511 uart_isr_register_functions(id);
512 s_uart[id].hal.id = id;
513 uart_hal_init(&s_uart[id].hal);
514 }
515
516 uart_statis_init();
517 s_uart_driver_is_init = true;
518
519 #ifndef CONFIG_BK_PRINTF_DISABLE
520 bk_printf_init();
521 #endif
522
523 return BK_OK;
524 }
525
bk_uart_driver_deinit(void)526 bk_err_t bk_uart_driver_deinit(void)
527 {
528 if (!s_uart_driver_is_init)
529 return BK_OK;
530
531 for (uart_id_t id = UART_ID_0; id < SOC_UART_ID_NUM_PER_UNIT; id++) {
532 uart_id_deinit_common(id);
533 }
534 s_uart_driver_is_init = false;
535
536 return BK_OK;
537 }
538
bk_uart_is_in_used(uart_id_t id)539 int bk_uart_is_in_used(uart_id_t id)
540 {
541 return (s_uart[id].id_init_bits & BIT((id)));
542 }
543
bk_uart_init(uart_id_t id,const uart_config_t * config)544 bk_err_t bk_uart_init(uart_id_t id, const uart_config_t *config)
545 {
546 UART_RETURN_ON_NOT_INIT();
547 BK_RETURN_ON_NULL(config);
548 UART_RETURN_ON_INVALID_ID(id);
549 UART_RETURN_ON_BAUD_RATE_NOT_SUPPORT(config->baud_rate);
550
551 uart_id_init_common(id);
552 #if (CONFIG_SYSTEM_CTRL)
553 if (config->src_clk == UART_SCLK_APLL)
554 sys_drv_uart_select_clock(id, UART_SCLK_APLL);
555 else
556 sys_drv_uart_select_clock(id, UART_SCLK_XTAL_26M);
557
558 #else
559 if (config->src_clk == UART_SCLK_DCO) {
560 clk_set_uart_clk_dco(id);
561 } else {
562 clk_set_uart_clk_26m(id);
563 }
564 #endif
565
566 uart_hal_init_uart(&s_uart[id].hal, id, config);
567 uart_hal_start_common(&s_uart[id].hal, id);
568
569 return BK_OK;
570 }
571
bk_uart_deinit(uart_id_t id)572 bk_err_t bk_uart_deinit(uart_id_t id)
573 {
574 UART_RETURN_ON_NOT_INIT();
575 UART_RETURN_ON_INVALID_ID(id);
576 uart_id_deinit_common(id);
577 return BK_OK;
578 }
579
bk_uart_set_baud_rate(uart_id_t id,uint32_t baud_rate)580 bk_err_t bk_uart_set_baud_rate(uart_id_t id, uint32_t baud_rate)
581 {
582 UART_RETURN_ON_NOT_INIT();
583 UART_RETURN_ON_INVALID_ID(id);
584 UART_RETURN_ON_BAUD_RATE_NOT_SUPPORT(baud_rate);
585 uint32_t uart_clk = clk_get_uart_clk(id);
586 uart_hal_set_baud_rate(&s_uart[id].hal, id, uart_clk, baud_rate);
587 return BK_OK;
588 }
589
bk_uart_set_data_bits(uart_id_t id,uart_data_bits_t data_bits)590 bk_err_t bk_uart_set_data_bits(uart_id_t id, uart_data_bits_t data_bits)
591 {
592 UART_RETURN_ON_NOT_INIT();
593 UART_RETURN_ON_INVALID_ID(id);
594 uart_hal_set_data_bits(&s_uart[id].hal, id, data_bits);
595 return BK_OK;
596 }
597
bk_uart_set_stop_bits(uart_id_t id,uart_stop_bits_t stop_bits)598 bk_err_t bk_uart_set_stop_bits(uart_id_t id, uart_stop_bits_t stop_bits)
599 {
600 UART_RETURN_ON_NOT_INIT();
601 UART_RETURN_ON_INVALID_ID(id);
602 uart_hal_set_stop_bits(&s_uart[id].hal, id, stop_bits);
603 return BK_OK;
604 }
605
bk_uart_set_parity(uart_id_t id,uart_parity_t partiy)606 bk_err_t bk_uart_set_parity(uart_id_t id, uart_parity_t partiy)
607 {
608 UART_RETURN_ON_NOT_INIT();
609 UART_RETURN_ON_INVALID_ID(id);
610 uart_hal_set_parity(&s_uart[id].hal, id, partiy);
611 return BK_OK;
612 }
613
bk_uart_enable_tx_interrupt(uart_id_t id)614 bk_err_t bk_uart_enable_tx_interrupt(uart_id_t id)
615 {
616 UART_RETURN_ON_NOT_INIT();
617 UART_RETURN_ON_INVALID_ID(id);
618 #if (CONFIG_SYSTEM_CTRL)
619 uart_interrupt_enable(id);
620 #else
621 icu_enable_uart_interrupt(id);
622 #endif
623 uart_hal_enable_tx_interrupt(&s_uart[id].hal, id);
624 return BK_OK;
625 }
626
bk_uart_disable_tx_interrupt(uart_id_t id)627 bk_err_t bk_uart_disable_tx_interrupt(uart_id_t id)
628 {
629 UART_RETURN_ON_NOT_INIT();
630 UART_RETURN_ON_INVALID_ID(id);
631 uart_hal_disable_tx_interrupt(&s_uart[id].hal, id);
632 uart_hal_clear_id_tx_interrupt_status(&s_uart[id].hal, id);
633 return BK_OK;
634 }
635
bk_uart_enable_rx_interrupt(uart_id_t id)636 bk_err_t bk_uart_enable_rx_interrupt(uart_id_t id)
637 {
638 UART_RETURN_ON_NOT_INIT();
639 UART_RETURN_ON_INVALID_ID(id);
640 #if (CONFIG_SYSTEM_CTRL)
641 uart_interrupt_enable(id);
642 #else
643 icu_enable_uart_interrupt(id);
644 #endif
645 uart_hal_enable_rx_interrupt(&s_uart[id].hal, id);
646 return BK_OK;
647 }
648
bk_uart_disable_rx_interrupt(uart_id_t id)649 bk_err_t bk_uart_disable_rx_interrupt(uart_id_t id)
650 {
651 UART_RETURN_ON_NOT_INIT();
652 UART_RETURN_ON_INVALID_ID(id);
653 uart_hal_disable_rx_interrupt(&s_uart[id].hal, id);
654 uart_hal_clear_id_rx_interrupt_status(&s_uart[id].hal, id);
655 return BK_OK;
656 }
657
bk_uart_register_rx_isr(uart_id_t id,uart_isr_t isr,void * param)658 bk_err_t bk_uart_register_rx_isr(uart_id_t id, uart_isr_t isr, void *param)
659 {
660 UART_RETURN_ON_NOT_INIT();
661 UART_RETURN_ON_INVALID_ID(id);
662
663 GLOBAL_INT_DECLARATION();
664 GLOBAL_INT_DISABLE();
665 s_uart_rx_isr[id].callback = isr;
666 s_uart_rx_isr[id].param = param;
667 GLOBAL_INT_RESTORE();
668
669 return BK_OK;
670 }
671
bk_uart_register_tx_isr(uart_id_t id,uart_isr_t isr,void * param)672 bk_err_t bk_uart_register_tx_isr(uart_id_t id, uart_isr_t isr, void *param)
673 {
674 UART_RETURN_ON_NOT_INIT();
675 UART_RETURN_ON_INVALID_ID(id);
676
677 GLOBAL_INT_DECLARATION();
678 GLOBAL_INT_DISABLE();
679 s_uart_tx_isr[id].callback = isr;
680 s_uart_tx_isr[id].param = param;
681 GLOBAL_INT_RESTORE();
682
683 return BK_OK;
684 }
685
686 static uart_callback_t s_last_uart_rx_isr = {0};
687
bk_uart_take_rx_isr(uart_id_t id,uart_isr_t isr,void * param)688 bk_err_t bk_uart_take_rx_isr(uart_id_t id, uart_isr_t isr, void *param)
689 {
690 s_last_uart_rx_isr.callback = s_uart_rx_isr[id].callback;
691 s_last_uart_rx_isr.param = s_uart_rx_isr[id].param;
692
693 BK_RETURN_ON_ERR(bk_uart_disable_sw_fifo(id));
694 BK_RETURN_ON_ERR(bk_uart_register_rx_isr(id, isr, param));
695
696 return BK_OK;
697 }
698
bk_uart_recover_rx_isr(uart_id_t id)699 bk_err_t bk_uart_recover_rx_isr(uart_id_t id)
700 {
701 bk_uart_register_rx_isr(id, s_last_uart_rx_isr.callback, s_last_uart_rx_isr.param);
702
703 #if (!CONFIG_SHELL_ASYNCLOG)
704 bk_uart_enable_sw_fifo(id);
705 #else
706 if (id != bk_get_printf_port()) {
707 bk_uart_enable_sw_fifo(id);
708 }
709 #endif
710
711 s_last_uart_rx_isr.callback = NULL;
712 s_last_uart_rx_isr.param = NULL;
713
714 return BK_OK;
715 }
716
bk_uart_write_bytes(uart_id_t id,const void * data,uint32_t size)717 bk_err_t bk_uart_write_bytes(uart_id_t id, const void *data, uint32_t size)
718 {
719 UART_RETURN_ON_NOT_INIT();
720 UART_RETURN_ON_INVALID_ID(id);
721 UART_RETURN_ON_ID_NOT_INIT(id);
722 for (int i = 0; i < size; i++) {
723 uart_write_byte(id, ((uint8 *)data)[i]);
724 }
725 return BK_OK;
726 }
727
bk_uart_read_bytes(uart_id_t id,void * data,uint32_t size,uint32_t timeout_ms)728 bk_err_t bk_uart_read_bytes(uart_id_t id, void *data, uint32_t size, uint32_t timeout_ms)
729 {
730 UART_RETURN_ON_NOT_INIT();
731 UART_RETURN_ON_INVALID_ID(id);
732 UART_RETURN_ON_ID_NOT_INIT(id);
733
734 if (uart_id_is_sw_fifo_enabled(id)) {
735 __attribute__((__unused__)) uart_statis_t* uart_statis = uart_statis_get_statis(id);
736 GLOBAL_INT_DECLARATION();
737 GLOBAL_INT_DISABLE();
738 uint32_t kfifo_data_len = kfifo_data_size(s_uart_rx_kfifo[id]);
739 /* Only kfifo_data_len=0, wait for semaphore */
740 if (kfifo_data_len == 0) {
741 UART_LOGD("kfifo is empty, wait for recv data\r\n");
742 /* when sema_cnt=0, rx_blocked=true, otherwise rx_blocked=false */
743 s_uart_sema[id].rx_blocked = true;
744 GLOBAL_INT_RESTORE();
745
746 uint32_t ret = rtos_get_semaphore(&(s_uart_sema[id].rx_int_sema), timeout_ms);
747 if (ret == kTimeoutErr) {
748 GLOBAL_INT_DISABLE();
749 if (!s_uart_sema[id].rx_blocked) {
750 rtos_get_semaphore(&(s_uart_sema[id].rx_int_sema), 0);
751 }
752 s_uart_sema[id].rx_blocked = false;
753 GLOBAL_INT_RESTORE();
754 UART_LOGW("recv data timeout:%d\n", timeout_ms);
755 UART_STATIS_INC(uart_statis->recv_timeout_cnt);
756 return BK_ERR_UART_RX_TIMEOUT;
757 }
758 } else {
759 GLOBAL_INT_RESTORE();
760 }
761
762 kfifo_data_len = kfifo_data_size(s_uart_rx_kfifo[id]); /* updata kfifo data size */
763 if (size > kfifo_data_len) {
764 if (kfifo_data_len) {
765 kfifo_get(s_uart_rx_kfifo[id], (uint8_t *)data, kfifo_data_len);
766 } else {
767 UART_LOGW("kfifo data is empty\n");
768 UART_STATIS_INC(uart_statis->kfifo_status.empty_cnt);
769 }
770 UART_STATIS_SET(uart_statis->kfifo_status.in, s_uart_rx_kfifo[id]->in);
771 UART_STATIS_SET(uart_statis->kfifo_status.out, s_uart_rx_kfifo[id]->out);
772
773 return kfifo_data_len;
774 }
775 kfifo_get(s_uart_rx_kfifo[id], (uint8_t *)data, size);
776 UART_STATIS_SET(uart_statis->kfifo_status.in, s_uart_rx_kfifo[id]->in);
777 UART_STATIS_SET(uart_statis->kfifo_status.out, s_uart_rx_kfifo[id]->out);
778
779 return size;
780 }else {
781 int ret = 0;
782 uint8_t rx_data;
783 int read_count = 0;
784 uint8_t *read_buffer = (uint8_t *)data;
785 int actual_bytes_to_read = size;
786
787 /* read all data from rx-FIFO. */
788 while (actual_bytes_to_read) {
789 ret = uart_read_byte_ex(id, &rx_data);
790 if (ret == -1)
791 break;
792
793 read_buffer[read_count] = rx_data;
794 read_count++;
795
796 actual_bytes_to_read--;
797 }
798
799 return read_count;
800 }
801
802
803 }
804
bk_uart_set_rx_full_threshold(uart_id_t id,uint8_t threshold)805 bk_err_t bk_uart_set_rx_full_threshold(uart_id_t id, uint8_t threshold)
806 {
807 UART_RETURN_ON_NOT_INIT();
808 UART_RETURN_ON_INVALID_ID(id);
809 uart_hal_set_rx_fifo_threshold(&s_uart[id].hal, id, threshold);
810 return BK_OK;
811 }
812
bk_uart_set_tx_empty_threshold(uart_id_t id,uint8_t threshold)813 bk_err_t bk_uart_set_tx_empty_threshold(uart_id_t id, uint8_t threshold)
814 {
815 UART_RETURN_ON_NOT_INIT();
816 UART_RETURN_ON_INVALID_ID(id);
817 uart_hal_set_tx_fifo_threshold(&s_uart[id].hal, id, threshold);
818 return BK_OK;
819 }
820
bk_uart_set_rx_timeout(uart_id_t id,uart_rx_stop_detect_time_t timeout_thresh)821 bk_err_t bk_uart_set_rx_timeout(uart_id_t id, uart_rx_stop_detect_time_t timeout_thresh)
822 {
823 UART_RETURN_ON_NOT_INIT();
824 UART_RETURN_ON_INVALID_ID(id);
825 uart_hal_set_rx_stop_detect_time(&s_uart[id].hal, id, timeout_thresh);
826 return BK_OK;
827 }
828
bk_uart_disable_rx(uart_id_t id)829 bk_err_t bk_uart_disable_rx(uart_id_t id)
830 {
831 UART_RETURN_ON_NOT_INIT();
832 UART_RETURN_ON_INVALID_ID(id);
833 uart_hal_disable_rx(&s_uart[id].hal, id);
834 uart_deinit_rx_gpio(id);
835
836 return BK_OK;
837 }
838
bk_uart_disable_tx(uart_id_t id)839 bk_err_t bk_uart_disable_tx(uart_id_t id)
840 {
841 UART_RETURN_ON_NOT_INIT();
842 UART_RETURN_ON_INVALID_ID(id);
843 uart_hal_disable_tx(&s_uart[id].hal, id);
844 uart_deinit_tx_gpio(id);
845
846 return BK_OK;
847 }
848
bk_uart_set_enable_rx(uart_id_t id,bool enable)849 bk_err_t bk_uart_set_enable_rx(uart_id_t id, bool enable)
850 {
851 UART_RETURN_ON_NOT_INIT();
852 UART_RETURN_ON_INVALID_ID(id);
853 uart_hal_set_rx_enable(&s_uart[id].hal, id, enable);
854
855 return BK_OK;
856 }
857
bk_uart_set_enable_tx(uart_id_t id,bool enable)858 bk_err_t bk_uart_set_enable_tx(uart_id_t id, bool enable)
859 {
860 UART_RETURN_ON_NOT_INIT();
861 UART_RETURN_ON_INVALID_ID(id);
862 uart_hal_set_tx_enable(&s_uart[id].hal, id, enable);
863
864 return BK_OK;
865 }
866
bk_uart_set_hw_flow_ctrl(uart_id_t id,uint8_t rx_threshold)867 bk_err_t bk_uart_set_hw_flow_ctrl(uart_id_t id, uint8_t rx_threshold)
868 {
869 UART_RETURN_ON_NOT_INIT();
870 UART_RETURN_ON_INVALID_ID(id);
871 uart_hal_set_hw_flow_ctrl(&s_uart[id].hal, id, rx_threshold);
872 uart_hal_enable_flow_control(&s_uart[id].hal, id);
873 return BK_OK;
874 }
875
bk_uart_disable_hw_flow_ctrl(uart_id_t id)876 bk_err_t bk_uart_disable_hw_flow_ctrl(uart_id_t id)
877 {
878 UART_RETURN_ON_NOT_INIT();
879 UART_RETURN_ON_INVALID_ID(id);
880 uart_hal_disable_hw_flow_ctrl(&s_uart[id].hal, id);
881 return BK_OK;
882 }
883
bk_uart_enable_sw_fifo(uart_id_t id)884 bk_err_t bk_uart_enable_sw_fifo(uart_id_t id)
885 {
886 UART_RETURN_ON_INVALID_ID(id);
887 s_uart[id].id_sw_fifo_enable_bits |= BIT(id);
888 return BK_OK;
889 }
890
bk_uart_disable_sw_fifo(uart_id_t id)891 bk_err_t bk_uart_disable_sw_fifo(uart_id_t id)
892 {
893 UART_RETURN_ON_INVALID_ID(id);
894 s_uart[id].id_sw_fifo_enable_bits &= ~BIT(id);
895 return BK_OK;
896 }
897
bk_uart_get_ate_detect_gpio(void)898 uint32_t bk_uart_get_ate_detect_gpio(void)
899 {
900 return uart_hal_get_tx_pin(CONFIG_UART_ATE_PORT);
901 }
902
uart_wait_tx_over(void)903 uint32_t uart_wait_tx_over(void)
904 {
905 return uart_hal_wait_tx_over();
906 }
907
uart_get_int_enable_status(uart_id_t id)908 uint32_t uart_get_int_enable_status(uart_id_t id)
909 {
910 return uart_hal_get_int_enable_status(&s_uart[id].hal, id);
911 }
912
uart_get_interrupt_status(uart_id_t id)913 uint32_t uart_get_interrupt_status(uart_id_t id)
914 {
915 return uart_hal_get_interrupt_status(&s_uart[id].hal, id);
916 }
917
uart_clear_interrupt_status(uart_id_t id,uint32_t int_status)918 void uart_clear_interrupt_status(uart_id_t id, uint32_t int_status)
919 {
920 uart_hal_clear_interrupt_status(&s_uart[id].hal, id, int_status);
921 }
922
923 /* read int enable status
924 * read int status
925 * clear int status
926 */
uart_isr_common(uart_id_t id)927 static void uart_isr_common(uart_id_t id)
928 {
929 uint32_t int_status = 0;
930 uint32_t int_enable_status = 0;
931 uint32_t status = 0;
932 UART_STATIS_DEC();
933
934 int_status = uart_hal_get_interrupt_status(&s_uart[id].hal, id);
935 int_enable_status = uart_hal_get_int_enable_status(&s_uart[id].hal, id);
936 status = int_status & int_enable_status;
937 uart_hal_clear_interrupt_status(&s_uart[id].hal, id, int_status);
938 UART_STATIS_GET(uart_statis, id);
939 UART_STATIS_INC(uart_statis->uart_isr_cnt);
940
941 if (uart_hal_is_rx_interrupt_triggered(&s_uart[id].hal, id, status)) {
942 UART_STATIS_INC(uart_statis->rx_isr_cnt);
943 UART_STATIS_SET(uart_statis->rx_fifo_cnt, uart_hal_get_rx_fifo_cnt(&s_uart[id].hal, id));
944 if (uart_id_is_sw_fifo_enabled(id)) {
945 if (uart_id_read_fifo_frame(id, s_uart_rx_kfifo[id]) > 0) {
946 if (s_uart_sema[id].rx_int_sema && s_uart_sema[id].rx_blocked) {
947 rtos_set_semaphore(&(s_uart_sema[id].rx_int_sema));
948 s_uart_sema[id].rx_blocked = false;
949 }
950 }
951 if (s_uart_rx_isr[id].callback) {
952 s_uart_rx_isr[id].callback(id, s_uart_rx_isr[id].param);
953 }
954 } else {
955 if (s_uart_rx_isr[id].callback) {
956 s_uart_rx_isr[id].callback(id, s_uart_rx_isr[id].param);
957 } else {
958 int ret = 0;
959 uint8_t rx_data;
960
961 /* read all data from rx-FIFO. */
962 while (1) {
963 ret = uart_read_byte_ex(id, &rx_data);
964 if (ret == -1) {
965 break;
966 }
967 }
968 }
969 }
970 }
971 if (uart_hal_is_tx_interrupt_triggered(&s_uart[id].hal, id, status)) {
972 if (s_uart_tx_isr[id].callback) {
973 s_uart_tx_isr[id].callback(id, s_uart_tx_isr[id].param);
974 }
975 }
976 if(uart_hal_is_rx_parity_err_int_triggered(&s_uart[id].hal, id, status)) {
977 uart_hal_flush_fifo(&s_uart[id].hal, id);
978 }
979 }
980
uart0_isr(void)981 void uart0_isr(void)
982 {
983 uart_isr_common(UART_ID_0);
984 }
985
uart1_isr(void)986 void uart1_isr(void)
987 {
988 uart_isr_common(UART_ID_1);
989 }
990
uart2_isr(void)991 void uart2_isr(void)
992 {
993 uart_isr_common(UART_ID_2);
994 }
995
996