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 <stdarg.h>
16 #include <driver/uart.h>
17 #include "bk_uart.h"
18 #include <common/sys_config.h>
19 #include <os/mem.h>
20 #include "printf_impl.h"
21
22
23 void bk_set_printf_port(uint8_t port_num);
24 int bk_get_printf_port(void);
25
26 static bool s_printf_init = false;
27 static uint8_t s_print_port = CONFIG_UART_PRINT_PORT;
28
printf_is_init(void)29 bool printf_is_init(void)
30 {
31 return s_printf_init;
32 }
33
bk_printf_deinit(void)34 bk_err_t bk_printf_deinit(void)
35 {
36 s_printf_init = false;
37 #if (!CONFIG_SLAVE_CORE)
38 bk_uart_deinit(bk_get_printf_port());
39 #endif
40 printf_lock_deinit();
41 return BK_OK;
42 }
43
bk_printf_init(void)44 bk_err_t bk_printf_init(void)
45 {
46 int ret;
47
48 #if (!CONFIG_SLAVE_CORE)
49 const uart_config_t config = {
50 .baud_rate = UART_BAUD_RATE,
51 .data_bits = UART_DATA_8_BITS,
52 .parity = UART_PARITY_NONE,
53 .stop_bits = UART_STOP_BITS_1,
54 .flow_ctrl = UART_FLOWCTRL_DISABLE,
55 .src_clk = UART_SCLK_XTAL_26M
56 };
57 #endif
58
59 ret = printf_lock_init();
60 if (BK_OK != ret) {
61 return ret;
62 }
63
64 #if (!CONFIG_SLAVE_CORE)
65 ret = bk_uart_init(bk_get_printf_port(), &config);
66 if (BK_OK != ret)
67 goto _bk_printf_init_fail;
68
69 #endif
70 s_printf_init = true;
71
72 return BK_OK;
73
74 #if (!CONFIG_SLAVE_CORE)
75 _bk_printf_init_fail:
76 bk_printf_deinit();
77 return ret;
78 #endif
79 }
80
bk_null_printf(const char * fmt,...)81 void bk_null_printf(const char *fmt, ...)
82 {
83 }
84
bk_get_printf_port(void)85 int bk_get_printf_port(void) {
86 return s_print_port;
87 }
88
bk_set_printf_port(uint8_t port_num)89 void bk_set_printf_port(uint8_t port_num) {
90 #if (!CONFIG_SLAVE_CORE)
91 if (s_print_port != port_num && port_num < UART_ID_MAX) {
92 bk_printf_deinit();
93 s_print_port = port_num;
94 bk_printf_init();
95 }
96 #endif
97 }
98