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