Merge from kseries.
[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.30  2007/01/28 09:18:06  batt
19  *#* Merge from project_ks.
20  *#*
21  *#* Revision 1.29  2006/11/23 13:19:39  batt
22  *#* Add BitBanged serial debug console.
23  *#*
24  *#* Revision 1.28  2006/07/19 12:56:25  bernie
25  *#* Convert to new Doxygen style.
26  *#*
27  *#* Revision 1.27  2006/06/01 12:32:06  marco
28  *#* Updated include reference.
29  *#*
30  *#* Revision 1.26  2006/04/27 05:40:27  bernie
31  *#* Break on assertion failures.
32  *#*
33  *#* Revision 1.25  2005/06/27 21:26:24  bernie
34  *#* Misc PGM fixes.
35  *#*
36  *#* Revision 1.24  2005/04/12 01:36:37  bernie
37  *#* Add hack to enable TX line at module initialization.
38  *#*
39  *#* Revision 1.23  2005/04/11 19:10:27  bernie
40  *#* Include top-level headers from cfg/ subdir.
41  *#*
42  *#* Revision 1.22  2005/02/18 11:18:33  bernie
43  *#* Fixes for Harvard processors from project_ks.
44  *#*
45  *#* Revision 1.21  2005/02/16 20:29:48  bernie
46  *#* TRACE(), TRACEMSG(): Reduce code and data footprint.
47  *#*
48  *#* Revision 1.20  2005/01/25 08:36:40  bernie
49  *#* kputnum(): Export.
50  *#*
51  *#* Revision 1.19  2004/12/31 17:47:45  bernie
52  *#* Rename UNUSED() to UNUSED_ARG().
53  *#*/
54
55 #include <cfg/debug.h>
56 #include <cfg/cpu.h>
57 #include <cfg/macros.h> /* for BV() */
58 #include <appconfig.h>
59 #include <hw_cpu.h>     /* for CLOCK_FREQ */
60 #include <hw_ser.h>     /* Required for bus macros overrides */
61
62 #include <mware/formatwr.h> /* for _formatted_write() */
63
64 #ifdef _DEBUG
65
66 #if defined(_EMUL)
67         #include <stdio.h>
68         #define KDBG_WAIT_READY()      do { /*nop*/ } while(0)
69         #define KDBG_WRITE_CHAR(c)     putchar((c))
70         #define KDBG_MASK_IRQ(old)     do { (void)(old); } while(0)
71         #define KDBG_RESTORE_IRQ(old)  do { /*nop*/ } while(0)
72         typedef char kdbg_irqsave_t; /* unused */
73
74         #if CONFIG_KDEBUG_PORT == 666
75                 #error BITBANG debug console missing for this platform
76         #endif
77 #elif CPU_I196
78         #include "Util196.h"
79         #define KDBG_WAIT_READY()      do {} while (!(SP_STAT & (SPSF_TX_EMPTY | SPSF_TX_INT)))
80         #define KDBG_WRITE_CHAR(c)     do { SBUF = (c); } while(0)
81         #define KDBG_MASK_IRQ(old) \
82                 do { \
83                         (old) = INT_MASK1 & INT1F_TI; \
84                         INT_MASK1 &= ~INT1F_TI; \
85                 } while(0)
86         #define KDBG_RESTORE_IRQ(old)  do { INT_MASK1 |= (old); }
87         typedef uint16_t kdbg_irqsave_t; /* FIXME: unconfirmed */
88
89         #if CONFIG_KDEBUG_PORT == 666
90                 #error BITBANG debug console missing for this platform
91         #endif
92 #elif CPU_AVR
93         #include <avr/io.h>
94
95         #if CONFIG_KDEBUG_PORT == 0
96
97                 /*
98                  * Support for special bus policies or external transceivers
99                  * on UART0 (to be overridden in "hw_ser.h").
100                  *
101                  * HACK: if we don't set TXEN, kdbg disables the transmitter
102                  * after each output statement until the serial driver
103                  * is initialized.  These glitches confuse the debug
104                  * terminal that ends up printing some trash.
105                  */
106                 #ifndef KDBG_UART0_BUS_INIT
107                 #define KDBG_UART0_BUS_INIT  do { \
108                                 UCSR0B = BV(TXEN); \
109                         } while (0)
110                 #endif
111                 #ifndef KDBG_UART0_BUS_RX
112                 #define KDBG_UART0_BUS_RX    do {} while (0)
113                 #endif
114                 #ifndef KDBG_UART0_BUS_TX
115                 #define KDBG_UART0_BUS_TX    do {} while (0)
116                 #endif
117
118                 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128
119                         #define UCR UCSR0B
120                         #define UDR UDR0
121                         #define USR UCSR0A
122                 #elif CPU_AVR_ATMEGA8
123                         #define UCR UCSRB
124                         #define USR UCSRA
125                 #else
126                         #error Unknown CPU
127                 #endif
128
129                 #define KDBG_WAIT_READY()     do { loop_until_bit_is_set(USR, UDRE); } while(0)
130                 #define KDBG_WAIT_TXDONE()    do { loop_until_bit_is_set(USR, TXC); } while(0)
131
132                 /*
133                  * We must clear the TXC flag before sending a new character to allow
134                  * KDBG_WAIT_TXDONE() to work properly.
135                  *
136                  * BUG: if KDBG_WRITE_CHAR() is called after the TXC flag is set by hardware,
137                  * a new TXC could be generated after we've cleared it and before the new
138                  * character is written to UDR.  On a 485 bus, the transceiver will be put
139                  * in RX mode while still transmitting the last char.
140                  */
141                 #define KDBG_WRITE_CHAR(c)    do { USR |= BV(TXC); UDR = (c); } while(0)
142
143                 #define KDBG_MASK_IRQ(old)    do { \
144                         (old) = UCR; \
145                         UCR |= BV(TXEN); \
146                         UCR &= ~(BV(TXCIE) | BV(UDRIE)); \
147                         KDBG_UART0_BUS_TX; \
148                 } while(0)
149
150                 #define KDBG_RESTORE_IRQ(old) do { \
151                         KDBG_WAIT_TXDONE(); \
152                         KDBG_UART0_BUS_RX; \
153                         UCR = (old); \
154                 } while(0)
155
156                 typedef uint8_t kdbg_irqsave_t;
157
158         #elif CONFIG_KDEBUG_PORT == 1
159
160                 /*
161                  * Support for special bus policies or external transceivers
162                  * on UART1 (to be overridden in "hw_ser.h").
163                  *
164                  * HACK: if we don't set TXEN, kdbg disables the transmitter
165                  * after each output statement until the serial driver
166                  * is initialized.  These glitches confuse the debug
167                  * terminal that ends up printing some trash.
168                  */
169                 #ifndef KDBG_UART1_BUS_INIT
170                 #define KDBG_UART1_BUS_INIT  do { \
171                                 UCSR1B = BV(TXEN); \
172                         } while (0)
173                 #endif
174                 #ifndef KDBG_UART1_BUS_RX
175                 #define KDBG_UART1_BUS_RX    do {} while (0)
176                 #endif
177                 #ifndef KDBG_UART1_BUS_TX
178                 #define KDBG_UART1_BUS_TX    do {} while (0)
179                 #endif
180
181                 #define KDBG_WAIT_READY()     do { loop_until_bit_is_set(UCSR1A, UDRE); } while(0)
182                 #define KDBG_WAIT_TXDONE()    do { loop_until_bit_is_set(UCSR1A, TXC); } while(0)
183                 #define KDBG_WRITE_CHAR(c)    do { UCSR1A |= BV(TXC); UDR1 = (c); } while(0)
184
185                 #define KDBG_MASK_IRQ(old)    do { \
186                         (old) = UCSR1B; \
187                         UCSR1B |= BV(TXEN); \
188                         UCSR1B &= ~(BV(TXCIE) | BV(UDRIE)); \
189                         KDBG_UART1_BUS_TX; \
190                 } while(0)
191
192                 #define KDBG_RESTORE_IRQ(old) do { \
193                         KDBG_WAIT_TXDONE(); \
194                         KDBG_UART1_BUS_RX; \
195                         UCSR1B = (old); \
196                 } while(0)
197
198                 typedef uint8_t kdbg_irqsave_t;
199
200         /*
201          * Special debug port for BitBanged Serial see below for details...
202          */
203         #elif CONFIG_KDEBUG_PORT == 666
204                 #include "hw_ser.h"
205                 #define KDBG_WAIT_READY()      do { /*nop*/ } while(0)
206                 #define KDBG_WRITE_CHAR(c)     _kdebug_bitbang_putchar((c))
207                 #define KDBG_MASK_IRQ(old)     do { IRQ_SAVE_DISABLE((old)); } while(0)
208                 #define KDBG_RESTORE_IRQ(old)  do { IRQ_RESTORE((old)); } while(0)
209                 typedef cpuflags_t kdbg_irqsave_t;
210
211                 #define KDBG_DELAY (((CLOCK_FREQ + CONFIG_KDEBUG_BAUDRATE / 2) / CONFIG_KDEBUG_BAUDRATE) + 7) / 14
212
213                 static void _kdebug_bitbang_delay(void)
214                 {
215                         unsigned long i;
216
217                         for (i = 0; i < KDBG_DELAY; i++)
218                         {
219                                 NOP;
220                                 NOP;
221                                 NOP;
222                                 NOP;
223                                 NOP;
224                         }
225                 }
226         #else
227                 #error CONFIG_KDEBUG_PORT should be either 0, 1 or 666
228         #endif
229 #elif defined(__MWERKS__) && CPU_DSP56K
230         /* Debugging go through the JTAG interface. The MSL library already
231            implements the console I/O correctly. */
232         #include <stdio.h>
233         #define KDBG_WAIT_READY()         do { } while (0)
234         #define KDBG_WRITE_CHAR(c)        __put_char(c, stdout)
235         #define KDBG_MASK_IRQ(old)        do { (void)(old); } while (0)
236         #define KDBG_RESTORE_IRQ(old)     do { (void)(old); } while (0)
237         typedef uint8_t kdbg_irqsave_t; /* unused */
238         #if CONFIG_KDEBUG_PORT == 666
239                 #error BITBANG debug console missing for this platform
240         #endif
241 #else
242         #error Unknown architecture
243 #endif
244
245 #if CONFIG_KDEBUG_PORT == 666
246         /**
247          * Putchar for BITBANG serial debug console.
248          * Sometimes, we can't permit to use a whole serial for debugging purpose.
249          * Since debug console is in output only it is usefull to use a single generic I/O pin for debug.
250          * This is achieved by this simple function, that shift out the data like a UART, but
251          * in software :)
252          * The only requirement is that SER_BITBANG_* macros will be defined somewhere (usually hw_ser.h)
253          * \note All interrupts are disabled during debug prints!
254          */
255         static void _kdebug_bitbang_putchar(char c)
256         {
257                 int i;
258                 uint16_t data = c;
259
260                 /* Add stop bit */
261                 data |= 0x0100;
262
263                 /* Add start bit*/
264                 data <<= 1;
265
266                 /* Shift out data */
267                 uint16_t shift = 1;
268                 for (i = 0; i < 10; i++)
269                 {
270                         if (data & shift)
271                                 SER_BITBANG_HIGH;
272                         else
273                                 SER_BITBANG_LOW;
274                         _kdebug_bitbang_delay();
275                         shift <<= 1;
276                 }
277         }
278 #endif
279
280
281
282
283 void kdbg_init(void)
284 {
285 #if CPU_I196
286
287         /* Set serial port for 19200bps 8N1 */
288         INT_MASK1 &= ~(INT1F_TI | INT1F_RI);
289         SP_CON = SPCF_RECEIVE_ENABLE | SPCF_MODE1;
290         ioc1_img |= IOC1F_TXD_SEL | IOC1F_EXTINT_SRC;
291         IOC1 = ioc1_img;
292         BAUD_RATE = 0x33;
293         BAUD_RATE = 0x80;
294
295 #elif CPU_AVR
296         #if CONFIG_KDEBUG_PORT == 666
297                 SER_BITBANG_INIT;
298         #else /* CONFIG_KDEBUG_PORT != 666 */
299                 /* Compute the baud rate */
300                 uint16_t period = (((CLOCK_FREQ / 16UL) + (CONFIG_KDEBUG_BAUDRATE / 2)) / CONFIG_KDEBUG_BAUDRATE) - 1;
301
302                 #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128
303                         #if CONFIG_KDEBUG_PORT == 0
304                                 UBRR0H = (uint8_t)(period>>8);
305                                 UBRR0L = (uint8_t)period;
306                                 KDBG_UART0_BUS_INIT;
307                         #elif CONFIG_KDEBUG_PORT == 1
308                                 UBRR1H = (uint8_t)(period>>8);
309                                 UBRR1L = (uint8_t)period;
310                                 KDBG_UART1_BUS_INIT;
311                         #else
312                                 #error CONFIG_KDEBUG_PORT must be either 0 or 1
313                         #endif
314                 #elif CPU_AVR_ATMEGA8
315                         UBRRH = (uint8_t)(period>>8);
316                         UBRRL = (uint8_t)period;
317                 #elif CPU_AVR_ATMEGA103
318                         UBRR = (uint8_t)period;
319                         KDBG_UART0_BUS_INIT;
320                 #else
321                         #error Unknown CPU
322                 #endif
323         #endif /* CONFIG_KDEBUG_PORT == 666 */
324
325 #endif /* !CPU_I196 && !CPU_AVR */
326
327         kputs("\n\n*** DBG START ***\n");
328 }
329
330
331 /**
332  * Output one character to the debug console
333  */
334 static void __kputchar(char c, UNUSED_ARG(void *, unused))
335 {
336         /* Poll while serial buffer is still busy */
337         KDBG_WAIT_READY();
338
339         /* Send '\n' as '\r\n' for dumb terminals */
340         if (c == '\n')
341         {
342                 KDBG_WRITE_CHAR('\r');
343                 KDBG_WAIT_READY();
344         }
345
346         KDBG_WRITE_CHAR(c);
347 }
348
349
350 void kputchar(char c)
351 {
352         /* Mask serial TX intr */
353         kdbg_irqsave_t irqsave;
354         KDBG_MASK_IRQ(irqsave);
355
356         __kputchar(c, 0);
357
358         /* Restore serial TX intr */
359         KDBG_RESTORE_IRQ(irqsave);
360 }
361
362
363 static void PGM_FUNC(kvprintf)(const char * PGM_ATTR fmt, va_list ap)
364 {
365 #if CONFIG_PRINTF
366         /* Mask serial TX intr */
367         kdbg_irqsave_t irqsave;
368         KDBG_MASK_IRQ(irqsave);
369
370         PGM_FUNC(_formatted_write)(fmt, __kputchar, 0, ap);
371
372         /* Restore serial TX intr */
373         KDBG_RESTORE_IRQ(irqsave);
374 #else
375         /* A better than nothing printf() surrogate. */
376         PGM_FUNC(kputs)(fmt);
377 #endif /* CONFIG_PRINTF */
378 }
379
380 void PGM_FUNC(kprintf)(const char * PGM_ATTR fmt, ...)
381 {
382         va_list ap;
383
384         va_start(ap, fmt);
385         PGM_FUNC(kvprintf)(fmt, ap);
386         va_end(ap);
387 }
388
389 void PGM_FUNC(kputs)(const char * PGM_ATTR str)
390 {
391         char c;
392
393         /* Mask serial TX intr */
394         kdbg_irqsave_t irqsave;
395         KDBG_MASK_IRQ(irqsave);
396
397         while ((c = PGM_READ_CHAR(str++)))
398                 __kputchar(c, 0);
399
400         KDBG_RESTORE_IRQ(irqsave);
401 }
402
403
404 /**
405  * Cheap function to print small integers without using printf().
406  */
407 int kputnum(int num)
408 {
409         int output_len = 0;
410         int divisor = 10000;
411         int digit;
412
413         do
414         {
415                 digit = num / divisor;
416                 num %= divisor;
417
418                 if (digit || output_len || divisor == 1)
419                 {
420                         kputchar(digit + '0');
421                         ++output_len;
422                 }
423         }
424         while (divisor /= 10);
425
426         return output_len;
427 }
428
429
430 static void klocation(const char * PGM_ATTR file, int line)
431 {
432         PGM_FUNC(kputs)(file);
433         kputchar(':');
434         kputnum(line);
435         PGM_FUNC(kputs)(PGM_STR(": "));
436 }
437
438 int PGM_FUNC(__assert)(const char * PGM_ATTR cond, const char * PGM_ATTR file, int line)
439 {
440         klocation(file, line);
441         PGM_FUNC(kputs)(PGM_STR("Assertion failed: "));
442         PGM_FUNC(kputs)(cond);
443         kputchar('\n');
444         BREAKPOINT;
445         return 1;
446 }
447
448 /*
449  * Unfortunately, there's no way to get __func__ in
450  * program memory, so we waste quite a lot of RAM in
451  * AVR and other Harvard processors.
452  */
453 void PGM_FUNC(__trace)(const char *name)
454 {
455         PGM_FUNC(kprintf)(PGM_STR("%s()\n"), name);
456 }
457
458 void PGM_FUNC(__tracemsg)(const char *name, const char * PGM_ATTR fmt, ...)
459 {
460         va_list ap;
461
462         PGM_FUNC(kprintf)(PGM_STR("%s(): "), name);
463         va_start(ap, fmt);
464         PGM_FUNC(kvprintf)(fmt, ap);
465         va_end(ap);
466         kputchar('\n');
467 }
468
469 int PGM_FUNC(__invalid_ptr)(void *value, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
470 {
471         klocation(file, line);
472         PGM_FUNC(kputs)(PGM_STR("Invalid ptr: "));
473         PGM_FUNC(kputs)(name);
474         #if CONFIG_PRINTF
475                 PGM_FUNC(kprintf)(PGM_STR(" = 0x%x\n"), (unsigned int)value);
476         #else
477                 (void)value;
478                 kputchar('\n');
479         #endif
480         return 1;
481 }
482
483
484 void __init_wall(long *wall, int size)
485 {
486         while(size--)
487                 *wall++ = WALL_VALUE;
488 }
489
490
491 int PGM_FUNC(__check_wall)(long *wall, int size, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
492 {
493         int i, fail = 0;
494
495         for (i = 0; i < size; i++)
496         {
497                 if (wall[i] != WALL_VALUE)
498                 {
499                         klocation(file, line);
500                         PGM_FUNC(kputs)(PGM_STR("Wall broken: "));
501                         PGM_FUNC(kputs)(name);
502                         #if CONFIG_PRINTF
503                                 PGM_FUNC(kprintf)(PGM_STR("[%d] (0x%p) = 0x%lx\n"), i, wall + i, wall[i]);
504                         #else
505                                 kputchar('\n');
506                         #endif
507                         fail = 1;
508                 }
509         }
510
511         return fail;
512 }
513
514
515 #if CONFIG_PRINTF
516
517 /**
518  * Dump binary data in hex
519  */
520 void kdump(const void *_buf, size_t len)
521 {
522         const unsigned char *buf = (const unsigned char *)_buf;
523
524         while (len--)
525                 kprintf("%02X", *buf++);
526         kputchar('\n');
527 }
528
529 #endif /* CONFIG_PRINTF */
530
531 #endif /* _DEBUG */