1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
26 static char debugmembuf
[100];
27 static char debugbuf
[200];
30 #ifndef SIMULATOR /* allow non archos platforms to display output */
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 */
43 IPRE
|= 0xf000; /* Set to highest priority */
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
66 static void debug_handle_error(char ssr
)
69 SSR1
&= ~(SCI_ORER
| SCI_PER
| SCI_FER
);
72 static int debug_rx_ready(void)
76 ssr
= SSR1
& ( SCI_PER
| SCI_FER
| SCI_ORER
);
78 debug_handle_error ( ssr
);
79 return SSR1
& SCI_RDRF
;
82 static char debug_rx_char(void)
87 while (!debug_rx_ready())
95 ssr
= SSR1
& (SCI_PER
| SCI_FER
| SCI_ORER
);
98 debug_handle_error (ssr
);
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 (const char *buffer
)
117 register int checksum
;
119 const 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 */
132 /* Do run length encoding */
133 for (runlen
= 0; runlen
< 100; runlen
++)
135 if (src
[0] != src
[runlen
] || runlen
== 99)
140 /* Got a useful amount */
141 debug_tx_char (*src
);
145 checksum
+= (encode
= runlen
+ ' ' - 4);
146 debug_tx_char (encode
);
151 debug_tx_char (*src
);
162 debug_tx_char (highhex(checksum
));
163 debug_tx_char (lowhex(checksum
));
165 /* Wait for the '+' */
168 /* Special debug hack. Enable the IRQ again */
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 (const char *mem
, char *buf
, int count
)
179 for (i
= 0; i
< count
; i
++)
182 *buf
++ = highhex (ch
);
183 *buf
++ = lowhex (ch
);
189 static void debug(const char *msg
)
193 mem2hex(msg
, &debugbuf
[1], strlen(msg
));
196 #endif /* end of DEBUG section */
199 void debugf(const char *fmt
, ...)
206 vsnprintf(debugmembuf
, sizeof(debugmembuf
), fmt
, ap
);
215 #else /* SIMULATOR code coming up */
217 void debug_init(void)
221 void debugf(const char *fmt
, ...)
225 vfprintf( stderr
, fmt
, ap
);
229 void ldebugf(const char* file
, int line
, const char *fmt
, ...)
233 fprintf( stderr
, "%s:%d ", file
, line
);
234 vfprintf( stderr
, fmt
, ap
);