TRACE(), TRACEMSG(): Reduce code and data footprint.
[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.21  2005/02/16 20:29:48  bernie
19  *#* TRACE(), TRACEMSG(): Reduce code and data footprint.
20  *#*
21  *#* Revision 1.20  2005/01/25 08:36:40  bernie
22  *#* kputnum(): Export.
23  *#*
24  *#* Revision 1.19  2004/12/31 17:47:45  bernie
25  *#* Rename UNUSED() to UNUSED_ARG().
26  *#*
27  *#* Revision 1.18  2004/12/08 08:52:00  bernie
28  *#* Save some more RAM on AVR.
29  *#*
30  *#* Revision 1.17  2004/10/03 18:41:28  bernie
31  *#* Restore \version header killed by mistake in previous commit.
32  *#*
33  *#* Revision 1.16  2004/10/03 18:40:50  bernie
34  *#* Use new CPU macros.
35  *#*
36  *#* Revision 1.15  2004/09/14 21:03:46  bernie
37  *#* Use debug.h instead of kdebug.h.
38  *#*
39  *#* Revision 1.14  2004/09/06 21:39:56  bernie
40  *#* Allow partial redefinition of BUS macros.
41  *#*
42  *#* Revision 1.13  2004/08/29 22:04:26  bernie
43  *#* Convert 485 macros to generic BUS macros;
44  *#* kputchar(): New public function.
45  *#*
46  *#* Revision 1.12  2004/08/25 14:12:08  rasky
47  *#* Aggiornato il comment block dei log RCS
48  *#*
49  *#* Revision 1.11  2004/08/24 16:19:08  bernie
50  *#* kputchar(): New public function; Add missing dummy inlines for \!_DEBUG.
51  *#*
52  *#* Revision 1.10  2004/08/04 15:57:50  rasky
53  *#* Cambiata la putchar per kdebug per DSP56k: la nuova funzione e' quella piu' a basso livello (assembly)
54  *#*
55  *#* Revision 1.9  2004/08/02 20:20:29  aleph
56  *#* Merge from project_ks
57  *#*
58  *#* Revision 1.8  2004/07/30 14:26:33  rasky
59  *#* Semplificato l'output dell'ASSERT
60  *#* Aggiunta ASSERT2 con stringa di help opzionalmente disattivabile
61  *#*
62  *#* Revision 1.7  2004/07/30 14:15:53  rasky
63  *#* Nuovo supporto unificato per detect della CPU
64  *#*
65  *#* Revision 1.6  2004/07/18 21:49:28  bernie
66  *#* Add ATmega8 support.
67  *#*
68  *#* Revision 1.5  2004/06/27 15:20:26  aleph
69  *#* Change UNUSED() macro to accept two arguments: type and name;
70  *#* Add macro GNUC_PREREQ to detect GCC version during build;
71  *#* Some spacing cleanups and typo fix
72  *#*
73  *#* Revision 1.4  2004/06/06 18:09:51  bernie
74  *#* Import DSP56800 changes; Print broken wall bricks in hex.
75  *#*
76  *#* Revision 1.3  2004/06/03 11:27:09  bernie
77  *#* Add dual-license information.
78  *#*
79  *#* Revision 1.2  2004/05/23 18:21:53  bernie
80  *#* Trim CVS logs and cleanup header info.
81  *#*
82  *#*/
83
84 #include <debug.h>
85 #include <cpu.h>
86 #include <macros.h> /* for BV() */
87 #include <config.h>
88 #include <hw.h>
89
90 #include <mware/formatwr.h> /* for _formatted_write() */
91
92 #ifdef _DEBUG
93
94 #if defined(_EMUL)
95         #include <stdio.h>
96         #define KDBG_WAIT_READY()      do { /*nop*/ } while(0)
97         #define KDBG_WRITE_CHAR(c)     putchar((c))
98         #define KDBG_MASK_IRQ(old)     do { (void)(old); } while(0)
99         #define KDBG_RESTORE_IRQ(old)  do { /*nop*/ } while(0)
100         typedef char kdbg_irqsave_t; /* unused */
101 #elif CPU_I196
102         #include "Util196.h"
103         #define KDBG_WAIT_READY()      do {} while (!(SP_STAT & (SPSF_TX_EMPTY | SPSF_TX_INT)))
104         #define KDBG_WRITE_CHAR(c)     do { SBUF = (c); } while(0)
105         #define KDBG_MASK_IRQ(old) \
106                 do { \
107                         (old) = INT_MASK1 & INT1F_TI; \
108                         INT_MASK1 &= ~INT1F_TI; \
109                 } while(0)
110         #define KDBG_RESTORE_IRQ(old)  do { INT_MASK1 |= (old); }
111         typedef uint16_t kdbg_irqsave_t; /* FIXME: unconfirmed */
112 #elif CPU_AVR
113         #include <avr/io.h>
114
115         #if CONFIG_KDEBUG_PORT == 0
116
117                 /*
118                  * Support for special bus policies or external transceivers
119                  * on UART0 (to be overridden in "hw.h").
120                  */
121                 #ifndef KDBG_UART0_BUS_INIT
122                 #define KDBG_UART0_BUS_INIT  do {} while (0)
123                 #endif
124                 #ifndef KDBG_UART0_BUS_RX
125                 #define KDBG_UART0_BUS_RX    do {} while (0)
126                 #endif
127                 #ifndef KDBG_UART0_BUS_TX
128                 #define KDBG_UART0_BUS_TX    do {} while (0)
129                 #endif
130
131                 #if CPU_AVR_ATMEGA64
132                         #define UCR UCSR0B
133                         #define UDR UDR0
134                         #define USR UCSR0A
135                 #elif CPU_AVR_ATMEGA8
136                         #define UCR UCSRB
137                         #define USR UCSRA
138                 #else
139                         #error Unknown CPU
140                 #endif
141
142                 #define KDBG_WAIT_READY()     do { loop_until_bit_is_set(USR, UDRE); } while(0)
143                 #define KDBG_WAIT_TXDONE()    do { loop_until_bit_is_set(USR, TXC); } while(0)
144
145                 /*
146                  * We must clear the TXC flag before sending a new character to allow
147                  * KDBG_WAIT_TXDONE() to work properly.
148                  *
149                  * BUG: if KDBG_WRITE_CHAR() is called after the TXC flag is set by hardware,
150                  * a new TXC could be generated after we've cleared it and before the new
151                  * character is written to UDR.  On a 485 bus, the transceiver will be put
152                  * in RX mode while still transmitting the last char.
153                  */
154                 #define KDBG_WRITE_CHAR(c)    do { USR |= BV(TXC); UDR = (c); } while(0)
155
156                 #define KDBG_MASK_IRQ(old)    do { \
157                         (old) = UCR; \
158                         UCR |= BV(TXEN); \
159                         UCR &= ~(BV(TXCIE) | BV(UDRIE)); \
160                         KDBG_UART0_BUS_TX; \
161                 } while(0)
162
163                 #define KDBG_RESTORE_IRQ(old) do { \
164                         KDBG_WAIT_TXDONE(); \
165                         KDBG_UART0_BUS_RX; \
166                         UCR = (old); \
167                 } while(0)
168
169                 typedef uint8_t kdbg_irqsave_t;
170
171         #elif CONFIG_KDEBUG_PORT == 1
172
173                 /*
174                  * Support for special bus policies or external transceivers
175                  * on UART1 (to be overridden in "hw.h").
176                  */
177                 #ifndef KDBG_UART1_BUS_INIT
178                 #define KDBG_UART1_BUS_INIT  do {} while (0)
179                 #endif
180                 #ifndef KDBG_UART1_BUS_RX
181                 #define KDBG_UART1_BUS_RX    do {} while (0)
182                 #endif
183                 #ifndef KDBG_UART1_BUS_TX
184                 #define KDBG_UART1_BUS_TX    do {} while (0)
185                 #endif
186
187                 #define KDBG_WAIT_READY()     do { loop_until_bit_is_set(UCSR1A, UDRE); } while(0)
188                 #define KDBG_WAIT_TXDONE()    do { loop_until_bit_is_set(UCSR1A, TXC); } while(0)
189                 #define KDBG_WRITE_CHAR(c)    do { UCSR1A |= BV(TXC); UDR1 = (c); } while(0)
190
191                 #define KDBG_MASK_IRQ(old)    do { \
192                         (old) = UCSR1B; \
193                         UCSR1B |= BV(TXEN); \
194                         UCSR1B &= ~(BV(TXCIE) | BV(UDRIE)); \
195                         KDBG_UART1_BUS_TX; \
196                 } while(0)
197
198                 #define KDBG_RESTORE_IRQ(old) do { \
199                         KDBG_WAIT_TXDONE(); \
200                         KDBG_UART1_BUS_RX; \
201                         UCSR1B = (old); \
202                 } while(0)
203
204                 typedef uint8_t kdbg_irqsave_t;
205         #else
206                 #error CONFIG_KDEBUG_PORT should be either 0 or 1
207         #endif
208 #elif defined(__MWERKS__) && CPU_DSP56K
209         /* Debugging go through the JTAG interface. The MSL library already
210            implements the console I/O correctly. */
211         #include <stdio.h>
212         #define KDBG_WAIT_READY()         do { } while (0)
213         #define KDBG_WRITE_CHAR(c)        __put_char(c, stdout)
214         #define KDBG_MASK_IRQ(old)        do { (void)(old); } while (0)
215         #define KDBG_RESTORE_IRQ(old)     do { (void)(old); } while (0)
216         typedef uint8_t kdbg_irqsave_t; /* unused */
217 #else
218         #error Unknown architecture
219 #endif
220
221
222 void kdbg_init(void)
223 {
224 #if CPU_I196
225
226         /* Set serial port for 19200bps 8N1 */
227         INT_MASK1 &= ~(INT1F_TI | INT1F_RI);
228         SP_CON = SPCF_RECEIVE_ENABLE | SPCF_MODE1;
229         ioc1_img |= IOC1F_TXD_SEL | IOC1F_EXTINT_SRC;
230         IOC1 = ioc1_img;
231         BAUD_RATE = 0x33;
232         BAUD_RATE = 0x80;
233
234 #elif CPU_AVR
235
236         /* Compute the baud rate */
237         uint16_t period = (((CLOCK_FREQ / 16UL) + (CONFIG_KDEBUG_BAUDRATE / 2)) / CONFIG_KDEBUG_BAUDRATE) - 1;
238
239         #if CPU_AVR_ATMEGA64
240                 #if CONFIG_KDEBUG_PORT == 0
241                         UBRR0H = (uint8_t)(period>>8);
242                         UBRR0L = (uint8_t)period;
243                         KDBG_UART0_BUS_INIT;
244                 #elif CONFIG_KDEBUG_PORT == 1
245                         UBRR1H = (uint8_t)(period>>8);
246                         UBRR1L = (uint8_t)period;
247                         KDBG_UART1_BUS_INIT;
248                 #else
249                         #error CONFIG_KDEBUG_PORT must be either 0 or 1
250                 #endif
251         #elif CPU_AVR_ATMEGA8
252                 UBRRH = (uint8_t)(period>>8);
253                 UBRRL = (uint8_t)period;
254         #elif CPU_AVR_ATMEGA103
255                 UBRR = (uint8_t)period;
256                 KDBG_UART0_BUS_INIT;
257         #else
258                 #error Unknown CPU
259         #endif
260
261 #endif /* !CPU_I196 && !CPU_AVR */
262
263         kputs("\n\n*** DBG START ***\n");
264 }
265
266
267 /*!
268  * Output one character to the debug console
269  */
270 static void __kputchar(char c, UNUSED_ARG(void *, unused))
271 {
272         /* Poll while serial buffer is still busy */
273         KDBG_WAIT_READY();
274
275         /* Send '\n' as '\r\n' for dumb terminals */
276         if (c == '\n')
277         {
278                 KDBG_WRITE_CHAR('\r');
279                 KDBG_WAIT_READY();
280         }
281
282         KDBG_WRITE_CHAR(c);
283 }
284
285
286 void kputchar(char c)
287 {
288         /* Mask serial TX intr */
289         kdbg_irqsave_t irqsave;
290         KDBG_MASK_IRQ(irqsave);
291
292         __kputchar(c, 0);
293
294         /* Restore serial TX intr */
295         KDBG_RESTORE_IRQ(irqsave);
296 }
297
298
299 void PGM_FUNC(kvprintf)(const char * PGM_ATTR fmt, va_list ap)
300 {
301 #if CONFIG_PRINTF
302         /* Mask serial TX intr */
303         kdbg_irqsave_t irqsave;
304         KDBG_MASK_IRQ(irqsave);
305
306         PGM_FUNC(_formatted_write)(fmt, __kputchar, 0, ap);
307
308         /* Restore serial TX intr */
309         KDBG_RESTORE_IRQ(irqsave);
310 #else
311         /* A better than nothing printf() surrogate. */
312         PGM_FUNC(kputs)(fmt);
313 #endif /* CONFIG_PRINTF */
314 }
315
316 void PGM_FUNC(kprintf)(const char * PGM_ATTR fmt, ...)
317 {
318         va_list ap;
319
320         va_start(ap, fmt);
321         PGM_FUNC(kvprintf)(fmt, ap);
322         va_end(ap);
323 }
324
325 void PGM_FUNC(kputs)(const char * PGM_ATTR str)
326 {
327         char c;
328
329         /* Mask serial TX intr */
330         kdbg_irqsave_t irqsave;
331         KDBG_MASK_IRQ(irqsave);
332
333         while ((c = PGM_READ_CHAR(str++)))
334                 __kputchar(c, 0);
335
336         KDBG_RESTORE_IRQ(irqsave);
337 }
338
339
340 /*!
341  * Cheap function to print small integers without using printf().
342  */
343 int kputnum(int num)
344 {
345         int output_len = 0;
346         int divisor = 10000;
347         int digit;
348
349         do
350         {
351                 digit = num / divisor;
352                 num %= divisor;
353
354                 if (digit || output_len || divisor == 1)
355                 {
356                         kputchar(digit + '0');
357                         ++output_len;
358                 }
359         }
360         while (divisor /= 10);
361
362         return output_len;
363 }
364
365
366 static void klocation(const char * PGM_ATTR file, int line)
367 {
368         PGM_FUNC(kputs)(file);
369         kputchar(':');
370         kputnum(line);
371         PGM_FUNC(kputs)(PSTR(": "));
372 }
373
374 int PGM_FUNC(__assert)(const char * PGM_ATTR cond, const char * PGM_ATTR file, int line)
375 {
376         klocation(file, line);
377         PGM_FUNC(kputs)(PSTR("Assertion failed: "));
378         PGM_FUNC(kputs)(cond);
379         kputchar('\n');
380         return 1;
381 }
382
383 void PGM_FUNC(__trace)(const char * PGM_ATTR name)
384 {
385         PGM_FUNC(kputs)(name);
386         PGM_FUNC(kputs)(PSTR("()\n"));
387 }
388
389 void PGM_FUNC(__tracemsg)(const char * PGM_ATTR name, const char * PGM_ATTR fmt, ...)
390 {
391         va_list ap;
392
393         PGM_FUNC(kputs)(name);
394         PGM_FUNC(kputs)(PSTR("(): "));
395         va_start(ap, fmt);
396         PGM_FUNC(kvprintf)(fmt, ap);
397         va_end(ap);
398 }
399
400 int PGM_FUNC(__invalid_ptr)(void *value, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
401 {
402         klocation(file, line);
403         PGM_FUNC(kputs)(PSTR("Invalid ptr: "));
404         PGM_FUNC(kputs)(name);
405         #if CONFIG_PRINTF
406                 PGM_FUNC(kprintf)(PSTR(" = 0x%x\n"), value);
407         #else
408                 (void)value;
409                 kputchar('\n');
410         #endif
411         return 1;
412 }
413
414
415 void __init_wall(long *wall, int size)
416 {
417         while(size--)
418                 *wall++ = WALL_VALUE;
419 }
420
421
422 int PGM_FUNC(__check_wall)(long *wall, int size, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
423 {
424         int i, fail = 0;
425
426         for (i = 0; i < size; i++)
427         {
428                 if (wall[i] != WALL_VALUE)
429                 {
430                         klocation(file, line);
431                         PGM_FUNC(kputs)(PSTR("Wall broken: "));
432                         PGM_FUNC(kputs)(name);
433                         #if CONFIG_PRINTF
434                                 PGM_FUNC(kprintf)(PSTR("[%d] (0x%p) = 0x%lx\n"), i, wall + i, wall[i]);
435                         #else
436                                 kputchar('\n');
437                         #endif
438                         fail = 1;
439                 }
440         }
441
442         return fail;
443 }
444
445
446 #if CONFIG_PRINTF
447
448 /*!
449  * Dump binary data in hex
450  */
451 void kdump(const void *_buf, size_t len)
452 {
453         const unsigned char *buf = (const unsigned char *)_buf;
454
455         while (len--)
456                 kprintf("%02X", *buf++);
457         kputchar('\n');
458 }
459
460 #endif /* CONFIG_PRINTF */
461
462 #endif /* _DEBUG */