fsl_debug_console.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * This is a modified version of the file printf.c, which was distributed
  3. * by Motorola as part of the M5407C3BOOT.zip package used to initialize
  4. * the M5407C3 evaluation board.
  5. *
  6. * Copyright:
  7. * 1999-2000 MOTOROLA, INC. All Rights Reserved.
  8. * You are hereby granted a copyright license to use, modify, and
  9. * distribute the SOFTWARE so long as this entire notice is
  10. * retained without alteration in any modified and/or redistributed
  11. * versions, and that such modified versions are clearly identified
  12. * as such. No licenses are granted by implication, estoppel or
  13. * otherwise under any patents or trademarks of Motorola, Inc. This
  14. * software is provided on an "AS IS" basis and without warranty.
  15. *
  16. * To the maximum extent permitted by applicable law, MOTOROLA
  17. * DISCLAIMS ALL WARRANTIES WHETHER EXPRESS OR IMPLIED, INCLUDING
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
  19. * PURPOSE AND ANY WARRANTY AGAINST INFRINGEMENT WITH REGARD TO THE
  20. * SOFTWARE (INCLUDING ANY MODIFIED VERSIONS THEREOF) AND ANY
  21. * ACCOMPANYING WRITTEN MATERIALS.
  22. *
  23. * To the maximum extent permitted by applicable law, IN NO EVENT
  24. * SHALL MOTOROLA BE LIABLE FOR ANY DAMAGES WHATSOEVER (INCLUDING
  25. * WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS
  26. * INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY
  27. * LOSS) ARISING OF THE USE OR INABILITY TO USE THE SOFTWARE.
  28. *
  29. * Motorola assumes no responsibility for the maintenance and support
  30. * of this software
  31. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  32. * Copyright 2016-2017 NXP
  33. *
  34. * Redistribution and use in source and binary forms, with or without modification,
  35. * are permitted provided that the following conditions are met:
  36. *
  37. * o Redistributions of source code must retain the above copyright notice, this list
  38. * of conditions and the following disclaimer.
  39. *
  40. * o Redistributions in binary form must reproduce the above copyright notice, this
  41. * list of conditions and the following disclaimer in the documentation and/or
  42. * other materials provided with the distribution.
  43. *
  44. * o Neither the name of the copyright holder nor the names of its
  45. * contributors may be used to endorse or promote products derived from this
  46. * software without specific prior written permission.
  47. *
  48. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  49. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  50. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  51. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  52. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  53. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  54. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  55. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  56. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  57. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  58. */
  59. #include <stdarg.h>
  60. #include <stdlib.h>
  61. #if defined(__CC_ARM)
  62. #include <stdio.h>
  63. #endif
  64. #include "fsl_debug_console.h"
  65. #include "fsl_debug_console_conf.h"
  66. #include "fsl_log.h"
  67. #include "fsl_str.h"
  68. /*******************************************************************************
  69. * Definitions
  70. ******************************************************************************/
  71. /*******************************************************************************
  72. * Variables
  73. ******************************************************************************/
  74. /*******************************************************************************
  75. * Prototypes
  76. ******************************************************************************/
  77. /*!
  78. * @brief This is a printf call back function which is used to relocate the log to buffer
  79. * or print the log immediately when the local buffer is full.
  80. *
  81. * @param[in] buf Buffer to store log.
  82. * @param[in] indicator Buffer index.
  83. * @param[in] val Target character to store.
  84. * @param[in] len length of the character
  85. *
  86. */
  87. #if SDK_DEBUGCONSOLE
  88. static void DbgConsole_RelocateLog(char *buf, int32_t *indicator, char val, int len);
  89. #endif
  90. /*******************************************************************************
  91. * Code
  92. ******************************************************************************/
  93. /*************Code for DbgConsole Init, Deinit, Printf, Scanf *******************************/
  94. /* See fsl_debug_console.h for documentation of this function. */
  95. status_t DbgConsole_Init(uint32_t baseAddr, uint32_t baudRate, uint8_t device, uint32_t clkSrcFreq)
  96. {
  97. assert(device != DEBUG_CONSOLE_DEVICE_TYPE_NONE);
  98. return LOG_Init(baseAddr, device, baudRate, clkSrcFreq);
  99. }
  100. /* See fsl_debug_console.h for documentation of this function. */
  101. status_t DbgConsole_Deinit(void)
  102. {
  103. /* LOG deinit */
  104. LOG_Deinit();
  105. return kStatus_Success;
  106. }
  107. status_t DbgConsole_Flush(void)
  108. {
  109. /* wait log and io idle */
  110. return LOG_WaitIdle();
  111. }
  112. #if SDK_DEBUGCONSOLE
  113. /* See fsl_debug_console.h for documentation of this function. */
  114. int DbgConsole_Printf(const char *fmt_s, ...)
  115. {
  116. va_list ap;
  117. int logLength = 0U, result = 0U;
  118. char printBuf[DEBUG_CONSOLE_PRINTF_MAX_LOG_LEN] = {0U};
  119. va_start(ap, fmt_s);
  120. /* format print log first */
  121. logLength = StrFormatPrintf(fmt_s, ap, printBuf, DbgConsole_RelocateLog);
  122. /* print log */
  123. result = LOG_Push((uint8_t *)printBuf, logLength);
  124. va_end(ap);
  125. return result;
  126. }
  127. /* See fsl_debug_console.h for documentation of this function. */
  128. int DbgConsole_Putchar(int ch)
  129. {
  130. /* print char */
  131. return LOG_Push((uint8_t *)&ch, 1U);
  132. }
  133. /* See fsl_debug_console.h for documentation of this function. */
  134. int DbgConsole_Scanf(char *fmt_ptr, ...)
  135. {
  136. va_list ap;
  137. int result;
  138. char scanfBuf[DEBUG_CONSOLE_SCANF_MAX_LOG_LEN + 1U] = {0U};
  139. /* scanf log */
  140. LOG_ReadLine((uint8_t *)scanfBuf, DEBUG_CONSOLE_SCANF_MAX_LOG_LEN);
  141. /* get va_list */
  142. va_start(ap, fmt_ptr);
  143. /* format scanf log */
  144. result = StrFormatScanf(scanfBuf, fmt_ptr, ap);
  145. va_end(ap);
  146. return result;
  147. }
  148. #ifdef DEBUG_CONSOLE_TRANSFER_NON_BLOCKING
  149. status_t DbgConsole_TryGetchar(char *ch)
  150. {
  151. if (NULL != ch)
  152. {
  153. return LOG_TryReadCharacter((uint8_t *)ch);
  154. }
  155. return kStatus_Fail;
  156. }
  157. #endif
  158. /* See fsl_debug_console.h for documentation of this function. */
  159. int DbgConsole_Getchar(void)
  160. {
  161. uint8_t ch;
  162. /* Get char */
  163. LOG_ReadCharacter(&ch);
  164. return ch;
  165. }
  166. static void DbgConsole_RelocateLog(char *buf, int32_t *indicator, char val, int len)
  167. {
  168. int i = 0;
  169. for (i = 0; i < len; i++)
  170. {
  171. if ((*indicator + 1) >= DEBUG_CONSOLE_PRINTF_MAX_LOG_LEN)
  172. {
  173. LOG_Push((uint8_t *)buf, *indicator);
  174. *indicator = 0U;
  175. }
  176. buf[*indicator] = val;
  177. (*indicator)++;
  178. }
  179. }
  180. #endif /* SDK_DEBUGCONSOLE */
  181. /*************Code to support toolchain's printf, scanf *******************************/
  182. /* These function __write and __read is used to support IAR toolchain to printf and scanf*/
  183. #if (defined(__ICCARM__))
  184. #if defined(SDK_DEBUGCONSOLE_UART)
  185. #pragma weak __write
  186. size_t __write(int handle, const unsigned char *buffer, size_t size)
  187. {
  188. if (buffer == 0)
  189. {
  190. /*
  191. * This means that we should flush internal buffers. Since we don't we just return.
  192. * (Remember, "handle" == -1 means that all handles should be flushed.)
  193. */
  194. return 0;
  195. }
  196. /* This function only writes to "standard out" and "standard err" for all other file handles it returns failure. */
  197. if ((handle != 1) && (handle != 2))
  198. {
  199. return ((size_t)-1);
  200. }
  201. /* Send data. */
  202. LOG_Push((uint8_t *)buffer, 1U);
  203. return size;
  204. }
  205. #pragma weak __read
  206. size_t __read(int handle, unsigned char *buffer, size_t size)
  207. {
  208. /* This function only reads from "standard in", for all other file handles it returns failure. */
  209. if (handle != 0)
  210. {
  211. return ((size_t)-1);
  212. }
  213. /* Receive data.*/
  214. LOG_ReadLine(buffer, size);
  215. return size;
  216. }
  217. #endif /* SDK_DEBUGCONSOLE_UART */
  218. /* support LPC Xpresso with RedLib */
  219. #elif(defined(__REDLIB__))
  220. #if (!SDK_DEBUGCONSOLE) && (defined(SDK_DEBUGCONSOLE_UART))
  221. int __attribute__((weak)) __sys_write(int handle, char *buffer, int size)
  222. {
  223. if (buffer == 0)
  224. {
  225. /* return -1 if error. */
  226. return -1;
  227. }
  228. /* This function only writes to "standard out" and "standard err" for all other file handles it returns failure. */
  229. if ((handle != 1) && (handle != 2))
  230. {
  231. return -1;
  232. }
  233. /* Send data. */
  234. LOG_Push((uint8_t *)buffer, size);
  235. return 0;
  236. }
  237. int __attribute__((weak)) __sys_readc(void)
  238. {
  239. char tmp;
  240. /* Receive data. */
  241. LOG_ReadCharacter((uint8_t *)&tmp);
  242. return tmp;
  243. }
  244. #endif
  245. /* These function __write and __read is used to support ARM_GCC, KDS, Atollic toolchains to printf and scanf*/
  246. #elif(defined(__GNUC__))
  247. #if ((defined(__GNUC__) && (!defined(__MCUXPRESSO)) && (defined(SDK_DEBUGCONSOLE_UART))) || \
  248. (defined(__MCUXPRESSO) && (!SDK_DEBUGCONSOLE) && (defined(SDK_DEBUGCONSOLE_UART))))
  249. int __attribute__((weak)) _write(int handle, char *buffer, int size)
  250. {
  251. if (buffer == 0)
  252. {
  253. /* return -1 if error. */
  254. return -1;
  255. }
  256. /* This function only writes to "standard out" and "standard err" for all other file handles it returns failure. */
  257. if ((handle != 1) && (handle != 2))
  258. {
  259. return -1;
  260. }
  261. /* Send data. */
  262. LOG_Push((uint8_t *)buffer, size);
  263. return size;
  264. }
  265. int __attribute__((weak)) _read(int handle, char *buffer, int size)
  266. {
  267. /* This function only reads from "standard in", for all other file handles it returns failure. */
  268. if (handle != 0)
  269. {
  270. return -1;
  271. }
  272. /* Receive data. */
  273. return LOG_ReadLine((uint8_t *)buffer, size);
  274. }
  275. #endif
  276. /* These function fputc and fgetc is used to support KEIL toolchain to printf and scanf*/
  277. #elif defined(__CC_ARM)
  278. #if defined(SDK_DEBUGCONSOLE_UART)
  279. struct __FILE
  280. {
  281. int handle;
  282. /*
  283. * Whatever you require here. If the only file you are using is standard output using printf() for debugging,
  284. * no file handling is required.
  285. */
  286. };
  287. /* FILE is typedef in stdio.h. */
  288. #pragma weak __stdout
  289. #pragma weak __stdin
  290. FILE __stdout;
  291. FILE __stdin;
  292. #pragma weak fputc
  293. int fputc(int ch, FILE *f)
  294. {
  295. /* Send data. */
  296. return LOG_Push((uint8_t *)(&ch), 1);
  297. }
  298. #pragma weak fgetc
  299. int fgetc(FILE *f)
  300. {
  301. char ch;
  302. /* Receive data. */
  303. LOG_ReadCharacter((uint8_t *)&ch);
  304. return ch;
  305. }
  306. #endif /* SDK_DEBUGCONSOLE_UART */
  307. #endif /* __ICCARM__ */