1 /*
2 * Copyright 2017-2018, 2020, 2022 NXP
3 * All rights reserved.
4 *
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 *
8 * Debug console shall provide input and output functions to scan and print formatted data.
9 * o Support a format specifier for PRINTF follows this prototype "%[flags][width][.precision][length]specifier"
10 * - [flags] :'-', '+', '#', ' ', '0'
11 * - [width]: number (0,1...)
12 * - [.precision]: number (0,1...)
13 * - [length]: do not support
14 * - [specifier]: 'd', 'i', 'f', 'F', 'x', 'X', 'o', 'p', 'u', 'c', 's', 'n'
15 * o Support a format specifier for SCANF follows this prototype " %[*][width][length]specifier"
16 * - [*]: is supported.
17 * - [width]: number (0,1...)
18 * - [length]: 'h', 'hh', 'l','ll','L'. ignore ('j','z','t')
19 * - [specifier]: 'd', 'i', 'u', 'f', 'F', 'e', 'E', 'g', 'G', 'a', 'A', 'o', 'c', 's'
20 */
21
22 #ifndef _FSL_DEBUGCONSOLE_H_
23 #define _FSL_DEBUGCONSOLE_H_
24
25 #include "fsl_common.h"
26
27 /*!
28 * @addtogroup debugconsolelite
29 * @{
30 */
31
32 /*******************************************************************************
33 * Definitions
34 ******************************************************************************/
35
36 /*! @brief Definition select redirect toolchain printf, scanf to uart or not. */
37 #define DEBUGCONSOLE_REDIRECT_TO_TOOLCHAIN 0U /*!< Select toolchain printf and scanf. */
38 #define DEBUGCONSOLE_REDIRECT_TO_SDK 1U /*!< Select SDK version printf, scanf. */
39 #define DEBUGCONSOLE_DISABLE 2U /*!< Disable debugconsole function. */
40
41 /*! @brief Definition to select sdk or toolchain printf, scanf. */
42 #ifndef SDK_DEBUGCONSOLE
43 #define SDK_DEBUGCONSOLE DEBUGCONSOLE_REDIRECT_TO_SDK
44 #endif
45
46 #if defined(SDK_DEBUGCONSOLE) && !(SDK_DEBUGCONSOLE)
47 #include <stdio.h>
48 #else
49 #include <stdarg.h>
50 #endif
51
52 /*! @brief Definition to printf the float number. */
53 #ifndef PRINTF_FLOAT_ENABLE
54 #define PRINTF_FLOAT_ENABLE 0U
55 #endif /* PRINTF_FLOAT_ENABLE */
56
57 /*! @brief Definition to scanf the float number. */
58 #ifndef SCANF_FLOAT_ENABLE
59 #define SCANF_FLOAT_ENABLE 0U
60 #endif /* SCANF_FLOAT_ENABLE */
61
62 /*! @brief Definition to support advanced format specifier for printf. */
63 #ifndef PRINTF_ADVANCED_ENABLE
64 #define PRINTF_ADVANCED_ENABLE 0U
65 #endif /* PRINTF_ADVANCED_ENABLE */
66
67 /*! @brief Definition to support advanced format specifier for scanf. */
68 #ifndef SCANF_ADVANCED_ENABLE
69 #define SCANF_ADVANCED_ENABLE 0U
70 #endif /* SCANF_ADVANCED_ENABLE */
71
72 /*! @brief Definition to select redirect toolchain printf, scanf to uart or not.
73 *
74 * if SDK_DEBUGCONSOLE defined to 0,it represents select toolchain printf, scanf.
75 * if SDK_DEBUGCONSOLE defined to 1,it represents select SDK version printf, scanf.
76 * if SDK_DEBUGCONSOLE defined to 2,it represents disable debugconsole function.
77 */
78 #if SDK_DEBUGCONSOLE == DEBUGCONSOLE_DISABLE /* Disable debug console */
DbgConsole_Disabled(void)79 static inline int DbgConsole_Disabled(void)
80 {
81 return -1;
82 }
83 #define PRINTF(...) DbgConsole_Disabled()
84 #define SCANF(...) DbgConsole_Disabled()
85 #define PUTCHAR(...) DbgConsole_Disabled()
86 #define GETCHAR() DbgConsole_Disabled()
87 #elif SDK_DEBUGCONSOLE == DEBUGCONSOLE_REDIRECT_TO_SDK /* Select printf, scanf, putchar, getchar of SDK version. */
88 #define PRINTF DbgConsole_Printf
89 #define SCANF DbgConsole_Scanf
90 #define PUTCHAR DbgConsole_Putchar
91 #define GETCHAR DbgConsole_Getchar
92 #elif SDK_DEBUGCONSOLE == \
93 DEBUGCONSOLE_REDIRECT_TO_TOOLCHAIN /* Select printf, scanf, putchar, getchar of toolchain. \ */
94 #define PRINTF printf
95 #define SCANF scanf
96 #define PUTCHAR putchar
97 #define GETCHAR getchar
98 #endif /* SDK_DEBUGCONSOLE */
99 /*! @} */
100
101 /*! @brief serial port type
102 *
103 * The serial port type aligned with the definition in serial manager, but please note
104 * only kSerialPort_Uart can be supported in debug console lite.
105 */
106 #ifndef _SERIAL_PORT_T_
107 #define _SERIAL_PORT_T_
108 typedef enum _serial_port_type
109 {
110 kSerialPort_None = 0U, /*!< Serial port is none */
111 kSerialPort_Uart = 1U, /*!< Serial port UART */
112 kSerialPort_UsbCdc, /*!< Serial port USB CDC */
113 kSerialPort_Swo, /*!< Serial port SWO */
114 kSerialPort_Virtual, /*!< Serial port Virtual */
115 kSerialPort_Rpmsg, /*!< Serial port RPMSG */
116 kSerialPort_UartDma, /*!< Serial port UART DMA*/
117 kSerialPort_SpiMaster, /*!< Serial port SPIMASTER*/
118 kSerialPort_SpiSlave, /*!< Serial port SPISLAVE*/
119 } serial_port_type_t;
120 #endif
121
122 /*!
123 * @addtogroup debugconsolelite
124 * @{
125 */
126 /*******************************************************************************
127 * Prototypes
128 ******************************************************************************/
129
130 #if defined(__cplusplus)
131 extern "C" {
132 #endif /* __cplusplus */
133
134 /*! @name Initialization*/
135 /* @{ */
136
137 #if ((SDK_DEBUGCONSOLE == DEBUGCONSOLE_REDIRECT_TO_SDK) || defined(SDK_DEBUGCONSOLE_UART))
138 /*!
139 * @brief Initializes the peripheral used for debug messages.
140 *
141 * Call this function to enable debug log messages to be output via the specified peripheral,
142 * frequency of peripheral source clock, and base address at the specified baud rate.
143 * After this function has returned, stdout and stdin are connected to the selected peripheral.
144 *
145 * @param instance The instance of the module.If the device is kSerialPort_Uart,
146 * the instance is UART peripheral instance. The UART hardware peripheral
147 * type is determined by UART adapter. For example, if the instance is 1,
148 * if the lpuart_adapter.c is added to the current project, the UART periheral
149 * is LPUART1.
150 * If the uart_adapter.c is added to the current project, the UART periheral
151 * is UART1.
152 * @param baudRate The desired baud rate in bits per second.
153 * @param device Low level device type for the debug console, can be one of the following.
154 * @arg kSerialPort_Uart.
155 * @param clkSrcFreq Frequency of peripheral source clock.
156 *
157 * @return Indicates whether initialization was successful or not.
158 * @retval kStatus_Success Execution successfully
159 * @retval kStatus_Fail Execution failure
160 */
161 status_t DbgConsole_Init(uint8_t instance, uint32_t baudRate, serial_port_type_t device, uint32_t clkSrcFreq);
162
163 /*!
164 * @brief De-initializes the peripheral used for debug messages.
165 *
166 * Call this function to disable debug log messages to be output via the specified peripheral
167 * base address and at the specified baud rate.
168 *
169 * @return Indicates whether de-initialization was successful or not.
170 */
171 status_t DbgConsole_Deinit(void);
172 /*!
173 * @brief Prepares to enter low power consumption.
174 *
175 * This function is used to prepare to enter low power consumption.
176 *
177 * @return Indicates whether de-initialization was successful or not.
178 */
179 status_t DbgConsole_EnterLowpower(void);
180
181 /*!
182 * @brief Restores from low power consumption.
183 *
184 * This function is used to restore from low power consumption.
185 *
186 * @return Indicates whether de-initialization was successful or not.
187 */
188 status_t DbgConsole_ExitLowpower(void);
189
190 #else
191 /*!
192 * Use an error to replace the DbgConsole_Init when SDK_DEBUGCONSOLE is not DEBUGCONSOLE_REDIRECT_TO_SDK and
193 * SDK_DEBUGCONSOLE_UART is not defined.
194 */
195 static inline status_t DbgConsole_Init(uint8_t instance,
196 uint32_t baudRate,
197 serial_port_type_t device,
198 uint32_t clkSrcFreq)
199 {
200 (void)instance;
201 (void)baudRate;
202 (void)device;
203 (void)clkSrcFreq;
204 return (status_t)kStatus_Fail;
205 }
206 /*!
207 * Use an error to replace the DbgConsole_Deinit when SDK_DEBUGCONSOLE is not DEBUGCONSOLE_REDIRECT_TO_SDK and
208 * SDK_DEBUGCONSOLE_UART is not defined.
209 */
210 static inline status_t DbgConsole_Deinit(void)
211 {
212 return (status_t)kStatus_Fail;
213 }
214
215 /*!
216 * Use an error to replace the DbgConsole_EnterLowpower when SDK_DEBUGCONSOLE is not DEBUGCONSOLE_REDIRECT_TO_SDK and
217 * SDK_DEBUGCONSOLE_UART is not defined.
218 */
219 static inline status_t DbgConsole_EnterLowpower(void)
220 {
221 return (status_t)kStatus_Fail;
222 }
223
224 /*!
225 * Use an error to replace the DbgConsole_ExitLowpower when SDK_DEBUGCONSOLE is not DEBUGCONSOLE_REDIRECT_TO_SDK and
226 * SDK_DEBUGCONSOLE_UART is not defined.
227 */
228 static inline status_t DbgConsole_ExitLowpower(void)
229 {
230 return (status_t)kStatus_Fail;
231 }
232
233 #endif /* ((SDK_DEBUGCONSOLE == DEBUGCONSOLE_REDIRECT_TO_SDK) || defined(SDK_DEBUGCONSOLE_UART)) */
234
235 #if (defined(SDK_DEBUGCONSOLE) && (SDK_DEBUGCONSOLE == DEBUGCONSOLE_REDIRECT_TO_SDK))
236 /*!
237 * @brief Writes formatted output to the standard output stream.
238 *
239 * Call this function to write a formatted output to the standard output stream.
240 *
241 * @param fmt_s Format control string.
242 * @return Returns the number of characters printed or a negative value if an error occurs.
243 */
244 int DbgConsole_Printf(const char *fmt_s, ...);
245
246 /*!
247 * @brief Writes formatted output to the standard output stream.
248 *
249 * Call this function to write a formatted output to the standard output stream.
250 *
251 * @param fmt_s Format control string.
252 * @param formatStringArg Format arguments.
253 * @return Returns the number of characters printed or a negative value if an error occurs.
254 */
255 int DbgConsole_Vprintf(const char *fmt_s, va_list formatStringArg);
256
257 /*!
258 * @brief Writes a character to stdout.
259 *
260 * Call this function to write a character to stdout.
261 *
262 * @param ch Character to be written.
263 * @return Returns the character written.
264 */
265 int DbgConsole_Putchar(int ch);
266
267 /*!
268 * @brief Reads formatted data from the standard input stream.
269 *
270 * Call this function to read formatted data from the standard input stream.
271 *
272 * @param fmt_s Format control string.
273 * @return Returns the number of fields successfully converted and assigned.
274 */
275 int DbgConsole_Scanf(char *fmt_s, ...);
276
277 /*!
278 * @brief Reads a character from standard input.
279 *
280 * Call this function to read a character from standard input.
281 *
282 * @return Returns the character read.
283 */
284 int DbgConsole_Getchar(void);
285
286 #endif /* SDK_DEBUGCONSOLE */
287
288 /*! @} */
289
290 #if defined(__cplusplus)
291 }
292 #endif /* __cplusplus */
293
294 /*! @} */
295 #endif /* _FSL_DEBUGCONSOLE_H_ */
296