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