SEGGER_RTT_Syscalls_SES.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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.18a *
  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. #include "sdk_config.h"
  68. #if !defined(RETARGET_ENABLED) || RETARGET_ENABLED == 0
  69. #if (defined __SES_ARM) || (defined __CROSSWORKS_ARM)
  70. #include "SEGGER_RTT.h"
  71. #include <stdarg.h>
  72. #include <stdio.h>
  73. #include "limits.h"
  74. #include "__libc.h"
  75. #include "__vfprintf.h"
  76. /*********************************************************************
  77. *
  78. * Defines, configurable
  79. *
  80. **********************************************************************
  81. */
  82. //
  83. // Select string formatting implementation.
  84. //
  85. // RTT printf formatting
  86. // - Configurable stack usage. (SEGGER_RTT_PRINTF_BUFFER_SIZE in SEGGER_RTT_Conf.h)
  87. // - No maximum string length.
  88. // - Limited conversion specifiers and flags. (See SEGGER_RTT_printf.c)
  89. // Standard library printf formatting
  90. // - Configurable formatting capabilities.
  91. // - Full conversion specifier and flag support.
  92. // - Maximum string length has to be known or (slightly) slower character-wise output.
  93. //
  94. // #define PRINTF_USE_SEGGER_RTT_FORMATTING 0 // Use standard library formatting
  95. // #define PRINTF_USE_SEGGER_RTT_FORMATTING 1 // Use RTT formatting
  96. //
  97. #ifndef PRINTF_USE_SEGGER_RTT_FORMATTING
  98. #define PRINTF_USE_SEGGER_RTT_FORMATTING 0
  99. #endif
  100. //
  101. // If using standard library formatting,
  102. // select maximum output string buffer size or character-wise output.
  103. //
  104. // #define PRINTF_BUFFER_SIZE 0 // Use character-wise output
  105. // #define PRINTF_BUFFER_SIZE 128 // Default maximum string length
  106. //
  107. #ifndef PRINTF_BUFFER_SIZE
  108. #define PRINTF_BUFFER_SIZE 128
  109. #endif
  110. #if PRINTF_USE_SEGGER_RTT_FORMATTING // Use SEGGER RTT formatting implementation
  111. /*********************************************************************
  112. *
  113. * Function prototypes
  114. *
  115. **********************************************************************
  116. */
  117. int SEGGER_RTT_vprintf(unsigned BufferIndex, const char * sFormat, va_list * pParamList);
  118. /*********************************************************************
  119. *
  120. * Global functions, printf
  121. *
  122. **********************************************************************
  123. */
  124. /*********************************************************************
  125. *
  126. * printf()
  127. *
  128. * Function description
  129. * print a formatted string using RTT and SEGGER RTT formatting.
  130. */
  131. int printf(const char *fmt,...) {
  132. int n;
  133. va_list args;
  134. va_start (args, fmt);
  135. n = SEGGER_RTT_vprintf(0, fmt, &args);
  136. va_end(args);
  137. return n;
  138. }
  139. #elif PRINTF_BUFFER_SIZE == 0 // Use standard library formatting with character-wise output
  140. /*********************************************************************
  141. *
  142. * Static functions
  143. *
  144. **********************************************************************
  145. */
  146. static int _putchar(int x, __printf_tag_ptr ctx) {
  147. (void)ctx;
  148. SEGGER_RTT_Write(0, (char *)&x, 1);
  149. return x;
  150. }
  151. /*********************************************************************
  152. *
  153. * Global functions, printf
  154. *
  155. **********************************************************************
  156. */
  157. /*********************************************************************
  158. *
  159. * printf()
  160. *
  161. * Function description
  162. * print a formatted string character-wise, using RTT and standard
  163. * library formatting.
  164. */
  165. int printf(const char *fmt, ...) {
  166. int n;
  167. va_list args;
  168. __printf_t iod;
  169. va_start(args, fmt);
  170. iod.string = 0;
  171. iod.maxchars = INT_MAX;
  172. iod.output_fn = _putchar;
  173. SEGGER_RTT_LOCK();
  174. n = __vfprintf(&iod, fmt, args);
  175. SEGGER_RTT_UNLOCK();
  176. va_end(args);
  177. return n;
  178. }
  179. #else // Use standard library formatting with static buffer
  180. /*********************************************************************
  181. *
  182. * Global functions, printf
  183. *
  184. **********************************************************************
  185. */
  186. /*********************************************************************
  187. *
  188. * printf()
  189. *
  190. * Function description
  191. * print a formatted string using RTT and standard library formatting.
  192. */
  193. int printf(const char *fmt,...) {
  194. int n;
  195. char aBuffer[PRINTF_BUFFER_SIZE];
  196. va_list args;
  197. va_start (args, fmt);
  198. n = vsnprintf(aBuffer, sizeof(aBuffer), fmt, args);
  199. if (n > (int)sizeof(aBuffer)) {
  200. SEGGER_RTT_Write(0, aBuffer, sizeof(aBuffer));
  201. } else if (n > 0) {
  202. SEGGER_RTT_Write(0, aBuffer, n);
  203. }
  204. va_end(args);
  205. return n;
  206. }
  207. #endif
  208. /*********************************************************************
  209. *
  210. * Global functions
  211. *
  212. **********************************************************************
  213. */
  214. /*********************************************************************
  215. *
  216. * puts()
  217. *
  218. * Function description
  219. * print a string using RTT.
  220. */
  221. int puts(const char *s) {
  222. return SEGGER_RTT_WriteString(0, s);
  223. }
  224. /*********************************************************************
  225. *
  226. * __putchar()
  227. *
  228. * Function description
  229. * Write one character via RTT.
  230. */
  231. int __putchar(int x, __printf_tag_ptr ctx) {
  232. (void)ctx;
  233. SEGGER_RTT_Write(0, (char *)&x, 1);
  234. return x;
  235. }
  236. /*********************************************************************
  237. *
  238. * __getchar()
  239. *
  240. * Function description
  241. * Wait for and get a character via RTT.
  242. */
  243. int __getchar() {
  244. return SEGGER_RTT_WaitKey();
  245. }
  246. #endif
  247. #endif
  248. /****** End Of File *************************************************/