nrf_fprintf_format.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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.14d *
  56. * *
  57. *********************************************************************/
  58. #include "sdk_common.h"
  59. #if NRF_MODULE_ENABLED(NRF_FPRINTF)
  60. #include <stdarg.h>
  61. #include "nrf_assert.h"
  62. #include "nrf_fprintf.h"
  63. #include "nrf_fprintf_format.h"
  64. #define NRF_CLI_FORMAT_FLAG_LEFT_JUSTIFY (1u << 0)
  65. #define NRF_CLI_FORMAT_FLAG_PAD_ZERO (1u << 1)
  66. #define NRF_CLI_FORMAT_FLAG_PRINT_SIGN (1u << 2)
  67. #define NRF_CLI_FORMAT_DOUBLE_DEF_PRECISION 6
  68. #define NRF_CLI_FORMAT_DOUBLE_SIGN_POSITION 63U
  69. #define NRF_CLI_FORMAT_DOUBLE_SIGN_MASK 1ULL
  70. #define NRF_CLI_FORMAT_DOUBLE_SIGN (NRF_CLI_FORMAT_DOUBLE_SIGN_MASK << NRF_CLI_FORMAT_DOUBLE_SIGN_POSITION)
  71. #define NRF_CLI_FORMAT_DOUBLE_EXP_POSITION 52U
  72. #define NRF_CLI_FORMAT_DOUBLE_EXP_MASK 0x7FFULL
  73. #define NRF_CLI_FORMAT_DOUBLE_EXP (NRF_CLI_FORMAT_DOUBLE_EXP_MASK << NRF_CLI_FORMAT_DOUBLE_EXP_POSITION)
  74. #define NRF_CLI_FORMAT_DOUBLE_MANT_POSITION 0U
  75. #define NRF_CLI_FORMAT_DOUBLE_MANT_MASK 0xFFFFFFFFFFFFF
  76. #define NRF_CLI_FORMAT_DOUBLE_MANT (NRF_CLI_FORMAT_DOUBLE_MANT_MASK << NRF_CLI_FORMAT_DOUBLE_MANT_POSITION)
  77. #define NRF_CLI_FORMAT_DOUBLE_SIGN_GET(v) (!!((v) & NRF_CLI_FORMAT_DOUBLE_SIGN))
  78. #define NRF_CLI_FORMAT_DOUBLE_EXP_GET(v) (((v) & NRF_CLI_FORMAT_DOUBLE_EXP) >> NRF_CLI_FORMAT_DOUBLE_EXP_POSITION)
  79. #define NRF_CLI_FORMAT_DOUBLE_MANT_GET(v) (((v) & NRF_CLI_FORMAT_DOUBLE_MANT) >> NRF_CLI_FORMAT_DOUBLE_MANT_POSITION)
  80. #define NRF_CLI_FORMAT_REQ_SIGN_SPACE(s, f) ((s) | (!!((f) & NRF_CLI_FORMAT_FLAG_PRINT_SIGN)))
  81. #define HIGH_32(v) ((v) >> 32)
  82. #define LOW_32(v) (((1ULL << 32) - 1) & v)
  83. static void buffer_add(nrf_fprintf_ctx_t * const p_ctx, char c)
  84. {
  85. #if NRF_MODULE_ENABLED(NRF_FPRINTF_FLAG_AUTOMATIC_CR_ON_LF)
  86. if (c == '\n')
  87. {
  88. buffer_add(p_ctx, '\r');
  89. }
  90. #endif
  91. p_ctx->p_io_buffer[p_ctx->io_buffer_cnt++] = c;
  92. if (p_ctx->io_buffer_cnt >= p_ctx->io_buffer_size)
  93. {
  94. nrf_fprintf_buffer_flush(p_ctx);
  95. }
  96. }
  97. static void string_print(nrf_fprintf_ctx_t * const p_ctx,
  98. char const * p_str,
  99. uint32_t FieldWidth,
  100. uint32_t FormatFlags)
  101. {
  102. uint32_t Width = 0;
  103. char c;
  104. if ((FormatFlags & NRF_CLI_FORMAT_FLAG_LEFT_JUSTIFY) == NRF_CLI_FORMAT_FLAG_LEFT_JUSTIFY)
  105. {
  106. while ((c = *p_str) != '\0')
  107. {
  108. p_str++;
  109. Width++;
  110. buffer_add(p_ctx, c);
  111. }
  112. while ((FieldWidth > Width) && (FieldWidth > 0))
  113. {
  114. FieldWidth--;
  115. buffer_add(p_ctx, ' ');
  116. }
  117. }
  118. else
  119. {
  120. if (p_str != 0)
  121. {
  122. Width = strlen(p_str);
  123. }
  124. while ((FieldWidth > Width) && (FieldWidth > 0))
  125. {
  126. FieldWidth--;
  127. buffer_add(p_ctx, ' ');
  128. }
  129. while ((c = *p_str) != '\0')
  130. {
  131. p_str++;
  132. Width++;
  133. buffer_add(p_ctx, c);
  134. }
  135. }
  136. }
  137. static void unsigned_print(nrf_fprintf_ctx_t * const p_ctx,
  138. uint32_t v,
  139. uint32_t Base,
  140. uint32_t NumDigits,
  141. uint32_t FieldWidth,
  142. uint32_t FormatFlags)
  143. {
  144. static const char _aV2C[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  145. 'A', 'B', 'C', 'D', 'E', 'F' };
  146. uint32_t Div;
  147. uint32_t Value;
  148. uint32_t Width;
  149. char c;
  150. Value = v;
  151. //
  152. // Get actual field width
  153. //
  154. Width = 1u;
  155. while (Value >= Base)
  156. {
  157. Value = (Value / Base);
  158. Width++;
  159. }
  160. if (NumDigits > Width)
  161. {
  162. Width = NumDigits;
  163. }
  164. //
  165. // Print leading chars if necessary
  166. //
  167. if ((FormatFlags & NRF_CLI_FORMAT_FLAG_LEFT_JUSTIFY) == 0u)
  168. {
  169. if (FieldWidth != 0u)
  170. {
  171. if (((FormatFlags & NRF_CLI_FORMAT_FLAG_PAD_ZERO) == NRF_CLI_FORMAT_FLAG_PAD_ZERO) &&
  172. (NumDigits == 0u))
  173. {
  174. c = '0';
  175. }
  176. else
  177. {
  178. c = ' ';
  179. }
  180. while ((FieldWidth != 0u) && (Width < FieldWidth))
  181. {
  182. FieldWidth--;
  183. buffer_add(p_ctx, c);
  184. }
  185. }
  186. }
  187. Value = 1;
  188. /*
  189. * Compute Digit.
  190. * Loop until Digit has the value of the highest digit required.
  191. * Example: If the output is 345 (Base 10), loop 2 times until Digit is 100.
  192. */
  193. while (1)
  194. {
  195. /* User specified a min number of digits to print? => Make sure we loop at least that
  196. * often, before checking anything else (> 1 check avoids problems with NumDigits
  197. * being signed / unsigned)
  198. */
  199. if (NumDigits > 1u)
  200. {
  201. NumDigits--;
  202. }
  203. else
  204. {
  205. Div = v / Value;
  206. // Is our divider big enough to extract the highest digit from value? => Done
  207. if (Div < Base)
  208. {
  209. break;
  210. }
  211. }
  212. Value *= Base;
  213. }
  214. //
  215. // Output digits
  216. //
  217. do
  218. {
  219. Div = v / Value;
  220. v -= Div * Value;
  221. buffer_add(p_ctx, _aV2C[Div]);
  222. Value /= Base;
  223. } while (Value);
  224. //
  225. // Print trailing spaces if necessary
  226. //
  227. if ((FormatFlags & NRF_CLI_FORMAT_FLAG_LEFT_JUSTIFY) == NRF_CLI_FORMAT_FLAG_LEFT_JUSTIFY)
  228. {
  229. if (FieldWidth != 0u)
  230. {
  231. while ((FieldWidth != 0u) && (Width < FieldWidth))
  232. {
  233. FieldWidth--;
  234. buffer_add(p_ctx, ' ');
  235. }
  236. }
  237. }
  238. }
  239. static void int_print(nrf_fprintf_ctx_t * const p_ctx,
  240. int32_t v,
  241. uint32_t Base,
  242. uint32_t NumDigits,
  243. uint32_t FieldWidth,
  244. uint32_t FormatFlags)
  245. {
  246. uint32_t Width;
  247. int32_t Number;
  248. Number = (v < 0) ? -v : v;
  249. //
  250. // Get actual field width
  251. //
  252. Width = 1u;
  253. while (Number >= (int32_t)Base)
  254. {
  255. Number = (Number / (int32_t)Base);
  256. Width++;
  257. }
  258. if (NumDigits > Width)
  259. {
  260. Width = NumDigits;
  261. }
  262. if ((FieldWidth > 0u) && ((v < 0) ||
  263. ((FormatFlags & NRF_CLI_FORMAT_FLAG_PRINT_SIGN) == NRF_CLI_FORMAT_FLAG_PRINT_SIGN)))
  264. {
  265. FieldWidth--;
  266. }
  267. //
  268. // Print leading spaces if necessary
  269. //
  270. if ((((FormatFlags & NRF_CLI_FORMAT_FLAG_PAD_ZERO) == 0u) || (NumDigits != 0u)) &&
  271. ((FormatFlags & NRF_CLI_FORMAT_FLAG_LEFT_JUSTIFY) == 0u))
  272. {
  273. if (FieldWidth != 0u)
  274. {
  275. while ((FieldWidth != 0u) && (Width < FieldWidth))
  276. {
  277. FieldWidth--;
  278. buffer_add(p_ctx, ' ');
  279. }
  280. }
  281. }
  282. //
  283. // Print sign if necessary
  284. //
  285. if (v < 0)
  286. {
  287. v = -v;
  288. buffer_add(p_ctx, '-');
  289. }
  290. else if ((FormatFlags & NRF_CLI_FORMAT_FLAG_PRINT_SIGN) == NRF_CLI_FORMAT_FLAG_PRINT_SIGN)
  291. {
  292. buffer_add(p_ctx, '+');
  293. }
  294. else
  295. {
  296. /* do nothing */
  297. }
  298. //
  299. // Print leading zeros if necessary
  300. //
  301. if (((FormatFlags & NRF_CLI_FORMAT_FLAG_PAD_ZERO) == NRF_CLI_FORMAT_FLAG_PAD_ZERO) &&
  302. ((FormatFlags & NRF_CLI_FORMAT_FLAG_LEFT_JUSTIFY) == 0u) && (NumDigits == 0u))
  303. {
  304. if (FieldWidth != 0u)
  305. {
  306. while ((FieldWidth != 0u) && (Width < FieldWidth))
  307. {
  308. FieldWidth--;
  309. buffer_add(p_ctx, '0');
  310. }
  311. }
  312. }
  313. //
  314. // Print number without sign
  315. //
  316. unsigned_print(p_ctx, (uint32_t)v, Base, NumDigits, FieldWidth, FormatFlags);
  317. }
  318. #if NRF_MODULE_ENABLED(NRF_FPRINTF_DOUBLE)
  319. static void fill_space(nrf_fprintf_ctx_t * const p_ctx,
  320. uint8_t len,
  321. bool zeros)
  322. {
  323. for (; len > 0; len--)
  324. {
  325. if (zeros)
  326. {
  327. buffer_add(p_ctx, '0');
  328. }
  329. else
  330. {
  331. buffer_add(p_ctx, ' ');
  332. }
  333. }
  334. }
  335. static void float_print(nrf_fprintf_ctx_t * const p_ctx,
  336. double v,
  337. uint32_t digits,
  338. uint32_t width,
  339. uint32_t format,
  340. bool uppercase)
  341. {
  342. bool sign, transform = false;
  343. uint64_t num, mant, lead, low, base, res, carry, x, s0, s1, s2, s3, fr;
  344. int32_t exp;
  345. uint8_t highest, offset, lead_len = 0, skipped = 0;
  346. uint8_t precision = digits ? digits + 1 : NRF_CLI_FORMAT_DOUBLE_DEF_PRECISION + 1;
  347. /* Default digits should be -1, because 0 could be a requirement, not the default.
  348. * This should be changed for the whole library.
  349. */
  350. if ((v > 0.0) && (v < 1.0))
  351. {
  352. v += 1.0;
  353. transform = true;
  354. }
  355. else if ((v > -1.0) && (v < 0.0))
  356. {
  357. v -= 1.0;
  358. transform = true;
  359. }
  360. memcpy(&num, &v, sizeof(num));
  361. sign = NRF_CLI_FORMAT_DOUBLE_SIGN_GET(num);
  362. exp = NRF_CLI_FORMAT_DOUBLE_EXP_GET(num);
  363. mant = NRF_CLI_FORMAT_DOUBLE_MANT_GET(num);
  364. /* Special cases */
  365. if (exp == NRF_CLI_FORMAT_DOUBLE_EXP_MASK)
  366. {
  367. if (width && (!(format & NRF_CLI_FORMAT_FLAG_LEFT_JUSTIFY)))
  368. {
  369. fill_space(p_ctx, width - 3 - NRF_CLI_FORMAT_REQ_SIGN_SPACE(sign, format), false);
  370. }
  371. if (sign)
  372. {
  373. buffer_add(p_ctx, '-');
  374. }
  375. else if (format & NRF_CLI_FORMAT_FLAG_PRINT_SIGN)
  376. {
  377. buffer_add(p_ctx, '+');
  378. }
  379. if (mant != 0)
  380. {
  381. if(uppercase)
  382. {
  383. buffer_add(p_ctx, 'N');
  384. buffer_add(p_ctx, 'A');
  385. buffer_add(p_ctx, 'N');
  386. }
  387. else
  388. {
  389. buffer_add(p_ctx, 'n');
  390. buffer_add(p_ctx, 'a');
  391. buffer_add(p_ctx, 'n');
  392. }
  393. }
  394. else
  395. {
  396. if(uppercase)
  397. {
  398. buffer_add(p_ctx, 'I');
  399. buffer_add(p_ctx, 'N');
  400. buffer_add(p_ctx, 'F');
  401. }
  402. else
  403. {
  404. buffer_add(p_ctx, 'i');
  405. buffer_add(p_ctx, 'n');
  406. buffer_add(p_ctx, 'f');
  407. }
  408. }
  409. return;
  410. }
  411. /* Add leading 1 to mantissa (except 0.0) */
  412. if ((mant != 0) || (exp != 0))
  413. {
  414. mant |= (1ULL << 52);
  415. }
  416. /* Convert the exponent */
  417. exp = exp - 1023;
  418. /* Whole numbers */
  419. offset = 52 - exp;
  420. if (offset > 64)
  421. {
  422. /* Float fraction offset overflow */
  423. return;
  424. }
  425. lead = (mant >> (offset));
  426. /* Fraction */
  427. low = mant & (~(lead << offset));
  428. while (((low & 0x1) == 0) && low > 0)
  429. {
  430. low = low >> 1U;
  431. skipped++;
  432. }
  433. highest = (offset - skipped);
  434. base = 1;
  435. for(uint8_t i = 0; i < precision; i++)
  436. {
  437. base *= 10;
  438. }
  439. /* Handle multiplication with possible overflow */
  440. x = LOW_32(low) * LOW_32(base);
  441. s0 = LOW_32(x);
  442. x = HIGH_32(low) * LOW_32(base) + HIGH_32(x);
  443. s1 = LOW_32(x);
  444. s2 = HIGH_32(x);
  445. x = s1 + LOW_32(low) * HIGH_32(base);
  446. s1 = LOW_32(x);
  447. x = s2 + HIGH_32(low) * HIGH_32(base) + HIGH_32(x);
  448. s2 = LOW_32(x);
  449. s3 = HIGH_32(x);
  450. res = s1 << 32 | s0;
  451. carry = s3 << 32 | s2;
  452. /* Divide and combine */
  453. carry = carry << (64 - highest);
  454. res = res >> highest;
  455. fr = res | carry;
  456. /* Roundup */
  457. if (fr%10 >= 5)
  458. {
  459. fr /= 10;
  460. fr++;
  461. }
  462. else
  463. {
  464. fr /= 10;
  465. }
  466. precision--;
  467. if (transform && (lead == 1))
  468. {
  469. lead = 0;
  470. }
  471. /* Maximum precision handled by int_print() is 10 */
  472. if (precision > 10)
  473. {
  474. for (uint8_t delta = precision - 10; delta > 0; delta--)
  475. {
  476. fr /= 10;
  477. }
  478. precision = 10;
  479. }
  480. res = lead;
  481. while (res > 0)
  482. {
  483. res /= 10;
  484. lead_len++;
  485. }
  486. if ((lead == 0) && (fr == 0))
  487. {
  488. lead_len = 1;
  489. }
  490. if(lead_len == 0)
  491. {
  492. lead_len = 1;
  493. }
  494. if (width && (!(format & NRF_CLI_FORMAT_FLAG_LEFT_JUSTIFY)))
  495. {
  496. int32_t space = width - lead_len - precision - NRF_CLI_FORMAT_REQ_SIGN_SPACE(sign, format) - 1;
  497. if (space > 0)
  498. {
  499. fill_space(p_ctx, space, format & NRF_CLI_FORMAT_FLAG_PAD_ZERO);
  500. }
  501. }
  502. if (sign)
  503. {
  504. buffer_add(p_ctx, '-');
  505. }
  506. else if (format & NRF_CLI_FORMAT_FLAG_PRINT_SIGN)
  507. {
  508. buffer_add(p_ctx, '+');
  509. }
  510. int_print(p_ctx,
  511. lead,
  512. 10u,
  513. 0,
  514. 0,
  515. 0);
  516. buffer_add(p_ctx, '.');
  517. int_print(p_ctx,
  518. fr,
  519. 10u,
  520. precision,
  521. 0,
  522. 0);
  523. if (width && (format & NRF_CLI_FORMAT_FLAG_LEFT_JUSTIFY))
  524. {
  525. int32_t space = width - lead_len - precision - NRF_CLI_FORMAT_REQ_SIGN_SPACE(sign, format) - 1;
  526. if (space > 0)
  527. {
  528. fill_space(p_ctx, space, false);
  529. }
  530. }
  531. }
  532. #endif
  533. void nrf_fprintf_fmt(nrf_fprintf_ctx_t * const p_ctx,
  534. char const * p_fmt,
  535. va_list * p_args)
  536. {
  537. ASSERT(p_ctx != NULL);
  538. ASSERT(p_ctx->fwrite != NULL);
  539. ASSERT(p_ctx->p_io_buffer != NULL);
  540. ASSERT(p_ctx->io_buffer_size > 0);
  541. if (p_fmt == NULL)
  542. {
  543. return;
  544. }
  545. char c;
  546. int32_t v;
  547. uint32_t NumDigits;
  548. uint32_t FormatFlags;
  549. uint32_t FieldWidth;
  550. do
  551. {
  552. c = *p_fmt;
  553. p_fmt++;
  554. if (c == 0u)
  555. {
  556. break;
  557. }
  558. if (c == '%')
  559. {
  560. //
  561. // Filter out flags
  562. //
  563. FormatFlags = 0u;
  564. v = 1;
  565. do
  566. {
  567. c = *p_fmt;
  568. switch (c)
  569. {
  570. case '-':
  571. FormatFlags |= NRF_CLI_FORMAT_FLAG_LEFT_JUSTIFY;
  572. p_fmt++;
  573. break;
  574. case '0':
  575. FormatFlags |= NRF_CLI_FORMAT_FLAG_PAD_ZERO;
  576. p_fmt++;
  577. break;
  578. case '+':
  579. FormatFlags |= NRF_CLI_FORMAT_FLAG_PRINT_SIGN;
  580. p_fmt++;
  581. break;
  582. default:
  583. v = 0;
  584. break;
  585. }
  586. } while (v);
  587. //
  588. // filter out field width
  589. //
  590. FieldWidth = 0u;
  591. do
  592. {
  593. if (c == '*')
  594. {
  595. /*lint -save -e64 -e56*/
  596. FieldWidth += va_arg(*p_args, unsigned);
  597. /*lint -restore*/
  598. p_fmt++;
  599. break;
  600. }
  601. c = *p_fmt;
  602. if ((c < '0') || (c > '9'))
  603. {
  604. break;
  605. }
  606. p_fmt++;
  607. FieldWidth = (FieldWidth * 10u) + (c - '0');
  608. } while (1);
  609. //
  610. // Filter out precision (number of digits to display)
  611. //
  612. NumDigits = 0u;
  613. c = *p_fmt;
  614. if (c == '.')
  615. {
  616. p_fmt++;
  617. do
  618. {
  619. c = *p_fmt;
  620. if ((c < '0') || (c > '9'))
  621. {
  622. break;
  623. }
  624. p_fmt++;
  625. NumDigits = NumDigits * 10u + (c - '0');
  626. } while (1);
  627. }
  628. //
  629. // Filter out length modifier
  630. //
  631. c = *p_fmt;
  632. do
  633. {
  634. if ((c == 'l') || (c == 'h'))
  635. {
  636. p_fmt++;
  637. c = *p_fmt;
  638. }
  639. else
  640. {
  641. break;
  642. }
  643. } while (1);
  644. //
  645. // Handle specifiers
  646. //
  647. /*lint -save -e64*/
  648. switch (c)
  649. {
  650. case 'c':
  651. {
  652. char c0;
  653. v = va_arg(*p_args, int32_t);
  654. c0 = (char)v;
  655. buffer_add(p_ctx, c0);
  656. break;
  657. }
  658. case 'd':
  659. case 'i':
  660. v = va_arg(*p_args, int32_t);
  661. int_print(p_ctx,
  662. v,
  663. 10u,
  664. NumDigits,
  665. FieldWidth,
  666. FormatFlags);
  667. break;
  668. case 'u':
  669. v = va_arg(*p_args, int32_t);
  670. unsigned_print(p_ctx,
  671. (uint32_t)v,
  672. 10u,
  673. NumDigits,
  674. FieldWidth,
  675. FormatFlags);
  676. break;
  677. case 'x':
  678. case 'X':
  679. v = va_arg(*p_args, int32_t);
  680. unsigned_print(p_ctx,
  681. (uint32_t)v,
  682. 16u,
  683. NumDigits,
  684. FieldWidth,
  685. FormatFlags);
  686. break;
  687. case 's':
  688. {
  689. char const * p_s = va_arg(*p_args, const char *);
  690. string_print(p_ctx, p_s, FieldWidth, FormatFlags);
  691. break;
  692. }
  693. case 'p':
  694. v = va_arg(*p_args, int32_t);
  695. buffer_add(p_ctx, '0');
  696. buffer_add(p_ctx, 'x');
  697. unsigned_print(p_ctx, (uint32_t)v, 16u, 8u, 8u, 0);
  698. break;
  699. case '%':
  700. buffer_add(p_ctx, '%');
  701. break;
  702. #if NRF_MODULE_ENABLED(NRF_FPRINTF_DOUBLE)
  703. case 'f':
  704. {
  705. double dbl = va_arg(*p_args, double);
  706. float_print(p_ctx,
  707. dbl,
  708. NumDigits,
  709. FieldWidth,
  710. FormatFlags,
  711. false);
  712. break;
  713. }
  714. case 'F':
  715. {
  716. double dbl = va_arg(*p_args, double);
  717. float_print(p_ctx,
  718. dbl,
  719. NumDigits,
  720. FieldWidth,
  721. FormatFlags,
  722. true);
  723. break;
  724. }
  725. #endif
  726. default:
  727. break;
  728. }
  729. /*lint -restore*/
  730. p_fmt++;
  731. }
  732. else
  733. {
  734. buffer_add(p_ctx, c);
  735. }
  736. } while (*p_fmt != '\0');
  737. if (p_ctx->auto_flush)
  738. {
  739. nrf_fprintf_buffer_flush(p_ctx);
  740. }
  741. }
  742. #endif // NRF_MODULE_ENABLED(NRF_FPRINTF)