3 Copyright (c) 2008 Daniel Mack <daniel@caiaq.de>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "wikireader.h"
25 int serial_input_available(void) {
26 return (0 != (REG_EFSIF0_STATUS
& RDBFx
));
30 int serial_input_char(void)
32 while (!serial_input_available()) {
35 return(REG_EFSIF0_RXD
);
39 #define WAIT_FOR_EFSIF0_RDY() \
41 } while (0 == (REG_EFSIF0_STATUS & TDBEx))
45 void print_char(const char c
)
48 WAIT_FOR_EFSIF0_RDY();
49 REG_EFSIF0_TXD
= '\r';
51 WAIT_FOR_EFSIF0_RDY();
56 void print(const char *txt
)
63 static void print_nibble(u8 nib
)
67 print_char(nib
- 10 + 'a');
69 print_char(nib
+ '0');
72 void print_byte(u8 byte
)
74 print_nibble(byte
>> 4);
78 void hex_dump(const u8
*buf
, u32 size
)
83 for (l
= 0; l
< size
; l
+= 16) {
90 for (i
= 0; i
< 16; i
++) {
92 print_byte(buf
[l
+ i
]);
99 for (i
= 0; i
< 16; i
++) {
101 if (buf
[l
+ i
] >= ' ' && buf
[l
+ i
] <= '~')
115 void print_u32(u32 val
)
118 print_byte(val
>> 24);
119 print_byte(val
>> 16);
120 print_byte(val
>> 8);
127 asm volatile ("nop");
130 void delay_us(unsigned int microsec
)
133 //asm volatile("nop");
134 // at 48 MHz this should take 1 micro second
146 void printf(const char *fmt
, ...) /* format to be printed */
148 int c
; /* next character in fmt */
150 unsigned long u
; /* hold number argument */
151 int base
; /* base of number arg */
152 int negative
; /* print minus sign */
153 static char x2c
[] = "0123456789ABCDEF"; /* nr conversion table */
154 char ascii
[8 * sizeof(long) / 3 + 2]; /* string for ascii number */
155 char *s
; /* string to be printed */
156 va_list argp
; /* optional arguments */
158 va_start(argp
, fmt
); /* init variable arguments */
160 while((c
=*fmt
++) != 0) {
162 if (c
== '%') { /* expect format '%key' */
163 negative
= 0; /* (re)initialize */
164 s
= NULL
; /* (re)initialize */
165 switch(c
= *fmt
++) { /* determine what to do */
167 /* Known keys are %d, %u, %x, %s, and %%. This is easily extended
168 * with number types like %b and %o by providing a different base.
169 * Number type keys don't set a string to 's', but use the general
170 * conversion after the switch statement.
172 case 'd': /* output decimal */
173 d
= va_arg(argp
, signed int);
174 if (d
< 0) { negative
= 1; u
= -d
; } else { u
= d
; }
177 case 'u': /* output unsigned long */
178 u
= va_arg(argp
, unsigned long);
181 case 'x': /* output hexadecimal */
182 u
= va_arg(argp
, unsigned long);
185 case 's': /* output string */
186 s
= va_arg(argp
, char *);
187 if (s
== NULL
) s
= "(null)";
189 case '%': /* output percent */
193 /* Unrecognized key. */
194 default: /* echo back %key */
196 s
[1] = c
; /* set unknown key */
199 /* Assume a number if no string is set. Convert to ascii. */
201 s
= ascii
+ sizeof(ascii
)-1;
203 do { *--s
= x2c
[(u
% base
)]; } /* work backwards */
204 while ((u
/= base
) > 0);
207 /* This is where the actual output for format "%key" is done. */
208 if (negative
) kputc('-'); /* print sign if negative */
209 while(*s
!= 0) { kputc(*s
++); } /* print string/ number */
212 kputc(c
); /* print and continue */
215 va_end(argp
); /* end variable arguments */