printf_retarget.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**************************************************************************/
  2. /*!
  3. @file printf_retarget.c
  4. @author hathach (tinyusb.org)
  5. @section LICENSE
  6. Software License Agreement (BSD License)
  7. Copyright (c) 2013, hathach (tinyusb.org)
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. 1. Redistributions of source code must retain the above copyright
  12. notice, this list of conditions and the following disclaimer.
  13. 2. Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. 3. Neither the name of the copyright holders nor the
  17. names of its contributors may be used to endorse or promote products
  18. derived from this software without specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
  20. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
  23. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND
  26. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. This file is part of the tinyusb stack.
  30. */
  31. /**************************************************************************/
  32. #include "board.h"
  33. #if CFG_PRINTF_TARGET != PRINTF_TARGET_SEMIHOST
  34. #if CFG_PRINTF_TARGET == PRINTF_TARGET_UART
  35. #define retarget_getchar() board_uart_getchar()
  36. #define retarget_putchar(c) board_uart_putchar(c);
  37. #elif CFG_PRINTF_TARGET == PRINTF_TARGET_SWO
  38. volatile int32_t ITM_RxBuffer; // keil variable to read from SWO
  39. #define retarget_getchar() ITM_ReceiveChar()
  40. #define retarget_putchar(c) ITM_SendChar(c)
  41. #else
  42. #error Target is not implemented yet
  43. #endif
  44. //--------------------------------------------------------------------+
  45. // LPCXPRESSO / RED SUITE
  46. //--------------------------------------------------------------------+
  47. #if defined __CODE_RED
  48. #if CFG_PRINTF_TARGET == PRINTF_TARGET_SWO
  49. #error author does not know how to retarget SWO with lpcxpresso/red-suite
  50. #endif
  51. // Called by bottom level of printf routine within RedLib C library to write
  52. // a character. With the default semihosting stub, this would write the character
  53. // to the debugger console window . But this version writes
  54. // the character to the UART.
  55. int __sys_write (int iFileHandle, char *buf, int length)
  56. {
  57. (void) iFileHandle;
  58. for (int i=0; i<length; i++)
  59. {
  60. if (buf[i] == '\n') retarget_putchar('\r');
  61. retarget_putchar( buf[i] );
  62. }
  63. return length;
  64. }
  65. // Called by bottom level of scanf routine within RedLib C library to read
  66. // a character. With the default semihosting stub, this would read the character
  67. // from the debugger console window (which acts as stdin). But this version reads
  68. // the character from the UART.
  69. int __sys_readc (void)
  70. {
  71. return (int) retarget_getchar();
  72. }
  73. #elif defined __SES_ARM && 0
  74. #include <stdarg.h>
  75. #include <stdio.h>
  76. #include "__libc.h"
  77. int printf(const char *fmt,...) {
  78. char buffer[128];
  79. va_list args;
  80. va_start (args, fmt);
  81. int n = vsnprintf(buffer, sizeof(buffer), fmt, args);
  82. for(int i=0; i < n; i++)
  83. {
  84. retarget_putchar( buffer[i] );
  85. }
  86. va_end(args);
  87. return n;
  88. }
  89. int __putchar(int ch, __printf_tag_ptr ctx)
  90. {
  91. (void)ctx;
  92. retarget_putchar( (uint8_t) ch );
  93. return 1;
  94. }
  95. int __getchar()
  96. {
  97. return retarget_getchar();
  98. }
  99. //--------------------------------------------------------------------+
  100. // KEIL
  101. //--------------------------------------------------------------------+
  102. #elif defined __CC_ARM // keil
  103. struct __FILE {
  104. uint32_t handle;
  105. };
  106. void _ttywrch(int ch)
  107. {
  108. if ( ch == '\n' ) retarget_putchar('\r');
  109. retarget_putchar(ch);
  110. }
  111. int fgetc(FILE *f)
  112. {
  113. return retarget_getchar();
  114. }
  115. int fputc(int ch, FILE *f)
  116. {
  117. _ttywrch(ch);
  118. return ch;
  119. }
  120. //--------------------------------------------------------------------+
  121. // IAR
  122. //--------------------------------------------------------------------+
  123. #elif defined __ICCARM__ // TODO could not able to retarget to UART with IAR
  124. #if CFG_PRINTF_TARGET == PRINTF_TARGET_UART
  125. #include <stddef.h>
  126. size_t __write(int handle, const unsigned char *buf, size_t length)
  127. {
  128. /* Check for the command to flush all handles */
  129. if (handle == -1) return 0;
  130. /* Check for stdout and stderr (only necessary if FILE descriptors are enabled.) */
  131. if (handle != 1 && handle != 2) return -1;
  132. for (size_t i=0; i<length; i++)
  133. {
  134. if (buf[i] == '\n') retarget_putchar('\r');
  135. retarget_putchar( buf[i] );
  136. }
  137. return length;
  138. }
  139. size_t __read(int handle, unsigned char *buf, size_t bufSize)
  140. {
  141. /* Check for stdin (only necessary if FILE descriptors are enabled) */
  142. if (handle != 0) return -1;
  143. size_t i;
  144. for (i=0; i<bufSize; i++)
  145. {
  146. uint8_t ch = board_uart_getchar();
  147. if (ch == 0) break;
  148. buf[i] = ch;
  149. }
  150. return i;
  151. }
  152. #endif
  153. #endif
  154. #endif // CFG_PRINTF_TARGET != PRINTF_TARGET_SEMIHOST