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