Battery blinks if >BATTERY_LEVEL_DANGEROUS
[kugel-rb.git] / firmware / debug.c
blob83eb1497060e44e1b0fbfe5786e2fbb02dfdc625
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "sh7034.h"
20 #include <string.h>
21 #include <stdio.h>
22 #include <stdarg.h>
23 #include "config.h"
25 #ifdef DEBUG
26 static char debugmembuf[100];
27 static char debugbuf[200];
28 #endif
30 #ifndef SIMULATOR /* allow non archos platforms to display output */
31 #include "kernel.h"
32 #include "system.h"
34 void debug_init(void)
36 /* Clear it all! */
37 SSR1 &= ~(SCI_RDRF | SCI_ORER | SCI_PER | SCI_FER);
39 /* This enables the serial Rx interrupt, to be able to exit into the
40 debugger when you hit CTRL-C */
41 SCR1 |= 0x40;
42 SCR1 &= ~0x80;
43 IPRE |= 0xf000; /* Set to highest priority */
46 #ifdef DEBUG
47 static int debug_tx_ready(void)
49 return (SSR1 & SCI_TDRE);
52 static void debug_tx_char(char ch)
54 while (!debug_tx_ready())
60 * Write data into TDR and clear TDRE
62 TDR1 = ch;
63 SSR1 &= ~SCI_TDRE;
66 static void debug_handle_error(char ssr)
68 (void)ssr;
69 SSR1 &= ~(SCI_ORER | SCI_PER | SCI_FER);
72 static int debug_rx_ready(void)
74 char ssr;
76 ssr = SSR1 & ( SCI_PER | SCI_FER | SCI_ORER );
77 if ( ssr )
78 debug_handle_error ( ssr );
79 return SSR1 & SCI_RDRF;
82 static char debug_rx_char(void)
84 char ch;
85 char ssr;
87 while (!debug_rx_ready())
92 ch = RDR1;
93 SSR1 &= ~SCI_RDRF;
95 ssr = SSR1 & (SCI_PER | SCI_FER | SCI_ORER);
97 if (ssr)
98 debug_handle_error (ssr);
100 return ch;
103 static const char hexchars[] = "0123456789abcdef";
105 static char highhex(int x)
107 return hexchars[(x >> 4) & 0xf];
110 static char lowhex(int x)
112 return hexchars[x & 0xf];
115 static void putpacket (char *buffer)
117 register int checksum;
119 char *src = buffer;
121 /* Special debug hack. Shut off the Rx IRQ during I/O to prevent the debug
122 stub from interrupting the message */
123 SCR1 &= ~0x40;
125 debug_tx_char ('$');
126 checksum = 0;
128 while (*src)
130 int runlen;
132 /* Do run length encoding */
133 for (runlen = 0; runlen < 100; runlen ++)
135 if (src[0] != src[runlen])
137 if (runlen > 3)
139 int encode;
140 /* Got a useful amount */
141 debug_tx_char (*src);
142 checksum += *src;
143 debug_tx_char ('*');
144 checksum += '*';
145 checksum += (encode = runlen + ' ' - 4);
146 debug_tx_char (encode);
147 src += runlen;
149 else
151 debug_tx_char (*src);
152 checksum += *src;
153 src++;
155 break;
161 debug_tx_char ('#');
162 debug_tx_char (highhex(checksum));
163 debug_tx_char (lowhex(checksum));
165 /* Wait for the '+' */
166 debug_rx_char();
168 /* Special debug hack. Enable the IRQ again */
169 SCR1 |= 0x40;
173 /* convert the memory, pointed to by mem into hex, placing result in buf */
174 /* return a pointer to the last char put in buf (null) */
175 static char *mem2hex (char *mem, char *buf, int count)
177 int i;
178 int ch;
179 for (i = 0; i < count; i++)
181 ch = *mem++;
182 *buf++ = highhex (ch);
183 *buf++ = lowhex (ch);
185 *buf = 0;
186 return (buf);
189 static void debug(char *msg)
191 debugbuf[0] = 'O';
193 mem2hex(msg, &debugbuf[1], strlen(msg));
194 putpacket(debugbuf);
196 #endif /* end of DEBUG section */
198 void debugf(char *fmt, ...)
200 #ifdef DEBUG
201 va_list ap;
203 va_start(ap, fmt);
204 vsnprintf(debugmembuf, sizeof(debugmembuf), fmt, ap);
205 va_end(ap);
206 debug(debugmembuf);
207 #else
208 (void)fmt;
209 #endif
213 #else /* SIMULATOR code coming up */
215 void debug_init(void)
219 void debugf(char *fmt, ...)
221 va_list ap;
222 va_start( ap, fmt );
223 vfprintf( stderr, fmt, ap );
224 va_end( ap );
226 #endif