Check for bootloader file on device before copying.
[maemo-rb.git] / firmware / debug.c
blobbc382335b67801495a2c2aadcbb9ef68e78db745
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Linus Nielsen Feltzing
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include "config.h"
26 #include "cpu.h"
27 #ifdef HAVE_GDB_API
28 #include "gdb_api.h"
29 #endif
31 #ifdef DEBUG
32 static char debugmembuf[200];
33 #if CONFIG_CPU == SH7034
34 static char debugbuf[400];
35 #endif
36 #endif
38 #include "kernel.h"
39 #include "system.h"
40 #include "debug.h"
42 #ifdef DEBUG
43 #if CONFIG_CPU == SH7034 /* these are still very SH-oriented */
44 void debug_init(void)
46 /* Clear it all! */
47 SSR1 &= ~(SCI_RDRF | SCI_ORER | SCI_PER | SCI_FER);
49 /* This enables the serial Rx interrupt, to be able to exit into the
50 debugger when you hit CTRL-C */
51 SCR1 |= 0x40;
52 SCR1 &= ~0x80;
53 IPRE |= 0xf000; /* Set to highest priority */
56 static int debug_tx_ready(void)
58 return (SSR1 & SCI_TDRE);
61 static void debug_tx_char(char ch)
63 while (!debug_tx_ready())
69 * Write data into TDR and clear TDRE
71 TDR1 = ch;
72 SSR1 &= ~SCI_TDRE;
75 static void debug_handle_error(char ssr)
77 (void)ssr;
78 SSR1 &= ~(SCI_ORER | SCI_PER | SCI_FER);
81 static int debug_rx_ready(void)
83 char ssr;
85 ssr = SSR1 & ( SCI_PER | SCI_FER | SCI_ORER );
86 if ( ssr )
87 debug_handle_error ( ssr );
88 return SSR1 & SCI_RDRF;
91 static char debug_rx_char(void)
93 char ch;
94 char ssr;
96 while (!debug_rx_ready())
101 ch = RDR1;
102 SSR1 &= ~SCI_RDRF;
104 ssr = SSR1 & (SCI_PER | SCI_FER | SCI_ORER);
106 if (ssr)
107 debug_handle_error (ssr);
109 return ch;
112 static const char hexchars[] = "0123456789abcdef";
114 static char highhex(int x)
116 return hexchars[(x >> 4) & 0xf];
119 static char lowhex(int x)
121 return hexchars[x & 0xf];
124 static void putpacket (const char *buffer)
126 register int checksum;
128 const char *src = buffer;
130 /* Special debug hack. Shut off the Rx IRQ during I/O to prevent the debug
131 stub from interrupting the message */
132 SCR1 &= ~0x40;
134 debug_tx_char ('$');
135 checksum = 0;
137 while (*src)
139 int runlen;
141 /* Do run length encoding */
142 for (runlen = 0; runlen < 100; runlen ++)
144 if (src[0] != src[runlen] || runlen == 99)
146 if (runlen > 3)
148 int encode;
149 /* Got a useful amount */
150 debug_tx_char (*src);
151 checksum += *src;
152 debug_tx_char ('*');
153 checksum += '*';
154 checksum += (encode = runlen + ' ' - 4);
155 debug_tx_char (encode);
156 src += runlen;
158 else
160 debug_tx_char (*src);
161 checksum += *src;
162 src++;
164 break;
170 debug_tx_char ('#');
171 debug_tx_char (highhex(checksum));
172 debug_tx_char (lowhex(checksum));
174 /* Wait for the '+' */
175 debug_rx_char();
177 /* Special debug hack. Enable the IRQ again */
178 SCR1 |= 0x40;
182 /* convert the memory, pointed to by mem into hex, placing result in buf */
183 /* return a pointer to the last char put in buf (null) */
184 static char *mem2hex (const char *mem, char *buf, int count)
186 int i;
187 int ch;
188 for (i = 0; i < count; i++)
190 ch = *mem++;
191 *buf++ = highhex (ch);
192 *buf++ = lowhex (ch);
194 *buf = 0;
195 return (buf);
198 static void debug(const char *msg)
200 debugbuf[0] = 'O';
202 mem2hex(msg, &debugbuf[1], strlen(msg));
203 putpacket(debugbuf);
205 #elif defined(HAVE_GDB_API)
206 static void *get_api_function(int n)
208 struct gdb_api *api = (struct gdb_api *)GDB_API_ADDRESS;
209 if (api->magic == GDB_API_MAGIC)
210 return api->func[n];
211 else
212 return NULL;
215 void breakpoint(void)
217 void (*f)(void) = get_api_function(0);
218 if (f) (*f)();
221 static void debug(char *msg)
223 void (*f)(char *) = get_api_function(1);
224 if (f) (*f)(msg);
227 void debug_init(void)
230 #else /* !SH7034 && !HAVE_GDB_API */
231 void debug_init(void)
235 static inline void debug(char *msg)
237 (void)msg;
239 #endif
241 #endif /* end of DEBUG section */
243 #ifdef __GNUC__
244 void debugf(const char *fmt, ...)
245 #endif
247 #ifdef DEBUG
248 va_list ap;
250 va_start(ap, fmt);
251 vsnprintf(debugmembuf, sizeof(debugmembuf), fmt, ap);
252 va_end(ap);
253 debug(debugmembuf);
254 #else
255 (void)fmt;
256 #endif