NinjaFlight
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
printf.h
Go to the documentation of this file.
1 /*
2 File: printf.h
3 
4 Copyright (c) 2004,2012 Kustaa Nyholm / SpareTimeLabs
5 
6 All rights reserved.
7 
8 Redistribution and use in source and binary forms, with or without modification,
9 are permitted provided that the following conditions are met:
10 
11 Redistributions of source code must retain the above copyright notice, this list
12 of conditions and the following disclaimer.
13 
14 Redistributions in binary form must reproduce the above copyright notice, this
15 list of conditions and the following disclaimer in the documentation and/or other
16 materials provided with the distribution.
17 
18 Neither the name of the Kustaa Nyholm or SpareTimeLabs nor the names of its
19 contributors may be used to endorse or promote products derived from this software
20 without specific prior written permission.
21 
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
26 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
28 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31 OF SUCH DAMAGE.
32 
33 ----------------------------------------------------------------------
34 
35 This library is realy just two files: 'printf.h' and 'printf.c'.
36 
37 They provide a simple and small (+200 loc) printf functionality to
38 be used in embedded systems.
39 
40 I've found them so usefull in debugging that I do not bother with a
41 debugger at all.
42 
43 They are distributed in source form, so to use them, just compile them
44 into your project.
45 
46 Two printf variants are provided: printf and sprintf.
47 
48 The formats supported by this implementation are: 'd' 'u' 'c' 's' 'x' 'X'.
49 
50 Zero padding and field width are also supported.
51 
52 If the library is compiled with 'PRINTF_SUPPORT_LONG' defined then the
53 long specifier is also
54 supported. Note that this will pull in some long math routines (pun intended!)
55 and thus make your executable noticably longer.
56 
57 The memory foot print of course depends on the target cpu, compiler and
58 compiler options, but a rough guestimate (based on a H8S target) is about
59 1.4 kB for code and some twenty 'int's and 'char's, say 60 bytes of stack space.
60 Not too bad. Your milage may vary. By hacking the source code you can
61 get rid of some hunred bytes, I'm sure, but personally I feel the balance of
62 functionality and flexibility versus code size is close to optimal for
63 many embedded systems.
64 
65 To use the printf you need to supply your own character output function,
66 something like :
67 
68 void putc ( void* p, char c)
69  {
70  while (!SERIAL_PORT_EMPTY) ;
71  SERIAL_PORT_TX_REGISTER = c;
72  }
73 
74 Before you can call printf you need to initialize it to use your
75 character output function with something like:
76 
77 init_printf(NULL,putc);
78 
79 Notice the 'NULL' in 'init_printf' and the parameter 'void* p' in 'putc',
80 the NULL (or any pointer) you pass into the 'init_printf' will eventually be
81 passed to your 'putc' routine. This allows you to pass some storage space (or
82 anything realy) to the character output function, if necessary.
83 This is not often needed but it was implemented like that because it made
84 implementing the sprintf function so neat (look at the source code).
85 
86 The code is re-entrant, except for the 'init_printf' function, so it
87 is safe to call it from interupts too, although this may result in mixed output.
88 If you rely on re-entrancy, take care that your 'putc' function is re-entrant!
89 
90 The printf and sprintf functions are actually macros that translate to
91 'tfp_printf' and 'tfp_sprintf'. This makes it possible
92 to use them along with 'stdio.h' printf's in a single source file.
93 You just need to undef the names before you include the 'stdio.h'.
94 Note that these are not function like macros, so if you have variables
95 or struct members with these names, things will explode in your face.
96 Without variadic macros this is the best we can do to wrap these
97 fucnction. If it is a problem just give up the macros and use the
98 functions directly or rename them.
99 
100 For further details see source code.
101 
102 regs Kusti, 23.10.2004
103 */
104 
105 #ifndef __TFP_PRINTF__
106 #define __TFP_PRINTF__
107 
108 #include <stdarg.h>
109 
110 #include "drivers/serial.h"
111 
112 void init_printf(void *putp, void (*putf) (void *, char));
113 
114 int tfp_printf(const char *fmt, ...);
115 int tfp_sprintf(char *s, const char *fmt, ...);
116 
117 int tfp_format(void *putp, void (*putf) (void *, char), const char *fmt, va_list va);
118 
119 #define printf tfp_printf
120 #define sprintf tfp_sprintf
121 
122 void setPrintfSerialPort(serialPort_t *serialPort);
123 
124 void printfSupportInit(void);
125 
126 #endif
int tfp_sprintf(char *s, const char *fmt,...)
void printfSupportInit(void)
Definition: printf.c:214
void setPrintfSerialPort(serialPort_t *serialPort)
Definition: printf.c:220
void init_printf(void *putp, void(*putf)(void *, char))
int tfp_printf(const char *fmt,...)
int tfp_format(void *putp, void(*putf)(void *, char), const char *fmt, va_list va)
Definition: serial.h:43