Retarget.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * @brief IO redirection support
  3. *
  4. * This file adds re-direction support to the library for various
  5. * projects. It can be configured in one of 3 ways - no redirection,
  6. * redirection via a UART, or redirection via semihosting. If DEBUG
  7. * is not defined, all printf statements will do nothing with the
  8. * output being throw away. If DEBUG is defined, then the choice of
  9. * output is selected by the DEBUG_SEMIHOSTING define. If the
  10. * DEBUG_SEMIHOSTING is not defined, then output is redirected via
  11. * the UART. If DEBUG_SEMIHOSTING is defined, then output will be
  12. * attempted to be redirected via semihosting. If the UART method
  13. * is used, then the Board_UARTPutChar and Board_UARTGetChar
  14. * functions must be defined to be used by this driver and the UART
  15. * must already be initialized to the correct settings.
  16. *
  17. * @note
  18. * Copyright(C) NXP Semiconductors, 2012
  19. * All rights reserved.
  20. *
  21. * @par
  22. * Software that is described herein is for illustrative purposes only
  23. * which provides customers with programming information regarding the
  24. * LPC products. This software is supplied "AS IS" without any warranties of
  25. * any kind, and NXP Semiconductors and its licensor disclaim any and
  26. * all warranties, express or implied, including all implied warranties of
  27. * merchantability, fitness for a particular purpose and non-infringement of
  28. * intellectual property rights. NXP Semiconductors assumes no responsibility
  29. * or liability for the use of the software, conveys no license or rights under any
  30. * patent, copyright, mask work right, or any other intellectual property rights in
  31. * or to any products. NXP Semiconductors reserves the right to make changes
  32. * in the software without notification. NXP Semiconductors also makes no
  33. * representation or warranty that such application will be suitable for the
  34. * specified use without further testing or modification.
  35. *
  36. * @par
  37. * Permission to use, copy, modify, and distribute this software and its
  38. * documentation is hereby granted, under NXP Semiconductors' and its
  39. * licensor's relevant copyrights in the software, without fee, provided that it
  40. * is used in conjunction with NXP Semiconductors microcontrollers. This
  41. * copyright, permission, and disclaimer notice must appear in all copies of
  42. * this code.
  43. */
  44. #include "sys_config.h"
  45. #include "board.h"
  46. /* Keil (Realview) support */
  47. #if defined(__CC_ARM)
  48. #include <stdio.h>
  49. #include <rt_misc.h>
  50. #if defined(DEBUG)
  51. #if defined(DEBUG_SEMIHOSTING)
  52. #define ITM_Port8(n) (*((volatile unsigned char *) (0xE0000000 + 4 * n)))
  53. #define ITM_Port16(n) (*((volatile unsigned short *) (0xE0000000 + 4 * n)))
  54. #define ITM_Port32(n) (*((volatile unsigned long *) (0xE0000000 + 4 * n)))
  55. #define DEMCR (*((volatile unsigned long *) (0xE000EDFC)))
  56. #define TRCENA 0x01000000
  57. /* Write to SWO */
  58. void _ttywrch(int ch)
  59. {
  60. if (DEMCR & TRCENA) {
  61. while (ITM_Port32(0) == 0) {}
  62. ITM_Port8(0) = ch;
  63. }
  64. }
  65. #else
  66. static INLINE void BoardOutChar(char ch)
  67. {
  68. Board_UARTPutChar(ch);
  69. }
  70. #endif /* defined(DEBUG_SEMIHOSTING) */
  71. #endif /* defined(DEBUG) */
  72. struct __FILE {
  73. int handle;
  74. };
  75. FILE __stdout;
  76. FILE __stdin;
  77. FILE __stderr;
  78. void *_sys_open(const char *name, int openmode)
  79. {
  80. return 0;
  81. }
  82. int fputc(int c, FILE *f)
  83. {
  84. #if defined(DEBUG)
  85. #if defined(DEBUG_SEMIHOSTING)
  86. _ttywrch(c);
  87. #else
  88. BoardOutChar((char) c);
  89. #endif
  90. #endif
  91. return 0;
  92. }
  93. int fgetc(FILE *f)
  94. {
  95. #if defined(DEBUG) && !defined(DEBUG_SEMIHOSTING)
  96. return Board_UARTGetChar();
  97. #else
  98. return 0;
  99. #endif
  100. }
  101. int ferror(FILE *f)
  102. {
  103. return EOF;
  104. }
  105. void _sys_exit(int return_code)
  106. {
  107. label: goto label; /* endless loop */
  108. }
  109. #endif /* defined (__CC_ARM) */
  110. /* IAR support */
  111. #if defined(__ICCARM__)
  112. /*******************
  113. *
  114. * Copyright 1998-2003 IAR Systems. All rights reserved.
  115. *
  116. * $Revision: 30870 $
  117. *
  118. * This is a template implementation of the "__write" function used by
  119. * the standard library. Replace it with a system-specific
  120. * implementation.
  121. *
  122. * The "__write" function should output "size" number of bytes from
  123. * "buffer" in some application-specific way. It should return the
  124. * number of characters written, or _LLIO_ERROR on failure.
  125. *
  126. * If "buffer" is zero then __write should perform flushing of
  127. * internal buffers, if any. In this case "handle" can be -1 to
  128. * indicate that all handles should be flushed.
  129. *
  130. * The template implementation below assumes that the application
  131. * provides the function "MyLowLevelPutchar". It should return the
  132. * character written, or -1 on failure.
  133. *
  134. ********************/
  135. #include <yfuns.h>
  136. _STD_BEGIN
  137. #pragma module_name = "?__write"
  138. #if defined(DEBUG)
  139. #if defined(DEBUG_SEMIHOSTING)
  140. #error Semihosting support not yet working on IAR
  141. #endif /* defined(DEBUG_SEMIHOSTING) */
  142. #endif /* defined(DEBUG) */
  143. /*
  144. If the __write implementation uses internal buffering, uncomment
  145. the following line to ensure that we are called with "buffer" as 0
  146. (i.e. flush) when the application terminates. */
  147. size_t __write(int handle, const unsigned char *buffer, size_t size)
  148. {
  149. #if defined(DEBUG)
  150. size_t nChars = 0;
  151. if (buffer == 0) {
  152. /*
  153. This means that we should flush internal buffers. Since we
  154. don't we just return. (Remember, "handle" == -1 means that all
  155. handles should be flushed.)
  156. */
  157. return 0;
  158. }
  159. /* This template only writes to "standard out" and "standard err",
  160. for all other file handles it returns failure. */
  161. if (( handle != _LLIO_STDOUT) && ( handle != _LLIO_STDERR) ) {
  162. return _LLIO_ERROR;
  163. }
  164. for ( /* Empty */; size != 0; --size) {
  165. Board_UARTPutChar(*buffer++);
  166. ++nChars;
  167. }
  168. return nChars;
  169. #else
  170. return size;
  171. #endif /* defined(DEBUG) */
  172. }
  173. _STD_END
  174. #endif /* defined (__ICCARM__) */
  175. #if defined( __GNUC__ )
  176. /* Include stdio.h to pull in __REDLIB_INTERFACE_VERSION__ */
  177. #include <stdio.h>
  178. #if (__REDLIB_INTERFACE_VERSION__ >= 20000)
  179. /* We are using new Redlib_v2 semihosting interface */
  180. #define WRITEFUNC __sys_write
  181. #define READFUNC __sys_readc
  182. #else
  183. /* We are using original Redlib semihosting interface */
  184. #define WRITEFUNC __write
  185. #define READFUNC __readc
  186. #endif
  187. #if defined(DEBUG)
  188. #if defined(DEBUG_SEMIHOSTING)
  189. /* Do nothing, semihosting is enabled by default in LPCXpresso */
  190. #endif /* defined(DEBUG_SEMIHOSTING) */
  191. #endif /* defined(DEBUG) */
  192. #if !defined(DEBUG_SEMIHOSTING)
  193. int WRITEFUNC(int iFileHandle, char *pcBuffer, int iLength)
  194. {
  195. #if defined(DEBUG)
  196. unsigned int i;
  197. for (i = 0; i < iLength; i++) {
  198. Board_UARTPutChar(pcBuffer[i]);
  199. }
  200. #endif
  201. return iLength;
  202. }
  203. /* Called by bottom level of scanf routine within RedLib C library to read
  204. a character. With the default semihosting stub, this would read the character
  205. from the debugger console window (which acts as stdin). But this version reads
  206. the character from the LPC1768/RDB1768 UART. */
  207. int READFUNC(void)
  208. {
  209. #if defined(DEBUG)
  210. char c = Board_UARTGetChar();
  211. return (int) c;
  212. #else
  213. return (int) -1;
  214. #endif
  215. }
  216. #endif /* !defined(DEBUG_SEMIHOSTING) */
  217. #endif /* defined ( __GNUC__ ) */