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.10 2004/08/04 15:57:50 rasky
20 * Cambiata la putchar per kdebug per DSP56k: la nuova funzione e' quella piu' a basso livello (assembly)
22 * Revision 1.9 2004/08/02 20:20:29 aleph
23 * Merge from project_ks
25 * Revision 1.8 2004/07/30 14:26:33 rasky
26 * Semplificato l'output dell'ASSERT
27 * Aggiunta ASSERT2 con stringa di help opzionalmente disattivabile
29 * Revision 1.7 2004/07/30 14:15:53 rasky
30 * Nuovo supporto unificato per detect della CPU
32 * Revision 1.6 2004/07/18 21:49:28 bernie
33 * Add ATmega8 support.
35 * Revision 1.5 2004/06/27 15:20:26 aleph
36 * Change UNUSED() macro to accept two arguments: type and name;
37 * Add macro GNUC_PREREQ to detect GCC version during build;
38 * Some spacing cleanups and typo fix
40 * Revision 1.4 2004/06/06 18:09:51 bernie
41 * Import DSP56800 changes; Print broken wall bricks in hex.
43 * Revision 1.3 2004/06/03 11:27:09 bernie
44 * Add dual-license information.
46 * Revision 1.2 2004/05/23 18:21:53 bernie
47 * Trim CVS logs and cleanup header info.
55 #include <mware/formatwr.h> /* for _formatted_write() */
61 #define KDBG_WAIT_READY() do {/*nop*/} while(0)
62 #define KDBG_WRITE_CHAR(c) putchar((c))
63 #define KDBG_MASK_IRQ(old) do {/*nop*/} while(0)
64 #define KDBG_RESTORE_IRQ() do {/*nop*/} while(0)
67 #define KDBG_WAIT_READY() do {} while (!(SP_STAT & (SPSF_TX_EMPTY | SPSF_TX_INT)))
68 #define KDBG_WRITE_CHAR(c) do { SBUF = (c); } while(0)
69 #define KDBG_MASK_IRQ(old) \
71 (old) = INT_MASK1 & INT1F_TI; \
72 INT_MASK1 &= ~INT1F_TI; \
74 #define KDBG_RESTORE_IRQ(old) do { INT_MASK1 |= (old); }
77 #if CONFIG_KDEBUG_PORT == 0
79 /* External 485 transceiver on UART0 (to be overridden in "hw.h"). */
80 #if !defined(SER_UART0_485_INIT)
81 #if defined(SER_UART0_485_RX) || defined(SER_UART0_485_TX)
82 #error SER_UART0_485_INIT, SER_UART0_485_RX and SER_UART0_485_TX must be defined together
84 #define SER_UART0_485_INIT do {} while (0)
85 #define SER_UART0_485_TX do {} while (0)
86 #define SER_UART0_485_RX do {} while (0)
87 #elif !defined(SER_UART0_485_RX) || !defined(SER_UART0_485_TX)
88 #error SER_UART0_485_INIT, SER_UART0_485_RX and SER_UART0_485_TX must be defined together
91 #if defined(__AVR_ATmega64__)
95 #elif defined(__AVR_ATmega8__)
100 #define KDBG_WAIT_READY() do { loop_until_bit_is_set(USR, UDRE); } while(0)
101 #define KDBG_WAIT_TXDONE() do { loop_until_bit_is_set(USR, TXC); } while(0)
103 * BUG: before sending a new character the TXC flag is cleared to allow
104 * KDBG_WAIT_TXDONE() to work properly, but, if KDBG_WRITE_CHAR() is called
105 * after the RXC flag is set by hardware, a new TXC could be generated
106 * after we clear it and before the new character is put in UDR. In this
107 * case if a 485 is used the transceiver will be put in RX mode while
108 * transmitting the last char.
110 #define KDBG_WRITE_CHAR(c) do { USR |= BV(TXC); UDR = (c); } while(0)
112 #define KDBG_MASK_IRQ(old) do { \
116 UCR &= ~(BV(TXCIE) | BV(UDRIE)); \
119 #define KDBG_RESTORE_IRQ(old) do { \
120 KDBG_WAIT_TXDONE(); \
125 #elif CONFIG_KDEBUG_PORT == 1
127 /* External 485 transceiver on UART1 (to be overridden in "hw.h"). */
128 #ifndef SER_UART1_485_INIT
129 #if defined(SER_UART1_485_RX) || defined(SER_UART1_485_TX)
130 #error SER_UART1_485_INIT, SER_UART1_485_RX and SER_UART1_485_TX must be defined together
132 #define SER_UART1_485_INIT do {} while (0)
133 #define SER_UART1_485_TX do {} while (0)
134 #define SER_UART1_485_RX do {} while (0)
135 #elif !defined(SER_UART1_485_RX) || !defined(SER_UART1_485_TX)
136 #error SER_UART1_485_INIT, SER_UART1_485_RX and SER_UART1_485_TX must be defined together
139 #define KDBG_WAIT_READY() do { loop_until_bit_is_set(UCSR1A, UDRE); } while(0)
140 #define KDBG_WAIT_TXDONE() do { loop_until_bit_is_set(UCSR1A, TXC); } while(0)
141 #define KDBG_WRITE_CHAR(c) do { UCSR1A |= BV(TXC); UDR1 = (c); } while(0)
143 #define KDBG_MASK_IRQ(old) do { \
146 UCSR1B |= BV(TXEN); \
147 UCSR1B &= ~(BV(TXCIE) | BV(UDRIE)); \
150 #define KDBG_RESTORE_IRQ(old) do { \
151 KDBG_WAIT_TXDONE(); \
157 #error CONFIG_KDEBUG_PORT should be either 0 or 1
159 #elif defined(__MWERKS__) && CPU_DSP56K
160 /* Debugging go through the JTAG interface. The MSL library already
161 implements the console I/O correctly. */
163 #define KDBG_WAIT_READY()
164 #define KDBG_WRITE_CHAR(c) __put_char(c, stdout)
165 #define KDBG_MASK_IRQ(old)
166 #define KDBG_RESTORE_IRQ(old)
168 #error Unknown architecture
176 /* Set serial port for 19200bps 8N1 */
177 INT_MASK1 &= ~(INT1F_TI | INT1F_RI);
178 SP_CON = SPCF_RECEIVE_ENABLE | SPCF_MODE1;
179 ioc1_img |= IOC1F_TXD_SEL | IOC1F_EXTINT_SRC;
186 /* Compute the baud rate */
187 uint16_t period = (((CLOCK_FREQ / 16UL) + (CONFIG_KDEBUG_BAUDRATE / 2)) / CONFIG_KDEBUG_BAUDRATE) - 1;
189 #if defined(__AVR_ATmega64__)
190 #if CONFIG_KDEBUG_PORT == 0
191 UBRR0H = (uint8_t)(period>>8);
192 UBRR0L = (uint8_t)period;
194 #elif CONFIG_KDEBUG_PORT == 1
195 UBRR1H = (uint8_t)(period>>8);
196 UBRR1L = (uint8_t)period;
199 #error CONFIG_KDEBUG_PORT must be either 0 or 1
201 #elif defined(__AVR_ATmega8__)
202 UBRRH = (uint8_t)(period>>8);
203 UBRRL = (uint8_t)period;
204 #elif defined(__AVR_ATmega103__)
205 UBRR = (uint8_t)period;
211 #endif /* !CPU_I196 && !CPU_AVR */
213 kputs("\n\n*** DBG START ***\n");
218 * Output one character to the debug console
220 static void kputchar(char c, UNUSED(void *, unused))
222 /* Poll while serial buffer is still busy */
225 /* Send '\n' as '\r\n' for dumb terminals */
228 KDBG_WRITE_CHAR('\r');
236 void PGM_FUNC(kprintf)(const char * PGM_ATTR fmt, ...)
240 /* Mask serial TX intr */
241 unsigned char irqsave;
242 KDBG_MASK_IRQ(irqsave);
245 PGM_FUNC(_formatted_write)(fmt, kputchar, 0, ap);
248 /* Restore serial TX intr */
249 KDBG_RESTORE_IRQ(irqsave);
253 void PGM_FUNC(kputs)(const char * PGM_ATTR str)
257 /* Mask serial TX intr */
258 unsigned char irqsave;
259 KDBG_MASK_IRQ(irqsave);
261 while ((c = PGM_READ_CHAR(str++)))
264 KDBG_RESTORE_IRQ(irqsave);
268 int PGM_FUNC(__assert)(const char * PGM_ATTR cond, const char *file, int line)
270 PGM_FUNC(kputs)(file);
271 PGM_FUNC(kprintf)(PSTR(":%d: Assertion failed: "), line);
272 PGM_FUNC(kputs)(cond);
273 PGM_FUNC(kputs)(PSTR("\n"));
278 int PGM_FUNC(__invalid_ptr)(void *value, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
280 PGM_FUNC(kputs)(file);
281 PGM_FUNC(kprintf)(PSTR(":%d: Invalid pointer: "), line);
282 PGM_FUNC(kputs)(name);
283 PGM_FUNC(kprintf)(PSTR(" = 0x%x\n"), value);
288 void __init_wall(long *wall, int size)
291 *wall++ = WALL_VALUE;
295 int __check_wall(long *wall, int size, const char *name, const char *file, int line)
299 for (i = 0; i < size; i++)
301 if (wall[i] != WALL_VALUE)
303 kprintf("%s:%d: Wall broken: %s[%d] (0x%p) = 0x%lx\n",
304 file, line, name, i, wall + i, wall[i]);
314 * Dump binary data in hex
316 void kdump(const void *_buf, size_t len)
318 const unsigned char *buf = (const unsigned char *)_buf;
321 kprintf("%02X", *buf++);