4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 2000, 2001, 2002 Bernardo Innocenti <bernie@codewiz.org>
34 * \brief General pourpose debug support for embedded systems (implementation).
37 * \author Bernardo Innocenti <bernie@develer.com>
38 * \author Stefano Fedrigo <aleph@develer.com>
43 *#* Revision 1.31 2007/06/07 14:25:30 batt
44 *#* Merge from project_ks
46 *#* Revision 1.30 2007/01/28 09:18:06 batt
47 *#* Merge from project_ks.
49 *#* Revision 1.29 2006/11/23 13:19:39 batt
50 *#* Add BitBanged serial debug console.
52 *#* Revision 1.28 2006/07/19 12:56:25 bernie
53 *#* Convert to new Doxygen style.
55 *#* Revision 1.27 2006/06/01 12:32:06 marco
56 *#* Updated include reference.
58 *#* Revision 1.26 2006/04/27 05:40:27 bernie
59 *#* Break on assertion failures.
61 *#* Revision 1.25 2005/06/27 21:26:24 bernie
64 *#* Revision 1.24 2005/04/12 01:36:37 bernie
65 *#* Add hack to enable TX line at module initialization.
67 *#* Revision 1.23 2005/04/11 19:10:27 bernie
68 *#* Include top-level headers from cfg/ subdir.
70 *#* Revision 1.22 2005/02/18 11:18:33 bernie
71 *#* Fixes for Harvard processors from project_ks.
73 *#* Revision 1.21 2005/02/16 20:29:48 bernie
74 *#* TRACE(), TRACEMSG(): Reduce code and data footprint.
76 *#* Revision 1.20 2005/01/25 08:36:40 bernie
77 *#* kputnum(): Export.
79 *#* Revision 1.19 2004/12/31 17:47:45 bernie
80 *#* Rename UNUSED() to UNUSED_ARG().
83 #include <cfg/debug.h>
85 #include <cfg/macros.h> /* for BV() */
86 #include <appconfig.h>
87 #include <hw_cpu.h> /* for CLOCK_FREQ */
88 #include <hw_ser.h> /* Required for bus macros overrides */
90 #include <mware/formatwr.h> /* for _formatted_write() */
94 #if CPU_HARVARD && !defined(_PROGMEM)
95 #error This module build correctly only in program memory!
100 #define KDBG_WAIT_READY() do { /*nop*/ } while(0)
101 #define KDBG_WRITE_CHAR(c) putchar((c))
102 #define KDBG_MASK_IRQ(old) do { (void)(old); } while(0)
103 #define KDBG_RESTORE_IRQ(old) do { /*nop*/ } while(0)
104 typedef char kdbg_irqsave_t; /* unused */
106 #if CONFIG_KDEBUG_PORT == 666
107 #error BITBANG debug console missing for this platform
111 #define KDBG_WAIT_READY() do {} while (!(SP_STAT & (SPSF_TX_EMPTY | SPSF_TX_INT)))
112 #define KDBG_WRITE_CHAR(c) do { SBUF = (c); } while(0)
113 #define KDBG_MASK_IRQ(old) \
115 (old) = INT_MASK1 & INT1F_TI; \
116 INT_MASK1 &= ~INT1F_TI; \
118 #define KDBG_RESTORE_IRQ(old) do { INT_MASK1 |= (old); }
119 typedef uint16_t kdbg_irqsave_t; /* FIXME: unconfirmed */
121 #if CONFIG_KDEBUG_PORT == 666
122 #error BITBANG debug console missing for this platform
127 #if CONFIG_KDEBUG_PORT == 0
130 * Support for special bus policies or external transceivers
131 * on UART0 (to be overridden in "hw_ser.h").
133 * HACK: if we don't set TXEN, kdbg disables the transmitter
134 * after each output statement until the serial driver
135 * is initialized. These glitches confuse the debug
136 * terminal that ends up printing some trash.
138 #ifndef KDBG_UART0_BUS_INIT
139 #define KDBG_UART0_BUS_INIT do { \
140 UCSR0B = BV(TXEN0); \
143 #ifndef KDBG_UART0_BUS_RX
144 #define KDBG_UART0_BUS_RX do {} while (0)
146 #ifndef KDBG_UART0_BUS_TX
147 #define KDBG_UART0_BUS_TX do {} while (0)
150 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA168
154 #elif CPU_AVR_ATMEGA8
161 #define KDBG_WAIT_READY() do { loop_until_bit_is_set(USR, UDRE0); } while(0)
162 #define KDBG_WAIT_TXDONE() do { loop_until_bit_is_set(USR, TXC0); } while(0)
165 * We must clear the TXC flag before sending a new character to allow
166 * KDBG_WAIT_TXDONE() to work properly.
168 * BUG: if KDBG_WRITE_CHAR() is called after the TXC flag is set by hardware,
169 * a new TXC could be generated after we've cleared it and before the new
170 * character is written to UDR. On a 485 bus, the transceiver will be put
171 * in RX mode while still transmitting the last char.
173 #define KDBG_WRITE_CHAR(c) do { USR |= BV(TXC0); UDR = (c); } while(0)
175 #define KDBG_MASK_IRQ(old) do { \
178 UCR &= ~(BV(TXCIE0) | BV(UDRIE0)); \
182 #define KDBG_RESTORE_IRQ(old) do { \
183 KDBG_WAIT_TXDONE(); \
188 typedef uint8_t kdbg_irqsave_t;
190 #elif CONFIG_KDEBUG_PORT == 1
193 * Support for special bus policies or external transceivers
194 * on UART1 (to be overridden in "hw_ser.h").
196 * HACK: if we don't set TXEN, kdbg disables the transmitter
197 * after each output statement until the serial driver
198 * is initialized. These glitches confuse the debug
199 * terminal that ends up printing some trash.
201 #ifndef KDBG_UART1_BUS_INIT
202 #define KDBG_UART1_BUS_INIT do { \
203 UCSR1B = BV(TXEN1); \
206 #ifndef KDBG_UART1_BUS_RX
207 #define KDBG_UART1_BUS_RX do {} while (0)
209 #ifndef KDBG_UART1_BUS_TX
210 #define KDBG_UART1_BUS_TX do {} while (0)
213 #define KDBG_WAIT_READY() do { loop_until_bit_is_set(UCSR1A, UDRE1); } while(0)
214 #define KDBG_WAIT_TXDONE() do { loop_until_bit_is_set(UCSR1A, TXC1); } while(0)
215 #define KDBG_WRITE_CHAR(c) do { UCSR1A |= BV(TXC1); UDR1 = (c); } while(0)
217 #define KDBG_MASK_IRQ(old) do { \
219 UCSR1B |= BV(TXEN1); \
220 UCSR1B &= ~(BV(TXCIE1) | BV(UDRIE1)); \
224 #define KDBG_RESTORE_IRQ(old) do { \
225 KDBG_WAIT_TXDONE(); \
230 typedef uint8_t kdbg_irqsave_t;
233 * Special debug port for BitBanged Serial see below for details...
235 #elif CONFIG_KDEBUG_PORT == 666
237 #define KDBG_WAIT_READY() do { /*nop*/ } while(0)
238 #define KDBG_WRITE_CHAR(c) _kdebug_bitbang_putchar((c))
239 #define KDBG_MASK_IRQ(old) do { IRQ_SAVE_DISABLE((old)); } while(0)
240 #define KDBG_RESTORE_IRQ(old) do { IRQ_RESTORE((old)); } while(0)
241 typedef cpuflags_t kdbg_irqsave_t;
243 #define KDBG_DELAY (((CLOCK_FREQ + CONFIG_KDEBUG_BAUDRATE / 2) / CONFIG_KDEBUG_BAUDRATE) + 7) / 14
245 static void _kdebug_bitbang_delay(void)
249 for (i = 0; i < KDBG_DELAY; i++)
259 #error CONFIG_KDEBUG_PORT should be either 0, 1 or 666
261 #elif defined(__MWERKS__) && CPU_DSP56K
262 /* Debugging go through the JTAG interface. The MSL library already
263 implements the console I/O correctly. */
265 #define KDBG_WAIT_READY() do { } while (0)
266 #define KDBG_WRITE_CHAR(c) __put_char(c, stdout)
267 #define KDBG_MASK_IRQ(old) do { (void)(old); } while (0)
268 #define KDBG_RESTORE_IRQ(old) do { (void)(old); } while (0)
269 typedef uint8_t kdbg_irqsave_t; /* unused */
270 #if CONFIG_KDEBUG_PORT == 666
271 #error BITBANG debug console missing for this platform
274 #error Unknown architecture
277 #if CONFIG_KDEBUG_PORT == 666
279 * Putchar for BITBANG serial debug console.
280 * Sometimes, we can't permit to use a whole serial for debugging purpose.
281 * Since debug console is in output only it is usefull to use a single generic I/O pin for debug.
282 * This is achieved by this simple function, that shift out the data like a UART, but
284 * The only requirement is that SER_BITBANG_* macros will be defined somewhere (usually hw_ser.h)
285 * \note All interrupts are disabled during debug prints!
287 static void _kdebug_bitbang_putchar(char c)
300 for (i = 0; i < 10; i++)
306 _kdebug_bitbang_delay();
319 /* Set serial port for 19200bps 8N1 */
320 INT_MASK1 &= ~(INT1F_TI | INT1F_RI);
321 SP_CON = SPCF_RECEIVE_ENABLE | SPCF_MODE1;
322 ioc1_img |= IOC1F_TXD_SEL | IOC1F_EXTINT_SRC;
328 #if CONFIG_KDEBUG_PORT == 666
330 #else /* CONFIG_KDEBUG_PORT != 666 */
331 /* Compute the baud rate */
332 uint16_t period = (((CLOCK_FREQ / 16UL) + (CONFIG_KDEBUG_BAUDRATE / 2)) / CONFIG_KDEBUG_BAUDRATE) - 1;
334 #if (CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128)
335 #if CONFIG_KDEBUG_PORT == 0
336 UBRR0H = (uint8_t)(period>>8);
337 UBRR0L = (uint8_t)period;
339 #elif CONFIG_KDEBUG_PORT == 1
340 UBRR1H = (uint8_t)(period>>8);
341 UBRR1L = (uint8_t)period;
344 #error CONFIG_KDEBUG_PORT must be either 0 or 1
347 #elif CPU_AVR_ATMEGA168
348 UBRR0H = (uint8_t)(period>>8);
349 UBRR0L = (uint8_t)period;
351 #elif CPU_AVR_ATMEGA8
352 UBRRH = (uint8_t)(period>>8);
353 UBRRL = (uint8_t)period;
354 #elif CPU_AVR_ATMEGA103
355 UBRR = (uint8_t)period;
360 #endif /* CONFIG_KDEBUG_PORT == 666 */
362 #endif /* !CPU_I196 && !CPU_AVR */
364 kputs("\n\n*** DBG START ***\n");
369 * Output one character to the debug console
371 static void __kputchar(char c, UNUSED_ARG(void *, unused))
373 /* Poll while serial buffer is still busy */
376 /* Send '\n' as '\r\n' for dumb terminals */
379 KDBG_WRITE_CHAR('\r');
387 void kputchar(char c)
389 /* Mask serial TX intr */
390 kdbg_irqsave_t irqsave;
391 KDBG_MASK_IRQ(irqsave);
395 /* Restore serial TX intr */
396 KDBG_RESTORE_IRQ(irqsave);
400 static void PGM_FUNC(kvprintf)(const char * PGM_ATTR fmt, va_list ap)
403 /* Mask serial TX intr */
404 kdbg_irqsave_t irqsave;
405 KDBG_MASK_IRQ(irqsave);
407 PGM_FUNC(_formatted_write)(fmt, __kputchar, 0, ap);
409 /* Restore serial TX intr */
410 KDBG_RESTORE_IRQ(irqsave);
412 /* A better than nothing printf() surrogate. */
413 PGM_FUNC(kputs)(fmt);
414 #endif /* CONFIG_PRINTF */
417 void PGM_FUNC(kprintf)(const char * PGM_ATTR fmt, ...)
422 PGM_FUNC(kvprintf)(fmt, ap);
426 void PGM_FUNC(kputs)(const char * PGM_ATTR str)
430 /* Mask serial TX intr */
431 kdbg_irqsave_t irqsave;
432 KDBG_MASK_IRQ(irqsave);
434 while ((c = PGM_READ_CHAR(str++)))
437 KDBG_RESTORE_IRQ(irqsave);
442 * Cheap function to print small integers without using printf().
452 digit = num / divisor;
455 if (digit || output_len || divisor == 1)
457 kputchar(digit + '0');
461 while (divisor /= 10);
467 static void klocation(const char * PGM_ATTR file, int line)
469 PGM_FUNC(kputs)(file);
472 PGM_FUNC(kputs)(PGM_STR(": "));
475 int PGM_FUNC(__assert)(const char * PGM_ATTR cond, const char * PGM_ATTR file, int line)
477 klocation(file, line);
478 PGM_FUNC(kputs)(PGM_STR("Assertion failed: "));
479 PGM_FUNC(kputs)(cond);
486 * Unfortunately, there's no way to get __func__ in
487 * program memory, so we waste quite a lot of RAM in
488 * AVR and other Harvard processors.
490 void PGM_FUNC(__trace)(const char *name)
492 PGM_FUNC(kprintf)(PGM_STR("%s()\n"), name);
495 void PGM_FUNC(__tracemsg)(const char *name, const char * PGM_ATTR fmt, ...)
499 PGM_FUNC(kprintf)(PGM_STR("%s(): "), name);
501 PGM_FUNC(kvprintf)(fmt, ap);
506 int PGM_FUNC(__invalid_ptr)(void *value, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
508 klocation(file, line);
509 PGM_FUNC(kputs)(PGM_STR("Invalid ptr: "));
510 PGM_FUNC(kputs)(name);
512 PGM_FUNC(kprintf)(PGM_STR(" = 0x%x\n"), (unsigned int)value);
521 void __init_wall(long *wall, int size)
524 *wall++ = WALL_VALUE;
528 int PGM_FUNC(__check_wall)(long *wall, int size, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
532 for (i = 0; i < size; i++)
534 if (wall[i] != WALL_VALUE)
536 klocation(file, line);
537 PGM_FUNC(kputs)(PGM_STR("Wall broken: "));
538 PGM_FUNC(kputs)(name);
540 PGM_FUNC(kprintf)(PGM_STR("[%d] (0x%p) = 0x%lx\n"), i, wall + i, wall[i]);
555 * Dump binary data in hex
557 void kdump(const void *_buf, size_t len)
559 const unsigned char *buf = (const unsigned char *)_buf;
562 kprintf("%02X", *buf++);
566 #endif /* CONFIG_PRINTF */