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.22 2005/02/18 11:18:33 bernie
19 *#* Fixes for Harvard processors from project_ks.
21 *#* Revision 1.21 2005/02/16 20:29:48 bernie
22 *#* TRACE(), TRACEMSG(): Reduce code and data footprint.
24 *#* Revision 1.20 2005/01/25 08:36:40 bernie
25 *#* kputnum(): Export.
27 *#* Revision 1.19 2004/12/31 17:47:45 bernie
28 *#* Rename UNUSED() to UNUSED_ARG().
33 #include <macros.h> /* for BV() */
37 #include <mware/formatwr.h> /* for _formatted_write() */
43 #define KDBG_WAIT_READY() do { /*nop*/ } while(0)
44 #define KDBG_WRITE_CHAR(c) putchar((c))
45 #define KDBG_MASK_IRQ(old) do { (void)(old); } while(0)
46 #define KDBG_RESTORE_IRQ(old) do { /*nop*/ } while(0)
47 typedef char kdbg_irqsave_t; /* unused */
50 #define KDBG_WAIT_READY() do {} while (!(SP_STAT & (SPSF_TX_EMPTY | SPSF_TX_INT)))
51 #define KDBG_WRITE_CHAR(c) do { SBUF = (c); } while(0)
52 #define KDBG_MASK_IRQ(old) \
54 (old) = INT_MASK1 & INT1F_TI; \
55 INT_MASK1 &= ~INT1F_TI; \
57 #define KDBG_RESTORE_IRQ(old) do { INT_MASK1 |= (old); }
58 typedef uint16_t kdbg_irqsave_t; /* FIXME: unconfirmed */
62 #if CONFIG_KDEBUG_PORT == 0
65 * Support for special bus policies or external transceivers
66 * on UART0 (to be overridden in "hw.h").
68 #ifndef KDBG_UART0_BUS_INIT
69 #define KDBG_UART0_BUS_INIT do {} while (0)
71 #ifndef KDBG_UART0_BUS_RX
72 #define KDBG_UART0_BUS_RX do {} while (0)
74 #ifndef KDBG_UART0_BUS_TX
75 #define KDBG_UART0_BUS_TX do {} while (0)
89 #define KDBG_WAIT_READY() do { loop_until_bit_is_set(USR, UDRE); } while(0)
90 #define KDBG_WAIT_TXDONE() do { loop_until_bit_is_set(USR, TXC); } while(0)
93 * We must clear the TXC flag before sending a new character to allow
94 * KDBG_WAIT_TXDONE() to work properly.
96 * BUG: if KDBG_WRITE_CHAR() is called after the TXC flag is set by hardware,
97 * a new TXC could be generated after we've cleared it and before the new
98 * character is written to UDR. On a 485 bus, the transceiver will be put
99 * in RX mode while still transmitting the last char.
101 #define KDBG_WRITE_CHAR(c) do { USR |= BV(TXC); UDR = (c); } while(0)
103 #define KDBG_MASK_IRQ(old) do { \
106 UCR &= ~(BV(TXCIE) | BV(UDRIE)); \
110 #define KDBG_RESTORE_IRQ(old) do { \
111 KDBG_WAIT_TXDONE(); \
116 typedef uint8_t kdbg_irqsave_t;
118 #elif CONFIG_KDEBUG_PORT == 1
121 * Support for special bus policies or external transceivers
122 * on UART1 (to be overridden in "hw.h").
124 #ifndef KDBG_UART1_BUS_INIT
125 #define KDBG_UART1_BUS_INIT do {} while (0)
127 #ifndef KDBG_UART1_BUS_RX
128 #define KDBG_UART1_BUS_RX do {} while (0)
130 #ifndef KDBG_UART1_BUS_TX
131 #define KDBG_UART1_BUS_TX do {} while (0)
134 #define KDBG_WAIT_READY() do { loop_until_bit_is_set(UCSR1A, UDRE); } while(0)
135 #define KDBG_WAIT_TXDONE() do { loop_until_bit_is_set(UCSR1A, TXC); } while(0)
136 #define KDBG_WRITE_CHAR(c) do { UCSR1A |= BV(TXC); UDR1 = (c); } while(0)
138 #define KDBG_MASK_IRQ(old) do { \
140 UCSR1B |= BV(TXEN); \
141 UCSR1B &= ~(BV(TXCIE) | BV(UDRIE)); \
145 #define KDBG_RESTORE_IRQ(old) do { \
146 KDBG_WAIT_TXDONE(); \
151 typedef uint8_t kdbg_irqsave_t;
153 #error CONFIG_KDEBUG_PORT should be either 0 or 1
155 #elif defined(__MWERKS__) && CPU_DSP56K
156 /* Debugging go through the JTAG interface. The MSL library already
157 implements the console I/O correctly. */
159 #define KDBG_WAIT_READY() do { } while (0)
160 #define KDBG_WRITE_CHAR(c) __put_char(c, stdout)
161 #define KDBG_MASK_IRQ(old) do { (void)(old); } while (0)
162 #define KDBG_RESTORE_IRQ(old) do { (void)(old); } while (0)
163 typedef uint8_t kdbg_irqsave_t; /* unused */
165 #error Unknown architecture
173 /* Set serial port for 19200bps 8N1 */
174 INT_MASK1 &= ~(INT1F_TI | INT1F_RI);
175 SP_CON = SPCF_RECEIVE_ENABLE | SPCF_MODE1;
176 ioc1_img |= IOC1F_TXD_SEL | IOC1F_EXTINT_SRC;
183 /* Compute the baud rate */
184 uint16_t period = (((CLOCK_FREQ / 16UL) + (CONFIG_KDEBUG_BAUDRATE / 2)) / CONFIG_KDEBUG_BAUDRATE) - 1;
187 #if CONFIG_KDEBUG_PORT == 0
188 UBRR0H = (uint8_t)(period>>8);
189 UBRR0L = (uint8_t)period;
191 #elif CONFIG_KDEBUG_PORT == 1
192 UBRR1H = (uint8_t)(period>>8);
193 UBRR1L = (uint8_t)period;
196 #error CONFIG_KDEBUG_PORT must be either 0 or 1
198 #elif CPU_AVR_ATMEGA8
199 UBRRH = (uint8_t)(period>>8);
200 UBRRL = (uint8_t)period;
201 #elif CPU_AVR_ATMEGA103
202 UBRR = (uint8_t)period;
208 #endif /* !CPU_I196 && !CPU_AVR */
210 kputs("\n\n*** DBG START ***\n");
215 * Output one character to the debug console
217 static void __kputchar(char c, UNUSED_ARG(void *, unused))
219 /* Poll while serial buffer is still busy */
222 /* Send '\n' as '\r\n' for dumb terminals */
225 KDBG_WRITE_CHAR('\r');
233 void kputchar(char c)
235 /* Mask serial TX intr */
236 kdbg_irqsave_t irqsave;
237 KDBG_MASK_IRQ(irqsave);
241 /* Restore serial TX intr */
242 KDBG_RESTORE_IRQ(irqsave);
246 static void PGM_FUNC(kvprintf)(const char * PGM_ATTR fmt, va_list ap)
249 /* Mask serial TX intr */
250 kdbg_irqsave_t irqsave;
251 KDBG_MASK_IRQ(irqsave);
253 PGM_FUNC(_formatted_write)(fmt, __kputchar, 0, ap);
255 /* Restore serial TX intr */
256 KDBG_RESTORE_IRQ(irqsave);
258 /* A better than nothing printf() surrogate. */
259 PGM_FUNC(kputs)(fmt);
260 #endif /* CONFIG_PRINTF */
263 void PGM_FUNC(kprintf)(const char * PGM_ATTR fmt, ...)
268 PGM_FUNC(kvprintf)(fmt, ap);
272 void PGM_FUNC(kputs)(const char * PGM_ATTR str)
276 /* Mask serial TX intr */
277 kdbg_irqsave_t irqsave;
278 KDBG_MASK_IRQ(irqsave);
280 while ((c = PGM_READ_CHAR(str++)))
283 KDBG_RESTORE_IRQ(irqsave);
288 * Cheap function to print small integers without using printf().
298 digit = num / divisor;
301 if (digit || output_len || divisor == 1)
303 kputchar(digit + '0');
307 while (divisor /= 10);
313 static void klocation(const char * PGM_ATTR file, int line)
315 PGM_FUNC(kputs)(file);
318 PGM_FUNC(kputs)(PSTR(": "));
321 int PGM_FUNC(__assert)(const char * PGM_ATTR cond, const char * PGM_ATTR file, int line)
323 klocation(file, line);
324 PGM_FUNC(kputs)(PSTR("Assertion failed: "));
325 PGM_FUNC(kputs)(cond);
331 * Unfortunately, there's no way to get __func__ in
332 * program memory, so we waste quite a lot of RAM in
333 * AVR and other Harvard processors.
335 void PGM_FUNC(__trace)(const char *name)
337 PGM_FUNC(kprintf)(PSTR("%s()\n"), name);
340 void PGM_FUNC(__tracemsg)(const char *name, const char * PGM_ATTR fmt, ...)
344 PGM_FUNC(kprintf)(PSTR("%s(): "), name);
346 PGM_FUNC(kvprintf)(fmt, ap);
350 int PGM_FUNC(__invalid_ptr)(void *value, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
352 klocation(file, line);
353 PGM_FUNC(kputs)(PSTR("Invalid ptr: "));
354 PGM_FUNC(kputs)(name);
356 PGM_FUNC(kprintf)(PSTR(" = 0x%x\n"), value);
365 void __init_wall(long *wall, int size)
368 *wall++ = WALL_VALUE;
372 int PGM_FUNC(__check_wall)(long *wall, int size, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
376 for (i = 0; i < size; i++)
378 if (wall[i] != WALL_VALUE)
380 klocation(file, line);
381 PGM_FUNC(kputs)(PSTR("Wall broken: "));
382 PGM_FUNC(kputs)(name);
384 PGM_FUNC(kprintf)(PSTR("[%d] (0x%p) = 0x%lx\n"), i, wall + i, wall[i]);
399 * Dump binary data in hex
401 void kdump(const void *_buf, size_t len)
403 const unsigned char *buf = (const unsigned char *)_buf;
406 kprintf("%02X", *buf++);
410 #endif /* CONFIG_PRINTF */