4 * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
5 * Copyright 2000, 2001, 2002 Bernardo Innocenti <bernie@codewiz.org>
6 * This file is part of DevLib - See README.devlib for information.
9 * \brief General pourpose debug support for embedded systems (implementation).
12 * \author Bernardo Innocenti <bernie@develer.com>
13 * \author Stefano Fedrigo <aleph@develer.com>
18 *#* Revision 1.21 2005/02/16 20:29:48 bernie
19 *#* TRACE(), TRACEMSG(): Reduce code and data footprint.
21 *#* Revision 1.20 2005/01/25 08:36:40 bernie
22 *#* kputnum(): Export.
24 *#* Revision 1.19 2004/12/31 17:47:45 bernie
25 *#* Rename UNUSED() to UNUSED_ARG().
27 *#* Revision 1.18 2004/12/08 08:52:00 bernie
28 *#* Save some more RAM on AVR.
30 *#* Revision 1.17 2004/10/03 18:41:28 bernie
31 *#* Restore \version header killed by mistake in previous commit.
33 *#* Revision 1.16 2004/10/03 18:40:50 bernie
34 *#* Use new CPU macros.
36 *#* Revision 1.15 2004/09/14 21:03:46 bernie
37 *#* Use debug.h instead of kdebug.h.
39 *#* Revision 1.14 2004/09/06 21:39:56 bernie
40 *#* Allow partial redefinition of BUS macros.
42 *#* Revision 1.13 2004/08/29 22:04:26 bernie
43 *#* Convert 485 macros to generic BUS macros;
44 *#* kputchar(): New public function.
46 *#* Revision 1.12 2004/08/25 14:12:08 rasky
47 *#* Aggiornato il comment block dei log RCS
49 *#* Revision 1.11 2004/08/24 16:19:08 bernie
50 *#* kputchar(): New public function; Add missing dummy inlines for \!_DEBUG.
52 *#* Revision 1.10 2004/08/04 15:57:50 rasky
53 *#* Cambiata la putchar per kdebug per DSP56k: la nuova funzione e' quella piu' a basso livello (assembly)
55 *#* Revision 1.9 2004/08/02 20:20:29 aleph
56 *#* Merge from project_ks
58 *#* Revision 1.8 2004/07/30 14:26:33 rasky
59 *#* Semplificato l'output dell'ASSERT
60 *#* Aggiunta ASSERT2 con stringa di help opzionalmente disattivabile
62 *#* Revision 1.7 2004/07/30 14:15:53 rasky
63 *#* Nuovo supporto unificato per detect della CPU
65 *#* Revision 1.6 2004/07/18 21:49:28 bernie
66 *#* Add ATmega8 support.
68 *#* Revision 1.5 2004/06/27 15:20:26 aleph
69 *#* Change UNUSED() macro to accept two arguments: type and name;
70 *#* Add macro GNUC_PREREQ to detect GCC version during build;
71 *#* Some spacing cleanups and typo fix
73 *#* Revision 1.4 2004/06/06 18:09:51 bernie
74 *#* Import DSP56800 changes; Print broken wall bricks in hex.
76 *#* Revision 1.3 2004/06/03 11:27:09 bernie
77 *#* Add dual-license information.
79 *#* Revision 1.2 2004/05/23 18:21:53 bernie
80 *#* Trim CVS logs and cleanup header info.
86 #include <macros.h> /* for BV() */
90 #include <mware/formatwr.h> /* for _formatted_write() */
96 #define KDBG_WAIT_READY() do { /*nop*/ } while(0)
97 #define KDBG_WRITE_CHAR(c) putchar((c))
98 #define KDBG_MASK_IRQ(old) do { (void)(old); } while(0)
99 #define KDBG_RESTORE_IRQ(old) do { /*nop*/ } while(0)
100 typedef char kdbg_irqsave_t; /* unused */
103 #define KDBG_WAIT_READY() do {} while (!(SP_STAT & (SPSF_TX_EMPTY | SPSF_TX_INT)))
104 #define KDBG_WRITE_CHAR(c) do { SBUF = (c); } while(0)
105 #define KDBG_MASK_IRQ(old) \
107 (old) = INT_MASK1 & INT1F_TI; \
108 INT_MASK1 &= ~INT1F_TI; \
110 #define KDBG_RESTORE_IRQ(old) do { INT_MASK1 |= (old); }
111 typedef uint16_t kdbg_irqsave_t; /* FIXME: unconfirmed */
115 #if CONFIG_KDEBUG_PORT == 0
118 * Support for special bus policies or external transceivers
119 * on UART0 (to be overridden in "hw.h").
121 #ifndef KDBG_UART0_BUS_INIT
122 #define KDBG_UART0_BUS_INIT do {} while (0)
124 #ifndef KDBG_UART0_BUS_RX
125 #define KDBG_UART0_BUS_RX do {} while (0)
127 #ifndef KDBG_UART0_BUS_TX
128 #define KDBG_UART0_BUS_TX do {} while (0)
135 #elif CPU_AVR_ATMEGA8
142 #define KDBG_WAIT_READY() do { loop_until_bit_is_set(USR, UDRE); } while(0)
143 #define KDBG_WAIT_TXDONE() do { loop_until_bit_is_set(USR, TXC); } while(0)
146 * We must clear the TXC flag before sending a new character to allow
147 * KDBG_WAIT_TXDONE() to work properly.
149 * BUG: if KDBG_WRITE_CHAR() is called after the TXC flag is set by hardware,
150 * a new TXC could be generated after we've cleared it and before the new
151 * character is written to UDR. On a 485 bus, the transceiver will be put
152 * in RX mode while still transmitting the last char.
154 #define KDBG_WRITE_CHAR(c) do { USR |= BV(TXC); UDR = (c); } while(0)
156 #define KDBG_MASK_IRQ(old) do { \
159 UCR &= ~(BV(TXCIE) | BV(UDRIE)); \
163 #define KDBG_RESTORE_IRQ(old) do { \
164 KDBG_WAIT_TXDONE(); \
169 typedef uint8_t kdbg_irqsave_t;
171 #elif CONFIG_KDEBUG_PORT == 1
174 * Support for special bus policies or external transceivers
175 * on UART1 (to be overridden in "hw.h").
177 #ifndef KDBG_UART1_BUS_INIT
178 #define KDBG_UART1_BUS_INIT do {} while (0)
180 #ifndef KDBG_UART1_BUS_RX
181 #define KDBG_UART1_BUS_RX do {} while (0)
183 #ifndef KDBG_UART1_BUS_TX
184 #define KDBG_UART1_BUS_TX do {} while (0)
187 #define KDBG_WAIT_READY() do { loop_until_bit_is_set(UCSR1A, UDRE); } while(0)
188 #define KDBG_WAIT_TXDONE() do { loop_until_bit_is_set(UCSR1A, TXC); } while(0)
189 #define KDBG_WRITE_CHAR(c) do { UCSR1A |= BV(TXC); UDR1 = (c); } while(0)
191 #define KDBG_MASK_IRQ(old) do { \
193 UCSR1B |= BV(TXEN); \
194 UCSR1B &= ~(BV(TXCIE) | BV(UDRIE)); \
198 #define KDBG_RESTORE_IRQ(old) do { \
199 KDBG_WAIT_TXDONE(); \
204 typedef uint8_t kdbg_irqsave_t;
206 #error CONFIG_KDEBUG_PORT should be either 0 or 1
208 #elif defined(__MWERKS__) && CPU_DSP56K
209 /* Debugging go through the JTAG interface. The MSL library already
210 implements the console I/O correctly. */
212 #define KDBG_WAIT_READY() do { } while (0)
213 #define KDBG_WRITE_CHAR(c) __put_char(c, stdout)
214 #define KDBG_MASK_IRQ(old) do { (void)(old); } while (0)
215 #define KDBG_RESTORE_IRQ(old) do { (void)(old); } while (0)
216 typedef uint8_t kdbg_irqsave_t; /* unused */
218 #error Unknown architecture
226 /* Set serial port for 19200bps 8N1 */
227 INT_MASK1 &= ~(INT1F_TI | INT1F_RI);
228 SP_CON = SPCF_RECEIVE_ENABLE | SPCF_MODE1;
229 ioc1_img |= IOC1F_TXD_SEL | IOC1F_EXTINT_SRC;
236 /* Compute the baud rate */
237 uint16_t period = (((CLOCK_FREQ / 16UL) + (CONFIG_KDEBUG_BAUDRATE / 2)) / CONFIG_KDEBUG_BAUDRATE) - 1;
240 #if CONFIG_KDEBUG_PORT == 0
241 UBRR0H = (uint8_t)(period>>8);
242 UBRR0L = (uint8_t)period;
244 #elif CONFIG_KDEBUG_PORT == 1
245 UBRR1H = (uint8_t)(period>>8);
246 UBRR1L = (uint8_t)period;
249 #error CONFIG_KDEBUG_PORT must be either 0 or 1
251 #elif CPU_AVR_ATMEGA8
252 UBRRH = (uint8_t)(period>>8);
253 UBRRL = (uint8_t)period;
254 #elif CPU_AVR_ATMEGA103
255 UBRR = (uint8_t)period;
261 #endif /* !CPU_I196 && !CPU_AVR */
263 kputs("\n\n*** DBG START ***\n");
268 * Output one character to the debug console
270 static void __kputchar(char c, UNUSED_ARG(void *, unused))
272 /* Poll while serial buffer is still busy */
275 /* Send '\n' as '\r\n' for dumb terminals */
278 KDBG_WRITE_CHAR('\r');
286 void kputchar(char c)
288 /* Mask serial TX intr */
289 kdbg_irqsave_t irqsave;
290 KDBG_MASK_IRQ(irqsave);
294 /* Restore serial TX intr */
295 KDBG_RESTORE_IRQ(irqsave);
299 void PGM_FUNC(kvprintf)(const char * PGM_ATTR fmt, va_list ap)
302 /* Mask serial TX intr */
303 kdbg_irqsave_t irqsave;
304 KDBG_MASK_IRQ(irqsave);
306 PGM_FUNC(_formatted_write)(fmt, __kputchar, 0, ap);
308 /* Restore serial TX intr */
309 KDBG_RESTORE_IRQ(irqsave);
311 /* A better than nothing printf() surrogate. */
312 PGM_FUNC(kputs)(fmt);
313 #endif /* CONFIG_PRINTF */
316 void PGM_FUNC(kprintf)(const char * PGM_ATTR fmt, ...)
321 PGM_FUNC(kvprintf)(fmt, ap);
325 void PGM_FUNC(kputs)(const char * PGM_ATTR str)
329 /* Mask serial TX intr */
330 kdbg_irqsave_t irqsave;
331 KDBG_MASK_IRQ(irqsave);
333 while ((c = PGM_READ_CHAR(str++)))
336 KDBG_RESTORE_IRQ(irqsave);
341 * Cheap function to print small integers without using printf().
351 digit = num / divisor;
354 if (digit || output_len || divisor == 1)
356 kputchar(digit + '0');
360 while (divisor /= 10);
366 static void klocation(const char * PGM_ATTR file, int line)
368 PGM_FUNC(kputs)(file);
371 PGM_FUNC(kputs)(PSTR(": "));
374 int PGM_FUNC(__assert)(const char * PGM_ATTR cond, const char * PGM_ATTR file, int line)
376 klocation(file, line);
377 PGM_FUNC(kputs)(PSTR("Assertion failed: "));
378 PGM_FUNC(kputs)(cond);
383 void PGM_FUNC(__trace)(const char * PGM_ATTR name)
385 PGM_FUNC(kputs)(name);
386 PGM_FUNC(kputs)(PSTR("()\n"));
389 void PGM_FUNC(__tracemsg)(const char * PGM_ATTR name, const char * PGM_ATTR fmt, ...)
393 PGM_FUNC(kputs)(name);
394 PGM_FUNC(kputs)(PSTR("(): "));
396 PGM_FUNC(kvprintf)(fmt, ap);
400 int PGM_FUNC(__invalid_ptr)(void *value, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
402 klocation(file, line);
403 PGM_FUNC(kputs)(PSTR("Invalid ptr: "));
404 PGM_FUNC(kputs)(name);
406 PGM_FUNC(kprintf)(PSTR(" = 0x%x\n"), value);
415 void __init_wall(long *wall, int size)
418 *wall++ = WALL_VALUE;
422 int PGM_FUNC(__check_wall)(long *wall, int size, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
426 for (i = 0; i < size; i++)
428 if (wall[i] != WALL_VALUE)
430 klocation(file, line);
431 PGM_FUNC(kputs)(PSTR("Wall broken: "));
432 PGM_FUNC(kputs)(name);
434 PGM_FUNC(kprintf)(PSTR("[%d] (0x%p) = 0x%lx\n"), i, wall + i, wall[i]);
449 * Dump binary data in hex
451 void kdump(const void *_buf, size_t len)
453 const unsigned char *buf = (const unsigned char *)_buf;
456 kprintf("%02X", *buf++);
460 #endif /* CONFIG_PRINTF */