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
16 #include <stdio.h>
17 #include "gr55xx.h"
18 #include "app_log.h"
19 #include "los_sem.h"
20 #include "uart.h"
21
22 #define UART_TX_TIMEOUT 1000
23 #define HILOG_IDX 2
24
25 static UINT32 rxSemHandle;
26 static bool uart_initialized = false;
27
uart_callback(app_uart_evt_t * p_evt)28 static void uart_callback(app_uart_evt_t *p_evt)
29 {
30 if ((p_evt->type == APP_UART_EVT_RX_DATA) ||
31 (p_evt->type == APP_UART_EVT_ERROR)) {
32 LOS_SemPost(rxSemHandle);
33 #if (LOSCFG_USE_SHELL == 1)
34 (void)LOS_EventWrite(&g_shellInputEvent, 0x1);
35 #endif
36 }
37 }
38
bsp_uart_init(void)39 void bsp_uart_init(void)
40 {
41 app_uart_tx_buf_t uart_buffer;
42 app_uart_params_t uart_param;
43
44 LOS_BinarySemCreate(0, &rxSemHandle);
45
46 uart_buffer.tx_buf = NULL;
47 uart_buffer.tx_buf_size = 0;
48
49 uart_param.id = LOG_UART_ID;
50 uart_param.init.baud_rate = LOG_UART_BAUDRATE;
51 uart_param.init.data_bits = UART_DATABITS_8;
52 uart_param.init.stop_bits = UART_STOPBITS_1;
53 uart_param.init.parity = UART_PARITY_NONE;
54 uart_param.init.hw_flow_ctrl = UART_HWCONTROL_NONE;
55 uart_param.init.rx_timeout_mode = UART_RECEIVER_TIMEOUT_ENABLE;
56 uart_param.pin_cfg.rx.type = LOG_UART_RX_IO_TYPE;
57 uart_param.pin_cfg.rx.pin = LOG_UART_RX_PIN;
58 uart_param.pin_cfg.rx.mux = LOG_UART_RX_PINMUX;
59 uart_param.pin_cfg.rx.pull = LOG_UART_RX_PULL;
60 uart_param.pin_cfg.tx.type = LOG_UART_TX_IO_TYPE;
61 uart_param.pin_cfg.tx.pin = LOG_UART_TX_PIN;
62 uart_param.pin_cfg.tx.mux = LOG_UART_TX_PINMUX;
63 uart_param.pin_cfg.tx.pull = LOG_UART_TX_PULL;
64 uart_param.use_mode.type = APP_UART_TYPE_INTERRUPT;
65 app_uart_init(&uart_param, uart_callback, &uart_buffer);
66 #if (LOSCFG_USE_SHELL == 1)
67 (void)LOS_EventWrite(&g_shellInputEvent, 0x1);
68 #endif
69 uart_initialized = true;
70 }
71
bsp_uart_send(uint8_t * p_data,uint16_t length)72 void bsp_uart_send(uint8_t *p_data, uint16_t length)
73 {
74 if (uart_initialized != true) {
75 return;
76 }
77
78 app_uart_transmit_sync(LOG_UART_ID, p_data, length, UART_TX_TIMEOUT);
79 }
80
bsp_uart_flush(void)81 void bsp_uart_flush(void)
82 {
83 app_uart_flush(LOG_UART_ID);
84 }
85
bsp_log_init(void)86 void bsp_log_init(void)
87 {
88 app_log_init_t log_init;
89
90 log_init.filter.level = APP_LOG_LVL_DEBUG;
91 log_init.fmt_set[APP_LOG_LVL_ERROR] = APP_LOG_FMT_ALL & (~APP_LOG_FMT_TAG);
92 log_init.fmt_set[APP_LOG_LVL_WARNING] = APP_LOG_FMT_LVL;
93 log_init.fmt_set[APP_LOG_LVL_INFO] = APP_LOG_FMT_LVL;
94 log_init.fmt_set[APP_LOG_LVL_DEBUG] = APP_LOG_FMT_LVL;
95
96 bsp_uart_init();
97 app_log_init(&log_init, bsp_uart_send, bsp_uart_flush);
98 app_assert_init();
99 }
100
HiLogWriteInternal(const char * buffer,size_t bufLen)101 int HiLogWriteInternal(const char *buffer, size_t bufLen)
102 {
103 size_t len = bufLen;
104 if (!buffer) {
105 return -1;
106 }
107 // because it's called as HiLogWriteInternal(buf, strlen(buf) + 1)
108 if (len < HILOG_IDX) {
109 return 0;
110 }
111 if (buffer[len - HILOG_IDX] != '\n') {
112 *((char *)buffer + bufLen - 1) = '\n';
113 } else {
114 len--;
115 }
116 int ret = app_uart_transmit_sync(LOG_UART_ID, buffer, bufLen, UART_TX_TIMEOUT);
117
118 return ret;
119 }
120
_putchar(char character)121 void _putchar(char character)
122 {
123 bsp_uart_send(&character, 1);
124 }
125
UartGetc(void)126 uint8_t UartGetc(void)
127 {
128 uint8_t ch = 0;
129
130 if (uart_initialized != true) {
131 return;
132 }
133 app_uart_receive_async(LOG_UART_ID, &ch, 1);
134 LOS_SemPend(rxSemHandle, LOS_WAIT_FOREVER);
135 return ch;
136 }
137