1 /*
2  * Copyright (c) 2013 - 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2018, 2020 NXP
4  * All rights reserved.
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 #include "fsl_component_serial_manager.h"
27 
28 /*!
29  * @addtogroup debugconsole
30  * @{
31  */
32 
33 /*******************************************************************************
34  * Definitions
35  ******************************************************************************/
36 
37 extern serial_handle_t g_serialHandle; /*!< serial manager handle */
38 
39 /*! @brief Definition select redirect toolchain printf, scanf to uart or not. */
40 #define DEBUGCONSOLE_REDIRECT_TO_TOOLCHAIN 0U /*!< Select toolchain printf and scanf. */
41 #define DEBUGCONSOLE_REDIRECT_TO_SDK       1U /*!< Select SDK version printf, scanf. */
42 #define DEBUGCONSOLE_DISABLE               2U /*!< Disable debugconsole function. */
43 
44 /*! @brief Definition to select sdk or toolchain printf, scanf. The macro only support
45  * to be redefined in project setting.
46  */
47 #ifndef SDK_DEBUGCONSOLE
48 #define SDK_DEBUGCONSOLE DEBUGCONSOLE_REDIRECT_TO_SDK
49 #endif
50 
51 #if defined(SDK_DEBUGCONSOLE) && !(SDK_DEBUGCONSOLE)
52 #include <stdio.h>
53 #else
54 #include <stdarg.h>
55 #endif
56 
57 /*! @brief Definition to select redirect toolchain printf, scanf to uart or not.
58  *
59  *  if SDK_DEBUGCONSOLE defined to 0,it represents select toolchain printf, scanf.
60  *  if SDK_DEBUGCONSOLE defined to 1,it represents select SDK version printf, scanf.
61  *  if SDK_DEBUGCONSOLE defined to 2,it represents disable debugconsole function.
62  */
63 #if SDK_DEBUGCONSOLE == DEBUGCONSOLE_DISABLE /* Disable debug console */
DbgConsole_Disabled(void)64 static inline int DbgConsole_Disabled(void)
65 {
66     return -1;
67 }
68 #define PRINTF(...)  DbgConsole_Disabled()
69 #define SCANF(...)   DbgConsole_Disabled()
70 #define PUTCHAR(...) DbgConsole_Disabled()
71 #define GETCHAR()    DbgConsole_Disabled()
72 #elif SDK_DEBUGCONSOLE == DEBUGCONSOLE_REDIRECT_TO_SDK /* Select printf, scanf, putchar, getchar of SDK version. */
73 #define PRINTF  DbgConsole_Printf
74 #define SCANF   DbgConsole_Scanf
75 #define PUTCHAR DbgConsole_Putchar
76 #define GETCHAR DbgConsole_Getchar
77 #elif SDK_DEBUGCONSOLE == DEBUGCONSOLE_REDIRECT_TO_TOOLCHAIN /* Select printf, scanf, putchar, getchar of toolchain. \ \
78                                                               */
79 #define PRINTF  printf
80 #define SCANF   scanf
81 #define PUTCHAR putchar
82 #define GETCHAR getchar
83 #endif /* SDK_DEBUGCONSOLE */
84 
85 /*******************************************************************************
86  * Prototypes
87  ******************************************************************************/
88 
89 #if defined(__cplusplus)
90 extern "C" {
91 #endif /* __cplusplus */
92 
93 /*! @name Initialization*/
94 /* @{ */
95 
96 #if ((SDK_DEBUGCONSOLE == DEBUGCONSOLE_REDIRECT_TO_SDK) || defined(SDK_DEBUGCONSOLE_UART))
97 /*!
98  * @brief Initializes the peripheral used for debug messages.
99  *
100  * Call this function to enable debug log messages to be output via the specified peripheral
101  * initialized by the serial manager module.
102  * After this function has returned, stdout and stdin are connected to the selected peripheral.
103  *
104  * @param instance      The instance of the module.If the device is kSerialPort_Uart,
105  *                      the instance is UART peripheral instance. The UART hardware peripheral
106  *                      type is determined by UART adapter. For example, if the instance is 1,
107  *                      if the lpuart_adapter.c is added to the current project, the UART periheral
108  *                      is LPUART1.
109  *                      If the uart_adapter.c is added to the current project, the UART periheral
110  *                      is UART1.
111  * @param baudRate      The desired baud rate in bits per second.
112  * @param device        Low level device type for the debug console, can be one of the following.
113  *                      @arg kSerialPort_Uart,
114  *                      @arg kSerialPort_UsbCdc
115  * @param clkSrcFreq    Frequency of peripheral source clock.
116  *
117  * @return              Indicates whether initialization was successful or not.
118  * @retval kStatus_Success          Execution successfully
119  */
120 status_t DbgConsole_Init(uint8_t instance, uint32_t baudRate, serial_port_type_t device, uint32_t clkSrcFreq);
121 
122 /*!
123  * @brief De-initializes the peripheral used for debug messages.
124  *
125  * Call this function to disable debug log messages to be output via the specified peripheral
126  * initialized by the serial manager module.
127  *
128  * @return Indicates whether de-initialization was successful or not.
129  */
130 status_t DbgConsole_Deinit(void);
131 /*!
132  * @brief Prepares to enter low power consumption.
133  *
134  * This function is used to prepare to enter low power consumption.
135  *
136  * @return Indicates whether de-initialization was successful or not.
137  */
138 status_t DbgConsole_EnterLowpower(void);
139 
140 /*!
141  * @brief Restores from low power consumption.
142  *
143  * This function is used to restore from low power consumption.
144  *
145  * @return Indicates whether de-initialization was successful or not.
146  */
147 status_t DbgConsole_ExitLowpower(void);
148 
149 #else
150 /*!
151  * Use an error to replace the DbgConsole_Init when SDK_DEBUGCONSOLE is not DEBUGCONSOLE_REDIRECT_TO_SDK and
152  * SDK_DEBUGCONSOLE_UART is not defined.
153  */
154 static inline status_t DbgConsole_Init(uint8_t instance,
155                                        uint32_t baudRate,
156                                        serial_port_type_t device,
157                                        uint32_t clkSrcFreq)
158 {
159     (void)instance;
160     (void)baudRate;
161     (void)device;
162     (void)clkSrcFreq;
163     return (status_t)kStatus_Fail;
164 }
165 /*!
166  * Use an error to replace the DbgConsole_Deinit when SDK_DEBUGCONSOLE is not DEBUGCONSOLE_REDIRECT_TO_SDK and
167  * SDK_DEBUGCONSOLE_UART is not defined.
168  */
169 static inline status_t DbgConsole_Deinit(void)
170 {
171     return (status_t)kStatus_Fail;
172 }
173 
174 /*!
175  * Use an error to replace the DbgConsole_EnterLowpower when SDK_DEBUGCONSOLE is not DEBUGCONSOLE_REDIRECT_TO_SDK and
176  * SDK_DEBUGCONSOLE_UART is not defined.
177  */
178 static inline status_t DbgConsole_EnterLowpower(void)
179 {
180     return (status_t)kStatus_Fail;
181 }
182 
183 /*!
184  * Use an error to replace the DbgConsole_ExitLowpower when SDK_DEBUGCONSOLE is not DEBUGCONSOLE_REDIRECT_TO_SDK and
185  * SDK_DEBUGCONSOLE_UART is not defined.
186  */
187 static inline status_t DbgConsole_ExitLowpower(void)
188 {
189     return (status_t)kStatus_Fail;
190 }
191 
192 #endif /* ((SDK_DEBUGCONSOLE == DEBUGCONSOLE_REDIRECT_TO_SDK) || defined(SDK_DEBUGCONSOLE_UART)) */
193 
194 #if (defined(SDK_DEBUGCONSOLE) && (SDK_DEBUGCONSOLE == DEBUGCONSOLE_REDIRECT_TO_SDK))
195 /*!
196  * @brief Writes formatted output to the standard output stream.
197  *
198  * Call this function to write a formatted output to the standard output stream.
199  *
200  * @param   fmt_s Format control string.
201  * @return  Returns the number of characters printed or a negative value if an error occurs.
202  */
203 int DbgConsole_Printf(const char *fmt_s, ...);
204 
205 /*!
206  * @brief Writes formatted output to the standard output stream.
207  *
208  * Call this function to write a formatted output to the standard output stream.
209  *
210  * @param   fmt_s Format control string.
211  * @param   formatStringArg Format arguments.
212  * @return  Returns the number of characters printed or a negative value if an error occurs.
213  */
214 int DbgConsole_Vprintf(const char *fmt_s, va_list formatStringArg);
215 
216 /*!
217  * @brief Writes a character to stdout.
218  *
219  * Call this function to write a character to stdout.
220  *
221  * @param   ch Character to be written.
222  * @return  Returns the character written.
223  */
224 int DbgConsole_Putchar(int ch);
225 
226 /*!
227  * @brief Reads formatted data from the standard input stream.
228  *
229  * Call this function to read formatted data from the standard input stream.
230  *
231  * @note Due the limitation in the BM OSA environment (CPU is blocked in the function,
232  * other tasks will not be scheduled), the function cannot be used when the
233  * DEBUG_CONSOLE_TRANSFER_NON_BLOCKING is set in the BM OSA environment.
234  * And an error is returned when the function called in this case. The suggestion
235  * is that polling the non-blocking function DbgConsole_TryGetchar to get the input char.
236  *
237  * @param   fmt_s Format control string.
238  * @return  Returns the number of fields successfully converted and assigned.
239  */
240 int DbgConsole_Scanf(char *fmt_s, ...);
241 
242 /*!
243  * @brief Reads a character from standard input.
244  *
245  * Call this function to read a character from standard input.
246  *
247  * @note Due the limitation in the BM OSA environment (CPU is blocked in the function,
248  * other tasks will not be scheduled), the function cannot be used when the
249  * DEBUG_CONSOLE_TRANSFER_NON_BLOCKING is set in the BM OSA environment.
250  * And an error is returned when the function called in this case. The suggestion
251  * is that polling the non-blocking function DbgConsole_TryGetchar to get the input char.
252  *
253  * @return Returns the character read.
254  */
255 int DbgConsole_Getchar(void);
256 
257 /*!
258  * @brief Writes formatted output to the standard output stream with the blocking mode.
259  *
260  * Call this function to write a formatted output to the standard output stream with the blocking mode.
261  * The function will send data with blocking mode no matter the DEBUG_CONSOLE_TRANSFER_NON_BLOCKING set
262  * or not.
263  * The function could be used in system ISR mode with DEBUG_CONSOLE_TRANSFER_NON_BLOCKING set.
264  *
265  * @param   fmt_s Format control string.
266  * @return  Returns the number of characters printed or a negative value if an error occurs.
267  */
268 int DbgConsole_BlockingPrintf(const char *fmt_s, ...);
269 
270 /*!
271  * @brief Writes formatted output to the standard output stream with the blocking mode.
272  *
273  * Call this function to write a formatted output to the standard output stream with the blocking mode.
274  * The function will send data with blocking mode no matter the DEBUG_CONSOLE_TRANSFER_NON_BLOCKING set
275  * or not.
276  * The function could be used in system ISR mode with DEBUG_CONSOLE_TRANSFER_NON_BLOCKING set.
277  *
278  * @param   fmt_s Format control string.
279  * @param   formatStringArg Format arguments.
280  * @return  Returns the number of characters printed or a negative value if an error occurs.
281  */
282 int DbgConsole_BlockingVprintf(const char *fmt_s, va_list formatStringArg);
283 
284 /*!
285  * @brief Debug console flush.
286  *
287  * Call this function to wait the tx buffer empty.
288  * If interrupt transfer is using, make sure the global IRQ is enable before call this function
289  * This function should be called when
290  * 1, before enter power down mode
291  * 2, log is required to print to terminal immediately
292  * @return Indicates whether wait idle was successful or not.
293  */
294 status_t DbgConsole_Flush(void);
295 
296 #ifdef DEBUG_CONSOLE_TRANSFER_NON_BLOCKING
297 /*!
298  * @brief Debug console try to get char
299  * This function provides a API which will not block current task, if character is
300  * available return it, otherwise return fail.
301  * @param ch the address of char to receive
302  * @return Indicates get char was successful or not.
303  */
304 status_t DbgConsole_TryGetchar(char *ch);
305 #endif
306 
307 #endif /* SDK_DEBUGCONSOLE */
308 
309 /*! @} */
310 
311 #if defined(__cplusplus)
312 }
313 #endif /* __cplusplus */
314 
315 /*! @} */
316 
317 #endif /* _FSL_DEBUGCONSOLE_H_ */
318