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.23 2005/04/11 19:10:27 bernie
19 *#* Include top-level headers from cfg/ subdir.
21 *#* Revision 1.22 2005/02/18 11:18:33 bernie
22 *#* Fixes for Harvard processors from project_ks.
24 *#* Revision 1.21 2005/02/16 20:29:48 bernie
25 *#* TRACE(), TRACEMSG(): Reduce code and data footprint.
27 *#* Revision 1.20 2005/01/25 08:36:40 bernie
28 *#* kputnum(): Export.
30 *#* Revision 1.19 2004/12/31 17:47:45 bernie
31 *#* Rename UNUSED() to UNUSED_ARG().
34 #include <cfg/debug.h>
36 #include <cfg/macros.h> /* for BV() */
37 #include <cfg/config.h>
40 #include <mware/formatwr.h> /* for _formatted_write() */
46 #define KDBG_WAIT_READY() do { /*nop*/ } while(0)
47 #define KDBG_WRITE_CHAR(c) putchar((c))
48 #define KDBG_MASK_IRQ(old) do { (void)(old); } while(0)
49 #define KDBG_RESTORE_IRQ(old) do { /*nop*/ } while(0)
50 typedef char kdbg_irqsave_t; /* unused */
53 #define KDBG_WAIT_READY() do {} while (!(SP_STAT & (SPSF_TX_EMPTY | SPSF_TX_INT)))
54 #define KDBG_WRITE_CHAR(c) do { SBUF = (c); } while(0)
55 #define KDBG_MASK_IRQ(old) \
57 (old) = INT_MASK1 & INT1F_TI; \
58 INT_MASK1 &= ~INT1F_TI; \
60 #define KDBG_RESTORE_IRQ(old) do { INT_MASK1 |= (old); }
61 typedef uint16_t kdbg_irqsave_t; /* FIXME: unconfirmed */
65 #if CONFIG_KDEBUG_PORT == 0
68 * Support for special bus policies or external transceivers
69 * on UART0 (to be overridden in "hw.h").
71 #ifndef KDBG_UART0_BUS_INIT
72 #define KDBG_UART0_BUS_INIT do {} while (0)
74 #ifndef KDBG_UART0_BUS_RX
75 #define KDBG_UART0_BUS_RX do {} while (0)
77 #ifndef KDBG_UART0_BUS_TX
78 #define KDBG_UART0_BUS_TX do {} while (0)
92 #define KDBG_WAIT_READY() do { loop_until_bit_is_set(USR, UDRE); } while(0)
93 #define KDBG_WAIT_TXDONE() do { loop_until_bit_is_set(USR, TXC); } while(0)
96 * We must clear the TXC flag before sending a new character to allow
97 * KDBG_WAIT_TXDONE() to work properly.
99 * BUG: if KDBG_WRITE_CHAR() is called after the TXC flag is set by hardware,
100 * a new TXC could be generated after we've cleared it and before the new
101 * character is written to UDR. On a 485 bus, the transceiver will be put
102 * in RX mode while still transmitting the last char.
104 #define KDBG_WRITE_CHAR(c) do { USR |= BV(TXC); UDR = (c); } while(0)
106 #define KDBG_MASK_IRQ(old) do { \
109 UCR &= ~(BV(TXCIE) | BV(UDRIE)); \
113 #define KDBG_RESTORE_IRQ(old) do { \
114 KDBG_WAIT_TXDONE(); \
119 typedef uint8_t kdbg_irqsave_t;
121 #elif CONFIG_KDEBUG_PORT == 1
124 * Support for special bus policies or external transceivers
125 * on UART1 (to be overridden in "hw.h").
127 #ifndef KDBG_UART1_BUS_INIT
128 #define KDBG_UART1_BUS_INIT do {} while (0)
130 #ifndef KDBG_UART1_BUS_RX
131 #define KDBG_UART1_BUS_RX do {} while (0)
133 #ifndef KDBG_UART1_BUS_TX
134 #define KDBG_UART1_BUS_TX do {} while (0)
137 #define KDBG_WAIT_READY() do { loop_until_bit_is_set(UCSR1A, UDRE); } while(0)
138 #define KDBG_WAIT_TXDONE() do { loop_until_bit_is_set(UCSR1A, TXC); } while(0)
139 #define KDBG_WRITE_CHAR(c) do { UCSR1A |= BV(TXC); UDR1 = (c); } while(0)
141 #define KDBG_MASK_IRQ(old) do { \
143 UCSR1B |= BV(TXEN); \
144 UCSR1B &= ~(BV(TXCIE) | BV(UDRIE)); \
148 #define KDBG_RESTORE_IRQ(old) do { \
149 KDBG_WAIT_TXDONE(); \
154 typedef uint8_t kdbg_irqsave_t;
156 #error CONFIG_KDEBUG_PORT should be either 0 or 1
158 #elif defined(__MWERKS__) && CPU_DSP56K
159 /* Debugging go through the JTAG interface. The MSL library already
160 implements the console I/O correctly. */
162 #define KDBG_WAIT_READY() do { } while (0)
163 #define KDBG_WRITE_CHAR(c) __put_char(c, stdout)
164 #define KDBG_MASK_IRQ(old) do { (void)(old); } while (0)
165 #define KDBG_RESTORE_IRQ(old) do { (void)(old); } while (0)
166 typedef uint8_t kdbg_irqsave_t; /* unused */
168 #error Unknown architecture
176 /* Set serial port for 19200bps 8N1 */
177 INT_MASK1 &= ~(INT1F_TI | INT1F_RI);
178 SP_CON = SPCF_RECEIVE_ENABLE | SPCF_MODE1;
179 ioc1_img |= IOC1F_TXD_SEL | IOC1F_EXTINT_SRC;
186 /* Compute the baud rate */
187 uint16_t period = (((CLOCK_FREQ / 16UL) + (CONFIG_KDEBUG_BAUDRATE / 2)) / CONFIG_KDEBUG_BAUDRATE) - 1;
190 #if CONFIG_KDEBUG_PORT == 0
191 UBRR0H = (uint8_t)(period>>8);
192 UBRR0L = (uint8_t)period;
194 #elif CONFIG_KDEBUG_PORT == 1
195 UBRR1H = (uint8_t)(period>>8);
196 UBRR1L = (uint8_t)period;
199 #error CONFIG_KDEBUG_PORT must be either 0 or 1
201 #elif CPU_AVR_ATMEGA8
202 UBRRH = (uint8_t)(period>>8);
203 UBRRL = (uint8_t)period;
204 #elif CPU_AVR_ATMEGA103
205 UBRR = (uint8_t)period;
211 #endif /* !CPU_I196 && !CPU_AVR */
213 kputs("\n\n*** DBG START ***\n");
218 * Output one character to the debug console
220 static void __kputchar(char c, UNUSED_ARG(void *, unused))
222 /* Poll while serial buffer is still busy */
225 /* Send '\n' as '\r\n' for dumb terminals */
228 KDBG_WRITE_CHAR('\r');
236 void kputchar(char c)
238 /* Mask serial TX intr */
239 kdbg_irqsave_t irqsave;
240 KDBG_MASK_IRQ(irqsave);
244 /* Restore serial TX intr */
245 KDBG_RESTORE_IRQ(irqsave);
249 static void PGM_FUNC(kvprintf)(const char * PGM_ATTR fmt, va_list ap)
252 /* Mask serial TX intr */
253 kdbg_irqsave_t irqsave;
254 KDBG_MASK_IRQ(irqsave);
256 PGM_FUNC(_formatted_write)(fmt, __kputchar, 0, ap);
258 /* Restore serial TX intr */
259 KDBG_RESTORE_IRQ(irqsave);
261 /* A better than nothing printf() surrogate. */
262 PGM_FUNC(kputs)(fmt);
263 #endif /* CONFIG_PRINTF */
266 void PGM_FUNC(kprintf)(const char * PGM_ATTR fmt, ...)
271 PGM_FUNC(kvprintf)(fmt, ap);
275 void PGM_FUNC(kputs)(const char * PGM_ATTR str)
279 /* Mask serial TX intr */
280 kdbg_irqsave_t irqsave;
281 KDBG_MASK_IRQ(irqsave);
283 while ((c = PGM_READ_CHAR(str++)))
286 KDBG_RESTORE_IRQ(irqsave);
291 * Cheap function to print small integers without using printf().
301 digit = num / divisor;
304 if (digit || output_len || divisor == 1)
306 kputchar(digit + '0');
310 while (divisor /= 10);
316 static void klocation(const char * PGM_ATTR file, int line)
318 PGM_FUNC(kputs)(file);
321 PGM_FUNC(kputs)(PSTR(": "));
324 int PGM_FUNC(__assert)(const char * PGM_ATTR cond, const char * PGM_ATTR file, int line)
326 klocation(file, line);
327 PGM_FUNC(kputs)(PSTR("Assertion failed: "));
328 PGM_FUNC(kputs)(cond);
334 * Unfortunately, there's no way to get __func__ in
335 * program memory, so we waste quite a lot of RAM in
336 * AVR and other Harvard processors.
338 void PGM_FUNC(__trace)(const char *name)
340 PGM_FUNC(kprintf)(PSTR("%s()\n"), name);
343 void PGM_FUNC(__tracemsg)(const char *name, const char * PGM_ATTR fmt, ...)
347 PGM_FUNC(kprintf)(PSTR("%s(): "), name);
349 PGM_FUNC(kvprintf)(fmt, ap);
353 int PGM_FUNC(__invalid_ptr)(void *value, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
355 klocation(file, line);
356 PGM_FUNC(kputs)(PSTR("Invalid ptr: "));
357 PGM_FUNC(kputs)(name);
359 PGM_FUNC(kprintf)(PSTR(" = 0x%x\n"), value);
368 void __init_wall(long *wall, int size)
371 *wall++ = WALL_VALUE;
375 int PGM_FUNC(__check_wall)(long *wall, int size, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
379 for (i = 0; i < size; i++)
381 if (wall[i] != WALL_VALUE)
383 klocation(file, line);
384 PGM_FUNC(kputs)(PSTR("Wall broken: "));
385 PGM_FUNC(kputs)(name);
387 PGM_FUNC(kprintf)(PSTR("[%d] (0x%p) = 0x%lx\n"), i, wall + i, wall[i]);
402 * Dump binary data in hex
404 void kdump(const void *_buf, size_t len)
406 const unsigned char *buf = (const unsigned char *)_buf;
409 kprintf("%02X", *buf++);
413 #endif /* CONFIG_PRINTF */