硬汉嵌入式论坛

 找回密码
 立即注册
查看: 6896|回复: 8
收起左侧

[SEGGER RTT] SEGGER RTT默认是不支持浮点打印的,简单的点,可以自己封装下做支持

[复制链接]

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106685
QQ
发表于 2020-8-23 10:16:48 | 显示全部楼层 |阅读模式


封装printf,代码如下:

  1. #include "SEGGER_RTT.h"
  2. #include "__libc.h"
  3. #include <stdarg.h>
  4. #include <stdio.h>

  5. int printf(const char *fmt,...) {
  6.   char buffer[128];
  7.   va_list args;
  8.   va_start (args, fmt);
  9.   int n = vsnprintf(buffer, sizeof(buffer), fmt, args);
  10.   SEGGER_RTT_Write(0, buffer, n);
  11.   va_end(args);
  12.   return n;
  13. }

  14. int puts(const char *s) {
  15.   return SEGGER_RTT_WriteString(0, s);
  16. }

  17. int __putchar(int x, __printf_tag_ptr ctx) {
  18.   (void)ctx;
  19.   SEGGER_RTT_Write(0, (char *)&x, 1);
  20.   return x;
  21. }

  22. int __getchar() {
  23.   return SEGGER_RTT_WaitKey();
  24. }
复制代码


回复

使用道具 举报

73

主题

1193

回帖

1412

积分

至尊会员

积分
1412
发表于 2020-8-23 20:33:17 | 显示全部楼层
谢谢
回复

使用道具 举报

73

主题

1193

回帖

1412

积分

至尊会员

积分
1412
发表于 2020-8-23 20:33:35 | 显示全部楼层
请问这个ac6可以使用吗?
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106685
QQ
 楼主| 发表于 2020-8-24 06:56:29 | 显示全部楼层
wdliming 发表于 2020-8-23 20:33
请问这个ac6可以使用吗?

可以。直接sprintf转换成字符后发送即可
回复

使用道具 举报

1

主题

369

回帖

372

积分

高级会员

积分
372
发表于 2020-8-24 16:33:19 | 显示全部楼层
搬运
  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.   
  71. #include "SEGGER_RTT.h"
  72. #include <stdarg.h>
  73. #include <stdio.h>
  74. #include "limits.h"
  75. #include "__libc.h"
  76. #include "__vfprintf.h"

  77. /*********************************************************************
  78. *
  79. *       Defines, configurable
  80. *
  81. **********************************************************************
  82. */
  83. //
  84. // Select string formatting implementation.
  85. //
  86. // RTT printf formatting
  87. //  - Configurable stack usage. (SEGGER_RTT_PRINTF_BUFFER_SIZE in SEGGER_RTT_Conf.h)
  88. //  - No maximum string length.
  89. //  - Limited conversion specifiers and flags. (See SEGGER_RTT_printf.c)
  90. // Standard library printf formatting
  91. //  - Configurable formatting capabilities.
  92. //  - Full conversion specifier and flag support.
  93. //  - Maximum string length has to be known or (slightly) slower character-wise output.
  94. //
  95. // #define PRINTF_USE_SEGGER_RTT_FORMATTING    0 // Use standard library formatting
  96. // #define PRINTF_USE_SEGGER_RTT_FORMATTING    1 // Use RTT formatting
  97. //
  98. #ifndef   PRINTF_USE_SEGGER_RTT_FORMATTING
  99.   #define PRINTF_USE_SEGGER_RTT_FORMATTING    0
  100. #endif
  101. //
  102. // If using standard library formatting,
  103. // select maximum output string buffer size or character-wise output.
  104. //
  105. // #define PRINTF_BUFFER_SIZE                  0 // Use character-wise output
  106. // #define PRINTF_BUFFER_SIZE                128 // Default maximum string length
  107. //
  108. #ifndef   PRINTF_BUFFER_SIZE
  109.   #define PRINTF_BUFFER_SIZE                128
  110. #endif

  111. #if PRINTF_USE_SEGGER_RTT_FORMATTING  // Use SEGGER RTT formatting implementation
  112. /*********************************************************************
  113. *
  114. *       Function prototypes
  115. *
  116. **********************************************************************
  117. */
  118. int SEGGER_RTT_vprintf(unsigned BufferIndex, const char * sFormat, va_list * pParamList);

  119. /*********************************************************************
  120. *
  121. *       Global functions, printf
  122. *
  123. **********************************************************************
  124. */
  125. /*********************************************************************
  126. *
  127. *       printf()
  128. *
  129. *  Function description
  130. *    print a formatted string using RTT and SEGGER RTT formatting.
  131. */
  132. int printf(const char *fmt,...) {
  133.   int     n;
  134.   va_list args;
  135.   
  136.   va_start (args, fmt);
  137.   n = SEGGER_RTT_vprintf(0, fmt, &args);
  138.   va_end(args);
  139.   return n;
  140. }

  141. #elif PRINTF_BUFFER_SIZE == 0 // Use standard library formatting with character-wise output

  142. /*********************************************************************
  143. *
  144. *       Static functions
  145. *
  146. **********************************************************************
  147. */
  148. static int _putchar(int x, __printf_tag_ptr ctx) {
  149.   (void)ctx;
  150.   SEGGER_RTT_Write(0, (char *)&x, 1);
  151.   return x;
  152. }

  153. /*********************************************************************
  154. *
  155. *       Global functions, printf
  156. *
  157. **********************************************************************
  158. */
  159. /*********************************************************************
  160. *
  161. *       printf()
  162. *
  163. *  Function description
  164. *    print a formatted string character-wise, using RTT and standard
  165. *    library formatting.
  166. */
  167. int printf(const char *fmt, ...) {
  168.   int         n;
  169.   va_list     args;
  170.   __printf_t  iod;
  171.   
  172.   va_start(args, fmt);
  173.   iod.string    = 0;
  174.   iod.maxchars  = INT_MAX;
  175.   iod.output_fn = _putchar;
  176.   SEGGER_RTT_LOCK();
  177.   n = __vfprintf(&iod, fmt, args);
  178.   SEGGER_RTT_UNLOCK();
  179.   va_end(args);
  180.   return n;
  181. }

  182. #else // Use standard library formatting with static buffer

  183. /*********************************************************************
  184. *
  185. *       Global functions, printf
  186. *
  187. **********************************************************************
  188. */
  189. /*********************************************************************
  190. *
  191. *       printf()
  192. *
  193. *  Function description
  194. *    print a formatted string using RTT and standard library formatting.
  195. */
  196. int printf(const char *fmt,...) {
  197.   int     n;
  198.   char    aBuffer[PRINTF_BUFFER_SIZE];
  199.   va_list args;
  200.   
  201.   va_start (args, fmt);
  202.   n = vsnprintf(aBuffer, sizeof(aBuffer), fmt, args);
  203.   if (n > (int)sizeof(aBuffer)) {
  204.     SEGGER_RTT_Write(0, aBuffer, sizeof(aBuffer));
  205.   } else if (n > 0) {
  206.     SEGGER_RTT_Write(0, aBuffer, n);
  207.   }
  208.   va_end(args);
  209.   return n;
  210. }
  211. #endif

  212. /*********************************************************************
  213. *
  214. *       Global functions
  215. *
  216. **********************************************************************
  217. */
  218. /*********************************************************************
  219. *
  220. *       puts()
  221. *
  222. *  Function description
  223. *    print a string using RTT.
  224. */
  225. int puts(const char *s) {
  226.   return SEGGER_RTT_WriteString(0, s);
  227. }

  228. /*********************************************************************
  229. *
  230. *       __putchar()
  231. *
  232. *  Function description
  233. *    Write one character via RTT.
  234. */
  235. int __putchar(int x, __printf_tag_ptr ctx) {
  236.   (void)ctx;
  237.   SEGGER_RTT_Write(0, (char *)&x, 1);
  238.   return x;
  239. }

  240. /*********************************************************************
  241. *
  242. *       __getchar()
  243. *
  244. *  Function description
  245. *    Wait for and get a character via RTT.
  246. */
  247. int __getchar() {
  248.   return SEGGER_RTT_WaitKey();
  249. }

  250. #endif
  251. #endif
  252. /****** End Of File *************************************************/
复制代码


回复

使用道具 举报

0

主题

10

回帖

10

积分

新手上路

积分
10
发表于 2021-3-9 15:01:06 | 显示全部楼层
在V7开发板  ThreadX的工程中 AC6编译器 添加浮点RTT打印   报缺少文件 #include "__libc.h"  
__libc.h 这个文件该如何添加?
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106685
QQ
 楼主| 发表于 2021-3-9 15:04:07 | 显示全部楼层
Rainier 发表于 2021-3-9 15:01
在V7开发板  ThreadX的工程中 AC6编译器 添加浮点RTT打印   报缺少文件 #include "__libc.h"  
__libc.h  ...

直接sprintf转换下,然后打印字符串方便些
回复

使用道具 举报

0

主题

10

回帖

10

积分

新手上路

积分
10
发表于 2021-3-9 15:16:43 | 显示全部楼层
eric2013 发表于 2021-3-9 15:04
直接sprintf转换下,然后打印字符串方便些

我是想通过printf直接打印浮点到RTT,putchar用重定向printf的话只能打印单个字符。不知道像楼主位这种直接重写printf是如何实现的?
回复

使用道具 举报

0

主题

14

回帖

14

积分

新手上路

积分
14
发表于 2022-3-31 09:50:15 | 显示全部楼层
代码第二行,#include "__libc.h",实际编译提示缺少文件,这个文件如何添加?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-4-29 16:38 , Processed in 0.195325 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表