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.31 2007/06/07 14:25:30 batt
19 *#* Merge from project_ks
21 *#* Revision 1.30 2007/01/28 09:18:06 batt
22 *#* Merge from project_ks.
24 *#* Revision 1.29 2006/11/23 13:19:39 batt
25 *#* Add BitBanged serial debug console.
27 *#* Revision 1.28 2006/07/19 12:56:25 bernie
28 *#* Convert to new Doxygen style.
30 *#* Revision 1.27 2006/06/01 12:32:06 marco
31 *#* Updated include reference.
33 *#* Revision 1.26 2006/04/27 05:40:27 bernie
34 *#* Break on assertion failures.
36 *#* Revision 1.25 2005/06/27 21:26:24 bernie
39 *#* Revision 1.24 2005/04/12 01:36:37 bernie
40 *#* Add hack to enable TX line at module initialization.
42 *#* Revision 1.23 2005/04/11 19:10:27 bernie
43 *#* Include top-level headers from cfg/ subdir.
45 *#* Revision 1.22 2005/02/18 11:18:33 bernie
46 *#* Fixes for Harvard processors from project_ks.
48 *#* Revision 1.21 2005/02/16 20:29:48 bernie
49 *#* TRACE(), TRACEMSG(): Reduce code and data footprint.
51 *#* Revision 1.20 2005/01/25 08:36:40 bernie
52 *#* kputnum(): Export.
54 *#* Revision 1.19 2004/12/31 17:47:45 bernie
55 *#* Rename UNUSED() to UNUSED_ARG().
58 #include <cfg/debug.h>
60 #include <cfg/macros.h> /* for BV() */
61 #include <appconfig.h>
62 #include <hw_cpu.h> /* for CLOCK_FREQ */
63 #include <hw_ser.h> /* Required for bus macros overrides */
65 #include <mware/formatwr.h> /* for _formatted_write() */
69 #if CPU_HARVARD && !defined(_PROGMEM)
70 #error This module build correctly only in program memory!
75 #define KDBG_WAIT_READY() do { /*nop*/ } while(0)
76 #define KDBG_WRITE_CHAR(c) putchar((c))
77 #define KDBG_MASK_IRQ(old) do { (void)(old); } while(0)
78 #define KDBG_RESTORE_IRQ(old) do { /*nop*/ } while(0)
79 typedef char kdbg_irqsave_t; /* unused */
81 #if CONFIG_KDEBUG_PORT == 666
82 #error BITBANG debug console missing for this platform
86 #define KDBG_WAIT_READY() do {} while (!(SP_STAT & (SPSF_TX_EMPTY | SPSF_TX_INT)))
87 #define KDBG_WRITE_CHAR(c) do { SBUF = (c); } while(0)
88 #define KDBG_MASK_IRQ(old) \
90 (old) = INT_MASK1 & INT1F_TI; \
91 INT_MASK1 &= ~INT1F_TI; \
93 #define KDBG_RESTORE_IRQ(old) do { INT_MASK1 |= (old); }
94 typedef uint16_t kdbg_irqsave_t; /* FIXME: unconfirmed */
96 #if CONFIG_KDEBUG_PORT == 666
97 #error BITBANG debug console missing for this platform
102 #if CONFIG_KDEBUG_PORT == 0
105 * Support for special bus policies or external transceivers
106 * on UART0 (to be overridden in "hw_ser.h").
108 * HACK: if we don't set TXEN, kdbg disables the transmitter
109 * after each output statement until the serial driver
110 * is initialized. These glitches confuse the debug
111 * terminal that ends up printing some trash.
113 #ifndef KDBG_UART0_BUS_INIT
114 #define KDBG_UART0_BUS_INIT do { \
115 UCSR0B = BV(TXEN0); \
118 #ifndef KDBG_UART0_BUS_RX
119 #define KDBG_UART0_BUS_RX do {} while (0)
121 #ifndef KDBG_UART0_BUS_TX
122 #define KDBG_UART0_BUS_TX do {} while (0)
125 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA168
129 #elif CPU_AVR_ATMEGA8
136 #define KDBG_WAIT_READY() do { loop_until_bit_is_set(USR, UDRE0); } while(0)
137 #define KDBG_WAIT_TXDONE() do { loop_until_bit_is_set(USR, TXC0); } while(0)
140 * We must clear the TXC flag before sending a new character to allow
141 * KDBG_WAIT_TXDONE() to work properly.
143 * BUG: if KDBG_WRITE_CHAR() is called after the TXC flag is set by hardware,
144 * a new TXC could be generated after we've cleared it and before the new
145 * character is written to UDR. On a 485 bus, the transceiver will be put
146 * in RX mode while still transmitting the last char.
148 #define KDBG_WRITE_CHAR(c) do { USR |= BV(TXC0); UDR = (c); } while(0)
150 #define KDBG_MASK_IRQ(old) do { \
153 UCR &= ~(BV(TXCIE0) | BV(UDRIE0)); \
157 #define KDBG_RESTORE_IRQ(old) do { \
158 KDBG_WAIT_TXDONE(); \
163 typedef uint8_t kdbg_irqsave_t;
165 #elif CONFIG_KDEBUG_PORT == 1
168 * Support for special bus policies or external transceivers
169 * on UART1 (to be overridden in "hw_ser.h").
171 * HACK: if we don't set TXEN, kdbg disables the transmitter
172 * after each output statement until the serial driver
173 * is initialized. These glitches confuse the debug
174 * terminal that ends up printing some trash.
176 #ifndef KDBG_UART1_BUS_INIT
177 #define KDBG_UART1_BUS_INIT do { \
178 UCSR1B = BV(TXEN1); \
181 #ifndef KDBG_UART1_BUS_RX
182 #define KDBG_UART1_BUS_RX do {} while (0)
184 #ifndef KDBG_UART1_BUS_TX
185 #define KDBG_UART1_BUS_TX do {} while (0)
188 #define KDBG_WAIT_READY() do { loop_until_bit_is_set(UCSR1A, UDRE1); } while(0)
189 #define KDBG_WAIT_TXDONE() do { loop_until_bit_is_set(UCSR1A, TXC1); } while(0)
190 #define KDBG_WRITE_CHAR(c) do { UCSR1A |= BV(TXC1); UDR1 = (c); } while(0)
192 #define KDBG_MASK_IRQ(old) do { \
194 UCSR1B |= BV(TXEN1); \
195 UCSR1B &= ~(BV(TXCIE1) | BV(UDRIE1)); \
199 #define KDBG_RESTORE_IRQ(old) do { \
200 KDBG_WAIT_TXDONE(); \
205 typedef uint8_t kdbg_irqsave_t;
208 * Special debug port for BitBanged Serial see below for details...
210 #elif CONFIG_KDEBUG_PORT == 666
212 #define KDBG_WAIT_READY() do { /*nop*/ } while(0)
213 #define KDBG_WRITE_CHAR(c) _kdebug_bitbang_putchar((c))
214 #define KDBG_MASK_IRQ(old) do { IRQ_SAVE_DISABLE((old)); } while(0)
215 #define KDBG_RESTORE_IRQ(old) do { IRQ_RESTORE((old)); } while(0)
216 typedef cpuflags_t kdbg_irqsave_t;
218 #define KDBG_DELAY (((CLOCK_FREQ + CONFIG_KDEBUG_BAUDRATE / 2) / CONFIG_KDEBUG_BAUDRATE) + 7) / 14
220 static void _kdebug_bitbang_delay(void)
224 for (i = 0; i < KDBG_DELAY; i++)
234 #error CONFIG_KDEBUG_PORT should be either 0, 1 or 666
236 #elif defined(__MWERKS__) && CPU_DSP56K
237 /* Debugging go through the JTAG interface. The MSL library already
238 implements the console I/O correctly. */
240 #define KDBG_WAIT_READY() do { } while (0)
241 #define KDBG_WRITE_CHAR(c) __put_char(c, stdout)
242 #define KDBG_MASK_IRQ(old) do { (void)(old); } while (0)
243 #define KDBG_RESTORE_IRQ(old) do { (void)(old); } while (0)
244 typedef uint8_t kdbg_irqsave_t; /* unused */
245 #if CONFIG_KDEBUG_PORT == 666
246 #error BITBANG debug console missing for this platform
249 #error Unknown architecture
252 #if CONFIG_KDEBUG_PORT == 666
254 * Putchar for BITBANG serial debug console.
255 * Sometimes, we can't permit to use a whole serial for debugging purpose.
256 * Since debug console is in output only it is usefull to use a single generic I/O pin for debug.
257 * This is achieved by this simple function, that shift out the data like a UART, but
259 * The only requirement is that SER_BITBANG_* macros will be defined somewhere (usually hw_ser.h)
260 * \note All interrupts are disabled during debug prints!
262 static void _kdebug_bitbang_putchar(char c)
275 for (i = 0; i < 10; i++)
281 _kdebug_bitbang_delay();
294 /* Set serial port for 19200bps 8N1 */
295 INT_MASK1 &= ~(INT1F_TI | INT1F_RI);
296 SP_CON = SPCF_RECEIVE_ENABLE | SPCF_MODE1;
297 ioc1_img |= IOC1F_TXD_SEL | IOC1F_EXTINT_SRC;
303 #if CONFIG_KDEBUG_PORT == 666
305 #else /* CONFIG_KDEBUG_PORT != 666 */
306 /* Compute the baud rate */
307 uint16_t period = (((CLOCK_FREQ / 16UL) + (CONFIG_KDEBUG_BAUDRATE / 2)) / CONFIG_KDEBUG_BAUDRATE) - 1;
309 #if (CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128)
310 #if CONFIG_KDEBUG_PORT == 0
311 UBRR0H = (uint8_t)(period>>8);
312 UBRR0L = (uint8_t)period;
314 #elif CONFIG_KDEBUG_PORT == 1
315 UBRR1H = (uint8_t)(period>>8);
316 UBRR1L = (uint8_t)period;
319 #error CONFIG_KDEBUG_PORT must be either 0 or 1
322 #elif CPU_AVR_ATMEGA168
323 UBRR0H = (uint8_t)(period>>8);
324 UBRR0L = (uint8_t)period;
326 #elif CPU_AVR_ATMEGA8
327 UBRRH = (uint8_t)(period>>8);
328 UBRRL = (uint8_t)period;
329 #elif CPU_AVR_ATMEGA103
330 UBRR = (uint8_t)period;
335 #endif /* CONFIG_KDEBUG_PORT == 666 */
337 #endif /* !CPU_I196 && !CPU_AVR */
339 kputs("\n\n*** DBG START ***\n");
344 * Output one character to the debug console
346 static void __kputchar(char c, UNUSED_ARG(void *, unused))
348 /* Poll while serial buffer is still busy */
351 /* Send '\n' as '\r\n' for dumb terminals */
354 KDBG_WRITE_CHAR('\r');
362 void kputchar(char c)
364 /* Mask serial TX intr */
365 kdbg_irqsave_t irqsave;
366 KDBG_MASK_IRQ(irqsave);
370 /* Restore serial TX intr */
371 KDBG_RESTORE_IRQ(irqsave);
375 static void PGM_FUNC(kvprintf)(const char * PGM_ATTR fmt, va_list ap)
378 /* Mask serial TX intr */
379 kdbg_irqsave_t irqsave;
380 KDBG_MASK_IRQ(irqsave);
382 PGM_FUNC(_formatted_write)(fmt, __kputchar, 0, ap);
384 /* Restore serial TX intr */
385 KDBG_RESTORE_IRQ(irqsave);
387 /* A better than nothing printf() surrogate. */
388 PGM_FUNC(kputs)(fmt);
389 #endif /* CONFIG_PRINTF */
392 void PGM_FUNC(kprintf)(const char * PGM_ATTR fmt, ...)
397 PGM_FUNC(kvprintf)(fmt, ap);
401 void PGM_FUNC(kputs)(const char * PGM_ATTR str)
405 /* Mask serial TX intr */
406 kdbg_irqsave_t irqsave;
407 KDBG_MASK_IRQ(irqsave);
409 while ((c = PGM_READ_CHAR(str++)))
412 KDBG_RESTORE_IRQ(irqsave);
417 * Cheap function to print small integers without using printf().
427 digit = num / divisor;
430 if (digit || output_len || divisor == 1)
432 kputchar(digit + '0');
436 while (divisor /= 10);
442 static void klocation(const char * PGM_ATTR file, int line)
444 PGM_FUNC(kputs)(file);
447 PGM_FUNC(kputs)(PGM_STR(": "));
450 int PGM_FUNC(__assert)(const char * PGM_ATTR cond, const char * PGM_ATTR file, int line)
452 klocation(file, line);
453 PGM_FUNC(kputs)(PGM_STR("Assertion failed: "));
454 PGM_FUNC(kputs)(cond);
461 * Unfortunately, there's no way to get __func__ in
462 * program memory, so we waste quite a lot of RAM in
463 * AVR and other Harvard processors.
465 void PGM_FUNC(__trace)(const char *name)
467 PGM_FUNC(kprintf)(PGM_STR("%s()\n"), name);
470 void PGM_FUNC(__tracemsg)(const char *name, const char * PGM_ATTR fmt, ...)
474 PGM_FUNC(kprintf)(PGM_STR("%s(): "), name);
476 PGM_FUNC(kvprintf)(fmt, ap);
481 int PGM_FUNC(__invalid_ptr)(void *value, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
483 klocation(file, line);
484 PGM_FUNC(kputs)(PGM_STR("Invalid ptr: "));
485 PGM_FUNC(kputs)(name);
487 PGM_FUNC(kprintf)(PGM_STR(" = 0x%x\n"), (unsigned int)value);
496 void __init_wall(long *wall, int size)
499 *wall++ = WALL_VALUE;
503 int PGM_FUNC(__check_wall)(long *wall, int size, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
507 for (i = 0; i < size; i++)
509 if (wall[i] != WALL_VALUE)
511 klocation(file, line);
512 PGM_FUNC(kputs)(PGM_STR("Wall broken: "));
513 PGM_FUNC(kputs)(name);
515 PGM_FUNC(kprintf)(PGM_STR("[%d] (0x%p) = 0x%lx\n"), i, wall + i, wall[i]);
530 * Dump binary data in hex
532 void kdump(const void *_buf, size_t len)
534 const unsigned char *buf = (const unsigned char *)_buf;
537 kprintf("%02X", *buf++);
541 #endif /* CONFIG_PRINTF */