tinystdio.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. File: tinyprintf.h
  3. Copyright (C) 2004 Kustaa Nyholm
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. This library is really just two files: 'tinyprintf.h' and 'tinyprintf.c'.
  16. They provide a simple and small (+400 loc) printf functionality to
  17. be used in embedded systems.
  18. I've found them so useful in debugging that I do not bother with a
  19. debugger at all.
  20. They are distributed in source form, so to use them, just compile them
  21. into your project.
  22. Two printf variants are provided: printf and the 'sprintf' family of
  23. functions ('snprintf', 'sprintf', 'vsnprintf', 'vsprintf').
  24. The formats supported by this implementation are:
  25. 'c' 'd' 'i' 'o' 'p' 'u' 's' 'x' 'X'.
  26. Zero padding and field width are also supported.
  27. If the library is compiled with 'PRINTF_SUPPORT_LONG' defined, then
  28. the long specifier is also supported. Note that this will pull in some
  29. long math routines (pun intended!) and thus make your executable
  30. noticeably longer. Likewise with 'PRINTF_LONG_LONG_SUPPORT' for the
  31. long long specifier, and with 'PRINTF_SIZE_T_SUPPORT' for the size_t
  32. specifier.
  33. The memory footprint of course depends on the target CPU, compiler and
  34. compiler options, but a rough guesstimate (based on a H8S target) is about
  35. 1.4 kB for code and some twenty 'int's and 'char's, say 60 bytes of stack space.
  36. Not too bad. Your mileage may vary. By hacking the source code you can
  37. get rid of some hundred bytes, I'm sure, but personally I feel the balance of
  38. functionality and flexibility versus code size is close to optimal for
  39. many embedded systems.
  40. To use the printf, you need to supply your own character output function,
  41. something like :
  42. void putc ( void* p, char c)
  43. {
  44. while (!SERIAL_PORT_EMPTY) ;
  45. SERIAL_PORT_TX_REGISTER = c;
  46. }
  47. Before you can call printf, you need to initialize it to use your
  48. character output function with something like:
  49. init_printf(NULL,putc);
  50. Notice the 'NULL' in 'init_printf' and the parameter 'void* p' in 'putc',
  51. the NULL (or any pointer) you pass into the 'init_printf' will eventually be
  52. passed to your 'putc' routine. This allows you to pass some storage space (or
  53. anything really) to the character output function, if necessary.
  54. This is not often needed but it was implemented like that because it made
  55. implementing the sprintf function so neat (look at the source code).
  56. The code is re-entrant, except for the 'init_printf' function, so it is safe
  57. to call it from interrupts too, although this may result in mixed output.
  58. If you rely on re-entrancy, take care that your 'putc' function is re-entrant!
  59. The printf and sprintf functions are actually macros that translate to
  60. 'tfp_printf' and 'tfp_sprintf' when 'TINYPRINTF_OVERRIDE_LIBC' is set
  61. (default). Setting it to 0 makes it possible to use them along with
  62. 'stdio.h' printf's in a single source file. When
  63. 'TINYPRINTF_OVERRIDE_LIBC' is set, please note that printf/sprintf are
  64. not function-like macros, so if you have variables or struct members
  65. with these names, things will explode in your face. Without variadic
  66. macros this is the best we can do to wrap these function. If it is a
  67. problem, just give up the macros and use the functions directly, or
  68. rename them.
  69. It is also possible to avoid defining tfp_printf and/or tfp_sprintf by
  70. clearing 'TINYPRINTF_DEFINE_TFP_PRINTF' and/or
  71. 'TINYPRINTF_DEFINE_TFP_SPRINTF' to 0. This allows for example to
  72. export only tfp_format, which is at the core of all the other
  73. functions.
  74. For further details see source code.
  75. regs Kusti, 23.10.2004
  76. 31.01.2015
  77. Update from Cebotari Vladislav
  78. cebotari.vladislav@gmail.com
  79. - Added floating point support with different precision in x.y format
  80. also with leading zeros possibility (like standart printf function).
  81. Floating point printf is tested on tiva launchpad (tm4c123gh6pm TI mcu)
  82. - Also vsscanf for floats and double %f - float, %F - double
  83. */
  84. #ifndef __TFP_PRINTF__
  85. #define __TFP_PRINTF__
  86. #include <stdarg.h>
  87. /* Global configuration */
  88. /* Set this to 0 if you do not want to provide tfp_printf */
  89. #ifndef TINYPRINTF_DEFINE_TFP_PRINTF
  90. # define TINYPRINTF_DEFINE_TFP_PRINTF 1
  91. #endif
  92. /* Set this to 0 if you do not want to provide
  93. tfp_sprintf/snprintf/vsprintf/vsnprintf */
  94. #ifndef TINYPRINTF_DEFINE_TFP_SPRINTF
  95. # define TINYPRINTF_DEFINE_TFP_SPRINTF 1
  96. #endif
  97. /* Set this to 0 if you do not want tfp_printf and
  98. tfp_{vsn,sn,vs,s}printf to be also available as
  99. printf/{vsn,sn,vs,s}printf */
  100. #ifndef TINYPRINTF_OVERRIDE_LIBC
  101. # define TINYPRINTF_OVERRIDE_LIBC 1
  102. #endif
  103. /* Optional external types dependencies */
  104. #if TINYPRINTF_DEFINE_TFP_SPRINTF
  105. // Do not depend on POSIX presence
  106. //# include <sys/types.h> /* size_t */
  107. // Use ISO C header
  108. #include <stddef.h>
  109. #endif
  110. /* Declarations */
  111. #ifdef __GNUC__
  112. # define _TFP_SPECIFY_PRINTF_FMT(fmt_idx,arg1_idx) \
  113. __attribute__((format (printf, fmt_idx, arg1_idx)))
  114. #else
  115. # define _TFP_SPECIFY_PRINTF_FMT(fmt_idx,arg1_idx)
  116. #endif
  117. #ifdef __cplusplus
  118. extern "C" {
  119. #endif
  120. struct __sFile
  121. {
  122. int unused;
  123. };
  124. typedef struct __sFILE FILE;
  125. typedef void (*putcf) (void *, char);
  126. /*
  127. 'tfp_format' really is the central function for all tinyprintf. For
  128. each output character after formatting, the 'putf' callback is
  129. called with 2 args:
  130. - an arbitrary void* 'putp' param defined by the user and
  131. passed unmodified from 'tfp_format',
  132. - the character.
  133. The 'tfp_printf' and 'tfp_sprintf' functions simply define their own
  134. callback and pass to it the right 'putp' it is expecting.
  135. */
  136. void tfp_format(void *putp, putcf putf, const char *fmt, va_list va);
  137. int tfp_vsscanf(const char* str, const char* format, ...);
  138. # if TINYPRINTF_OVERRIDE_LIBC
  139. # define sscanf tfp_vsscanf
  140. # endif
  141. #if TINYPRINTF_DEFINE_TFP_SPRINTF
  142. int tfp_vsnprintf(char *str, size_t size, const char *fmt, va_list ap);
  143. int tfp_snprintf(char *str, size_t size, const char *fmt, ...) \
  144. _TFP_SPECIFY_PRINTF_FMT(3, 4);
  145. int tfp_vsprintf(char *str, const char *fmt, va_list ap);
  146. int tfp_sprintf(char *str, const char *fmt, ...) \
  147. _TFP_SPECIFY_PRINTF_FMT(2, 3);
  148. # if TINYPRINTF_OVERRIDE_LIBC
  149. # define vsnprintf tfp_vsnprintf
  150. # define snprintf tfp_snprintf
  151. # define vsprintf tfp_vsprintf
  152. # define sprintf tfp_sprintf
  153. # endif
  154. #endif
  155. #if TINYPRINTF_DEFINE_TFP_PRINTF
  156. void init_printf(void *putp, putcf putf);
  157. void tfp_printf(char *fmt, ...) _TFP_SPECIFY_PRINTF_FMT(1, 2);
  158. # if TINYPRINTF_OVERRIDE_LIBC
  159. # define printf(...) tfp_printf(__VA_ARGS__)
  160. # endif
  161. #endif
  162. #ifdef __cplusplus
  163. }
  164. #endif
  165. #endif