printf.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**
  2. * File: printf.h
  3. *
  4. * Copyright (c) 2004,2012 Kustaa Nyholm / SpareTimeLabs
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Kustaa Nyholm or SpareTimeLabs nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * You should have received a copy of the GNU Lesser General Public License
  30. * along with this library; if not, write to the Free Software Foundation,
  31. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  32. *
  33. * This library is really just two files: 'tinyprintf.h' and 'tinyprintf.c'.
  34. *
  35. * They provide a simple and small (+400 loc) printf functionality to be used
  36. * in embedded systems.
  37. *
  38. * I've found them so useful in debugging that I do not bother with a debugger
  39. * at all.
  40. *
  41. * They are distributed in source form, so to use them, just compile them into
  42. * your project.
  43. *
  44. * Two printf variants are provided: printf and the 'sprintf' family of
  45. * functions ('snprintf', 'sprintf', 'vsnprintf', 'vsprintf').
  46. *
  47. * The formats supported by this implementation are: 'c' 'd' 'i' 'o' 'p' 'u'
  48. * 's' 'x' 'X'.
  49. *
  50. * Zero padding, field width, and precision are also supported.
  51. *
  52. * If the library is compiled with 'PRINTF_SUPPORT_LONG' defined, then the
  53. * long specifier is also supported. Note that this will pull in some long
  54. * math routines (pun intended!) and thus make your executable noticeably
  55. * longer. Likewise with 'PRINTF_LONG_LONG_SUPPORT' for the long long
  56. * specifier, and with 'PRINTF_SIZE_T_SUPPORT' for the size_t specifier.
  57. *
  58. * The memory footprint of course depends on the target CPU, compiler and
  59. * compiler options, but a rough guesstimate (based on a H8S target) is about
  60. * 1.4 kB for code and some twenty 'int's and 'char's, say 60 bytes of stack
  61. * space. Not too bad. Your mileage may vary. By hacking the source code you
  62. * can get rid of some hundred bytes, I'm sure, but personally I feel the
  63. * balance of functionality and flexibility versus code size is close to
  64. * optimal for many embedded systems.
  65. *
  66. * To use the printf, you need to supply your own character output function,
  67. * something like :
  68. *
  69. * void putc ( void* p, char c) { while (!SERIAL_PORT_EMPTY) ;
  70. * SERIAL_PORT_TX_REGISTER = c; }
  71. *
  72. * Before you can call printf, you need to initialize it to use your character
  73. * output function with something like:
  74. *
  75. * init_printf(NULL,putc);
  76. *
  77. * Notice the 'NULL' in 'init_printf' and the parameter 'void* p' in 'putc',
  78. * the NULL (or any pointer) you pass into the 'init_printf' will eventually
  79. * be passed to your 'putc' routine. This allows you to pass some storage
  80. * space (or anything really) to the character output function, if necessary.
  81. * This is not often needed but it was implemented like that because it made
  82. * implementing the sprintf function so neat (look at the source code).
  83. *
  84. * The code is re-entrant, except for the 'init_printf' function, so it is
  85. * safe to call it from interrupts too, although this may result in mixed
  86. * output. If you rely on re-entrancy, take care that your 'putc' function is
  87. * re-entrant!
  88. *
  89. * The printf and sprintf functions are actually macros that translate to
  90. * 'tfp_printf' and 'tfp_sprintf' when 'TINYPRINTF_OVERRIDE_LIBC' is set
  91. * (default). Setting it to 0 makes it possible to use them along with
  92. * 'stdio.h' printf's in a single source file. When 'TINYPRINTF_OVERRIDE_LIBC'
  93. * is set, please note that printf/sprintf are not function-like macros, so if
  94. * you have variables or struct members with these names, things will explode
  95. * in your face. Without variadic macros this is the best we can do to wrap
  96. * these function. If it is a problem, just give up the macros and use the
  97. * functions directly, or rename them.
  98. *
  99. * It is also possible to avoid defining tfp_printf and/or tfp_sprintf by
  100. * clearing 'TINYPRINTF_DEFINE_TFP_PRINTF' and/or
  101. * 'TINYPRINTF_DEFINE_TFP_SPRINTF' to 0. This allows for example to export
  102. * only tfp_format, which is at the core of all the other functions.
  103. *
  104. * For further details see source code.
  105. *
  106. * regs Kusti, 23.10.2004
  107. */
  108. #ifndef _BSP_PRINTF_H
  109. #define _BSP_PRINTF_H
  110. #include <stdarg.h>
  111. #include <stddef.h>
  112. /* Global configuration */
  113. /* Set this to 0 if you do not want to provide tfp_printf */
  114. #ifndef TINYPRINTF_DEFINE_TFP_PRINTF
  115. #define TINYPRINTF_DEFINE_TFP_PRINTF 1
  116. #endif
  117. /**
  118. * Set this to 0 if you do not want to provide
  119. * tfp_sprintf/snprintf/vsprintf/vsnprintf
  120. */
  121. #ifndef TINYPRINTF_DEFINE_TFP_SPRINTF
  122. #define TINYPRINTF_DEFINE_TFP_SPRINTF 1
  123. #endif
  124. /**
  125. * Set this to 0 if you do not want tfp_printf and
  126. * tfp_{vsn,sn,vs,s}printf to be also available as
  127. * printf/{vsn,sn,vs,s}printf
  128. */
  129. #ifndef TINYPRINTF_OVERRIDE_LIBC
  130. #define TINYPRINTF_OVERRIDE_LIBC 0
  131. #endif
  132. /* Optional external types dependencies */
  133. /* Declarations */
  134. #if defined(__GNUC__)
  135. #define _TFP_SPECIFY_PRINTF_FMT(fmt_idx, arg1_idx) \
  136. __attribute__((format(printf, fmt_idx, arg1_idx)))
  137. #else
  138. #define _TFP_SPECIFY_PRINTF_FMT(fmt_idx, arg1_idx)
  139. #endif
  140. #ifdef __cplusplus
  141. extern "C" {
  142. #endif
  143. typedef void (*putcf)(void *, char);
  144. /**
  145. * 'tfp_format' really is the central function for all tinyprintf. For
  146. * each output character after formatting, the 'putf' callback is
  147. * called with 2 args:
  148. * - an arbitrary void* 'putp' param defined by the user and
  149. * passed unmodified from 'tfp_format',
  150. * - the character.
  151. * The 'tfp_printf' and 'tfp_sprintf' functions simply define their own
  152. * callback and pass to it the right 'putp' it is expecting.
  153. */
  154. void tfp_format(void *putp, putcf putf, const char *fmt, va_list va);
  155. #if TINYPRINTF_DEFINE_TFP_SPRINTF
  156. int tfp_vsnprintf(char *str, size_t size, const char *fmt, va_list ap);
  157. int tfp_snprintf(char *str, size_t size, const char *fmt, ...)
  158. _TFP_SPECIFY_PRINTF_FMT(3, 4);
  159. int tfp_vsprintf(char *str, const char *fmt, va_list ap);
  160. int tfp_sprintf(char *str, const char *fmt, ...) _TFP_SPECIFY_PRINTF_FMT(2, 3);
  161. #if TINYPRINTF_OVERRIDE_LIBC
  162. #define vsnprintf tfp_vsnprintf
  163. #define snprintf tfp_snprintf
  164. #define vsprintf tfp_vsprintf
  165. #define sprintf tfp_sprintf
  166. #endif
  167. #endif
  168. #if TINYPRINTF_DEFINE_TFP_PRINTF
  169. void init_printf(void *putp, putcf putf);
  170. void tfp_printf(char *fmt, ...) _TFP_SPECIFY_PRINTF_FMT(1, 2);
  171. #if TINYPRINTF_OVERRIDE_LIBC
  172. #define printf tfp_printf
  173. #ifdef __cplusplus
  174. #include <forward_list>
  175. namespace std
  176. {
  177. template <typename... Args>
  178. auto tfp_printf(Args &&... args) -> decltype(::tfp_printf(std::forward<Args>(args)...))
  179. {
  180. return ::tfp_printf(std::forward<Args>(args)...);
  181. }
  182. } // namespace std
  183. #endif
  184. #endif
  185. #endif
  186. int printk(const char *format, ...) _TFP_SPECIFY_PRINTF_FMT(1, 2);
  187. #ifdef __cplusplus
  188. }
  189. #endif
  190. #endif /* _BSP_PRINTF_H */