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