4 * Copyright 2003,2004 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 devlib/README for information.
9 * \brief General pourpose debug functions.
13 * \author Bernardo Innocenti <bernie@develer.com>
14 * \author Stefano Fedrigo <aleph@develer.com>
19 *#* Revision 1.13 2004/08/29 22:04:26 bernie
20 *#* Convert 485 macros to generic BUS macros;
21 *#* kputchar(): New public function.
23 *#* Revision 1.12 2004/08/25 14:12:08 rasky
24 *#* Aggiornato il comment block dei log RCS
26 *#* Revision 1.11 2004/08/24 16:19:08 bernie
27 *#* kputchar(): New public function; Add missing dummy inlines for \!_DEBUG.
29 *#* Revision 1.10 2004/08/04 15:57:50 rasky
30 *#* Cambiata la putchar per kdebug per DSP56k: la nuova funzione e' quella piu' a basso livello (assembly)
32 *#* Revision 1.9 2004/08/02 20:20:29 aleph
33 *#* Merge from project_ks
35 *#* Revision 1.8 2004/07/30 14:26:33 rasky
36 *#* Semplificato l'output dell'ASSERT
37 *#* Aggiunta ASSERT2 con stringa di help opzionalmente disattivabile
39 *#* Revision 1.7 2004/07/30 14:15:53 rasky
40 *#* Nuovo supporto unificato per detect della CPU
42 *#* Revision 1.6 2004/07/18 21:49:28 bernie
43 *#* Add ATmega8 support.
45 *#* Revision 1.5 2004/06/27 15:20:26 aleph
46 *#* Change UNUSED() macro to accept two arguments: type and name;
47 *#* Add macro GNUC_PREREQ to detect GCC version during build;
48 *#* Some spacing cleanups and typo fix
50 *#* Revision 1.4 2004/06/06 18:09:51 bernie
51 *#* Import DSP56800 changes; Print broken wall bricks in hex.
53 *#* Revision 1.3 2004/06/03 11:27:09 bernie
54 *#* Add dual-license information.
56 *#* Revision 1.2 2004/05/23 18:21:53 bernie
57 *#* Trim CVS logs and cleanup header info.
65 #include <mware/formatwr.h> /* for _formatted_write() */
71 #define KDBG_WAIT_READY() do { /*nop*/ } while(0)
72 #define KDBG_WRITE_CHAR(c) putchar((c))
73 #define KDBG_MASK_IRQ(old) do { (void)(old); } while(0)
74 #define KDBG_RESTORE_IRQ(old) do { /*nop*/ } while(0)
75 typedef char kdbg_irqsave_t; /* unused */
78 #define KDBG_WAIT_READY() do {} while (!(SP_STAT & (SPSF_TX_EMPTY | SPSF_TX_INT)))
79 #define KDBG_WRITE_CHAR(c) do { SBUF = (c); } while(0)
80 #define KDBG_MASK_IRQ(old) \
82 (old) = INT_MASK1 & INT1F_TI; \
83 INT_MASK1 &= ~INT1F_TI; \
85 #define KDBG_RESTORE_IRQ(old) do { INT_MASK1 |= (old); }
86 typedef uint16_t kdbg_irqsave_t; /* FIXME: unconfirmed */
90 #if CONFIG_KDEBUG_PORT == 0
93 * Support for special bus policies or external transceivers
94 * on UART0 (to be overridden in "hw.h").
96 #if !defined(KDBG_UART0_BUS_INIT)
97 #if defined(KDBG_UART0_BUS_RX) || defined(KDBG_UART0_BUS_TX)
98 #error KDBG_UART0_BUS_INIT, KDBG_UART0_BUS_RX and KDBG_UART0_BUS_TX must be defined together
100 #define KDBG_UART0_BUS_INIT do {} while (0)
101 #define KDBG_UART0_BUS_TX do {} while (0)
102 #define KDBG_UART0_BUS_RX do {} while (0)
103 #elif !defined(KDBG_UART0_BUS_RX) || !defined(KDBG_UART0_BUS_TX)
104 #error KDBG_UART0_BUS_INIT, KDBG_UART0_BUS_RX and KDBG_UART0_BUS_TX must be defined together
107 #if defined(__AVR_ATmega64__)
111 #elif defined(__AVR_ATmega8__)
116 #define KDBG_WAIT_READY() do { loop_until_bit_is_set(USR, UDRE); } while(0)
117 #define KDBG_WAIT_TXDONE() do { loop_until_bit_is_set(USR, TXC); } while(0)
119 * We must clear the TXC flag before sending a new character to allow
120 * KDBG_WAIT_TXDONE() to work properly.
122 * BUG: if KDBG_WRITE_CHAR() is called after the TXC flag is set by hardware,
123 * a new TXC could be generated after we've cleared it and before the new
124 * character is written to UDR. On a 485 bus, the transceiver will be put
125 * in RX mode while still transmitting the last char.
127 #define KDBG_WRITE_CHAR(c) do { USR |= BV(TXC); UDR = (c); } while(0)
129 #define KDBG_MASK_IRQ(old) do { \
133 UCR &= ~(BV(TXCIE) | BV(UDRIE)); \
136 #define KDBG_RESTORE_IRQ(old) do { \
137 KDBG_WAIT_TXDONE(); \
142 typedef uint8_t kdbg_irqsave_t;
144 #elif CONFIG_KDEBUG_PORT == 1
147 * Support for special bus policies or external transceivers
148 * on UART1 (to be overridden in "hw.h").
150 #ifndef KDBG_UART1_BUS_INIT
151 #if defined(KDBG_UART1_BUS_RX) || defined(KDBG_UART1_BUS_TX)
152 #error KDBG_UART1_BUS_INIT, KDBG_UART1_BUS_RX and KDBG_UART1_BUS_TX must be defined together
154 #define KDBG_UART1_BUS_INIT do {} while (0)
155 #define KDBG_UART1_BUS_TX do {} while (0)
156 #define KDBG_UART1_BUS_RX do {} while (0)
157 #elif !defined(KDBG_UART1_BUS_RX) || !defined(KDBG_UART1_BUS_TX)
158 #error KDBG_UART1_BUS_INIT, KDBG_UART1_BUS_RX and KDBG_UART1_BUS_TX must be defined together
161 #define KDBG_WAIT_READY() do { loop_until_bit_is_set(UCSR1A, UDRE); } while(0)
162 #define KDBG_WAIT_TXDONE() do { loop_until_bit_is_set(UCSR1A, TXC); } while(0)
163 #define KDBG_WRITE_CHAR(c) do { UCSR1A |= BV(TXC); UDR1 = (c); } while(0)
165 #define KDBG_MASK_IRQ(old) do { \
168 UCSR1B |= BV(TXEN); \
169 UCSR1B &= ~(BV(TXCIE) | BV(UDRIE)); \
172 #define KDBG_RESTORE_IRQ(old) do { \
173 KDBG_WAIT_TXDONE(); \
178 typedef uint8_t kdbg_irqsave_t;
180 #error CONFIG_KDEBUG_PORT should be either 0 or 1
182 #elif defined(__MWERKS__) && CPU_DSP56K
183 /* Debugging go through the JTAG interface. The MSL library already
184 implements the console I/O correctly. */
186 #define KDBG_WAIT_READY() do { } while (0)
187 #define KDBG_WRITE_CHAR(c) __put_char(c, stdout)
188 #define KDBG_MASK_IRQ(old) do { (void)(old); } while (0)
189 #define KDBG_RESTORE_IRQ(old) do { (void)(old); } while (0)
190 typedef uint8_t kdbg_irqsave_t; /* unused */
192 #error Unknown architecture
200 /* Set serial port for 19200bps 8N1 */
201 INT_MASK1 &= ~(INT1F_TI | INT1F_RI);
202 SP_CON = SPCF_RECEIVE_ENABLE | SPCF_MODE1;
203 ioc1_img |= IOC1F_TXD_SEL | IOC1F_EXTINT_SRC;
210 /* Compute the baud rate */
211 uint16_t period = (((CLOCK_FREQ / 16UL) + (CONFIG_KDEBUG_BAUDRATE / 2)) / CONFIG_KDEBUG_BAUDRATE) - 1;
213 #if defined(__AVR_ATmega64__)
214 #if CONFIG_KDEBUG_PORT == 0
215 UBRR0H = (uint8_t)(period>>8);
216 UBRR0L = (uint8_t)period;
218 #elif CONFIG_KDEBUG_PORT == 1
219 UBRR1H = (uint8_t)(period>>8);
220 UBRR1L = (uint8_t)period;
223 #error CONFIG_KDEBUG_PORT must be either 0 or 1
225 #elif defined(__AVR_ATmega8__)
226 UBRRH = (uint8_t)(period>>8);
227 UBRRL = (uint8_t)period;
228 #elif defined(__AVR_ATmega103__)
229 UBRR = (uint8_t)period;
235 #endif /* !CPU_I196 && !CPU_AVR */
237 kputs("\n\n*** DBG START ***\n");
242 * Output one character to the debug console
244 static void __kputchar(char c, UNUSED(void *, unused))
246 /* Poll while serial buffer is still busy */
249 /* Send '\n' as '\r\n' for dumb terminals */
252 KDBG_WRITE_CHAR('\r');
260 void kputchar(char c)
262 /* Mask serial TX intr */
263 kdbg_irqsave_t irqsave;
264 KDBG_MASK_IRQ(irqsave);
268 /* Restore serial TX intr */
269 KDBG_RESTORE_IRQ(irqsave);
273 void PGM_FUNC(kprintf)(const char * PGM_ATTR fmt, ...)
277 /* Mask serial TX intr */
278 kdbg_irqsave_t irqsave;
279 KDBG_MASK_IRQ(irqsave);
282 PGM_FUNC(_formatted_write)(fmt, __kputchar, 0, ap);
285 /* Restore serial TX intr */
286 KDBG_RESTORE_IRQ(irqsave);
290 void PGM_FUNC(kputs)(const char * PGM_ATTR str)
294 /* Mask serial TX intr */
295 kdbg_irqsave_t irqsave;
296 KDBG_MASK_IRQ(irqsave);
298 while ((c = PGM_READ_CHAR(str++)))
301 KDBG_RESTORE_IRQ(irqsave);
305 int PGM_FUNC(__assert)(const char * PGM_ATTR cond, const char * PGM_ATTR file, int line)
307 PGM_FUNC(kputs)(file);
308 PGM_FUNC(kprintf)(PSTR(":%d: Assertion failed: "), line);
309 PGM_FUNC(kputs)(cond);
310 PGM_FUNC(kputs)(PSTR("\n"));
315 int PGM_FUNC(__invalid_ptr)(void *value, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
317 PGM_FUNC(kputs)(file);
318 PGM_FUNC(kprintf)(PSTR(":%d: Invalid pointer: "), line);
319 PGM_FUNC(kputs)(name);
320 PGM_FUNC(kprintf)(PSTR(" = 0x%x\n"), value);
325 void __init_wall(long *wall, int size)
328 *wall++ = WALL_VALUE;
332 int __check_wall(long *wall, int size, const char *name, const char *file, int line)
336 for (i = 0; i < size; i++)
338 if (wall[i] != WALL_VALUE)
340 kprintf("%s:%d: Wall broken: %s[%d] (0x%p) = 0x%lx\n",
341 file, line, name, i, wall + i, wall[i]);
351 * Dump binary data in hex
353 void kdump(const void *_buf, size_t len)
355 const unsigned char *buf = (const unsigned char *)_buf;
358 kprintf("%02X", *buf++);