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