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