SEGGER_RTT_Syscalls_SES.c 8.7 KB

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