1+ /*
2+ File: printf.h
3+
4+ Copyright (C) 2004 Kustaa Nyholm
5+
6+ This library is free software; you can redistribute it and/or
7+ modify it under the terms of the GNU Lesser General Public
8+ License as published by the Free Software Foundation; either
9+ version 2.1 of the License, or (at your option) any later version.
10+
11+ This library is distributed in the hope that it will be useful,
12+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14+ See the GNU Lesser General Public License for more details.
15+
16+ You should have received a copy of the GNU Lesser General Public
17+ License along with this library; if not, write to the Free Software
18+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19+
20+ This library is really just two files: 'printf.h' and 'printf.c'.
21+
22+ They provide a simple and small (+200 loc) printf functionality to
23+ be used in embedded systems.
24+
25+ I've found them so usefull in debugging that I do not bother with a
26+ debugger at all.
27+
28+ They are distributed in source form, so to use them, just compile them
29+ into your project.
30+
31+ Two printf variants are provided: printf and sprintf.
32+
33+ The formats supported by this implementation are: 'd' 'u' 'c' 's' 'x' 'X'.
34+
35+ Zero padding and field width are also supported.
36+
37+ If the library is compiled with 'PRINTF_SUPPORT_LONG' defined then the
38+ long specifier is also
39+ supported. Note that this will pull in some long math routines (pun intended!)
40+ and thus make your executable noticably longer.
41+
42+ The memory foot print of course depends on the target cpu, compiler and
43+ compiler options, but a rough guestimate (based on a H8S target) is about
44+ 1.4 kB for code and some twenty 'int's and 'char's, say 60 bytes of stack space.
45+ Not too bad. Your milage may vary. By hacking the source code you can
46+ get rid of some hunred bytes, I'm sure, but personally I feel the balance of
47+ functionality and flexibility versus code size is close to optimal for
48+ many embedded systems.
49+
50+ To use the printf you need to supply your own character output function,
51+ something like :
52+
53+ void putc ( void* p, char c)
54+ {
55+ while (!SERIAL_PORT_EMPTY) ;
56+ SERIAL_PORT_TX_REGISTER = c;
57+ }
58+
59+ Before you can call printf you need to initialize it to use your
60+ character output function with something like:
61+
62+ init_printf(NULL,putc);
63+
64+ Notice the 'NULL' in 'init_printf' and the parameter 'void* p' in 'putc',
65+ the NULL (or any pointer) you pass into the 'init_printf' will eventually be
66+ passed to your 'putc' routine. This allows you to pass some storage space (or
67+ anything really) to the character output function, if necessary.
68+ This is not often needed but it was implemented like that because it made
69+ implementing the sprintf function so neat (look at the source code).
70+
71+ The code is re-entrant, except for the 'init_printf' function, so it
72+ is safe to call it from interupts too, although this may result in mixed output.
73+ If you rely on re-entrancy, take care that your 'putc' function is re-entrant!
74+
75+ The printf and sprintf functions are actually macros that translate to
76+ 'tfp_printf' and 'tfp_sprintf'. This makes it possible
77+ to use them along with 'stdio.h' printf's in a single source file.
78+ You just need to undef the names before you include the 'stdio.h'.
79+ Note that these are not function like macros, so if you have variables
80+ or struct members with these names, things will explode in your face.
81+ Without variadic macros this is the best we can do to wrap these
82+ fucnction. If it is a problem just give up the macros and use the
83+ functions directly or rename them.
84+
85+ For further details see source code.
86+
87+ regs Kusti, 23.10.2004
88+ */
89+
90+
91+ #ifndef __TFP_PRINTF__
92+ #define __TFP_PRINTF__
93+
94+ #include <stdarg.h>
95+
96+ void init_printf (void * putp ,void (* putf ) (void * ,char ));
97+
98+ void tfp_printf (char * fmt , ...);
99+ void tfp_sprintf (char * s ,char * fmt , ...);
100+
101+ void tfp_format (void * putp ,void (* putf ) (void * ,char ),char * fmt , va_list va );
102+
103+ void test (char c );
104+
105+ #define printf tfp_printf
106+ #define sprintf tfp_sprintf
107+
108+ #endif
0 commit comments