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