Break on assertion failures.
[bertos.git] / drv / kdebug.c
1 /*!
2  * \file
3  * <!--
4  * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 2000, 2001, 2002 Bernardo Innocenti <bernie@codewiz.org>
6  * This file is part of DevLib - See README.devlib for information.
7  * -->
8  *
9  * \brief General pourpose debug support for embedded systems (implementation).
10  *
11  * \version $Id$
12  * \author Bernardo Innocenti <bernie@develer.com>
13  * \author Stefano Fedrigo <aleph@develer.com>
14  */
15
16 /*#*
17  *#* $Log$
18  *#* Revision 1.26  2006/04/27 05:40:27  bernie
19  *#* Break on assertion failures.
20  *#*
21  *#* Revision 1.25  2005/06/27 21:26:24  bernie
22  *#* Misc PGM fixes.
23  *#*
24  *#* Revision 1.24  2005/04/12 01:36:37  bernie
25  *#* Add hack to enable TX line at module initialization.
26  *#*
27  *#* Revision 1.23  2005/04/11 19:10:27  bernie
28  *#* Include top-level headers from cfg/ subdir.
29  *#*
30  *#* Revision 1.22  2005/02/18 11:18:33  bernie
31  *#* Fixes for Harvard processors from project_ks.
32  *#*
33  *#* Revision 1.21  2005/02/16 20:29:48  bernie
34  *#* TRACE(), TRACEMSG(): Reduce code and data footprint.
35  *#*
36  *#* Revision 1.20  2005/01/25 08:36:40  bernie
37  *#* kputnum(): Export.
38  *#*
39  *#* Revision 1.19  2004/12/31 17:47:45  bernie
40  *#* Rename UNUSED() to UNUSED_ARG().
41  *#*/
42
43 #include <cfg/debug.h>
44 #include <cfg/cpu.h>
45 #include <cfg/macros.h> /* for BV() */
46 #include <appconfig.h>
47 #include <hw.h>
48
49 #include <mware/formatwr.h> /* for _formatted_write() */
50
51 #ifdef _DEBUG
52
53 #if defined(_EMUL)
54         #include <stdio.h>
55         #define KDBG_WAIT_READY()      do { /*nop*/ } while(0)
56         #define KDBG_WRITE_CHAR(c)     putchar((c))
57         #define KDBG_MASK_IRQ(old)     do { (void)(old); } while(0)
58         #define KDBG_RESTORE_IRQ(old)  do { /*nop*/ } while(0)
59         typedef char kdbg_irqsave_t; /* unused */
60 #elif CPU_I196
61         #include "Util196.h"
62         #define KDBG_WAIT_READY()      do {} while (!(SP_STAT & (SPSF_TX_EMPTY | SPSF_TX_INT)))
63         #define KDBG_WRITE_CHAR(c)     do { SBUF = (c); } while(0)
64         #define KDBG_MASK_IRQ(old) \
65                 do { \
66                         (old) = INT_MASK1 & INT1F_TI; \
67                         INT_MASK1 &= ~INT1F_TI; \
68                 } while(0)
69         #define KDBG_RESTORE_IRQ(old)  do { INT_MASK1 |= (old); }
70         typedef uint16_t kdbg_irqsave_t; /* FIXME: unconfirmed */
71 #elif CPU_AVR
72         #include <avr/io.h>
73
74         #if CONFIG_KDEBUG_PORT == 0
75
76                 /*
77                  * Support for special bus policies or external transceivers
78                  * on UART0 (to be overridden in "hw.h").
79                  *
80                  * HACK: if we don't set TXEN, kdbg disables the transmitter
81                  * after each output statement until the serial driver
82                  * is initialized.  These glitches confuse the debug
83                  * terminal that ends up printing some trash.
84                  */
85                 #ifndef KDBG_UART0_BUS_INIT
86                 #define KDBG_UART0_BUS_INIT  do { \
87                                 UCSR0B = BV(TXEN); \
88                         } while (0)
89                 #endif
90                 #ifndef KDBG_UART0_BUS_RX
91                 #define KDBG_UART0_BUS_RX    do {} while (0)
92                 #endif
93                 #ifndef KDBG_UART0_BUS_TX
94                 #define KDBG_UART0_BUS_TX    do {} while (0)
95                 #endif
96
97                 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128
98                         #define UCR UCSR0B
99                         #define UDR UDR0
100                         #define USR UCSR0A
101                 #elif CPU_AVR_ATMEGA8
102                         #define UCR UCSRB
103                         #define USR UCSRA
104                 #else
105                         #error Unknown CPU
106                 #endif
107
108                 #define KDBG_WAIT_READY()     do { loop_until_bit_is_set(USR, UDRE); } while(0)
109                 #define KDBG_WAIT_TXDONE()    do { loop_until_bit_is_set(USR, TXC); } while(0)
110
111                 /*
112                  * We must clear the TXC flag before sending a new character to allow
113                  * KDBG_WAIT_TXDONE() to work properly.
114                  *
115                  * BUG: if KDBG_WRITE_CHAR() is called after the TXC flag is set by hardware,
116                  * a new TXC could be generated after we've cleared it and before the new
117                  * character is written to UDR.  On a 485 bus, the transceiver will be put
118                  * in RX mode while still transmitting the last char.
119                  */
120                 #define KDBG_WRITE_CHAR(c)    do { USR |= BV(TXC); UDR = (c); } while(0)
121
122                 #define KDBG_MASK_IRQ(old)    do { \
123                         (old) = UCR; \
124                         UCR |= BV(TXEN); \
125                         UCR &= ~(BV(TXCIE) | BV(UDRIE)); \
126                         KDBG_UART0_BUS_TX; \
127                 } while(0)
128
129                 #define KDBG_RESTORE_IRQ(old) do { \
130                         KDBG_WAIT_TXDONE(); \
131                         KDBG_UART0_BUS_RX; \
132                         UCR = (old); \
133                 } while(0)
134
135                 typedef uint8_t kdbg_irqsave_t;
136
137         #elif CONFIG_KDEBUG_PORT == 1
138
139                 /*
140                  * Support for special bus policies or external transceivers
141                  * on UART1 (to be overridden in "hw.h").
142                  *
143                  * HACK: if we don't set TXEN, kdbg disables the transmitter
144                  * after each output statement until the serial driver
145                  * is initialized.  These glitches confuse the debug
146                  * terminal that ends up printing some trash.
147                  */
148                 #ifndef KDBG_UART1_BUS_INIT
149                 #define KDBG_UART1_BUS_INIT  do { \
150                                 UCSR1B = BV(TXEN); \
151                         } while (0)
152                 #endif
153                 #ifndef KDBG_UART1_BUS_RX
154                 #define KDBG_UART1_BUS_RX    do {} while (0)
155                 #endif
156                 #ifndef KDBG_UART1_BUS_TX
157                 #define KDBG_UART1_BUS_TX    do {} while (0)
158                 #endif
159
160                 #define KDBG_WAIT_READY()     do { loop_until_bit_is_set(UCSR1A, UDRE); } while(0)
161                 #define KDBG_WAIT_TXDONE()    do { loop_until_bit_is_set(UCSR1A, TXC); } while(0)
162                 #define KDBG_WRITE_CHAR(c)    do { UCSR1A |= BV(TXC); UDR1 = (c); } while(0)
163
164                 #define KDBG_MASK_IRQ(old)    do { \
165                         (old) = UCSR1B; \
166                         UCSR1B |= BV(TXEN); \
167                         UCSR1B &= ~(BV(TXCIE) | BV(UDRIE)); \
168                         KDBG_UART1_BUS_TX; \
169                 } while(0)
170
171                 #define KDBG_RESTORE_IRQ(old) do { \
172                         KDBG_WAIT_TXDONE(); \
173                         KDBG_UART1_BUS_RX; \
174                         UCSR1B = (old); \
175                 } while(0)
176
177                 typedef uint8_t kdbg_irqsave_t;
178         #else
179                 #error CONFIG_KDEBUG_PORT should be either 0 or 1
180         #endif
181 #elif defined(__MWERKS__) && CPU_DSP56K
182         /* Debugging go through the JTAG interface. The MSL library already
183            implements the console I/O correctly. */
184         #include <stdio.h>
185         #define KDBG_WAIT_READY()         do { } while (0)
186         #define KDBG_WRITE_CHAR(c)        __put_char(c, stdout)
187         #define KDBG_MASK_IRQ(old)        do { (void)(old); } while (0)
188         #define KDBG_RESTORE_IRQ(old)     do { (void)(old); } while (0)
189         typedef uint8_t kdbg_irqsave_t; /* unused */
190 #else
191         #error Unknown architecture
192 #endif
193
194
195 void kdbg_init(void)
196 {
197 #if CPU_I196
198
199         /* Set serial port for 19200bps 8N1 */
200         INT_MASK1 &= ~(INT1F_TI | INT1F_RI);
201         SP_CON = SPCF_RECEIVE_ENABLE | SPCF_MODE1;
202         ioc1_img |= IOC1F_TXD_SEL | IOC1F_EXTINT_SRC;
203         IOC1 = ioc1_img;
204         BAUD_RATE = 0x33;
205         BAUD_RATE = 0x80;
206
207 #elif CPU_AVR
208
209         /* Compute the baud rate */
210         uint16_t period = (((CLOCK_FREQ / 16UL) + (CONFIG_KDEBUG_BAUDRATE / 2)) / CONFIG_KDEBUG_BAUDRATE) - 1;
211
212         #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128
213                 #if CONFIG_KDEBUG_PORT == 0
214                         UBRR0H = (uint8_t)(period>>8);
215                         UBRR0L = (uint8_t)period;
216                         KDBG_UART0_BUS_INIT;
217                 #elif CONFIG_KDEBUG_PORT == 1
218                         UBRR1H = (uint8_t)(period>>8);
219                         UBRR1L = (uint8_t)period;
220                         KDBG_UART1_BUS_INIT;
221                 #else
222                         #error CONFIG_KDEBUG_PORT must be either 0 or 1
223                 #endif
224         #elif CPU_AVR_ATMEGA8
225                 UBRRH = (uint8_t)(period>>8);
226                 UBRRL = (uint8_t)period;
227         #elif CPU_AVR_ATMEGA103
228                 UBRR = (uint8_t)period;
229                 KDBG_UART0_BUS_INIT;
230         #else
231                 #error Unknown CPU
232         #endif
233
234 #endif /* !CPU_I196 && !CPU_AVR */
235
236         kputs("\n\n*** DBG START ***\n");
237 }
238
239
240 /*!
241  * Output one character to the debug console
242  */
243 static void __kputchar(char c, UNUSED_ARG(void *, unused))
244 {
245         /* Poll while serial buffer is still busy */
246         KDBG_WAIT_READY();
247
248         /* Send '\n' as '\r\n' for dumb terminals */
249         if (c == '\n')
250         {
251                 KDBG_WRITE_CHAR('\r');
252                 KDBG_WAIT_READY();
253         }
254
255         KDBG_WRITE_CHAR(c);
256 }
257
258
259 void kputchar(char c)
260 {
261         /* Mask serial TX intr */
262         kdbg_irqsave_t irqsave;
263         KDBG_MASK_IRQ(irqsave);
264
265         __kputchar(c, 0);
266
267         /* Restore serial TX intr */
268         KDBG_RESTORE_IRQ(irqsave);
269 }
270
271
272 static void PGM_FUNC(kvprintf)(const char * PGM_ATTR fmt, va_list ap)
273 {
274 #if CONFIG_PRINTF
275         /* Mask serial TX intr */
276         kdbg_irqsave_t irqsave;
277         KDBG_MASK_IRQ(irqsave);
278
279         PGM_FUNC(_formatted_write)(fmt, __kputchar, 0, ap);
280
281         /* Restore serial TX intr */
282         KDBG_RESTORE_IRQ(irqsave);
283 #else
284         /* A better than nothing printf() surrogate. */
285         PGM_FUNC(kputs)(fmt);
286 #endif /* CONFIG_PRINTF */
287 }
288
289 void PGM_FUNC(kprintf)(const char * PGM_ATTR fmt, ...)
290 {
291         va_list ap;
292
293         va_start(ap, fmt);
294         PGM_FUNC(kvprintf)(fmt, ap);
295         va_end(ap);
296 }
297
298 void PGM_FUNC(kputs)(const char * PGM_ATTR str)
299 {
300         char c;
301
302         /* Mask serial TX intr */
303         kdbg_irqsave_t irqsave;
304         KDBG_MASK_IRQ(irqsave);
305
306         while ((c = PGM_READ_CHAR(str++)))
307                 __kputchar(c, 0);
308
309         KDBG_RESTORE_IRQ(irqsave);
310 }
311
312
313 /*!
314  * Cheap function to print small integers without using printf().
315  */
316 int kputnum(int num)
317 {
318         int output_len = 0;
319         int divisor = 10000;
320         int digit;
321
322         do
323         {
324                 digit = num / divisor;
325                 num %= divisor;
326
327                 if (digit || output_len || divisor == 1)
328                 {
329                         kputchar(digit + '0');
330                         ++output_len;
331                 }
332         }
333         while (divisor /= 10);
334
335         return output_len;
336 }
337
338
339 static void klocation(const char * PGM_ATTR file, int line)
340 {
341         PGM_FUNC(kputs)(file);
342         kputchar(':');
343         kputnum(line);
344         PGM_FUNC(kputs)(PGM_STR(": "));
345 }
346
347 int PGM_FUNC(__assert)(const char * PGM_ATTR cond, const char * PGM_ATTR file, int line)
348 {
349         klocation(file, line);
350         PGM_FUNC(kputs)(PGM_STR("Assertion failed: "));
351         PGM_FUNC(kputs)(cond);
352         kputchar('\n');
353         BREAKPOINT;
354         return 1;
355 }
356
357 /*
358  * Unfortunately, there's no way to get __func__ in
359  * program memory, so we waste quite a lot of RAM in
360  * AVR and other Harvard processors.
361  */
362 void PGM_FUNC(__trace)(const char *name)
363 {
364         PGM_FUNC(kprintf)(PGM_STR("%s()\n"), name);
365 }
366
367 void PGM_FUNC(__tracemsg)(const char *name, const char * PGM_ATTR fmt, ...)
368 {
369         va_list ap;
370
371         PGM_FUNC(kprintf)(PGM_STR("%s(): "), name);
372         va_start(ap, fmt);
373         PGM_FUNC(kvprintf)(fmt, ap);
374         va_end(ap);
375         kputchar('\n');
376 }
377
378 int PGM_FUNC(__invalid_ptr)(void *value, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
379 {
380         klocation(file, line);
381         PGM_FUNC(kputs)(PGM_STR("Invalid ptr: "));
382         PGM_FUNC(kputs)(name);
383         #if CONFIG_PRINTF
384                 PGM_FUNC(kprintf)(PGM_STR(" = 0x%x\n"), (unsigned int)value);
385         #else
386                 (void)value;
387                 kputchar('\n');
388         #endif
389         return 1;
390 }
391
392
393 void __init_wall(long *wall, int size)
394 {
395         while(size--)
396                 *wall++ = WALL_VALUE;
397 }
398
399
400 int PGM_FUNC(__check_wall)(long *wall, int size, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
401 {
402         int i, fail = 0;
403
404         for (i = 0; i < size; i++)
405         {
406                 if (wall[i] != WALL_VALUE)
407                 {
408                         klocation(file, line);
409                         PGM_FUNC(kputs)(PGM_STR("Wall broken: "));
410                         PGM_FUNC(kputs)(name);
411                         #if CONFIG_PRINTF
412                                 PGM_FUNC(kprintf)(PGM_STR("[%d] (0x%p) = 0x%lx\n"), i, wall + i, wall[i]);
413                         #else
414                                 kputchar('\n');
415                         #endif
416                         fail = 1;
417                 }
418         }
419
420         return fail;
421 }
422
423
424 #if CONFIG_PRINTF
425
426 /*!
427  * Dump binary data in hex
428  */
429 void kdump(const void *_buf, size_t len)
430 {
431         const unsigned char *buf = (const unsigned char *)_buf;
432
433         while (len--)
434                 kprintf("%02X", *buf++);
435         kputchar('\n');
436 }
437
438 #endif /* CONFIG_PRINTF */
439
440 #endif /* _DEBUG */