SEGGER_RTT_printf.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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 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. * 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. * RTT version: 7.96b *
  46. * *
  47. **********************************************************************
  48. ---------------------------END-OF-HEADER------------------------------
  49. File : SEGGER_RTT_printf.c
  50. Purpose : Replacement for printf to write formatted data via RTT
  51. Revision: $Rev: 17697 $
  52. ----------------------------------------------------------------------
  53. */
  54. #include "SEGGER_RTT.h"
  55. #include "SEGGER_RTT_Conf.h"
  56. #include "stdio.h"
  57. /*********************************************************************
  58. *
  59. * Defines, configurable
  60. *
  61. **********************************************************************
  62. */
  63. #ifndef SEGGER_RTT_PRINTF_BUFFER_SIZE
  64. #define SEGGER_RTT_PRINTF_BUFFER_SIZE (64)
  65. #endif
  66. #include <stdlib.h>
  67. #include <stdarg.h>
  68. #define FORMAT_FLAG_LEFT_JUSTIFY (1u << 0)
  69. #define FORMAT_FLAG_PAD_ZERO (1u << 1)
  70. #define FORMAT_FLAG_PRINT_SIGN (1u << 2)
  71. #define FORMAT_FLAG_ALTERNATE (1u << 3)
  72. /*********************************************************************
  73. *
  74. * Types
  75. *
  76. **********************************************************************
  77. */
  78. typedef struct {
  79. char* pBuffer;
  80. unsigned BufferSize;
  81. unsigned Cnt;
  82. int ReturnValue;
  83. unsigned RTTBufferIndex;
  84. } SEGGER_RTT_PRINTF_DESC;
  85. /*********************************************************************
  86. *
  87. * Function prototypes
  88. *
  89. **********************************************************************
  90. */
  91. /*********************************************************************
  92. *
  93. * Static code
  94. *
  95. **********************************************************************
  96. */
  97. /*********************************************************************
  98. *
  99. * _StoreChar
  100. */
  101. static void _StoreChar(SEGGER_RTT_PRINTF_DESC * p, char c) {
  102. unsigned Cnt;
  103. Cnt = p->Cnt;
  104. if ((Cnt + 1u) <= p->BufferSize) {
  105. *(p->pBuffer + Cnt) = c;
  106. p->Cnt = Cnt + 1u;
  107. p->ReturnValue++;
  108. }
  109. //
  110. // Write part of string, when the buffer is full
  111. //
  112. if (p->Cnt == p->BufferSize) {
  113. if (SEGGER_RTT_Write(p->RTTBufferIndex, p->pBuffer, p->Cnt) != p->Cnt) {
  114. p->ReturnValue = -1;
  115. } else {
  116. p->Cnt = 0u;
  117. }
  118. }
  119. }
  120. /*********************************************************************
  121. *
  122. * _PrintUnsigned
  123. */
  124. static void _PrintUnsigned(SEGGER_RTT_PRINTF_DESC * pBufferDesc, unsigned v, unsigned Base, unsigned NumDigits, unsigned FieldWidth, unsigned FormatFlags) {
  125. static const char _aV2C[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  126. unsigned Div;
  127. unsigned Digit;
  128. unsigned Number;
  129. unsigned Width;
  130. char c;
  131. Number = v;
  132. Digit = 1u;
  133. //
  134. // Get actual field width
  135. //
  136. Width = 1u;
  137. while (Number >= Base) {
  138. Number = (Number / Base);
  139. Width++;
  140. }
  141. if (NumDigits > Width) {
  142. Width = NumDigits;
  143. }
  144. //
  145. // Print leading chars if necessary
  146. //
  147. if ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == 0u) {
  148. if (FieldWidth != 0u) {
  149. if (((FormatFlags & FORMAT_FLAG_PAD_ZERO) == FORMAT_FLAG_PAD_ZERO) && (NumDigits == 0u)) {
  150. c = '0';
  151. } else {
  152. c = ' ';
  153. }
  154. while ((FieldWidth != 0u) && (Width < FieldWidth)) {
  155. FieldWidth--;
  156. _StoreChar(pBufferDesc, c);
  157. if (pBufferDesc->ReturnValue < 0) {
  158. break;
  159. }
  160. }
  161. }
  162. }
  163. if (pBufferDesc->ReturnValue >= 0) {
  164. //
  165. // Compute Digit.
  166. // Loop until Digit has the value of the highest digit required.
  167. // Example: If the output is 345 (Base 10), loop 2 times until Digit is 100.
  168. //
  169. while (1) {
  170. if (NumDigits > 1u) { // User specified a min number of digits to print? => Make sure we loop at least that often, before checking anything else (> 1 check avoids problems with NumDigits being signed / unsigned)
  171. NumDigits--;
  172. } else {
  173. Div = v / Digit;
  174. if (Div < Base) { // Is our divider big enough to extract the highest digit from value? => Done
  175. break;
  176. }
  177. }
  178. Digit *= Base;
  179. }
  180. //
  181. // Output digits
  182. //
  183. do {
  184. Div = v / Digit;
  185. v -= Div * Digit;
  186. _StoreChar(pBufferDesc, _aV2C[Div]);
  187. if (pBufferDesc->ReturnValue < 0) {
  188. break;
  189. }
  190. Digit /= Base;
  191. } while (Digit);
  192. //
  193. // Print trailing spaces if necessary
  194. //
  195. if ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == FORMAT_FLAG_LEFT_JUSTIFY) {
  196. if (FieldWidth != 0u) {
  197. while ((FieldWidth != 0u) && (Width < FieldWidth)) {
  198. FieldWidth--;
  199. _StoreChar(pBufferDesc, ' ');
  200. if (pBufferDesc->ReturnValue < 0) {
  201. break;
  202. }
  203. }
  204. }
  205. }
  206. }
  207. }
  208. /*********************************************************************
  209. *
  210. * _PrintInt
  211. */
  212. static void _PrintInt(SEGGER_RTT_PRINTF_DESC * pBufferDesc, int v, unsigned Base, unsigned NumDigits, unsigned FieldWidth, unsigned FormatFlags) {
  213. unsigned Width;
  214. int Number;
  215. Number = (v < 0) ? -v : v;
  216. //
  217. // Get actual field width
  218. //
  219. Width = 1u;
  220. while (Number >= (int)Base) {
  221. Number = (Number / (int)Base);
  222. Width++;
  223. }
  224. if (NumDigits > Width) {
  225. Width = NumDigits;
  226. }
  227. if ((FieldWidth > 0u) && ((v < 0) || ((FormatFlags & FORMAT_FLAG_PRINT_SIGN) == FORMAT_FLAG_PRINT_SIGN))) {
  228. FieldWidth--;
  229. }
  230. //
  231. // Print leading spaces if necessary
  232. //
  233. if ((((FormatFlags & FORMAT_FLAG_PAD_ZERO) == 0u) || (NumDigits != 0u)) && ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == 0u)) {
  234. if (FieldWidth != 0u) {
  235. while ((FieldWidth != 0u) && (Width < FieldWidth)) {
  236. FieldWidth--;
  237. _StoreChar(pBufferDesc, ' ');
  238. if (pBufferDesc->ReturnValue < 0) {
  239. break;
  240. }
  241. }
  242. }
  243. }
  244. //
  245. // Print sign if necessary
  246. //
  247. if (pBufferDesc->ReturnValue >= 0) {
  248. if (v < 0) {
  249. v = -v;
  250. _StoreChar(pBufferDesc, '-');
  251. } else if ((FormatFlags & FORMAT_FLAG_PRINT_SIGN) == FORMAT_FLAG_PRINT_SIGN) {
  252. _StoreChar(pBufferDesc, '+');
  253. } else {
  254. }
  255. if (pBufferDesc->ReturnValue >= 0) {
  256. //
  257. // Print leading zeros if necessary
  258. //
  259. if (((FormatFlags & FORMAT_FLAG_PAD_ZERO) == FORMAT_FLAG_PAD_ZERO) && ((FormatFlags & FORMAT_FLAG_LEFT_JUSTIFY) == 0u) && (NumDigits == 0u)) {
  260. if (FieldWidth != 0u) {
  261. while ((FieldWidth != 0u) && (Width < FieldWidth)) {
  262. FieldWidth--;
  263. _StoreChar(pBufferDesc, '0');
  264. if (pBufferDesc->ReturnValue < 0) {
  265. break;
  266. }
  267. }
  268. }
  269. }
  270. if (pBufferDesc->ReturnValue >= 0) {
  271. //
  272. // Print number without sign
  273. //
  274. _PrintUnsigned(pBufferDesc, (unsigned)v, Base, NumDigits, FieldWidth, FormatFlags);
  275. }
  276. }
  277. }
  278. }
  279. /*********************************************************************
  280. *
  281. * Public code
  282. *
  283. **********************************************************************
  284. */
  285. // 修改后的浮点数打印函数
  286. static void _PrintFloat(SEGGER_RTT_PRINTF_DESC * pBufferDesc, double v, unsigned NumDigits, unsigned FieldWidth, unsigned FormatFlags) {
  287. int integer_part = (int)v;
  288. double fractional_part = v - integer_part;
  289. if (fractional_part < 0) {
  290. fractional_part = -fractional_part;
  291. }
  292. // 添加舍入补偿值,避免精度误差
  293. fractional_part += 1e-10; // 关键修改点:添加微小的舍入补偿
  294. // 打印整数部分
  295. _PrintInt(pBufferDesc, integer_part, 10u, 0u, FieldWidth, FormatFlags);
  296. // 打印小数点
  297. if (pBufferDesc->ReturnValue >= 0) {
  298. _StoreChar(pBufferDesc, '.');
  299. }
  300. // 处理小数部分
  301. if (pBufferDesc->ReturnValue >= 0) {
  302. // 最多打印9位小数
  303. if (NumDigits > 9) {
  304. NumDigits = 9;
  305. }
  306. for (unsigned i = 0; i < NumDigits; i++) {
  307. fractional_part *= 10;
  308. int digit = (int)fractional_part;
  309. _StoreChar(pBufferDesc, '0' + digit);
  310. fractional_part -= digit;
  311. if (pBufferDesc->ReturnValue < 0) {
  312. break;
  313. }
  314. }
  315. }
  316. }
  317. /*********************************************************************
  318. *
  319. * SEGGER_RTT_vprintf
  320. *
  321. * Function description
  322. * Stores a formatted string in SEGGER RTT control block.
  323. * This data is read by the host.
  324. *
  325. * Parameters
  326. * BufferIndex Index of "Up"-buffer to be used. (e.g. 0 for "Terminal")
  327. * sFormat Pointer to format string
  328. * pParamList Pointer to the list of arguments for the format string
  329. *
  330. * Return values
  331. * >= 0: Number of bytes which have been stored in the "Up"-buffer.
  332. * < 0: Error
  333. */
  334. int SEGGER_RTT_vprintf(unsigned BufferIndex, const char * sFormat, va_list * pParamList) {
  335. char c;
  336. SEGGER_RTT_PRINTF_DESC BufferDesc;
  337. int v;
  338. unsigned NumDigits;
  339. unsigned FormatFlags;
  340. unsigned FieldWidth;
  341. char acBuffer[SEGGER_RTT_PRINTF_BUFFER_SIZE];
  342. BufferDesc.pBuffer = acBuffer;
  343. BufferDesc.BufferSize = SEGGER_RTT_PRINTF_BUFFER_SIZE;
  344. BufferDesc.Cnt = 0u;
  345. BufferDesc.RTTBufferIndex = BufferIndex;
  346. BufferDesc.ReturnValue = 0;
  347. do {
  348. c = *sFormat;
  349. sFormat++;
  350. if (c == 0u) {
  351. break;
  352. }
  353. if (c == '%') {
  354. //
  355. // Filter out flags
  356. //
  357. FormatFlags = 0u;
  358. v = 1;
  359. do {
  360. c = *sFormat;
  361. switch (c) {
  362. case '-': FormatFlags |= FORMAT_FLAG_LEFT_JUSTIFY; sFormat++; break;
  363. case '0': FormatFlags |= FORMAT_FLAG_PAD_ZERO; sFormat++; break;
  364. case '+': FormatFlags |= FORMAT_FLAG_PRINT_SIGN; sFormat++; break;
  365. case '#': FormatFlags |= FORMAT_FLAG_ALTERNATE; sFormat++; break;
  366. default: v = 0; break;
  367. }
  368. } while (v);
  369. //
  370. // filter out field with
  371. //
  372. FieldWidth = 0u;
  373. do {
  374. c = *sFormat;
  375. if ((c < '0') || (c > '9')) {
  376. break;
  377. }
  378. sFormat++;
  379. FieldWidth = (FieldWidth * 10u) + ((unsigned)c - '0');
  380. } while (1);
  381. //
  382. // Filter out precision (number of digits to display)
  383. //
  384. NumDigits = 0u;
  385. c = *sFormat;
  386. if (c == '.') {
  387. sFormat++;
  388. do {
  389. c = *sFormat;
  390. if ((c < '0') || (c > '9')) {
  391. break;
  392. }
  393. sFormat++;
  394. NumDigits = NumDigits * 10u + ((unsigned)c - '0');
  395. } while (1);
  396. }
  397. //
  398. // Filter out length modifier
  399. //
  400. c = *sFormat;
  401. do {
  402. if ((c == 'l') || (c == 'h')) {
  403. sFormat++;
  404. c = *sFormat;
  405. } else {
  406. break;
  407. }
  408. } while (1);
  409. //
  410. // Handle specifiers
  411. //
  412. switch (c) {
  413. case 'c': {
  414. char c0;
  415. v = va_arg(*pParamList, int);
  416. c0 = (char)v;
  417. _StoreChar(&BufferDesc, c0);
  418. break;
  419. }
  420. case 'd':
  421. v = va_arg(*pParamList, int);
  422. if (NumDigits != 0u) {
  423. FormatFlags &= ~FORMAT_FLAG_PAD_ZERO;
  424. }
  425. _PrintInt(&BufferDesc, v, 10u, NumDigits, FieldWidth, FormatFlags);
  426. break;
  427. case 'u':
  428. v = va_arg(*pParamList, int);
  429. if (NumDigits != 0u) {
  430. FormatFlags &= ~FORMAT_FLAG_PAD_ZERO;
  431. }
  432. _PrintUnsigned(&BufferDesc, (unsigned)v, 10u, NumDigits, FieldWidth, FormatFlags);
  433. break;
  434. case 'x':
  435. case 'X':
  436. v = va_arg(*pParamList, int);
  437. if (NumDigits != 0u) {
  438. FormatFlags &= ~FORMAT_FLAG_PAD_ZERO;
  439. }
  440. _PrintUnsigned(&BufferDesc, (unsigned)v, 16u, NumDigits, FieldWidth, FormatFlags);
  441. break;
  442. case 's':
  443. {
  444. const char * s = va_arg(*pParamList, const char *);
  445. if (s == NULL) {
  446. s = "(NULL)"; // Print (NULL) instead of crashing or breaking, as it is more informative to the user.
  447. }
  448. do {
  449. c = *s;
  450. s++;
  451. if (c == '\0') {
  452. break;
  453. }
  454. _StoreChar(&BufferDesc, c);
  455. } while (BufferDesc.ReturnValue >= 0);
  456. }
  457. break;
  458. case 'p':
  459. v = va_arg(*pParamList, int);
  460. _PrintUnsigned(&BufferDesc, (unsigned)v, 16u, 8u, 8u, 0u);
  461. break;
  462. case '%':
  463. _StoreChar(&BufferDesc, '%');
  464. break;
  465. case 'f':{
  466. double f = va_arg(*pParamList, double);
  467. _PrintFloat(&BufferDesc, f, NumDigits, FieldWidth, FormatFlags);
  468. }
  469. break;
  470. default:
  471. break;
  472. }
  473. sFormat++;
  474. } else {
  475. _StoreChar(&BufferDesc, c);
  476. }
  477. } while (BufferDesc.ReturnValue >= 0);
  478. if (BufferDesc.ReturnValue > 0) {
  479. //
  480. // Write remaining data, if any
  481. //
  482. if (BufferDesc.Cnt != 0u) {
  483. SEGGER_RTT_Write(BufferIndex, acBuffer, BufferDesc.Cnt);
  484. }
  485. BufferDesc.ReturnValue += (int)BufferDesc.Cnt;
  486. }
  487. return BufferDesc.ReturnValue;
  488. }
  489. /*********************************************************************
  490. *
  491. * SEGGER_RTT_printf
  492. *
  493. * Function description
  494. * Stores a formatted string in SEGGER RTT control block.
  495. * This data is read by the host.
  496. *
  497. * Parameters
  498. * BufferIndex Index of "Up"-buffer to be used. (e.g. 0 for "Terminal")
  499. * sFormat Pointer to format string, followed by the arguments for conversion
  500. *
  501. * Return values
  502. * >= 0: Number of bytes which have been stored in the "Up"-buffer.
  503. * < 0: Error
  504. *
  505. * Notes
  506. * (1) Conversion specifications have following syntax:
  507. * %[flags][FieldWidth][.Precision]ConversionSpecifier
  508. * (2) Supported flags:
  509. * -: Left justify within the field width
  510. * +: Always print sign extension for signed conversions
  511. * 0: Pad with 0 instead of spaces. Ignored when using '-'-flag or precision
  512. * Supported conversion specifiers:
  513. * c: Print the argument as one char
  514. * d: Print the argument as a signed integer
  515. * u: Print the argument as an unsigned integer
  516. * x: Print the argument as an hexadecimal integer
  517. * s: Print the string pointed to by the argument
  518. * p: Print the argument as an 8-digit hexadecimal integer. (Argument shall be a pointer to void.)
  519. */
  520. int SEGGER_RTT_printf(unsigned BufferIndex, const char * sFormat, ...) {
  521. int r;
  522. va_list ParamList;
  523. va_start(ParamList, sFormat);
  524. r = SEGGER_RTT_vprintf(BufferIndex, sFormat, &ParamList);
  525. va_end(ParamList);
  526. return r;
  527. }
  528. //重定义fputc函数
  529. int fputc(int ch, FILE *f)
  530. {
  531. SEGGER_RTT_PutChar(0, ch);
  532. return ch;
  533. }
  534. /*************************** End of file ****************************/