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 "at_printf.h"
17
18 #include <hi3861_platform_base.h>
19 #include <serial_dw.h>
20 #include <stdarg.h>
21
22 #include "hi_at.h"
23
24 uintptr_t g_at_uart_baseaddr = HI_UART1_REG_BASE;
25 uintptr_t g_sysinfo_uart_baseaddr = HI_UART0_REG_BASE;
26
27 /* send */
serial_putc_at(hi_char c)28 hi_void serial_putc_at(hi_char c)
29 {
30 /* Wait until there is space in the FIFO */
31 while ((hi_reg_read_val16(g_at_uart_baseaddr + UART_FR) & UARTFR_TXFF_MASK) != 0) {}
32
33 /* Send the character */
34 hi_reg_write16(g_at_uart_baseaddr + UART_DR, (hi_uchar)c);
35 }
36
uart_puts_at(const hi_char * ptr,hi_u32 len,const hi_void * state)37 hi_s32 uart_puts_at(const hi_char *ptr, hi_u32 len, const hi_void *state)
38 {
39 hi_s32 len_ret = (hi_s32)len;
40 hi_at_output_func at_output_func = hi_at_get_register_output_func();
41
42 (hi_void)state;
43 while (len-- && (*ptr != 0)) {
44 if (*ptr == '\n') {
45 if (at_output_func == HI_NULL) {
46 serial_putc_at(*ptr++);
47 } else {
48 at_output_func((const hi_u8*)ptr++, 1);
49 }
50 } else {
51 if (at_output_func == HI_NULL) {
52 serial_putc_at(*ptr++);
53 } else {
54 at_output_func((const hi_u8*)ptr++, 1);
55 }
56 }
57 }
58 return len_ret;
59 }
60
61
hi_at_printf(const hi_char * fmt,...)62 hi_s32 hi_at_printf(const hi_char *fmt, ...)
63 {
64 va_list ap = 0;
65 hi_s32 len;
66 va_start(ap, fmt);
67
68 #if defined(CONFIG_AT_COMMAND) || defined(CONFIG_FACTORY_TEST_MODE)
69 OUTPUT_FUNC fn_put = (OUTPUT_FUNC)uart_puts_at;
70 len = __dprintf(fmt, ap, fn_put, (void *)NULL);
71 #else
72 len = 0;
73 #endif
74
75 va_end(ap);
76 return len;
77 }
78
79 /* send */
serial_putc_crashinfo(hi_char c)80 hi_void serial_putc_crashinfo(hi_char c)
81 {
82 #if defined(CONFIG_AT_COMMAND) || defined(CONFIG_FACTORY_TEST_MODE)
83 /* Wait until there is space in the FIFO */
84 while ((hi_reg_read_val16(g_at_uart_baseaddr + UART_FR) & UARTFR_TXFF_MASK) != 0) {}
85
86 /* Send the character */
87 hi_reg_write16(g_at_uart_baseaddr + UART_DR, (hi_uchar)c);
88 #else
89 /* Wait until there is space in the FIFO */
90 while ((hi_reg_read_val16(g_sysinfo_uart_baseaddr + UART_FR) & UARTFR_TXFF_MASK) != 0) {}
91
92 /* Send the character */
93 hi_reg_write16(g_sysinfo_uart_baseaddr + UART_DR, (hi_uchar)c);
94 #endif
95 }
96
uart_puts_sysinfo(const hi_char * ptr,hi_u32 len,const hi_void * state)97 hi_s32 uart_puts_sysinfo(const hi_char *ptr, hi_u32 len, const hi_void *state)
98 {
99 hi_s32 len_ret = (hi_s32)len;
100 hi_at_output_func at_output_func = hi_at_get_register_output_func();
101
102 (hi_void)state;
103 while (len-- && (*ptr != 0)) {
104 if (*ptr == '\n') {
105 if (at_output_func == HI_NULL) {
106 serial_putc_crashinfo('\r');
107 serial_putc_crashinfo(*ptr++);
108 } else {
109 #if defined(CONFIG_AT_COMMAND) || defined(CONFIG_FACTORY_TEST_MODE)
110 hi_char tmp_char = '\r';
111 at_output_func((const hi_u8*)&tmp_char, 1);
112 at_output_func((const hi_u8*)ptr++, 1);
113 #else
114 serial_putc_crashinfo('\r');
115 serial_putc_crashinfo(*ptr++);
116 #endif
117 }
118 } else {
119 if (at_output_func == HI_NULL) {
120 serial_putc_crashinfo(*ptr++);
121 } else {
122 #if defined(CONFIG_AT_COMMAND) || defined(CONFIG_FACTORY_TEST_MODE)
123 at_output_func((const hi_u8*)ptr++, 1);
124 #else
125 serial_putc_crashinfo(*ptr++);
126 #endif
127 }
128 }
129 }
130 return len_ret;
131 }
132
hi_at_printf_crashinfo(const hi_char * fmt,...)133 hi_s32 hi_at_printf_crashinfo(const hi_char *fmt, ...)
134 {
135 va_list ap = 0;
136 hi_s32 len;
137 va_start(ap, fmt);
138
139 OUTPUT_FUNC fn_put = (OUTPUT_FUNC)uart_puts_sysinfo;
140 len = __dprintf(fmt, ap, fn_put, (void *)NULL);
141
142 va_end(ap);
143 return len;
144 }
145
146