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