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