1 /*
2  * Copyright (c) 2013 - 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 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 /*
27  * @addtogroup debugconsole
28  * @{
29  */
30 
31 /*******************************************************************************
32  * Definitions
33  ******************************************************************************/
34 
35 /*! @brief Definition to select sdk or toolchain printf, scanf. */
36 #ifndef SDK_DEBUGCONSOLE
37 #define SDK_DEBUGCONSOLE 1U
38 #endif
39 
40 #if defined(SDK_DEBUGCONSOLE) && !(SDK_DEBUGCONSOLE)
41 #include <stdio.h>
42 #endif
43 
44 #if SDK_DEBUGCONSOLE /* Select printf, scanf, putchar, getchar of SDK version. */
45 #define PRINTF DbgConsole_Printf
46 #define SCANF DbgConsole_Scanf
47 #define PUTCHAR DbgConsole_Putchar
48 #define GETCHAR DbgConsole_Getchar
49 #else /* Select printf, scanf, putchar, getchar of toolchain. */
50 #define PRINTF printf
51 #define SCANF scanf
52 #define PUTCHAR putchar
53 #define GETCHAR getchar
54 #endif /* SDK_DEBUGCONSOLE */
55 
56 /*******************************************************************************
57  * Prototypes
58  ******************************************************************************/
59 
60 #if defined(__cplusplus)
61 extern "C" {
62 #endif /* __cplusplus */
63 
64 /*! @name Initialization*/
65 /* @{ */
66 
67 /*!
68  * @brief Initializes the the peripheral used for debug messages.
69  *
70  * Call this function to enable debug log messages to be output via the specified peripheral,
71  * frequency of peripheral source clock, and base address at the specified baud rate.
72  * After this function has returned, stdout and stdin are connected to the selected peripheral.
73  *
74  * @param baseAddr      Indicates the address of the peripheral used to send debug messages.
75  * @param baudRate      The desired baud rate in bits per second.
76  * @param device        Low level device type for the debug console, can be one of the following.
77  *                      @arg DEBUG_CONSOLE_DEVICE_TYPE_UART,
78  *                      @arg DEBUG_CONSOLE_DEVICE_TYPE_LPUART,
79  *                      @arg DEBUG_CONSOLE_DEVICE_TYPE_LPSCI,
80  *                      @arg DEBUG_CONSOLE_DEVICE_TYPE_USBCDC.
81  * @param clkSrcFreq    Frequency of peripheral source clock.
82  *
83  * @return              Indicates whether initialization was successful or not.
84  * @retval kStatus_Success          Execution successfully
85  * @retval kStatus_Fail             Execution failure
86  * @retval kStatus_InvalidArgument  Invalid argument existed
87  */
88 status_t DbgConsole_Init(uint32_t baseAddr, uint32_t baudRate, uint8_t device, uint32_t clkSrcFreq);
89 
90 /*!
91  * @brief De-initializes the peripheral used for debug messages.
92  *
93  * Call this function to disable debug log messages to be output via the specified peripheral
94  * base address and at the specified baud rate.
95  *
96  * @return Indicates whether de-initialization was successful or not.
97  */
98 status_t DbgConsole_Deinit(void);
99 
100 /*!
101  * @brief Debug console flush log.
102  *
103  * Call this function to wait the buffer empty and io idle before.
104  * If interrupt transfer is using, make sure the global IRQ is enable before call this function
105  * This function should be called when
106  * 1, before enter power down mode
107  * 2, log is required to print to terminal immediately
108  * @return Indicates whether wait idle was successful or not.
109  */
110 status_t DbgConsole_Flush(void);
111 
112 #if SDK_DEBUGCONSOLE
113 /*!
114  * @brief Writes formatted output to the standard output stream.
115  *
116  * Call this function to write a formatted output to the standard output stream.
117  *
118  * @param   fmt_s Format control string.
119  * @return  Returns the number of characters printed or a negative value if an error occurs.
120  */
121 int DbgConsole_Printf(const char *fmt_s, ...);
122 
123 /*!
124  * @brief Writes a character to stdout.
125  *
126  * Call this function to write a character to stdout.
127  *
128  * @param   ch Character to be written.
129  * @return  Returns the character written.
130  */
131 int DbgConsole_Putchar(int ch);
132 
133 /*!
134  * @brief Reads formatted data from the standard input stream.
135  *
136  * Call this function to read formatted data from the standard input stream.
137  *
138  * @param   fmt_ptr Format control string.
139  * @return  Returns the number of fields successfully converted and assigned.
140  */
141 int DbgConsole_Scanf(char *fmt_ptr, ...);
142 
143 /*!
144  * @brief Reads a character from standard input.
145  *
146  * Call this function to read a character from standard input.
147  *
148  * @return Returns the character read.
149  */
150 int DbgConsole_Getchar(void);
151 
152 #endif /* SDK_DEBUGCONSOLE */
153 
154 /*! @} */
155 
156 #if defined(__cplusplus)
157 }
158 #endif /* __cplusplus */
159 
160 /*! @} */
161 
162 #endif /* _FSL_DEBUGCONSOLE_H_ */
163