SEGGER_RTT_Syscalls_SES.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*********************************************************************
  2. * SEGGER Microcontroller GmbH & Co. KG *
  3. * The Embedded Experts *
  4. **********************************************************************
  5. * *
  6. * (c) 2014 - 2017 SEGGER Microcontroller GmbH & Co. KG *
  7. * *
  8. * www.segger.com Support: support@segger.com *
  9. * *
  10. **********************************************************************
  11. * *
  12. * SEGGER RTT * Real Time Transfer for embedded targets *
  13. * *
  14. **********************************************************************
  15. * *
  16. * All rights reserved. *
  17. * *
  18. * SEGGER strongly recommends to not make any changes *
  19. * to or modify the source code of this software in order to stay *
  20. * compatible with the RTT protocol and J-Link. *
  21. * *
  22. * Redistribution and use in source and binary forms, with or *
  23. * without modification, are permitted provided that the following *
  24. * conditions are met: *
  25. * *
  26. * o Redistributions of source code must retain the above copyright *
  27. * notice, this list of conditions and the following disclaimer. *
  28. * *
  29. * o Redistributions in binary form must reproduce the above *
  30. * copyright notice, this list of conditions and the following *
  31. * disclaimer in the documentation and/or other materials provided *
  32. * with the distribution. *
  33. * *
  34. * o Neither the name of SEGGER Microcontroller GmbH & Co. KG *
  35. * nor the names of its contributors may be used to endorse or *
  36. * promote products derived from this software without specific *
  37. * prior written permission. *
  38. * *
  39. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND *
  40. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, *
  41. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF *
  42. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE *
  43. * DISCLAIMED. IN NO EVENT SHALL SEGGER Microcontroller BE LIABLE FOR *
  44. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR *
  45. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT *
  46. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; *
  47. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
  48. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
  49. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE *
  50. * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
  51. * DAMAGE. *
  52. * *
  53. **********************************************************************
  54. * *
  55. * RTT version: 6.22a *
  56. * *
  57. **********************************************************************
  58. ---------------------------END-OF-HEADER------------------------------
  59. File : SEGGER_RTT_Syscalls_SES.c
  60. Purpose : Reimplementation of printf, puts and __getchar using RTT
  61. in SEGGER Embedded Studio.
  62. To use RTT for printf output, include this file in your
  63. application.
  64. Revision: $Rev: 4351 $
  65. ----------------------------------------------------------------------
  66. */
  67. #if (defined __SES_ARM) || (defined __CROSSWORKS_ARM)
  68. #include "SEGGER_RTT.h"
  69. #include <stdarg.h>
  70. #include <stdio.h>
  71. #include "limits.h"
  72. #include "__libc.h"
  73. #include "__vfprintf.h"
  74. /*********************************************************************
  75. *
  76. * Defines, configurable
  77. *
  78. **********************************************************************
  79. */
  80. //
  81. // Select string formatting implementation.
  82. //
  83. // RTT printf formatting
  84. // - Configurable stack usage. (SEGGER_RTT_PRINTF_BUFFER_SIZE in SEGGER_RTT_Conf.h)
  85. // - No maximum string length.
  86. // - Limited conversion specifiers and flags. (See SEGGER_RTT_printf.c)
  87. // Standard library printf formatting
  88. // - Configurable formatting capabilities.
  89. // - Full conversion specifier and flag support.
  90. // - Maximum string length has to be known or (slightly) slower character-wise output.
  91. //
  92. // #define PRINTF_USE_SEGGER_RTT_FORMATTING 0 // Use standard library formatting
  93. // #define PRINTF_USE_SEGGER_RTT_FORMATTING 1 // Use RTT formatting
  94. //
  95. #ifndef PRINTF_USE_SEGGER_RTT_FORMATTING
  96. #define PRINTF_USE_SEGGER_RTT_FORMATTING 0
  97. #endif
  98. //
  99. // If using standard library formatting,
  100. // select maximum output string buffer size or character-wise output.
  101. //
  102. // #define PRINTF_BUFFER_SIZE 0 // Use character-wise output
  103. // #define PRINTF_BUFFER_SIZE 128 // Default maximum string length
  104. //
  105. #ifndef PRINTF_BUFFER_SIZE
  106. #define PRINTF_BUFFER_SIZE 128
  107. #endif
  108. #if PRINTF_USE_SEGGER_RTT_FORMATTING // Use SEGGER RTT formatting implementation
  109. /*********************************************************************
  110. *
  111. * Function prototypes
  112. *
  113. **********************************************************************
  114. */
  115. int SEGGER_RTT_vprintf(unsigned BufferIndex, const char * sFormat, va_list * pParamList);
  116. /*********************************************************************
  117. *
  118. * Global functions, printf
  119. *
  120. **********************************************************************
  121. */
  122. /*********************************************************************
  123. *
  124. * printf()
  125. *
  126. * Function description
  127. * print a formatted string using RTT and SEGGER RTT formatting.
  128. */
  129. int printf(const char *fmt,...) {
  130. int n;
  131. va_list args;
  132. va_start (args, fmt);
  133. n = SEGGER_RTT_vprintf(0, fmt, &args);
  134. va_end(args);
  135. return n;
  136. }
  137. #elif PRINTF_BUFFER_SIZE == 0 // Use standard library formatting with character-wise output
  138. /*********************************************************************
  139. *
  140. * Static functions
  141. *
  142. **********************************************************************
  143. */
  144. static int _putchar(int x, __printf_tag_ptr ctx) {
  145. (void)ctx;
  146. SEGGER_RTT_Write(0, (char *)&x, 1);
  147. return x;
  148. }
  149. /*********************************************************************
  150. *
  151. * Global functions, printf
  152. *
  153. **********************************************************************
  154. */
  155. /*********************************************************************
  156. *
  157. * printf()
  158. *
  159. * Function description
  160. * print a formatted string character-wise, using RTT and standard
  161. * library formatting.
  162. */
  163. int printf(const char *fmt, ...) {
  164. int n;
  165. va_list args;
  166. __printf_t iod;
  167. va_start(args, fmt);
  168. iod.string = 0;
  169. iod.maxchars = INT_MAX;
  170. iod.output_fn = _putchar;
  171. SEGGER_RTT_LOCK();
  172. n = __vfprintf(&iod, fmt, args);
  173. SEGGER_RTT_UNLOCK();
  174. va_end(args);
  175. return n;
  176. }
  177. #else // Use standard library formatting with static buffer
  178. /*********************************************************************
  179. *
  180. * Global functions, printf
  181. *
  182. **********************************************************************
  183. */
  184. /*********************************************************************
  185. *
  186. * printf()
  187. *
  188. * Function description
  189. * print a formatted string using RTT and standard library formatting.
  190. */
  191. int printf(const char *fmt,...) {
  192. int n;
  193. char aBuffer[PRINTF_BUFFER_SIZE];
  194. va_list args;
  195. va_start (args, fmt);
  196. n = vsnprintf(aBuffer, sizeof(aBuffer), fmt, args);
  197. if (n > (int)sizeof(aBuffer)) {
  198. SEGGER_RTT_Write(0, aBuffer, sizeof(aBuffer));
  199. } else if (n > 0) {
  200. SEGGER_RTT_Write(0, aBuffer, n);
  201. }
  202. va_end(args);
  203. return n;
  204. }
  205. #endif
  206. /*********************************************************************
  207. *
  208. * Global functions
  209. *
  210. **********************************************************************
  211. */
  212. /*********************************************************************
  213. *
  214. * puts()
  215. *
  216. * Function description
  217. * print a string using RTT.
  218. */
  219. int puts(const char *s) {
  220. return SEGGER_RTT_WriteString(0, s);
  221. }
  222. /*********************************************************************
  223. *
  224. * __putchar()
  225. *
  226. * Function description
  227. * Write one character via RTT.
  228. */
  229. int __putchar(int x, __printf_tag_ptr ctx) {
  230. (void)ctx;
  231. SEGGER_RTT_Write(0, (char *)&x, 1);
  232. return x;
  233. }
  234. /*********************************************************************
  235. *
  236. * __getchar()
  237. *
  238. * Function description
  239. * Wait for and get a character via RTT.
  240. */
  241. int __getchar() {
  242. return SEGGER_RTT_WaitKey();
  243. }
  244. #endif
  245. /****** End Of File *************************************************/