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