Test for ticket system bug #14.
[bertos.git] / drv / kdebug.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
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>
31  *
32  * -->
33  *
34  * \brief General pourpose debug support for embedded systems (implementation).
35  *
36  * \version $Id$
37  * \author Bernardo Innocenti <bernie@develer.com>
38  * \author Stefano Fedrigo <aleph@develer.com>
39  */
40
41
42 #include <cfg/debug.h>
43 #include <cpu/irq.h>
44 #include <cpu/attr.h>
45 #include <cpu/types.h>
46 #include <cfg/macros.h> /* for BV() */
47 #include <appconfig.h>
48 #warning what about these?
49 //#include <hw_cpu.h>     /* for CLOCK_FREQ */
50 //#include <hw_ser.h>     /* Required for bus macros overrides */
51
52 #include <mware/formatwr.h> /* for _formatted_write() */
53 #include <mware/pgm.h>
54
55 #ifdef _DEBUG
56
57 #if CPU_HARVARD && !defined(_PROGMEM)
58         #error This module build correctly only in program memory!
59 #endif
60
61
62 #if OS_HOSTED
63         #include <stdio.h>
64         #define KDBG_WAIT_READY()      do { /*nop*/ } while(0)
65         #define KDBG_WRITE_CHAR(c)     putc((c), stderr)
66         #define KDBG_MASK_IRQ(old)     do { (void)(old); } while(0)
67         #define KDBG_RESTORE_IRQ(old)  do { /*nop*/ } while(0)
68         typedef char kdbg_irqsave_t; /* unused */
69
70         #define kdbg_hw_init() do {} while (0) ///< Not needed
71
72         #if CONFIG_KDEBUG_PORT == 666
73                 #error BITBANG debug console missing for this platform
74         #endif
75 #else
76         #include CPU_CSOURCE(kdebug)
77 #endif
78
79
80 void kdbg_init(void)
81 {
82         /* Init debug hw */
83         kdbg_hw_init();
84         kputs("\n\n*** BeRTOS DBG START ***\n");
85 }
86
87
88 /**
89  * Output one character to the debug console
90  */
91 static void __kputchar(char c, UNUSED_ARG(void *, unused))
92 {
93         /* Poll while serial buffer is still busy */
94         KDBG_WAIT_READY();
95
96         /* Send '\n' as '\r\n' for dumb terminals */
97         if (c == '\n')
98         {
99                 KDBG_WRITE_CHAR('\r');
100                 KDBG_WAIT_READY();
101         }
102
103         KDBG_WRITE_CHAR(c);
104 }
105
106
107 void kputchar(char c)
108 {
109         /* Mask serial TX intr */
110         kdbg_irqsave_t irqsave;
111         KDBG_MASK_IRQ(irqsave);
112
113         __kputchar(c, 0);
114
115         /* Restore serial TX intr */
116         KDBG_RESTORE_IRQ(irqsave);
117 }
118
119
120 static void PGM_FUNC(kvprintf)(const char * PGM_ATTR fmt, va_list ap)
121 {
122 #if CONFIG_PRINTF
123         /* Mask serial TX intr */
124         kdbg_irqsave_t irqsave;
125         KDBG_MASK_IRQ(irqsave);
126
127         PGM_FUNC(_formatted_write)(fmt, __kputchar, 0, ap);
128
129         /* Restore serial TX intr */
130         KDBG_RESTORE_IRQ(irqsave);
131 #else
132         /* A better than nothing printf() surrogate. */
133         PGM_FUNC(kputs)(fmt);
134 #endif /* CONFIG_PRINTF */
135 }
136
137 void PGM_FUNC(kprintf)(const char * PGM_ATTR fmt, ...)
138 {
139         va_list ap;
140
141         va_start(ap, fmt);
142         PGM_FUNC(kvprintf)(fmt, ap);
143         va_end(ap);
144 }
145
146 void PGM_FUNC(kputs)(const char * PGM_ATTR str)
147 {
148         char c;
149
150         /* Mask serial TX intr */
151         kdbg_irqsave_t irqsave;
152         KDBG_MASK_IRQ(irqsave);
153
154         while ((c = PGM_READ_CHAR(str++)))
155                 __kputchar(c, 0);
156
157         KDBG_RESTORE_IRQ(irqsave);
158 }
159
160
161 /**
162  * Cheap function to print small integers without using printf().
163  */
164 int kputnum(int num)
165 {
166         int output_len = 0;
167         int divisor = 10000;
168         int digit;
169
170         do
171         {
172                 digit = num / divisor;
173                 num %= divisor;
174
175                 if (digit || output_len || divisor == 1)
176                 {
177                         kputchar(digit + '0');
178                         ++output_len;
179                 }
180         }
181         while (divisor /= 10);
182
183         return output_len;
184 }
185
186
187 static void klocation(const char * PGM_ATTR file, int line)
188 {
189         PGM_FUNC(kputs)(file);
190         kputchar(':');
191         kputnum(line);
192         PGM_FUNC(kputs)(PGM_STR(": "));
193 }
194
195 int PGM_FUNC(__assert)(const char * PGM_ATTR cond, const char * PGM_ATTR file, int line)
196 {
197         klocation(file, line);
198         PGM_FUNC(kputs)(PGM_STR("Assertion failed: "));
199         PGM_FUNC(kputs)(cond);
200         kputchar('\n');
201         BREAKPOINT;
202         return 1;
203 }
204
205 /*
206  * Unfortunately, there's no way to get __func__ in
207  * program memory, so we waste quite a lot of RAM in
208  * AVR and other Harvard processors.
209  */
210 void PGM_FUNC(__trace)(const char *name)
211 {
212         PGM_FUNC(kprintf)(PGM_STR("%s()\n"), name);
213 }
214
215 void PGM_FUNC(__tracemsg)(const char *name, const char * PGM_ATTR fmt, ...)
216 {
217         va_list ap;
218
219         PGM_FUNC(kprintf)(PGM_STR("%s(): "), name);
220         va_start(ap, fmt);
221         PGM_FUNC(kvprintf)(fmt, ap);
222         va_end(ap);
223         kputchar('\n');
224 }
225
226 int PGM_FUNC(__invalid_ptr)(void *value, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
227 {
228         klocation(file, line);
229         PGM_FUNC(kputs)(PGM_STR("Invalid ptr: "));
230         PGM_FUNC(kputs)(name);
231         #if CONFIG_PRINTF
232                 PGM_FUNC(kprintf)(PGM_STR(" = 0x%p\n"), value);
233         #else
234                 (void)value;
235                 kputchar('\n');
236         #endif
237         return 1;
238 }
239
240
241 void __init_wall(long *wall, int size)
242 {
243         while(size--)
244                 *wall++ = WALL_VALUE;
245 }
246
247
248 int PGM_FUNC(__check_wall)(long *wall, int size, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
249 {
250         int i, fail = 0;
251
252         for (i = 0; i < size; i++)
253         {
254                 if (wall[i] != WALL_VALUE)
255                 {
256                         klocation(file, line);
257                         PGM_FUNC(kputs)(PGM_STR("Wall broken: "));
258                         PGM_FUNC(kputs)(name);
259                         #if CONFIG_PRINTF
260                                 PGM_FUNC(kprintf)(PGM_STR("[%d] (0x%p) = 0x%lx\n"), i, wall + i, wall[i]);
261                         #else
262                                 kputchar('\n');
263                         #endif
264                         fail = 1;
265                 }
266         }
267
268         return fail;
269 }
270
271
272 #if CONFIG_PRINTF
273
274 /**
275  * Dump binary data in hex
276  */
277 void kdump(const void *_buf, size_t len)
278 {
279         const unsigned char *buf = (const unsigned char *)_buf;
280
281         while (len--)
282                 kprintf("%02X", *buf++);
283         kputchar('\n');
284 }
285
286 #endif /* CONFIG_PRINTF */
287
288 #endif /* _DEBUG */