fsl_debug_console.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2013 - 2015, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. * Debug console shall provide input and output functions to scan and print formatted data.
  35. * o Support a format specifier for PRINTF follows this prototype "%[flags][width][.precision][length]specifier"
  36. * - [flags] :'-', '+', '#', ' ', '0'
  37. * - [width]: number (0,1...)
  38. * - [.precision]: number (0,1...)
  39. * - [length]: do not support
  40. * - [specifier]: 'd', 'i', 'f', 'F', 'x', 'X', 'o', 'p', 'u', 'c', 's', 'n'
  41. * o Support a format specifier for SCANF follows this prototype " %[*][width][length]specifier"
  42. * - [*]: is supported.
  43. * - [width]: number (0,1...)
  44. * - [length]: 'h', 'hh', 'l','ll','L'. ignore ('j','z','t')
  45. * - [specifier]: 'd', 'i', 'u', 'f', 'F', 'e', 'E', 'g', 'G', 'a', 'A', 'o', 'c', 's'
  46. */
  47. #ifndef _FSL_DEBUGCONSOLE_H_
  48. #define _FSL_DEBUGCONSOLE_H_
  49. #include "fsl_common.h"
  50. /*!
  51. * @addtogroup debugconsole
  52. * @{
  53. */
  54. /*******************************************************************************
  55. * Definitions
  56. ******************************************************************************/
  57. /*! @brief Definition to select sdk or toolchain printf, scanf. */
  58. #ifndef SDK_DEBUGCONSOLE
  59. #define SDK_DEBUGCONSOLE 1U
  60. #endif
  61. /*! @brief Definition to select redirect toolchain printf, scanf to uart or not. */
  62. #ifndef SDK_DEBUGCONSOLE_UART
  63. /* mcux will handle this macro, not define it here */
  64. #if (!defined(__MCUXPRESSO))
  65. #define SDK_DEBUGCONSOLE_UART
  66. #endif
  67. #endif
  68. #if defined(SDK_DEBUGCONSOLE) && !(SDK_DEBUGCONSOLE)
  69. #include <stdio.h>
  70. #endif
  71. #if SDK_DEBUGCONSOLE /* Select printf, scanf, putchar, getchar of SDK version. */
  72. #define PRINTF DbgConsole_Printf
  73. #define SCANF DbgConsole_Scanf
  74. #define PUTCHAR DbgConsole_Putchar
  75. #define GETCHAR DbgConsole_Getchar
  76. #else /* Select printf, scanf, putchar, getchar of toolchain. */
  77. #define PRINTF printf
  78. #define SCANF scanf
  79. #define PUTCHAR putchar
  80. #define GETCHAR getchar
  81. #endif /* SDK_DEBUGCONSOLE */
  82. /*******************************************************************************
  83. * Prototypes
  84. ******************************************************************************/
  85. #if defined(__cplusplus)
  86. extern "C" {
  87. #endif /* __cplusplus */
  88. /*! @name Initialization*/
  89. /* @{ */
  90. /*!
  91. * @brief Initializes the peripheral used for debug messages.
  92. *
  93. * Call this function to enable debug log messages to be output via the specified peripheral,
  94. * frequency of peripheral source clock, and base address at the specified baud rate.
  95. * After this function has returned, stdout and stdin are connected to the selected peripheral.
  96. *
  97. * @param baseAddr Indicates the address of the peripheral used to send debug messages.
  98. * @param baudRate The desired baud rate in bits per second.
  99. * @param device Low level device type for the debug console, can be one of the following.
  100. * @arg DEBUG_CONSOLE_DEVICE_TYPE_UART,
  101. * @arg DEBUG_CONSOLE_DEVICE_TYPE_LPUART,
  102. * @arg DEBUG_CONSOLE_DEVICE_TYPE_LPSCI,
  103. * @arg DEBUG_CONSOLE_DEVICE_TYPE_USBCDC.
  104. * @param clkSrcFreq Frequency of peripheral source clock.
  105. *
  106. * @return Indicates whether initialization was successful or not.
  107. * @retval kStatus_Success Execution successfully
  108. * @retval kStatus_Fail Execution failure
  109. * @retval kStatus_InvalidArgument Invalid argument existed
  110. */
  111. status_t DbgConsole_Init(uint32_t baseAddr, uint32_t baudRate, uint8_t device, uint32_t clkSrcFreq);
  112. /*!
  113. * @brief De-initializes the peripheral used for debug messages.
  114. *
  115. * Call this function to disable debug log messages to be output via the specified peripheral
  116. * base address and at the specified baud rate.
  117. *
  118. * @return Indicates whether de-initialization was successful or not.
  119. */
  120. status_t DbgConsole_Deinit(void);
  121. #if SDK_DEBUGCONSOLE
  122. /*!
  123. * @brief Writes formatted output to the standard output stream.
  124. *
  125. * Call this function to write a formatted output to the standard output stream.
  126. *
  127. * @param fmt_s Format control string.
  128. * @return Returns the number of characters printed or a negative value if an error occurs.
  129. */
  130. int DbgConsole_Printf(const char *fmt_s, ...);
  131. /*!
  132. * @brief Writes a character to stdout.
  133. *
  134. * Call this function to write a character to stdout.
  135. *
  136. * @param ch Character to be written.
  137. * @return Returns the character written.
  138. */
  139. int DbgConsole_Putchar(int ch);
  140. /*!
  141. * @brief Reads formatted data from the standard input stream.
  142. *
  143. * Call this function to read formatted data from the standard input stream.
  144. *
  145. * @param fmt_ptr Format control string.
  146. * @return Returns the number of fields successfully converted and assigned.
  147. */
  148. int DbgConsole_Scanf(char *fmt_ptr, ...);
  149. /*!
  150. * @brief Reads a character from standard input.
  151. *
  152. * Call this function to read a character from standard input.
  153. *
  154. * @return Returns the character read.
  155. */
  156. int DbgConsole_Getchar(void);
  157. /*!
  158. * @brief Debug console flush log.
  159. *
  160. * Call this function to wait the buffer empty and io idle before.
  161. * If interrupt transfer is using, make sure the global IRQ is enable before call this function
  162. * This function should be called when
  163. * 1, before enter power down mode
  164. * 2, log is required to print to terminal immediately
  165. * @return Indicates whether wait idle was successful or not.
  166. */
  167. status_t DbgConsole_Flush(void);
  168. #ifdef DEBUG_CONSOLE_TRANSFER_NON_BLOCKING
  169. /*!
  170. * @brief Debug console try to get char
  171. * This function provide a api which will not block current task, if character is
  172. * avaliable return it , otherwise return fail.
  173. * @param ch the address of char to receive
  174. * @return Indicates get char was successful or not.
  175. */
  176. status_t DbgConsole_TryGetchar(char *ch);
  177. #endif
  178. #endif /* SDK_DEBUGCONSOLE */
  179. /*! @} */
  180. #if defined(__cplusplus)
  181. }
  182. #endif /* __cplusplus */
  183. /*! @} */
  184. #endif /* _FSL_DEBUGCONSOLE_H_ */