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