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