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.9 2004/08/02 20:20:29 aleph
20 * Merge from project_ks
22 * Revision 1.8 2004/07/30 14:26:33 rasky
23 * Semplificato l'output dell'ASSERT
24 * Aggiunta ASSERT2 con stringa di help opzionalmente disattivabile
26 * Revision 1.7 2004/07/30 14:15:53 rasky
27 * Nuovo supporto unificato per detect della CPU
29 * Revision 1.6 2004/07/18 21:49:28 bernie
30 * Add ATmega8 support.
32 * Revision 1.5 2004/06/27 15:20:26 aleph
33 * Change UNUSED() macro to accept two arguments: type and name;
34 * Add macro GNUC_PREREQ to detect GCC version during build;
35 * Some spacing cleanups and typo fix
37 * Revision 1.4 2004/06/06 18:09:51 bernie
38 * Import DSP56800 changes; Print broken wall bricks in hex.
40 * Revision 1.3 2004/06/03 11:27:09 bernie
41 * Add dual-license information.
43 * Revision 1.2 2004/05/23 18:21:53 bernie
44 * Trim CVS logs and cleanup header info.
52 #include <mware/formatwr.h> /* for _formatted_write() */
58 #define KDBG_WAIT_READY() do {/*nop*/} while(0)
59 #define KDBG_WRITE_CHAR(c) putchar((c))
60 #define KDBG_MASK_IRQ(old) do {/*nop*/} while(0)
61 #define KDBG_RESTORE_IRQ() do {/*nop*/} while(0)
64 #define KDBG_WAIT_READY() do {} while (!(SP_STAT & (SPSF_TX_EMPTY | SPSF_TX_INT)))
65 #define KDBG_WRITE_CHAR(c) do { SBUF = (c); } while(0)
66 #define KDBG_MASK_IRQ(old) \
68 (old) = INT_MASK1 & INT1F_TI; \
69 INT_MASK1 &= ~INT1F_TI; \
71 #define KDBG_RESTORE_IRQ(old) do { INT_MASK1 |= (old); }
74 #if CONFIG_KDEBUG_PORT == 0
76 /* External 485 transceiver on UART0 (to be overridden in "hw.h"). */
77 #if !defined(SER_UART0_485_INIT)
78 #if defined(SER_UART0_485_RX) || defined(SER_UART0_485_TX)
79 #error SER_UART0_485_INIT, SER_UART0_485_RX and SER_UART0_485_TX must be defined together
81 #define SER_UART0_485_INIT do {} while (0)
82 #define SER_UART0_485_TX do {} while (0)
83 #define SER_UART0_485_RX do {} while (0)
84 #elif !defined(SER_UART0_485_RX) || !defined(SER_UART0_485_TX)
85 #error SER_UART0_485_INIT, SER_UART0_485_RX and SER_UART0_485_TX must be defined together
88 #if defined(__AVR_ATmega64__)
92 #elif defined(__AVR_ATmega8__)
97 #define KDBG_WAIT_READY() do { loop_until_bit_is_set(USR, UDRE); } while(0)
98 #define KDBG_WAIT_TXDONE() do { loop_until_bit_is_set(USR, TXC); } while(0)
100 * BUG: before sending a new character the TXC flag is cleared to allow
101 * KDBG_WAIT_TXDONE() to work properly, but, if KDBG_WRITE_CHAR() is called
102 * after the RXC flag is set by hardware, a new TXC could be generated
103 * after we clear it and before the new character is put in UDR. In this
104 * case if a 485 is used the transceiver will be put in RX mode while
105 * transmitting the last char.
107 #define KDBG_WRITE_CHAR(c) do { USR |= BV(TXC); UDR = (c); } while(0)
109 #define KDBG_MASK_IRQ(old) do { \
113 UCR &= ~(BV(TXCIE) | BV(UDRIE)); \
116 #define KDBG_RESTORE_IRQ(old) do { \
117 KDBG_WAIT_TXDONE(); \
122 #elif CONFIG_KDEBUG_PORT == 1
124 /* External 485 transceiver on UART1 (to be overridden in "hw.h"). */
125 #ifndef SER_UART1_485_INIT
126 #if defined(SER_UART1_485_RX) || defined(SER_UART1_485_TX)
127 #error SER_UART1_485_INIT, SER_UART1_485_RX and SER_UART1_485_TX must be defined together
129 #define SER_UART1_485_INIT do {} while (0)
130 #define SER_UART1_485_TX do {} while (0)
131 #define SER_UART1_485_RX do {} while (0)
132 #elif !defined(SER_UART1_485_RX) || !defined(SER_UART1_485_TX)
133 #error SER_UART1_485_INIT, SER_UART1_485_RX and SER_UART1_485_TX must be defined together
136 #define KDBG_WAIT_READY() do { loop_until_bit_is_set(UCSR1A, UDRE); } while(0)
137 #define KDBG_WAIT_TXDONE() do { loop_until_bit_is_set(UCSR1A, TXC); } while(0)
138 #define KDBG_WRITE_CHAR(c) do { UCSR1A |= BV(TXC); UDR1 = (c); } while(0)
140 #define KDBG_MASK_IRQ(old) do { \
143 UCSR1B |= BV(TXEN); \
144 UCSR1B &= ~(BV(TXCIE) | BV(UDRIE)); \
147 #define KDBG_RESTORE_IRQ(old) do { \
148 KDBG_WAIT_TXDONE(); \
154 #error CONFIG_KDEBUG_PORT should be either 0 or 1
156 #elif defined(__MWERKS__) && CPU_DSP56K
157 /* Debugging go through the JTAG interface. The MSL library already
158 implements the console I/O correctly. */
160 #define KDBG_WAIT_READY()
161 #define KDBG_WRITE_CHAR(c) do { char ch=c; fwrite(&ch,1,1,stdout); } while (0)
162 #define KDBG_MASK_IRQ(old)
163 #define KDBG_RESTORE_IRQ(old)
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;
186 #if defined(__AVR_ATmega64__)
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 defined(__AVR_ATmega8__)
199 UBRRH = (uint8_t)(period>>8);
200 UBRRL = (uint8_t)period;
201 #elif defined(__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(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 PGM_FUNC(kprintf)(const char * PGM_ATTR fmt, ...)
237 /* Mask serial TX intr */
238 unsigned char irqsave;
239 KDBG_MASK_IRQ(irqsave);
242 PGM_FUNC(_formatted_write)(fmt, kputchar, 0, ap);
245 /* Restore serial TX intr */
246 KDBG_RESTORE_IRQ(irqsave);
250 void PGM_FUNC(kputs)(const char * PGM_ATTR str)
254 /* Mask serial TX intr */
255 unsigned char irqsave;
256 KDBG_MASK_IRQ(irqsave);
258 while ((c = PGM_READ_CHAR(str++)))
261 KDBG_RESTORE_IRQ(irqsave);
265 int PGM_FUNC(__assert)(const char * PGM_ATTR cond, const char *file, int line)
267 PGM_FUNC(kputs)(file);
268 PGM_FUNC(kprintf)(PSTR(":%d: Assertion failed: "), line);
269 PGM_FUNC(kputs)(cond);
270 PGM_FUNC(kputs)(PSTR("\n"));
275 int PGM_FUNC(__invalid_ptr)(void *value, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
277 PGM_FUNC(kputs)(file);
278 PGM_FUNC(kprintf)(PSTR(":%d: Invalid pointer: "), line);
279 PGM_FUNC(kputs)(name);
280 PGM_FUNC(kprintf)(PSTR(" = 0x%x\n"), value);
285 void __init_wall(long *wall, int size)
288 *wall++ = WALL_VALUE;
292 int __check_wall(long *wall, int size, const char *name, const char *file, int line)
296 for (i = 0; i < size; i++)
298 if (wall[i] != WALL_VALUE)
300 kprintf("%s:%d: Wall broken: %s[%d] (0x%p) = 0x%lx\n",
301 file, line, name, i, wall + i, wall[i]);
311 * Dump binary data in hex
313 void kdump(const void *_buf, size_t len)
315 const unsigned char *buf = (const unsigned char *)_buf;
318 kprintf("%02X", *buf++);