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, 2006, 2007 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 #include <cpu/types.h>
45 #include <cfg/macros.h> /* for BV() */
46 #include <cfg/debug.h>
48 #include <appconfig.h>
49 #warning what about these?
50 //#include <hw_cpu.h> /* for CLOCK_FREQ */
51 //#include <hw_ser.h> /* Required for bus macros overrides */
53 #include <mware/formatwr.h> /* for _formatted_write() */
54 #include <mware/pgm.h>
58 #if CPU_HARVARD && !defined(_PROGMEM)
59 #error This module build correctly only in program memory!
65 #define KDBG_WAIT_READY() do { /*nop*/ } while(0)
66 #define KDBG_WRITE_CHAR(c) putc((c), stderr)
67 #define KDBG_MASK_IRQ(old) do { (void)(old); } while(0)
68 #define KDBG_RESTORE_IRQ(old) do { /*nop*/ } while(0)
69 typedef char kdbg_irqsave_t; /* unused */
71 #define kdbg_hw_init() do {} while (0) ///< Not needed
73 #if CONFIG_KDEBUG_PORT == 666
74 #error BITBANG debug console missing for this platform
77 #include CPU_CSOURCE(kdebug)
85 kputs("\n\n*** BeRTOS DBG START ***\n");
90 * Output one character to the debug console
92 static void __kputchar(char c, UNUSED_ARG(void *, unused))
94 /* Poll while serial buffer is still busy */
97 /* Send '\n' as '\r\n' for dumb terminals */
100 KDBG_WRITE_CHAR('\r');
108 void kputchar(char c)
110 /* Mask serial TX intr */
111 kdbg_irqsave_t irqsave;
112 KDBG_MASK_IRQ(irqsave);
116 /* Restore serial TX intr */
117 KDBG_RESTORE_IRQ(irqsave);
121 static void PGM_FUNC(kvprintf)(const char * PGM_ATTR fmt, va_list ap)
124 /* Mask serial TX intr */
125 kdbg_irqsave_t irqsave;
126 KDBG_MASK_IRQ(irqsave);
128 PGM_FUNC(_formatted_write)(fmt, __kputchar, 0, ap);
130 /* Restore serial TX intr */
131 KDBG_RESTORE_IRQ(irqsave);
133 /* A better than nothing printf() surrogate. */
134 PGM_FUNC(kputs)(fmt);
135 #endif /* CONFIG_PRINTF */
138 void PGM_FUNC(kprintf)(const char * PGM_ATTR fmt, ...)
143 PGM_FUNC(kvprintf)(fmt, ap);
147 void PGM_FUNC(kputs)(const char * PGM_ATTR str)
151 /* Mask serial TX intr */
152 kdbg_irqsave_t irqsave;
153 KDBG_MASK_IRQ(irqsave);
155 while ((c = PGM_READ_CHAR(str++)))
158 KDBG_RESTORE_IRQ(irqsave);
163 * Cheap function to print small integers without using printf().
173 digit = num / divisor;
176 if (digit || output_len || divisor == 1)
178 kputchar(digit + '0');
182 while (divisor /= 10);
188 static void klocation(const char * PGM_ATTR file, int line)
190 PGM_FUNC(kputs)(file);
193 PGM_FUNC(kputs)(PGM_STR(": "));
196 int PGM_FUNC(__assert)(const char * PGM_ATTR cond, const char * PGM_ATTR file, int line)
198 klocation(file, line);
199 PGM_FUNC(kputs)(PGM_STR("Assertion failed: "));
200 PGM_FUNC(kputs)(cond);
207 * Unfortunately, there's no way to get __func__ in
208 * program memory, so we waste quite a lot of RAM in
209 * AVR and other Harvard processors.
211 void PGM_FUNC(__trace)(const char *name)
213 PGM_FUNC(kprintf)(PGM_STR("%s()\n"), name);
216 void PGM_FUNC(__tracemsg)(const char *name, const char * PGM_ATTR fmt, ...)
220 PGM_FUNC(kprintf)(PGM_STR("%s(): "), name);
222 PGM_FUNC(kvprintf)(fmt, ap);
227 int PGM_FUNC(__invalid_ptr)(void *value, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
229 klocation(file, line);
230 PGM_FUNC(kputs)(PGM_STR("Invalid ptr: "));
231 PGM_FUNC(kputs)(name);
233 PGM_FUNC(kprintf)(PGM_STR(" = 0x%p\n"), value);
242 void __init_wall(long *wall, int size)
245 *wall++ = WALL_VALUE;
249 int PGM_FUNC(__check_wall)(long *wall, int size, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
253 for (i = 0; i < size; i++)
255 if (wall[i] != WALL_VALUE)
257 klocation(file, line);
258 PGM_FUNC(kputs)(PGM_STR("Wall broken: "));
259 PGM_FUNC(kputs)(name);
261 PGM_FUNC(kprintf)(PGM_STR("[%d] (0x%p) = 0x%lx\n"), i, wall + i, wall[i]);
276 * Dump binary data in hex
278 void kdump(const void *_buf, size_t len)
280 const unsigned char *buf = (const unsigned char *)_buf;
283 kprintf("%02X", *buf++);
287 #endif /* CONFIG_PRINTF */