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.5 2004/06/27 15:20:26 aleph
20 * Change UNUSED() macro to accept two arguments: type and name;
21 * Add macro GNUC_PREREQ to detect GCC version during build;
22 * Some spacing cleanups and typo fix
24 * Revision 1.4 2004/06/06 18:09:51 bernie
25 * Import DSP56800 changes; Print broken wall bricks in hex.
27 * Revision 1.3 2004/06/03 11:27:09 bernie
28 * Add dual-license information.
30 * Revision 1.2 2004/05/23 18:21:53 bernie
31 * Trim CVS logs and cleanup header info.
39 #include <mware/formatwr.h> /* for _formatted_write() */
45 #define KDBG_WAIT_READY() do {/*nop*/} while(0)
46 #define KDBG_WRITE_CHAR(c) putchar((c))
47 #define KDBG_MASK_IRQ(old) do {/*nop*/} while(0)
48 #define KDBG_RESTORE_IRQ() do {/*nop*/} while(0)
49 #elif defined(__I196__)
51 #define KDBG_WAIT_READY() do {} while (!(SP_STAT & (SPSF_TX_EMPTY | SPSF_TX_INT)))
52 #define KDBG_WRITE_CHAR(c) do { SBUF = (c); } while(0)
53 #define KDBG_MASK_IRQ(old) \
55 (old) = INT_MASK1 & INT1F_TI; \
56 INT_MASK1 &= ~INT1F_TI; \
58 #define KDBG_RESTORE_IRQ(old) do { INT_MASK1 |= (old); }
59 #elif defined(__AVR__)
61 #if CONFIG_KDEBUG_PORT == 0
62 #ifndef __AVR_ATmega103__
67 #define KDBG_WAIT_READY() do { loop_until_bit_is_set(USR, UDRE); } while(0)
68 #define KDBG_WRITE_CHAR(c) do { UCR |= BV(TXEN); UDR = (c); } while(0)
69 #define KDBG_MASK_IRQ(old) do { (old) = UCR & BV(TXCIE); cbi(UCR, TXCIE); } while(0)
70 #define KDBG_RESTORE_IRQ(old) do { UCR |= (old); } while(0)
71 #elif CONFIG_KDEBUG_PORT == 1
72 #define KDBG_WAIT_READY() do { loop_until_bit_is_set(UCSR1A, UDRE); } while(0)
73 #define KDBG_WRITE_CHAR(c) do { UCSR1B |= BV(TXEN); UDR1 = (c); } while(0)
74 #define KDBG_MASK_IRQ(old) do { (old) = UCSR1B & BV(TXCIE); cbi(UCSR1B, TXCIE); } while(0)
75 #define KDBG_RESTORE_IRQ(old) do { UCSR1B |= (old); } while(0)
77 #error CONFIG_KDEBUG_PORT should be either 0 or 1
79 #elif defined(__MWERKS__) && (defined(__m56800E__) || defined(__m56800__))
80 /* Debugging go through the JTAG interface. The MSL library already
81 implements the console I/O correctly. */
83 #define KDBG_WAIT_READY()
84 #define KDBG_WRITE_CHAR(c) do { char ch=c; fwrite(&ch,1,1,stdout); } while (0)
85 #define KDBG_MASK_IRQ(old)
86 #define KDBG_RESTORE_IRQ(old)
88 #error Unknown architecture
96 /* Set serial port for 19200bps 8N1 */
97 INT_MASK1 &= ~(INT1F_TI | INT1F_RI);
98 SP_CON = SPCF_RECEIVE_ENABLE | SPCF_MODE1;
99 ioc1_img |= IOC1F_TXD_SEL | IOC1F_EXTINT_SRC;
104 #elif defined(__AVR__)
106 /* Compute the baud rate */
107 uint16_t period = (((CLOCK_FREQ / 16UL) + (CONFIG_KDEBUG_BAUDRATE / 2)) / CONFIG_KDEBUG_BAUDRATE) - 1;
109 #ifdef __AVR_ATmega64__
110 #if CONFIG_KDEBUG_PORT == 0
112 /* Set the baud rate */
113 UBRR0H = (uint8_t)(period>>8);
114 UBRR0L = (uint8_t)period;
116 #elif CONFIG_KDEBUG_PORT == 1
118 UBRR1H = (uint8_t)(period>>8);
119 UBRR1L = (uint8_t)period;
122 #error CONFIG_KDEBUG_PORT should be either 0 or 1
124 #elif defined (__AVR_ATmega103__)
126 /* Set the baud rate */
127 UBRR = (uint8_t)period;
133 #endif /* !__I196__ && !__AVR__ */
135 kputs("\n\n*** DBG START ***\n");
140 * Output one character to the debug console
142 static void kputchar(char c, UNUSED(void *, unused))
144 /* Poll while serial buffer is still busy */
147 /* Send '\n' as '\r\n' for dumb terminals */
150 KDBG_WRITE_CHAR('\r');
158 void PGM_FUNC(kprintf)(const char * PGM_ATTR fmt, ...)
162 /* Mask serial TX intr */
163 unsigned char irqsave;
164 KDBG_MASK_IRQ(irqsave);
167 PGM_FUNC(_formatted_write)(fmt, kputchar, 0, ap);
170 /* Restore serial TX intr */
171 KDBG_RESTORE_IRQ(irqsave);
175 void PGM_FUNC(kputs)(const char * PGM_ATTR str)
179 /* Mask serial TX intr */
180 unsigned char irqsave;
181 KDBG_MASK_IRQ(irqsave);
183 while ((c = PGM_READ_CHAR(str++)))
186 KDBG_RESTORE_IRQ(irqsave);
190 int PGM_FUNC(__assert)(const char * PGM_ATTR cond, const char *file, int line)
192 PGM_FUNC(kputs)(file);
193 PGM_FUNC(kprintf)(PSTR(":%d: Assertion failed: \""), line);
194 PGM_FUNC(kputs)(cond);
195 PGM_FUNC(kputs)(PSTR("\"\n"));
200 int PGM_FUNC(__invalid_ptr)(void *value, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
202 PGM_FUNC(kputs)(file);
203 PGM_FUNC(kprintf)(PSTR(":%d: Invalid pointer: "), line);
204 PGM_FUNC(kputs)(name);
205 PGM_FUNC(kprintf)(PSTR(" = 0x%x\n"), value);
210 void __init_wall(long *wall, int size)
213 *wall++ = WALL_VALUE;
217 int __check_wall(long *wall, int size, const char *name, const char *file, int line)
221 for (i = 0; i < size; i++)
223 if (wall[i] != WALL_VALUE)
225 kprintf("%s:%d: Wall broken: %s[%d] (0x%p) = 0x%lx\n",
226 file, line, name, i, wall + i, wall[i]);
236 * Dump binary data in hex
238 void kdump(const void *_buf, size_t len)
240 const unsigned char *buf = (const unsigned char *)_buf;
243 kprintf("%02X", *buf++);