kputchar(): New public function; Add missing dummy inlines for \!_DEBUG.
[bertos.git] / drv / kdebug.c
1 /*!
2  * \file
3  * <!--
4  * Copyright 2003,2004 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 devlib/README for information.
7  * -->
8  *
9  * \brief General pourpose debug functions.
10  *
11  * \version $Id$
12  *
13  * \author Bernardo Innocenti <bernie@develer.com>
14  * \author Stefano Fedrigo <aleph@develer.com>
15  */
16
17 /*
18  * $Log$
19  * Revision 1.11  2004/08/24 16:19:08  bernie
20  * kputchar(): New public function; Add missing dummy inlines for \!_DEBUG.
21  *
22  * Revision 1.10  2004/08/04 15:57:50  rasky
23  * Cambiata la putchar per kdebug per DSP56k: la nuova funzione e' quella piu' a basso livello (assembly)
24  *
25  * Revision 1.9  2004/08/02 20:20:29  aleph
26  * Merge from project_ks
27  *
28  * Revision 1.8  2004/07/30 14:26:33  rasky
29  * Semplificato l'output dell'ASSERT
30  * Aggiunta ASSERT2 con stringa di help opzionalmente disattivabile
31  *
32  * Revision 1.7  2004/07/30 14:15:53  rasky
33  * Nuovo supporto unificato per detect della CPU
34  *
35  * Revision 1.6  2004/07/18 21:49:28  bernie
36  * Add ATmega8 support.
37  *
38  * Revision 1.5  2004/06/27 15:20:26  aleph
39  * Change UNUSED() macro to accept two arguments: type and name;
40  * Add macro GNUC_PREREQ to detect GCC version during build;
41  * Some spacing cleanups and typo fix
42  *
43  * Revision 1.4  2004/06/06 18:09:51  bernie
44  * Import DSP56800 changes; Print broken wall bricks in hex.
45  *
46  * Revision 1.3  2004/06/03 11:27:09  bernie
47  * Add dual-license information.
48  *
49  * Revision 1.2  2004/05/23 18:21:53  bernie
50  * Trim CVS logs and cleanup header info.
51  *
52  */
53
54 #include "kdebug.h"
55 #include "hw.h"
56 #include "config.h"
57
58 #include <mware/formatwr.h> /* for _formatted_write() */
59
60 #ifdef _DEBUG
61
62 #if defined(_EMUL)
63         #include <stdio.h>
64         #define KDBG_WAIT_READY()      do { /*nop*/ } while(0)
65         #define KDBG_WRITE_CHAR(c)     putchar((c))
66         #define KDBG_MASK_IRQ(old)     do { (void)(old); } while(0)
67         #define KDBG_RESTORE_IRQ()     do { /*nop*/ } while(0)
68         typedef kdbg_irqsave_t         char; /* unused */
69 #elif CPU_I196
70         #include "Util196.h"
71         #define KDBG_WAIT_READY()      do {} while (!(SP_STAT & (SPSF_TX_EMPTY | SPSF_TX_INT)))
72         #define KDBG_WRITE_CHAR(c)     do { SBUF = (c); } while(0)
73         #define KDBG_MASK_IRQ(old) \
74                 do { \
75                         (old) = INT_MASK1 & INT1F_TI; \
76                         INT_MASK1 &= ~INT1F_TI; \
77                 } while(0)
78         #define KDBG_RESTORE_IRQ(old)  do { INT_MASK1 |= (old); }
79         typedef kdbg_irqsave_t         uint16_t; /* FIXME: unconfirmed */
80 #elif CPU_AVR
81         #include <avr/io.h>
82         #if CONFIG_KDEBUG_PORT == 0
83
84                 /* External 485 transceiver on UART0 (to be overridden in "hw.h").  */
85                 #if !defined(SER_UART0_485_INIT)
86                         #if defined(SER_UART0_485_RX) || defined(SER_UART0_485_TX)
87                                 #error SER_UART0_485_INIT, SER_UART0_485_RX and SER_UART0_485_TX must be defined together
88                         #endif
89                         #define SER_UART0_485_INIT  do {} while (0)
90                         #define SER_UART0_485_TX    do {} while (0)
91                         #define SER_UART0_485_RX    do {} while (0)
92                 #elif !defined(SER_UART0_485_RX) || !defined(SER_UART0_485_TX)
93                         #error SER_UART0_485_INIT, SER_UART0_485_RX and SER_UART0_485_TX must be defined together
94                 #endif
95
96                 #if defined(__AVR_ATmega64__)
97                         #define UCR UCSR0B
98                         #define UDR UDR0
99                         #define USR UCSR0A
100                 #elif defined(__AVR_ATmega8__)
101                         #define UCR UCSRB
102                         #define USR UCSRA
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                  * BUG: before sending a new character the TXC flag is cleared to allow
109                  * KDBG_WAIT_TXDONE() to work properly, but, if KDBG_WRITE_CHAR() is called
110                  * after the RXC flag is set by hardware, a new TXC could be generated
111                  * after we clear it and before the new character is put in UDR. In this
112                  * case if a 485 is used the transceiver will be put in RX mode while
113                  * transmitting the last char.
114                  */
115                 #define KDBG_WRITE_CHAR(c)    do { USR |= BV(TXC); UDR = (c); } while(0)
116
117                 #define KDBG_MASK_IRQ(old)    do { \
118                         SER_UART0_485_TX; \
119                         (old) = UCR; \
120                         UCR |= BV(TXEN); \
121                         UCR &= ~(BV(TXCIE) | BV(UDRIE)); \
122                 } while(0)
123
124                 #define KDBG_RESTORE_IRQ(old) do { \
125                         KDBG_WAIT_TXDONE(); \
126                         SER_UART0_485_RX; \
127                         UCR = (old); \
128                 } while(0)
129
130                 typedef kdbg_irqsave_t uint8_t;
131
132         #elif CONFIG_KDEBUG_PORT == 1
133
134                 /* External 485 transceiver on UART1 (to be overridden in "hw.h").  */
135                 #ifndef SER_UART1_485_INIT
136                         #if defined(SER_UART1_485_RX) || defined(SER_UART1_485_TX)
137                                 #error SER_UART1_485_INIT, SER_UART1_485_RX and SER_UART1_485_TX must be defined together
138                         #endif
139                         #define SER_UART1_485_INIT  do {} while (0)
140                         #define SER_UART1_485_TX    do {} while (0)
141                         #define SER_UART1_485_RX    do {} while (0)
142                 #elif !defined(SER_UART1_485_RX) || !defined(SER_UART1_485_TX)
143                         #error SER_UART1_485_INIT, SER_UART1_485_RX and SER_UART1_485_TX must be defined together
144                 #endif
145
146                 #define KDBG_WAIT_READY()     do { loop_until_bit_is_set(UCSR1A, UDRE); } while(0)
147                 #define KDBG_WAIT_TXDONE()    do { loop_until_bit_is_set(UCSR1A, TXC); } while(0)
148                 #define KDBG_WRITE_CHAR(c)    do { UCSR1A |= BV(TXC); UDR1 = (c); } while(0)
149
150                 #define KDBG_MASK_IRQ(old)    do { \
151                         SER_UART1_485_TX; \
152                         (old) = UCSR1B; \
153                         UCSR1B |= BV(TXEN); \
154                         UCSR1B &= ~(BV(TXCIE) | BV(UDRIE)); \
155                 } while(0)
156
157                 #define KDBG_RESTORE_IRQ(old) do { \
158                         KDBG_WAIT_TXDONE(); \
159                         SER_UART1_485_RX; \
160                         UCSR1B = (old); \
161                 } while(0)
162
163                 typedef kdbg_irqsave_t uint8_t;
164         #else
165                 #error CONFIG_KDEBUG_PORT should be either 0 or 1
166         #endif
167 #elif defined(__MWERKS__) && CPU_DSP56K
168         /* Debugging go through the JTAG interface. The MSL library already
169            implements the console I/O correctly. */
170         #include <stdio.h>
171         #define KDBG_WAIT_READY()         do { } while (0)
172         #define KDBG_WRITE_CHAR(c)        __put_char(c, stdout)
173         #define KDBG_MASK_IRQ(old)        do { (void)(old); } while (0)
174         #define KDBG_RESTORE_IRQ(old)     do { (void)(old); } while (0)
175         typedef kdbg_irqsave_t            uint8_t; /* unused */
176 #else
177         #error Unknown architecture
178 #endif
179
180
181 void kdbg_init(void)
182 {
183 #if CPU_I196
184
185         /* Set serial port for 19200bps 8N1 */
186         INT_MASK1 &= ~(INT1F_TI | INT1F_RI);
187         SP_CON = SPCF_RECEIVE_ENABLE | SPCF_MODE1;
188         ioc1_img |= IOC1F_TXD_SEL | IOC1F_EXTINT_SRC;
189         IOC1 = ioc1_img;
190         BAUD_RATE = 0x33;
191         BAUD_RATE = 0x80;
192
193 #elif CPU_AVR
194
195         /* Compute the baud rate */
196         uint16_t period = (((CLOCK_FREQ / 16UL) + (CONFIG_KDEBUG_BAUDRATE / 2)) / CONFIG_KDEBUG_BAUDRATE) - 1;
197
198         #if defined(__AVR_ATmega64__)
199                 #if CONFIG_KDEBUG_PORT == 0
200                         UBRR0H = (uint8_t)(period>>8);
201                         UBRR0L = (uint8_t)period;
202                         SER_UART0_485_INIT;
203                 #elif CONFIG_KDEBUG_PORT == 1
204                         UBRR1H = (uint8_t)(period>>8);
205                         UBRR1L = (uint8_t)period;
206                         SER_UART1_485_INIT;
207                 #else
208                         #error CONFIG_KDEBUG_PORT must be either 0 or 1
209                 #endif
210         #elif defined(__AVR_ATmega8__)
211                 UBRRH = (uint8_t)(period>>8);
212                 UBRRL = (uint8_t)period;
213         #elif defined(__AVR_ATmega103__)
214                 UBRR = (uint8_t)period;
215                 SER_UART0_485_INIT;
216         #else
217                 #error Unknown arch
218         #endif
219
220 #endif /* !CPU_I196 && !CPU_AVR */
221
222         kputs("\n\n*** DBG START ***\n");
223 }
224
225
226 /*!
227  * Output one character to the debug console
228  */
229 static void __kputchar(char c, UNUSED(void *, unused))
230 {
231         /* Poll while serial buffer is still busy */
232         KDBG_WAIT_READY();
233
234         /* Send '\n' as '\r\n' for dumb terminals */
235         if (c == '\n')
236         {
237                 KDBG_WRITE_CHAR('\r');
238                 KDBG_WAIT_READY();
239         }
240
241         KDBG_WRITE_CHAR(c);
242 }
243
244
245 void kputchar(char c)
246 {
247         /* Mask serial TX intr */
248         kdbg_irqsave_t irqsave;
249         KDBG_MASK_IRQ(irqsave);
250
251         __kputchar(c, 0);
252
253         /* Restore serial TX intr */
254         KDBG_RESTORE_IRQ(irqsave);
255 }
256
257
258 void PGM_FUNC(kprintf)(const char * PGM_ATTR fmt, ...)
259 {
260         va_list ap;
261
262         /* Mask serial TX intr */
263         kdbg_irqsave_t irqsave;
264         KDBG_MASK_IRQ(irqsave);
265
266         va_start(ap, fmt);
267         PGM_FUNC(_formatted_write)(fmt, __kputchar, 0, ap);
268         va_end(ap);
269
270         /* Restore serial TX intr */
271         KDBG_RESTORE_IRQ(irqsave);
272 }
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 int PGM_FUNC(__assert)(const char * PGM_ATTR cond, const char *file, int line)
291 {
292         PGM_FUNC(kputs)(file);
293         PGM_FUNC(kprintf)(PSTR(":%d: Assertion failed: "), line);
294         PGM_FUNC(kputs)(cond);
295         PGM_FUNC(kputs)(PSTR("\n"));
296         return 1;
297 }
298
299
300 int PGM_FUNC(__invalid_ptr)(void *value, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
301 {
302         PGM_FUNC(kputs)(file);
303         PGM_FUNC(kprintf)(PSTR(":%d: Invalid pointer: "), line);
304         PGM_FUNC(kputs)(name);
305         PGM_FUNC(kprintf)(PSTR(" = 0x%x\n"), value);
306         return 1;
307 }
308
309
310 void __init_wall(long *wall, int size)
311 {
312         while(size--)
313                 *wall++ = WALL_VALUE;
314 }
315
316
317 int __check_wall(long *wall, int size, const char *name, const char *file, int line)
318 {
319         int i, fail = 0;
320
321         for (i = 0; i < size; i++)
322         {
323                 if (wall[i] != WALL_VALUE)
324                 {
325                         kprintf("%s:%d: Wall broken: %s[%d] (0x%p) = 0x%lx\n",
326                                 file, line, name, i, wall + i, wall[i]);
327                         fail = 1;
328                 }
329         }
330
331         return fail;
332 }
333
334
335 /*!
336  * Dump binary data in hex
337  */
338 void kdump(const void *_buf, size_t len)
339 {
340         const unsigned char *buf = (const unsigned char *)_buf;
341
342         while (len--)
343                 kprintf("%02X", *buf++);
344         kputs("\n");
345 }
346
347 #endif /* _DEBUG */