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