1 | /* This source file is part of the ATMEL AVR32-SoftwareFramework-AT32UC3A-1.4.0 Release */ |
---|
2 | |
---|
3 | /*This file has been prepared for Doxygen automatic documentation generation.*/ |
---|
4 | /*! \file ********************************************************************* |
---|
5 | * |
---|
6 | * \brief sprintf functions to replace newlib for AVR32 UC3. |
---|
7 | * |
---|
8 | * - Compiler: IAR EWAVR32 and GNU GCC for AVR32 |
---|
9 | * - Supported devices: All AVR32 devices can be used. |
---|
10 | * - AppNote: |
---|
11 | * |
---|
12 | * \author Atmel Corporation: http://www.atmel.com \n |
---|
13 | * Support and FAQ: http://support.atmel.no/ |
---|
14 | * |
---|
15 | *****************************************************************************/ |
---|
16 | |
---|
17 | /* Copyright (C) 2006-2008, Atmel Corporation All rights reserved. |
---|
18 | * |
---|
19 | * Redistribution and use in source and binary forms, with or without |
---|
20 | * modification, are permitted provided that the following conditions are met: |
---|
21 | * |
---|
22 | * 1. Redistributions of source code must retain the above copyright notice, |
---|
23 | * this list of conditions and the following disclaimer. |
---|
24 | * |
---|
25 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
---|
26 | * this list of conditions and the following disclaimer in the documentation |
---|
27 | * and/or other materials provided with the distribution. |
---|
28 | * |
---|
29 | * 3. The name of ATMEL may not be used to endorse or promote products derived |
---|
30 | * from this software without specific prior written permission. |
---|
31 | * |
---|
32 | * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED |
---|
33 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
---|
34 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND |
---|
35 | * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, |
---|
36 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
37 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
---|
38 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
---|
39 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
40 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
---|
41 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
42 | */ |
---|
43 | |
---|
44 | /* |
---|
45 | Copyright 2001, 2002 Georges Menie (www.menie.org) |
---|
46 | stdarg version contributed by Christian Ettinger |
---|
47 | |
---|
48 | This program is free software; you can redistribute it and/or modify |
---|
49 | it under the terms of the GNU Lesser General Public License as published by |
---|
50 | the Free Software Foundation; either version 2 of the License, or |
---|
51 | (at your option) any later version. |
---|
52 | |
---|
53 | This program is distributed in the hope that it will be useful, |
---|
54 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
55 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
56 | GNU Lesser General Public License for more details. |
---|
57 | |
---|
58 | You should have received a copy of the GNU Lesser General Public License |
---|
59 | along with this program; if not, write to the Free Software |
---|
60 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
61 | */ |
---|
62 | |
---|
63 | /* |
---|
64 | putchar is the only external dependency for this file, |
---|
65 | if you have a working putchar, leave it commented out. |
---|
66 | If not, uncomment the define below and |
---|
67 | replace outbyte(c) by your own function call. |
---|
68 | |
---|
69 | */ |
---|
70 | |
---|
71 | |
---|
72 | #include <stdarg.h> |
---|
73 | |
---|
74 | static void printchar(char **str, int c) |
---|
75 | { |
---|
76 | extern int board_putchar(char c); |
---|
77 | |
---|
78 | if (str) { |
---|
79 | **str = c; |
---|
80 | ++(*str); |
---|
81 | } |
---|
82 | else (void) board_putchar(c); |
---|
83 | } |
---|
84 | |
---|
85 | #define PAD_RIGHT 1 |
---|
86 | #define PAD_ZERO 2 |
---|
87 | |
---|
88 | static int prints(char **out, const char *string, int width, int pad) |
---|
89 | { |
---|
90 | register int pc = 0, padchar = ' '; |
---|
91 | |
---|
92 | if (width > 0) { |
---|
93 | register int len = 0; |
---|
94 | register const char *ptr; |
---|
95 | for (ptr = string; *ptr; ++ptr) ++len; |
---|
96 | if (len >= width) width = 0; |
---|
97 | else width -= len; |
---|
98 | if (pad & PAD_ZERO) padchar = '0'; |
---|
99 | } |
---|
100 | if (!(pad & PAD_RIGHT)) { |
---|
101 | for ( ; width > 0; --width) { |
---|
102 | printchar (out, padchar); |
---|
103 | ++pc; |
---|
104 | } |
---|
105 | } |
---|
106 | for ( ; *string ; ++string) { |
---|
107 | printchar (out, *string); |
---|
108 | ++pc; |
---|
109 | } |
---|
110 | for ( ; width > 0; --width) { |
---|
111 | printchar (out, padchar); |
---|
112 | ++pc; |
---|
113 | } |
---|
114 | |
---|
115 | return pc; |
---|
116 | } |
---|
117 | |
---|
118 | /* the following should be enough for 32 bit int */ |
---|
119 | #define PRINT_BUF_LEN 12 |
---|
120 | |
---|
121 | static int printi(char **out, int i, int b, int sg, int width, int pad, int letbase) |
---|
122 | { |
---|
123 | char print_buf[PRINT_BUF_LEN]; |
---|
124 | register char *s; |
---|
125 | register int t, neg = 0, pc = 0; |
---|
126 | register unsigned int u = i; |
---|
127 | |
---|
128 | if (i == 0) { |
---|
129 | print_buf[0] = '0'; |
---|
130 | print_buf[1] = '\0'; |
---|
131 | return prints (out, print_buf, width, pad); |
---|
132 | } |
---|
133 | |
---|
134 | if (sg && b == 10 && i < 0) { |
---|
135 | neg = 1; |
---|
136 | u = -i; |
---|
137 | } |
---|
138 | |
---|
139 | s = print_buf + PRINT_BUF_LEN-1; |
---|
140 | *s = '\0'; |
---|
141 | |
---|
142 | while (u) { |
---|
143 | t = u % b; |
---|
144 | if( t >= 10 ) |
---|
145 | t += letbase - '0' - 10; |
---|
146 | *--s = t + '0'; |
---|
147 | u /= b; |
---|
148 | } |
---|
149 | |
---|
150 | if (neg) { |
---|
151 | if( width && (pad & PAD_ZERO) ) { |
---|
152 | printchar (out, '-'); |
---|
153 | ++pc; |
---|
154 | --width; |
---|
155 | } |
---|
156 | else { |
---|
157 | *--s = '-'; |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | return pc + prints (out, s, width, pad); |
---|
162 | } |
---|
163 | |
---|
164 | #if 0 |
---|
165 | int fprintf(__FILE *stream, const char *format, ...) |
---|
166 | { |
---|
167 | return 0; |
---|
168 | } |
---|
169 | #endif |
---|
170 | |
---|
171 | int printk_va(char **out, const char *format, va_list args ) |
---|
172 | { |
---|
173 | register int width, pad; |
---|
174 | register int pc = 0; |
---|
175 | char scr[2]; |
---|
176 | |
---|
177 | for (; *format != 0; ++format) { |
---|
178 | if (*format == '%') { |
---|
179 | ++format; |
---|
180 | width = pad = 0; |
---|
181 | if (*format == '\0') break; |
---|
182 | if (*format == '%') goto out; |
---|
183 | if (*format == '-') { |
---|
184 | ++format; |
---|
185 | pad = PAD_RIGHT; |
---|
186 | } |
---|
187 | while (*format == '0') { |
---|
188 | ++format; |
---|
189 | pad |= PAD_ZERO; |
---|
190 | } |
---|
191 | for ( ; *format >= '0' && *format <= '9'; ++format) { |
---|
192 | width *= 10; |
---|
193 | width += *format - '0'; |
---|
194 | } |
---|
195 | if( *format == 's' ) { |
---|
196 | register char *s = (char *)va_arg( args, int ); |
---|
197 | pc += prints (out, s?s:"(null)", width, pad); |
---|
198 | continue; |
---|
199 | } |
---|
200 | if( *format == 'd' ) { |
---|
201 | pc += printi (out, va_arg( args, int ), 10, 1, width, pad, 'a'); |
---|
202 | continue; |
---|
203 | } |
---|
204 | if( *format == 'p' ) { |
---|
205 | pad = 8; |
---|
206 | pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'a'); |
---|
207 | continue; |
---|
208 | } |
---|
209 | if( *format == 'x' ) { |
---|
210 | pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'a'); |
---|
211 | continue; |
---|
212 | } |
---|
213 | if( *format == 'X' ) { |
---|
214 | pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'A'); |
---|
215 | continue; |
---|
216 | } |
---|
217 | if( *format == 'u' ) { |
---|
218 | pc += printi (out, va_arg( args, int ), 10, 0, width, pad, 'a'); |
---|
219 | continue; |
---|
220 | } |
---|
221 | if( *format == 'c' ) { |
---|
222 | /* char are converted to int then pushed on the stack */ |
---|
223 | scr[0] = (char)va_arg( args, int ); |
---|
224 | scr[1] = '\0'; |
---|
225 | pc += prints (out, scr, width, pad); |
---|
226 | continue; |
---|
227 | } |
---|
228 | } |
---|
229 | else { |
---|
230 | out: |
---|
231 | printchar (out, *format); |
---|
232 | ++pc; |
---|
233 | } |
---|
234 | } |
---|
235 | if (out) **out = '\0'; |
---|
236 | va_end( args ); |
---|
237 | return pc; |
---|
238 | } |
---|
239 | |
---|
240 | int printk(const char *format, ...) |
---|
241 | { |
---|
242 | va_list args; |
---|
243 | |
---|
244 | va_start( args, format ); |
---|
245 | return printk_va( 0, format, args ); |
---|
246 | } |
---|
247 | |
---|
248 | #ifndef __ARM__ |
---|
249 | int sprintf(char *out, const char *format, ...) |
---|
250 | { |
---|
251 | va_list args; |
---|
252 | |
---|
253 | va_start( args, format ); |
---|
254 | return printk_va( &out, format, args ); |
---|
255 | } |
---|
256 | #endif |
---|
257 | |
---|
258 | #ifdef TEST_PRINTF |
---|
259 | int main(void) |
---|
260 | { |
---|
261 | char *ptr = "Hello world!"; |
---|
262 | char *np = 0; |
---|
263 | int i = 5; |
---|
264 | unsigned int bs = sizeof(int)*8; |
---|
265 | int mi; |
---|
266 | char buf[80]; |
---|
267 | |
---|
268 | mi = (1 << (bs-1)) + 1; |
---|
269 | printf("%s\n", ptr); |
---|
270 | printf("printf test\n"); |
---|
271 | printf("%s is null pointer\n", np); |
---|
272 | printf("%d = 5\n", i); |
---|
273 | printf("%d = - max int\n", mi); |
---|
274 | printf("char %c = 'a'\n", 'a'); |
---|
275 | printf("hex %x = ff\n", 0xff); |
---|
276 | printf("hex %02x = 00\n", 0); |
---|
277 | printf("signed %d = unsigned %u = hex %x\n", -3, -3, -3); |
---|
278 | printf("%d %s(s)%", 0, "message"); |
---|
279 | printf("\n"); |
---|
280 | printf("%d %s(s) with %%\n", 0, "message"); |
---|
281 | sprintf(buf, "justif: \"%-10s\"\n", "left"); printf("%s", buf); |
---|
282 | sprintf(buf, "justif: \"%10s\"\n", "right"); printf("%s", buf); |
---|
283 | sprintf(buf, " 3: %04d zero padded\n", 3); printf("%s", buf); |
---|
284 | sprintf(buf, " 3: %-4d left justif.\n", 3); printf("%s", buf); |
---|
285 | sprintf(buf, " 3: %4d right justif.\n", 3); printf("%s", buf); |
---|
286 | sprintf(buf, "-3: %04d zero padded\n", -3); printf("%s", buf); |
---|
287 | sprintf(buf, "-3: %-4d left justif.\n", -3); printf("%s", buf); |
---|
288 | sprintf(buf, "-3: %4d right justif.\n", -3); printf("%s", buf); |
---|
289 | |
---|
290 | return 0; |
---|
291 | } |
---|
292 | |
---|
293 | /* |
---|
294 | * if you compile this file with |
---|
295 | * gcc -Wall $(YOUR_C_OPTIONS) -DTEST_PRINTF -c printf.c |
---|
296 | * you will get a normal warning: |
---|
297 | * printf.c:214: warning: spurious trailing `%' in format |
---|
298 | * this line is testing an invalid % at the end of the format string. |
---|
299 | * |
---|
300 | * this should display (on 32bit int machine) : |
---|
301 | * |
---|
302 | * Hello world! |
---|
303 | * printf test |
---|
304 | * (null) is null pointer |
---|
305 | * 5 = 5 |
---|
306 | * -2147483647 = - max int |
---|
307 | * char a = 'a' |
---|
308 | * hex ff = ff |
---|
309 | * hex 00 = 00 |
---|
310 | * signed -3 = unsigned 4294967293 = hex fffffffd |
---|
311 | * 0 message(s) |
---|
312 | * 0 message(s) with % |
---|
313 | * justif: "left " |
---|
314 | * justif: " right" |
---|
315 | * 3: 0003 zero padded |
---|
316 | * 3: 3 left justif. |
---|
317 | * 3: 3 right justif. |
---|
318 | * -3: -003 zero padded |
---|
319 | * -3: -3 left justif. |
---|
320 | * -3: -3 right justif. |
---|
321 | */ |
---|
322 | |
---|
323 | #endif |
---|