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