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