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.11 2004/08/24 16:19:08 bernie
20 * kputchar(): New public function; Add missing dummy inlines for \!_DEBUG.
22 * Revision 1.10 2004/08/04 15:57:50 rasky
23 * Cambiata la putchar per kdebug per DSP56k: la nuova funzione e' quella piu' a basso livello (assembly)
25 * Revision 1.9 2004/08/02 20:20:29 aleph
26 * Merge from project_ks
28 * Revision 1.8 2004/07/30 14:26:33 rasky
29 * Semplificato l'output dell'ASSERT
30 * Aggiunta ASSERT2 con stringa di help opzionalmente disattivabile
32 * Revision 1.7 2004/07/30 14:15:53 rasky
33 * Nuovo supporto unificato per detect della CPU
35 * Revision 1.6 2004/07/18 21:49:28 bernie
36 * Add ATmega8 support.
38 * Revision 1.5 2004/06/27 15:20:26 aleph
39 * Change UNUSED() macro to accept two arguments: type and name;
40 * Add macro GNUC_PREREQ to detect GCC version during build;
41 * Some spacing cleanups and typo fix
43 * Revision 1.4 2004/06/06 18:09:51 bernie
44 * Import DSP56800 changes; Print broken wall bricks in hex.
46 * Revision 1.3 2004/06/03 11:27:09 bernie
47 * Add dual-license information.
49 * Revision 1.2 2004/05/23 18:21:53 bernie
50 * Trim CVS logs and cleanup header info.
58 #include <mware/formatwr.h> /* for _formatted_write() */
64 #define KDBG_WAIT_READY() do { /*nop*/ } while(0)
65 #define KDBG_WRITE_CHAR(c) putchar((c))
66 #define KDBG_MASK_IRQ(old) do { (void)(old); } while(0)
67 #define KDBG_RESTORE_IRQ() do { /*nop*/ } while(0)
68 typedef kdbg_irqsave_t char; /* unused */
71 #define KDBG_WAIT_READY() do {} while (!(SP_STAT & (SPSF_TX_EMPTY | SPSF_TX_INT)))
72 #define KDBG_WRITE_CHAR(c) do { SBUF = (c); } while(0)
73 #define KDBG_MASK_IRQ(old) \
75 (old) = INT_MASK1 & INT1F_TI; \
76 INT_MASK1 &= ~INT1F_TI; \
78 #define KDBG_RESTORE_IRQ(old) do { INT_MASK1 |= (old); }
79 typedef kdbg_irqsave_t uint16_t; /* FIXME: unconfirmed */
82 #if CONFIG_KDEBUG_PORT == 0
84 /* External 485 transceiver on UART0 (to be overridden in "hw.h"). */
85 #if !defined(SER_UART0_485_INIT)
86 #if defined(SER_UART0_485_RX) || defined(SER_UART0_485_TX)
87 #error SER_UART0_485_INIT, SER_UART0_485_RX and SER_UART0_485_TX must be defined together
89 #define SER_UART0_485_INIT do {} while (0)
90 #define SER_UART0_485_TX do {} while (0)
91 #define SER_UART0_485_RX do {} while (0)
92 #elif !defined(SER_UART0_485_RX) || !defined(SER_UART0_485_TX)
93 #error SER_UART0_485_INIT, SER_UART0_485_RX and SER_UART0_485_TX must be defined together
96 #if defined(__AVR_ATmega64__)
100 #elif defined(__AVR_ATmega8__)
105 #define KDBG_WAIT_READY() do { loop_until_bit_is_set(USR, UDRE); } while(0)
106 #define KDBG_WAIT_TXDONE() do { loop_until_bit_is_set(USR, TXC); } while(0)
108 * BUG: before sending a new character the TXC flag is cleared to allow
109 * KDBG_WAIT_TXDONE() to work properly, but, if KDBG_WRITE_CHAR() is called
110 * after the RXC flag is set by hardware, a new TXC could be generated
111 * after we clear it and before the new character is put in UDR. In this
112 * case if a 485 is used the transceiver will be put in RX mode while
113 * transmitting the last char.
115 #define KDBG_WRITE_CHAR(c) do { USR |= BV(TXC); UDR = (c); } while(0)
117 #define KDBG_MASK_IRQ(old) do { \
121 UCR &= ~(BV(TXCIE) | BV(UDRIE)); \
124 #define KDBG_RESTORE_IRQ(old) do { \
125 KDBG_WAIT_TXDONE(); \
130 typedef kdbg_irqsave_t uint8_t;
132 #elif CONFIG_KDEBUG_PORT == 1
134 /* External 485 transceiver on UART1 (to be overridden in "hw.h"). */
135 #ifndef SER_UART1_485_INIT
136 #if defined(SER_UART1_485_RX) || defined(SER_UART1_485_TX)
137 #error SER_UART1_485_INIT, SER_UART1_485_RX and SER_UART1_485_TX must be defined together
139 #define SER_UART1_485_INIT do {} while (0)
140 #define SER_UART1_485_TX do {} while (0)
141 #define SER_UART1_485_RX do {} while (0)
142 #elif !defined(SER_UART1_485_RX) || !defined(SER_UART1_485_TX)
143 #error SER_UART1_485_INIT, SER_UART1_485_RX and SER_UART1_485_TX must be defined together
146 #define KDBG_WAIT_READY() do { loop_until_bit_is_set(UCSR1A, UDRE); } while(0)
147 #define KDBG_WAIT_TXDONE() do { loop_until_bit_is_set(UCSR1A, TXC); } while(0)
148 #define KDBG_WRITE_CHAR(c) do { UCSR1A |= BV(TXC); UDR1 = (c); } while(0)
150 #define KDBG_MASK_IRQ(old) do { \
153 UCSR1B |= BV(TXEN); \
154 UCSR1B &= ~(BV(TXCIE) | BV(UDRIE)); \
157 #define KDBG_RESTORE_IRQ(old) do { \
158 KDBG_WAIT_TXDONE(); \
163 typedef kdbg_irqsave_t uint8_t;
165 #error CONFIG_KDEBUG_PORT should be either 0 or 1
167 #elif defined(__MWERKS__) && CPU_DSP56K
168 /* Debugging go through the JTAG interface. The MSL library already
169 implements the console I/O correctly. */
171 #define KDBG_WAIT_READY() do { } while (0)
172 #define KDBG_WRITE_CHAR(c) __put_char(c, stdout)
173 #define KDBG_MASK_IRQ(old) do { (void)(old); } while (0)
174 #define KDBG_RESTORE_IRQ(old) do { (void)(old); } while (0)
175 typedef kdbg_irqsave_t uint8_t; /* unused */
177 #error Unknown architecture
185 /* Set serial port for 19200bps 8N1 */
186 INT_MASK1 &= ~(INT1F_TI | INT1F_RI);
187 SP_CON = SPCF_RECEIVE_ENABLE | SPCF_MODE1;
188 ioc1_img |= IOC1F_TXD_SEL | IOC1F_EXTINT_SRC;
195 /* Compute the baud rate */
196 uint16_t period = (((CLOCK_FREQ / 16UL) + (CONFIG_KDEBUG_BAUDRATE / 2)) / CONFIG_KDEBUG_BAUDRATE) - 1;
198 #if defined(__AVR_ATmega64__)
199 #if CONFIG_KDEBUG_PORT == 0
200 UBRR0H = (uint8_t)(period>>8);
201 UBRR0L = (uint8_t)period;
203 #elif CONFIG_KDEBUG_PORT == 1
204 UBRR1H = (uint8_t)(period>>8);
205 UBRR1L = (uint8_t)period;
208 #error CONFIG_KDEBUG_PORT must be either 0 or 1
210 #elif defined(__AVR_ATmega8__)
211 UBRRH = (uint8_t)(period>>8);
212 UBRRL = (uint8_t)period;
213 #elif defined(__AVR_ATmega103__)
214 UBRR = (uint8_t)period;
220 #endif /* !CPU_I196 && !CPU_AVR */
222 kputs("\n\n*** DBG START ***\n");
227 * Output one character to the debug console
229 static void __kputchar(char c, UNUSED(void *, unused))
231 /* Poll while serial buffer is still busy */
234 /* Send '\n' as '\r\n' for dumb terminals */
237 KDBG_WRITE_CHAR('\r');
245 void kputchar(char c)
247 /* Mask serial TX intr */
248 kdbg_irqsave_t irqsave;
249 KDBG_MASK_IRQ(irqsave);
253 /* Restore serial TX intr */
254 KDBG_RESTORE_IRQ(irqsave);
258 void PGM_FUNC(kprintf)(const char * PGM_ATTR fmt, ...)
262 /* Mask serial TX intr */
263 kdbg_irqsave_t irqsave;
264 KDBG_MASK_IRQ(irqsave);
267 PGM_FUNC(_formatted_write)(fmt, __kputchar, 0, ap);
270 /* Restore serial TX intr */
271 KDBG_RESTORE_IRQ(irqsave);
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);
290 int PGM_FUNC(__assert)(const char * PGM_ATTR cond, const char *file, int line)
292 PGM_FUNC(kputs)(file);
293 PGM_FUNC(kprintf)(PSTR(":%d: Assertion failed: "), line);
294 PGM_FUNC(kputs)(cond);
295 PGM_FUNC(kputs)(PSTR("\n"));
300 int PGM_FUNC(__invalid_ptr)(void *value, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
302 PGM_FUNC(kputs)(file);
303 PGM_FUNC(kprintf)(PSTR(":%d: Invalid pointer: "), line);
304 PGM_FUNC(kputs)(name);
305 PGM_FUNC(kprintf)(PSTR(" = 0x%x\n"), value);
310 void __init_wall(long *wall, int size)
313 *wall++ = WALL_VALUE;
317 int __check_wall(long *wall, int size, const char *name, const char *file, int line)
321 for (i = 0; i < size; i++)
323 if (wall[i] != WALL_VALUE)
325 kprintf("%s:%d: Wall broken: %s[%d] (0x%p) = 0x%lx\n",
326 file, line, name, i, wall + i, wall[i]);
336 * Dump binary data in hex
338 void kdump(const void *_buf, size_t len)
340 const unsigned char *buf = (const unsigned char *)_buf;
343 kprintf("%02X", *buf++);