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.28 2006/07/19 12:56:25 bernie
19 *#* Convert to new Doxygen style.
21 *#* Revision 1.27 2006/06/01 12:32:06 marco
22 *#* Updated include reference.
24 *#* Revision 1.26 2006/04/27 05:40:27 bernie
25 *#* Break on assertion failures.
27 *#* Revision 1.25 2005/06/27 21:26:24 bernie
30 *#* Revision 1.24 2005/04/12 01:36:37 bernie
31 *#* Add hack to enable TX line at module initialization.
33 *#* Revision 1.23 2005/04/11 19:10:27 bernie
34 *#* Include top-level headers from cfg/ subdir.
36 *#* Revision 1.22 2005/02/18 11:18:33 bernie
37 *#* Fixes for Harvard processors from project_ks.
39 *#* Revision 1.21 2005/02/16 20:29:48 bernie
40 *#* TRACE(), TRACEMSG(): Reduce code and data footprint.
42 *#* Revision 1.20 2005/01/25 08:36:40 bernie
43 *#* kputnum(): Export.
45 *#* Revision 1.19 2004/12/31 17:47:45 bernie
46 *#* Rename UNUSED() to UNUSED_ARG().
49 #include <cfg/debug.h>
51 #include <cfg/macros.h> /* for BV() */
52 #include <appconfig.h>
53 #include <hw_cpu.h> /* for CLOCK_FREQ */
55 #include <mware/formatwr.h> /* for _formatted_write() */
61 #define KDBG_WAIT_READY() do { /*nop*/ } while(0)
62 #define KDBG_WRITE_CHAR(c) putchar((c))
63 #define KDBG_MASK_IRQ(old) do { (void)(old); } while(0)
64 #define KDBG_RESTORE_IRQ(old) do { /*nop*/ } while(0)
65 typedef char kdbg_irqsave_t; /* unused */
68 #define KDBG_WAIT_READY() do {} while (!(SP_STAT & (SPSF_TX_EMPTY | SPSF_TX_INT)))
69 #define KDBG_WRITE_CHAR(c) do { SBUF = (c); } while(0)
70 #define KDBG_MASK_IRQ(old) \
72 (old) = INT_MASK1 & INT1F_TI; \
73 INT_MASK1 &= ~INT1F_TI; \
75 #define KDBG_RESTORE_IRQ(old) do { INT_MASK1 |= (old); }
76 typedef uint16_t kdbg_irqsave_t; /* FIXME: unconfirmed */
80 #if CONFIG_KDEBUG_PORT == 0
83 * Support for special bus policies or external transceivers
84 * on UART0 (to be overridden in "hw.h").
86 * HACK: if we don't set TXEN, kdbg disables the transmitter
87 * after each output statement until the serial driver
88 * is initialized. These glitches confuse the debug
89 * terminal that ends up printing some trash.
91 #ifndef KDBG_UART0_BUS_INIT
92 #define KDBG_UART0_BUS_INIT do { \
96 #ifndef KDBG_UART0_BUS_RX
97 #define KDBG_UART0_BUS_RX do {} while (0)
99 #ifndef KDBG_UART0_BUS_TX
100 #define KDBG_UART0_BUS_TX do {} while (0)
103 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128
107 #elif CPU_AVR_ATMEGA8
114 #define KDBG_WAIT_READY() do { loop_until_bit_is_set(USR, UDRE); } while(0)
115 #define KDBG_WAIT_TXDONE() do { loop_until_bit_is_set(USR, TXC); } while(0)
118 * We must clear the TXC flag before sending a new character to allow
119 * KDBG_WAIT_TXDONE() to work properly.
121 * BUG: if KDBG_WRITE_CHAR() is called after the TXC flag is set by hardware,
122 * a new TXC could be generated after we've cleared it and before the new
123 * character is written to UDR. On a 485 bus, the transceiver will be put
124 * in RX mode while still transmitting the last char.
126 #define KDBG_WRITE_CHAR(c) do { USR |= BV(TXC); UDR = (c); } while(0)
128 #define KDBG_MASK_IRQ(old) do { \
131 UCR &= ~(BV(TXCIE) | BV(UDRIE)); \
135 #define KDBG_RESTORE_IRQ(old) do { \
136 KDBG_WAIT_TXDONE(); \
141 typedef uint8_t kdbg_irqsave_t;
143 #elif CONFIG_KDEBUG_PORT == 1
146 * Support for special bus policies or external transceivers
147 * on UART1 (to be overridden in "hw.h").
149 * HACK: if we don't set TXEN, kdbg disables the transmitter
150 * after each output statement until the serial driver
151 * is initialized. These glitches confuse the debug
152 * terminal that ends up printing some trash.
154 #ifndef KDBG_UART1_BUS_INIT
155 #define KDBG_UART1_BUS_INIT do { \
159 #ifndef KDBG_UART1_BUS_RX
160 #define KDBG_UART1_BUS_RX do {} while (0)
162 #ifndef KDBG_UART1_BUS_TX
163 #define KDBG_UART1_BUS_TX do {} while (0)
166 #define KDBG_WAIT_READY() do { loop_until_bit_is_set(UCSR1A, UDRE); } while(0)
167 #define KDBG_WAIT_TXDONE() do { loop_until_bit_is_set(UCSR1A, TXC); } while(0)
168 #define KDBG_WRITE_CHAR(c) do { UCSR1A |= BV(TXC); UDR1 = (c); } while(0)
170 #define KDBG_MASK_IRQ(old) do { \
172 UCSR1B |= BV(TXEN); \
173 UCSR1B &= ~(BV(TXCIE) | BV(UDRIE)); \
177 #define KDBG_RESTORE_IRQ(old) do { \
178 KDBG_WAIT_TXDONE(); \
183 typedef uint8_t kdbg_irqsave_t;
185 #error CONFIG_KDEBUG_PORT should be either 0 or 1
187 #elif defined(__MWERKS__) && CPU_DSP56K
188 /* Debugging go through the JTAG interface. The MSL library already
189 implements the console I/O correctly. */
191 #define KDBG_WAIT_READY() do { } while (0)
192 #define KDBG_WRITE_CHAR(c) __put_char(c, stdout)
193 #define KDBG_MASK_IRQ(old) do { (void)(old); } while (0)
194 #define KDBG_RESTORE_IRQ(old) do { (void)(old); } while (0)
195 typedef uint8_t kdbg_irqsave_t; /* unused */
197 #error Unknown architecture
205 /* Set serial port for 19200bps 8N1 */
206 INT_MASK1 &= ~(INT1F_TI | INT1F_RI);
207 SP_CON = SPCF_RECEIVE_ENABLE | SPCF_MODE1;
208 ioc1_img |= IOC1F_TXD_SEL | IOC1F_EXTINT_SRC;
215 /* Compute the baud rate */
216 uint16_t period = (((CLOCK_FREQ / 16UL) + (CONFIG_KDEBUG_BAUDRATE / 2)) / CONFIG_KDEBUG_BAUDRATE) - 1;
218 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128
219 #if CONFIG_KDEBUG_PORT == 0
220 UBRR0H = (uint8_t)(period>>8);
221 UBRR0L = (uint8_t)period;
223 #elif CONFIG_KDEBUG_PORT == 1
224 UBRR1H = (uint8_t)(period>>8);
225 UBRR1L = (uint8_t)period;
228 #error CONFIG_KDEBUG_PORT must be either 0 or 1
230 #elif CPU_AVR_ATMEGA8
231 UBRRH = (uint8_t)(period>>8);
232 UBRRL = (uint8_t)period;
233 #elif CPU_AVR_ATMEGA103
234 UBRR = (uint8_t)period;
240 #endif /* !CPU_I196 && !CPU_AVR */
242 kputs("\n\n*** DBG START ***\n");
247 * Output one character to the debug console
249 static void __kputchar(char c, UNUSED_ARG(void *, unused))
251 /* Poll while serial buffer is still busy */
254 /* Send '\n' as '\r\n' for dumb terminals */
257 KDBG_WRITE_CHAR('\r');
265 void kputchar(char c)
267 /* Mask serial TX intr */
268 kdbg_irqsave_t irqsave;
269 KDBG_MASK_IRQ(irqsave);
273 /* Restore serial TX intr */
274 KDBG_RESTORE_IRQ(irqsave);
278 static void PGM_FUNC(kvprintf)(const char * PGM_ATTR fmt, va_list ap)
281 /* Mask serial TX intr */
282 kdbg_irqsave_t irqsave;
283 KDBG_MASK_IRQ(irqsave);
285 PGM_FUNC(_formatted_write)(fmt, __kputchar, 0, ap);
287 /* Restore serial TX intr */
288 KDBG_RESTORE_IRQ(irqsave);
290 /* A better than nothing printf() surrogate. */
291 PGM_FUNC(kputs)(fmt);
292 #endif /* CONFIG_PRINTF */
295 void PGM_FUNC(kprintf)(const char * PGM_ATTR fmt, ...)
300 PGM_FUNC(kvprintf)(fmt, ap);
304 void PGM_FUNC(kputs)(const char * PGM_ATTR str)
308 /* Mask serial TX intr */
309 kdbg_irqsave_t irqsave;
310 KDBG_MASK_IRQ(irqsave);
312 while ((c = PGM_READ_CHAR(str++)))
315 KDBG_RESTORE_IRQ(irqsave);
320 * Cheap function to print small integers without using printf().
330 digit = num / divisor;
333 if (digit || output_len || divisor == 1)
335 kputchar(digit + '0');
339 while (divisor /= 10);
345 static void klocation(const char * PGM_ATTR file, int line)
347 PGM_FUNC(kputs)(file);
350 PGM_FUNC(kputs)(PGM_STR(": "));
353 int PGM_FUNC(__assert)(const char * PGM_ATTR cond, const char * PGM_ATTR file, int line)
355 klocation(file, line);
356 PGM_FUNC(kputs)(PGM_STR("Assertion failed: "));
357 PGM_FUNC(kputs)(cond);
364 * Unfortunately, there's no way to get __func__ in
365 * program memory, so we waste quite a lot of RAM in
366 * AVR and other Harvard processors.
368 void PGM_FUNC(__trace)(const char *name)
370 PGM_FUNC(kprintf)(PGM_STR("%s()\n"), name);
373 void PGM_FUNC(__tracemsg)(const char *name, const char * PGM_ATTR fmt, ...)
377 PGM_FUNC(kprintf)(PGM_STR("%s(): "), name);
379 PGM_FUNC(kvprintf)(fmt, ap);
384 int PGM_FUNC(__invalid_ptr)(void *value, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
386 klocation(file, line);
387 PGM_FUNC(kputs)(PGM_STR("Invalid ptr: "));
388 PGM_FUNC(kputs)(name);
390 PGM_FUNC(kprintf)(PGM_STR(" = 0x%x\n"), (unsigned int)value);
399 void __init_wall(long *wall, int size)
402 *wall++ = WALL_VALUE;
406 int PGM_FUNC(__check_wall)(long *wall, int size, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
410 for (i = 0; i < size; i++)
412 if (wall[i] != WALL_VALUE)
414 klocation(file, line);
415 PGM_FUNC(kputs)(PGM_STR("Wall broken: "));
416 PGM_FUNC(kputs)(name);
418 PGM_FUNC(kprintf)(PGM_STR("[%d] (0x%p) = 0x%lx\n"), i, wall + i, wall[i]);
433 * Dump binary data in hex
435 void kdump(const void *_buf, size_t len)
437 const unsigned char *buf = (const unsigned char *)_buf;
440 kprintf("%02X", *buf++);
444 #endif /* CONFIG_PRINTF */