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.26 2006/04/27 05:40:27 bernie
19 *#* Break on assertion failures.
21 *#* Revision 1.25 2005/06/27 21:26:24 bernie
24 *#* Revision 1.24 2005/04/12 01:36:37 bernie
25 *#* Add hack to enable TX line at module initialization.
27 *#* Revision 1.23 2005/04/11 19:10:27 bernie
28 *#* Include top-level headers from cfg/ subdir.
30 *#* Revision 1.22 2005/02/18 11:18:33 bernie
31 *#* Fixes for Harvard processors from project_ks.
33 *#* Revision 1.21 2005/02/16 20:29:48 bernie
34 *#* TRACE(), TRACEMSG(): Reduce code and data footprint.
36 *#* Revision 1.20 2005/01/25 08:36:40 bernie
37 *#* kputnum(): Export.
39 *#* Revision 1.19 2004/12/31 17:47:45 bernie
40 *#* Rename UNUSED() to UNUSED_ARG().
43 #include <cfg/debug.h>
45 #include <cfg/macros.h> /* for BV() */
46 #include <appconfig.h>
49 #include <mware/formatwr.h> /* for _formatted_write() */
55 #define KDBG_WAIT_READY() do { /*nop*/ } while(0)
56 #define KDBG_WRITE_CHAR(c) putchar((c))
57 #define KDBG_MASK_IRQ(old) do { (void)(old); } while(0)
58 #define KDBG_RESTORE_IRQ(old) do { /*nop*/ } while(0)
59 typedef char kdbg_irqsave_t; /* unused */
62 #define KDBG_WAIT_READY() do {} while (!(SP_STAT & (SPSF_TX_EMPTY | SPSF_TX_INT)))
63 #define KDBG_WRITE_CHAR(c) do { SBUF = (c); } while(0)
64 #define KDBG_MASK_IRQ(old) \
66 (old) = INT_MASK1 & INT1F_TI; \
67 INT_MASK1 &= ~INT1F_TI; \
69 #define KDBG_RESTORE_IRQ(old) do { INT_MASK1 |= (old); }
70 typedef uint16_t kdbg_irqsave_t; /* FIXME: unconfirmed */
74 #if CONFIG_KDEBUG_PORT == 0
77 * Support for special bus policies or external transceivers
78 * on UART0 (to be overridden in "hw.h").
80 * HACK: if we don't set TXEN, kdbg disables the transmitter
81 * after each output statement until the serial driver
82 * is initialized. These glitches confuse the debug
83 * terminal that ends up printing some trash.
85 #ifndef KDBG_UART0_BUS_INIT
86 #define KDBG_UART0_BUS_INIT do { \
90 #ifndef KDBG_UART0_BUS_RX
91 #define KDBG_UART0_BUS_RX do {} while (0)
93 #ifndef KDBG_UART0_BUS_TX
94 #define KDBG_UART0_BUS_TX do {} while (0)
97 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128
101 #elif CPU_AVR_ATMEGA8
108 #define KDBG_WAIT_READY() do { loop_until_bit_is_set(USR, UDRE); } while(0)
109 #define KDBG_WAIT_TXDONE() do { loop_until_bit_is_set(USR, TXC); } while(0)
112 * We must clear the TXC flag before sending a new character to allow
113 * KDBG_WAIT_TXDONE() to work properly.
115 * BUG: if KDBG_WRITE_CHAR() is called after the TXC flag is set by hardware,
116 * a new TXC could be generated after we've cleared it and before the new
117 * character is written to UDR. On a 485 bus, the transceiver will be put
118 * in RX mode while still transmitting the last char.
120 #define KDBG_WRITE_CHAR(c) do { USR |= BV(TXC); UDR = (c); } while(0)
122 #define KDBG_MASK_IRQ(old) do { \
125 UCR &= ~(BV(TXCIE) | BV(UDRIE)); \
129 #define KDBG_RESTORE_IRQ(old) do { \
130 KDBG_WAIT_TXDONE(); \
135 typedef uint8_t kdbg_irqsave_t;
137 #elif CONFIG_KDEBUG_PORT == 1
140 * Support for special bus policies or external transceivers
141 * on UART1 (to be overridden in "hw.h").
143 * HACK: if we don't set TXEN, kdbg disables the transmitter
144 * after each output statement until the serial driver
145 * is initialized. These glitches confuse the debug
146 * terminal that ends up printing some trash.
148 #ifndef KDBG_UART1_BUS_INIT
149 #define KDBG_UART1_BUS_INIT do { \
153 #ifndef KDBG_UART1_BUS_RX
154 #define KDBG_UART1_BUS_RX do {} while (0)
156 #ifndef KDBG_UART1_BUS_TX
157 #define KDBG_UART1_BUS_TX do {} while (0)
160 #define KDBG_WAIT_READY() do { loop_until_bit_is_set(UCSR1A, UDRE); } while(0)
161 #define KDBG_WAIT_TXDONE() do { loop_until_bit_is_set(UCSR1A, TXC); } while(0)
162 #define KDBG_WRITE_CHAR(c) do { UCSR1A |= BV(TXC); UDR1 = (c); } while(0)
164 #define KDBG_MASK_IRQ(old) do { \
166 UCSR1B |= BV(TXEN); \
167 UCSR1B &= ~(BV(TXCIE) | BV(UDRIE)); \
171 #define KDBG_RESTORE_IRQ(old) do { \
172 KDBG_WAIT_TXDONE(); \
177 typedef uint8_t kdbg_irqsave_t;
179 #error CONFIG_KDEBUG_PORT should be either 0 or 1
181 #elif defined(__MWERKS__) && CPU_DSP56K
182 /* Debugging go through the JTAG interface. The MSL library already
183 implements the console I/O correctly. */
185 #define KDBG_WAIT_READY() do { } while (0)
186 #define KDBG_WRITE_CHAR(c) __put_char(c, stdout)
187 #define KDBG_MASK_IRQ(old) do { (void)(old); } while (0)
188 #define KDBG_RESTORE_IRQ(old) do { (void)(old); } while (0)
189 typedef uint8_t kdbg_irqsave_t; /* unused */
191 #error Unknown architecture
199 /* Set serial port for 19200bps 8N1 */
200 INT_MASK1 &= ~(INT1F_TI | INT1F_RI);
201 SP_CON = SPCF_RECEIVE_ENABLE | SPCF_MODE1;
202 ioc1_img |= IOC1F_TXD_SEL | IOC1F_EXTINT_SRC;
209 /* Compute the baud rate */
210 uint16_t period = (((CLOCK_FREQ / 16UL) + (CONFIG_KDEBUG_BAUDRATE / 2)) / CONFIG_KDEBUG_BAUDRATE) - 1;
212 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128
213 #if CONFIG_KDEBUG_PORT == 0
214 UBRR0H = (uint8_t)(period>>8);
215 UBRR0L = (uint8_t)period;
217 #elif CONFIG_KDEBUG_PORT == 1
218 UBRR1H = (uint8_t)(period>>8);
219 UBRR1L = (uint8_t)period;
222 #error CONFIG_KDEBUG_PORT must be either 0 or 1
224 #elif CPU_AVR_ATMEGA8
225 UBRRH = (uint8_t)(period>>8);
226 UBRRL = (uint8_t)period;
227 #elif CPU_AVR_ATMEGA103
228 UBRR = (uint8_t)period;
234 #endif /* !CPU_I196 && !CPU_AVR */
236 kputs("\n\n*** DBG START ***\n");
241 * Output one character to the debug console
243 static void __kputchar(char c, UNUSED_ARG(void *, unused))
245 /* Poll while serial buffer is still busy */
248 /* Send '\n' as '\r\n' for dumb terminals */
251 KDBG_WRITE_CHAR('\r');
259 void kputchar(char c)
261 /* Mask serial TX intr */
262 kdbg_irqsave_t irqsave;
263 KDBG_MASK_IRQ(irqsave);
267 /* Restore serial TX intr */
268 KDBG_RESTORE_IRQ(irqsave);
272 static void PGM_FUNC(kvprintf)(const char * PGM_ATTR fmt, va_list ap)
275 /* Mask serial TX intr */
276 kdbg_irqsave_t irqsave;
277 KDBG_MASK_IRQ(irqsave);
279 PGM_FUNC(_formatted_write)(fmt, __kputchar, 0, ap);
281 /* Restore serial TX intr */
282 KDBG_RESTORE_IRQ(irqsave);
284 /* A better than nothing printf() surrogate. */
285 PGM_FUNC(kputs)(fmt);
286 #endif /* CONFIG_PRINTF */
289 void PGM_FUNC(kprintf)(const char * PGM_ATTR fmt, ...)
294 PGM_FUNC(kvprintf)(fmt, ap);
298 void PGM_FUNC(kputs)(const char * PGM_ATTR str)
302 /* Mask serial TX intr */
303 kdbg_irqsave_t irqsave;
304 KDBG_MASK_IRQ(irqsave);
306 while ((c = PGM_READ_CHAR(str++)))
309 KDBG_RESTORE_IRQ(irqsave);
314 * Cheap function to print small integers without using printf().
324 digit = num / divisor;
327 if (digit || output_len || divisor == 1)
329 kputchar(digit + '0');
333 while (divisor /= 10);
339 static void klocation(const char * PGM_ATTR file, int line)
341 PGM_FUNC(kputs)(file);
344 PGM_FUNC(kputs)(PGM_STR(": "));
347 int PGM_FUNC(__assert)(const char * PGM_ATTR cond, const char * PGM_ATTR file, int line)
349 klocation(file, line);
350 PGM_FUNC(kputs)(PGM_STR("Assertion failed: "));
351 PGM_FUNC(kputs)(cond);
358 * Unfortunately, there's no way to get __func__ in
359 * program memory, so we waste quite a lot of RAM in
360 * AVR and other Harvard processors.
362 void PGM_FUNC(__trace)(const char *name)
364 PGM_FUNC(kprintf)(PGM_STR("%s()\n"), name);
367 void PGM_FUNC(__tracemsg)(const char *name, const char * PGM_ATTR fmt, ...)
371 PGM_FUNC(kprintf)(PGM_STR("%s(): "), name);
373 PGM_FUNC(kvprintf)(fmt, ap);
378 int PGM_FUNC(__invalid_ptr)(void *value, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
380 klocation(file, line);
381 PGM_FUNC(kputs)(PGM_STR("Invalid ptr: "));
382 PGM_FUNC(kputs)(name);
384 PGM_FUNC(kprintf)(PGM_STR(" = 0x%x\n"), (unsigned int)value);
393 void __init_wall(long *wall, int size)
396 *wall++ = WALL_VALUE;
400 int PGM_FUNC(__check_wall)(long *wall, int size, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
404 for (i = 0; i < size; i++)
406 if (wall[i] != WALL_VALUE)
408 klocation(file, line);
409 PGM_FUNC(kputs)(PGM_STR("Wall broken: "));
410 PGM_FUNC(kputs)(name);
412 PGM_FUNC(kprintf)(PGM_STR("[%d] (0x%p) = 0x%lx\n"), i, wall + i, wall[i]);
427 * Dump binary data in hex
429 void kdump(const void *_buf, size_t len)
431 const unsigned char *buf = (const unsigned char *)_buf;
434 kprintf("%02X", *buf++);
438 #endif /* CONFIG_PRINTF */