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