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