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.24 2005/04/12 01:36:37 bernie
19 *#* Add hack to enable TX line at module initialization.
21 *#* Revision 1.23 2005/04/11 19:10:27 bernie
22 *#* Include top-level headers from cfg/ subdir.
24 *#* Revision 1.22 2005/02/18 11:18:33 bernie
25 *#* Fixes for Harvard processors from project_ks.
27 *#* Revision 1.21 2005/02/16 20:29:48 bernie
28 *#* TRACE(), TRACEMSG(): Reduce code and data footprint.
30 *#* Revision 1.20 2005/01/25 08:36:40 bernie
31 *#* kputnum(): Export.
33 *#* Revision 1.19 2004/12/31 17:47:45 bernie
34 *#* Rename UNUSED() to UNUSED_ARG().
37 #include <cfg/debug.h>
39 #include <cfg/macros.h> /* for BV() */
40 #include <cfg/config.h>
43 #include <mware/formatwr.h> /* for _formatted_write() */
49 #define KDBG_WAIT_READY() do { /*nop*/ } while(0)
50 #define KDBG_WRITE_CHAR(c) putchar((c))
51 #define KDBG_MASK_IRQ(old) do { (void)(old); } while(0)
52 #define KDBG_RESTORE_IRQ(old) do { /*nop*/ } while(0)
53 typedef char kdbg_irqsave_t; /* unused */
56 #define KDBG_WAIT_READY() do {} while (!(SP_STAT & (SPSF_TX_EMPTY | SPSF_TX_INT)))
57 #define KDBG_WRITE_CHAR(c) do { SBUF = (c); } while(0)
58 #define KDBG_MASK_IRQ(old) \
60 (old) = INT_MASK1 & INT1F_TI; \
61 INT_MASK1 &= ~INT1F_TI; \
63 #define KDBG_RESTORE_IRQ(old) do { INT_MASK1 |= (old); }
64 typedef uint16_t kdbg_irqsave_t; /* FIXME: unconfirmed */
68 #if CONFIG_KDEBUG_PORT == 0
71 * Support for special bus policies or external transceivers
72 * on UART0 (to be overridden in "hw.h").
74 * HACK: if we don't set TXEN, kdbg disables the transmitter
75 * after each output statement until the serial driver
76 * is initialized. These glitches confuse the debug
77 * terminal that ends up printing some trash.
79 #ifndef KDBG_UART0_BUS_INIT
80 #define KDBG_UART0_BUS_INIT do { \
84 #ifndef KDBG_UART0_BUS_RX
85 #define KDBG_UART0_BUS_RX do {} while (0)
87 #ifndef KDBG_UART0_BUS_TX
88 #define KDBG_UART0_BUS_TX do {} while (0)
102 #define KDBG_WAIT_READY() do { loop_until_bit_is_set(USR, UDRE); } while(0)
103 #define KDBG_WAIT_TXDONE() do { loop_until_bit_is_set(USR, TXC); } while(0)
106 * We must clear the TXC flag before sending a new character to allow
107 * KDBG_WAIT_TXDONE() to work properly.
109 * BUG: if KDBG_WRITE_CHAR() is called after the TXC flag is set by hardware,
110 * a new TXC could be generated after we've cleared it and before the new
111 * character is written to UDR. On a 485 bus, the transceiver will be put
112 * in RX mode while still transmitting the last char.
114 #define KDBG_WRITE_CHAR(c) do { USR |= BV(TXC); UDR = (c); } while(0)
116 #define KDBG_MASK_IRQ(old) do { \
119 UCR &= ~(BV(TXCIE) | BV(UDRIE)); \
123 #define KDBG_RESTORE_IRQ(old) do { \
124 KDBG_WAIT_TXDONE(); \
129 typedef uint8_t kdbg_irqsave_t;
131 #elif CONFIG_KDEBUG_PORT == 1
134 * Support for special bus policies or external transceivers
135 * on UART1 (to be overridden in "hw.h").
137 * HACK: if we don't set TXEN, kdbg disables the transmitter
138 * after each output statement until the serial driver
139 * is initialized. These glitches confuse the debug
140 * terminal that ends up printing some trash.
142 #ifndef KDBG_UART1_BUS_INIT
143 #define KDBG_UART1_BUS_INIT do { \
147 #ifndef KDBG_UART1_BUS_RX
148 #define KDBG_UART1_BUS_RX do {} while (0)
150 #ifndef KDBG_UART1_BUS_TX
151 #define KDBG_UART1_BUS_TX do {} while (0)
154 #define KDBG_WAIT_READY() do { loop_until_bit_is_set(UCSR1A, UDRE); } while(0)
155 #define KDBG_WAIT_TXDONE() do { loop_until_bit_is_set(UCSR1A, TXC); } while(0)
156 #define KDBG_WRITE_CHAR(c) do { UCSR1A |= BV(TXC); UDR1 = (c); } while(0)
158 #define KDBG_MASK_IRQ(old) do { \
160 UCSR1B |= BV(TXEN); \
161 UCSR1B &= ~(BV(TXCIE) | BV(UDRIE)); \
165 #define KDBG_RESTORE_IRQ(old) do { \
166 KDBG_WAIT_TXDONE(); \
171 typedef uint8_t kdbg_irqsave_t;
173 #error CONFIG_KDEBUG_PORT should be either 0 or 1
175 #elif defined(__MWERKS__) && CPU_DSP56K
176 /* Debugging go through the JTAG interface. The MSL library already
177 implements the console I/O correctly. */
179 #define KDBG_WAIT_READY() do { } while (0)
180 #define KDBG_WRITE_CHAR(c) __put_char(c, stdout)
181 #define KDBG_MASK_IRQ(old) do { (void)(old); } while (0)
182 #define KDBG_RESTORE_IRQ(old) do { (void)(old); } while (0)
183 typedef uint8_t kdbg_irqsave_t; /* unused */
185 #error Unknown architecture
193 /* Set serial port for 19200bps 8N1 */
194 INT_MASK1 &= ~(INT1F_TI | INT1F_RI);
195 SP_CON = SPCF_RECEIVE_ENABLE | SPCF_MODE1;
196 ioc1_img |= IOC1F_TXD_SEL | IOC1F_EXTINT_SRC;
203 /* Compute the baud rate */
204 uint16_t period = (((CLOCK_FREQ / 16UL) + (CONFIG_KDEBUG_BAUDRATE / 2)) / CONFIG_KDEBUG_BAUDRATE) - 1;
207 #if CONFIG_KDEBUG_PORT == 0
208 UBRR0H = (uint8_t)(period>>8);
209 UBRR0L = (uint8_t)period;
211 #elif CONFIG_KDEBUG_PORT == 1
212 UBRR1H = (uint8_t)(period>>8);
213 UBRR1L = (uint8_t)period;
216 #error CONFIG_KDEBUG_PORT must be either 0 or 1
218 #elif CPU_AVR_ATMEGA8
219 UBRRH = (uint8_t)(period>>8);
220 UBRRL = (uint8_t)period;
221 #elif CPU_AVR_ATMEGA103
222 UBRR = (uint8_t)period;
228 #endif /* !CPU_I196 && !CPU_AVR */
230 kputs("\n\n*** DBG START ***\n");
235 * Output one character to the debug console
237 static void __kputchar(char c, UNUSED_ARG(void *, unused))
239 /* Poll while serial buffer is still busy */
242 /* Send '\n' as '\r\n' for dumb terminals */
245 KDBG_WRITE_CHAR('\r');
253 void kputchar(char c)
255 /* Mask serial TX intr */
256 kdbg_irqsave_t irqsave;
257 KDBG_MASK_IRQ(irqsave);
261 /* Restore serial TX intr */
262 KDBG_RESTORE_IRQ(irqsave);
266 static void PGM_FUNC(kvprintf)(const char * PGM_ATTR fmt, va_list ap)
269 /* Mask serial TX intr */
270 kdbg_irqsave_t irqsave;
271 KDBG_MASK_IRQ(irqsave);
273 PGM_FUNC(_formatted_write)(fmt, __kputchar, 0, ap);
275 /* Restore serial TX intr */
276 KDBG_RESTORE_IRQ(irqsave);
278 /* A better than nothing printf() surrogate. */
279 PGM_FUNC(kputs)(fmt);
280 #endif /* CONFIG_PRINTF */
283 void PGM_FUNC(kprintf)(const char * PGM_ATTR fmt, ...)
288 PGM_FUNC(kvprintf)(fmt, ap);
292 void PGM_FUNC(kputs)(const char * PGM_ATTR str)
296 /* Mask serial TX intr */
297 kdbg_irqsave_t irqsave;
298 KDBG_MASK_IRQ(irqsave);
300 while ((c = PGM_READ_CHAR(str++)))
303 KDBG_RESTORE_IRQ(irqsave);
308 * Cheap function to print small integers without using printf().
318 digit = num / divisor;
321 if (digit || output_len || divisor == 1)
323 kputchar(digit + '0');
327 while (divisor /= 10);
333 static void klocation(const char * PGM_ATTR file, int line)
335 PGM_FUNC(kputs)(file);
338 PGM_FUNC(kputs)(PSTR(": "));
341 int PGM_FUNC(__assert)(const char * PGM_ATTR cond, const char * PGM_ATTR file, int line)
343 klocation(file, line);
344 PGM_FUNC(kputs)(PSTR("Assertion failed: "));
345 PGM_FUNC(kputs)(cond);
351 * Unfortunately, there's no way to get __func__ in
352 * program memory, so we waste quite a lot of RAM in
353 * AVR and other Harvard processors.
355 void PGM_FUNC(__trace)(const char *name)
357 PGM_FUNC(kprintf)(PSTR("%s()\n"), name);
360 void PGM_FUNC(__tracemsg)(const char *name, const char * PGM_ATTR fmt, ...)
364 PGM_FUNC(kprintf)(PSTR("%s(): "), name);
366 PGM_FUNC(kvprintf)(fmt, ap);
370 int PGM_FUNC(__invalid_ptr)(void *value, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
372 klocation(file, line);
373 PGM_FUNC(kputs)(PSTR("Invalid ptr: "));
374 PGM_FUNC(kputs)(name);
376 PGM_FUNC(kprintf)(PSTR(" = 0x%x\n"), value);
385 void __init_wall(long *wall, int size)
388 *wall++ = WALL_VALUE;
392 int PGM_FUNC(__check_wall)(long *wall, int size, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
396 for (i = 0; i < size; i++)
398 if (wall[i] != WALL_VALUE)
400 klocation(file, line);
401 PGM_FUNC(kputs)(PSTR("Wall broken: "));
402 PGM_FUNC(kputs)(name);
404 PGM_FUNC(kprintf)(PSTR("[%d] (0x%p) = 0x%lx\n"), i, wall + i, wall[i]);
419 * Dump binary data in hex
421 void kdump(const void *_buf, size_t len)
423 const unsigned char *buf = (const unsigned char *)_buf;
426 kprintf("%02X", *buf++);
430 #endif /* CONFIG_PRINTF */