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