Cambiata la putchar per kdebug per DSP56k: la nuova funzione e' quella piu' a basso...
[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.10  2004/08/04 15:57:50  rasky
20  * Cambiata la putchar per kdebug per DSP56k: la nuova funzione e' quella piu' a basso livello (assembly)
21  *
22  * Revision 1.9  2004/08/02 20:20:29  aleph
23  * Merge from project_ks
24  *
25  * Revision 1.8  2004/07/30 14:26:33  rasky
26  * Semplificato l'output dell'ASSERT
27  * Aggiunta ASSERT2 con stringa di help opzionalmente disattivabile
28  *
29  * Revision 1.7  2004/07/30 14:15:53  rasky
30  * Nuovo supporto unificato per detect della CPU
31  *
32  * Revision 1.6  2004/07/18 21:49:28  bernie
33  * Add ATmega8 support.
34  *
35  * Revision 1.5  2004/06/27 15:20:26  aleph
36  * Change UNUSED() macro to accept two arguments: type and name;
37  * Add macro GNUC_PREREQ to detect GCC version during build;
38  * Some spacing cleanups and typo fix
39  *
40  * Revision 1.4  2004/06/06 18:09:51  bernie
41  * Import DSP56800 changes; Print broken wall bricks in hex.
42  *
43  * Revision 1.3  2004/06/03 11:27:09  bernie
44  * Add dual-license information.
45  *
46  * Revision 1.2  2004/05/23 18:21:53  bernie
47  * Trim CVS logs and cleanup header info.
48  *
49  */
50
51 #include "kdebug.h"
52 #include "hw.h"
53 #include "config.h"
54
55 #include <mware/formatwr.h> /* for _formatted_write() */
56
57 #ifdef _DEBUG
58
59 #if defined(_EMUL)
60         #include <stdio.h>
61         #define KDBG_WAIT_READY()      do {/*nop*/} while(0)
62         #define KDBG_WRITE_CHAR(c)     putchar((c))
63         #define KDBG_MASK_IRQ(old)     do {/*nop*/} while(0)
64         #define KDBG_RESTORE_IRQ()     do {/*nop*/} while(0)
65 #elif CPU_I196
66         #include "Util196.h"
67         #define KDBG_WAIT_READY()      do {} while (!(SP_STAT & (SPSF_TX_EMPTY | SPSF_TX_INT)))
68         #define KDBG_WRITE_CHAR(c)     do { SBUF = (c); } while(0)
69         #define KDBG_MASK_IRQ(old) \
70                 do { \
71                         (old) = INT_MASK1 & INT1F_TI; \
72                         INT_MASK1 &= ~INT1F_TI; \
73                 } while(0)
74         #define KDBG_RESTORE_IRQ(old)  do { INT_MASK1 |= (old); }
75 #elif CPU_AVR
76         #include <avr/io.h>
77         #if CONFIG_KDEBUG_PORT == 0
78
79                 /* External 485 transceiver on UART0 (to be overridden in "hw.h").  */
80                 #if !defined(SER_UART0_485_INIT)
81                         #if defined(SER_UART0_485_RX) || defined(SER_UART0_485_TX)
82                                 #error SER_UART0_485_INIT, SER_UART0_485_RX and SER_UART0_485_TX must be defined together
83                         #endif
84                         #define SER_UART0_485_INIT  do {} while (0)
85                         #define SER_UART0_485_TX    do {} while (0)
86                         #define SER_UART0_485_RX    do {} while (0)
87                 #elif !defined(SER_UART0_485_RX) || !defined(SER_UART0_485_TX)
88                         #error SER_UART0_485_INIT, SER_UART0_485_RX and SER_UART0_485_TX must be defined together
89                 #endif
90
91                 #if defined(__AVR_ATmega64__)
92                         #define UCR UCSR0B
93                         #define UDR UDR0
94                         #define USR UCSR0A
95                 #elif defined(__AVR_ATmega8__)
96                         #define UCR UCSRB
97                         #define USR UCSRA
98                 #endif
99
100                 #define KDBG_WAIT_READY()     do { loop_until_bit_is_set(USR, UDRE); } while(0)
101                 #define KDBG_WAIT_TXDONE()    do { loop_until_bit_is_set(USR, TXC); } while(0)
102                 /*
103                  * BUG: before sending a new character the TXC flag is cleared to allow
104                  * KDBG_WAIT_TXDONE() to work properly, but, if KDBG_WRITE_CHAR() is called
105                  * after the RXC flag is set by hardware, a new TXC could be generated
106                  * after we clear it and before the new character is put in UDR. In this
107                  * case if a 485 is used the transceiver will be put in RX mode while
108                  * transmitting the last char.
109                  */
110                 #define KDBG_WRITE_CHAR(c)    do { USR |= BV(TXC); UDR = (c); } while(0)
111
112                 #define KDBG_MASK_IRQ(old)    do { \
113                         SER_UART0_485_TX; \
114                         (old) = UCR; \
115                         UCR |= BV(TXEN); \
116                         UCR &= ~(BV(TXCIE) | BV(UDRIE)); \
117                 } while(0)
118
119                 #define KDBG_RESTORE_IRQ(old) do { \
120                         KDBG_WAIT_TXDONE(); \
121                         SER_UART0_485_RX; \
122                         UCR = (old); \
123                 } while(0)
124
125         #elif CONFIG_KDEBUG_PORT == 1
126
127                 /* External 485 transceiver on UART1 (to be overridden in "hw.h").  */
128                 #ifndef SER_UART1_485_INIT
129                         #if defined(SER_UART1_485_RX) || defined(SER_UART1_485_TX)
130                                 #error SER_UART1_485_INIT, SER_UART1_485_RX and SER_UART1_485_TX must be defined together
131                         #endif
132                         #define SER_UART1_485_INIT  do {} while (0)
133                         #define SER_UART1_485_TX    do {} while (0)
134                         #define SER_UART1_485_RX    do {} while (0)
135                 #elif !defined(SER_UART1_485_RX) || !defined(SER_UART1_485_TX)
136                         #error SER_UART1_485_INIT, SER_UART1_485_RX and SER_UART1_485_TX must be defined together
137                 #endif
138
139                 #define KDBG_WAIT_READY()     do { loop_until_bit_is_set(UCSR1A, UDRE); } while(0)
140                 #define KDBG_WAIT_TXDONE()    do { loop_until_bit_is_set(UCSR1A, TXC); } while(0)
141                 #define KDBG_WRITE_CHAR(c)    do { UCSR1A |= BV(TXC); UDR1 = (c); } while(0)
142
143                 #define KDBG_MASK_IRQ(old)    do { \
144                         SER_UART1_485_TX; \
145                         (old) = UCSR1B; \
146                         UCSR1B |= BV(TXEN); \
147                         UCSR1B &= ~(BV(TXCIE) | BV(UDRIE)); \
148                 } while(0)
149
150                 #define KDBG_RESTORE_IRQ(old) do { \
151                         KDBG_WAIT_TXDONE(); \
152                         SER_UART1_485_RX; \
153                         UCSR1B = (old); \
154                 } while(0)
155
156         #else
157                 #error CONFIG_KDEBUG_PORT should be either 0 or 1
158         #endif
159 #elif defined(__MWERKS__) && CPU_DSP56K
160         /* Debugging go through the JTAG interface. The MSL library already
161            implements the console I/O correctly. */
162         #include <stdio.h>
163         #define KDBG_WAIT_READY()
164         #define KDBG_WRITE_CHAR(c)        __put_char(c, stdout)
165         #define KDBG_MASK_IRQ(old)
166         #define KDBG_RESTORE_IRQ(old)
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 defined(__AVR_ATmega64__)
190                 #if CONFIG_KDEBUG_PORT == 0
191                         UBRR0H = (uint8_t)(period>>8);
192                         UBRR0L = (uint8_t)period;
193                         SER_UART0_485_INIT;
194                 #elif CONFIG_KDEBUG_PORT == 1
195                         UBRR1H = (uint8_t)(period>>8);
196                         UBRR1L = (uint8_t)period;
197                         SER_UART1_485_INIT;
198                 #else
199                         #error CONFIG_KDEBUG_PORT must be either 0 or 1
200                 #endif
201         #elif defined(__AVR_ATmega8__)
202                 UBRRH = (uint8_t)(period>>8);
203                 UBRRL = (uint8_t)period;
204         #elif defined(__AVR_ATmega103__)
205                 UBRR = (uint8_t)period;
206                 SER_UART0_485_INIT;
207         #else
208                 #error Unknown arch
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(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 PGM_FUNC(kprintf)(const char * PGM_ATTR fmt, ...)
237 {
238         va_list ap;
239
240         /* Mask serial TX intr */
241         unsigned char irqsave;
242         KDBG_MASK_IRQ(irqsave);
243
244         va_start(ap, fmt);
245         PGM_FUNC(_formatted_write)(fmt, kputchar, 0, ap);
246         va_end(ap);
247
248         /* Restore serial TX intr */
249         KDBG_RESTORE_IRQ(irqsave);
250 }
251
252
253 void PGM_FUNC(kputs)(const char * PGM_ATTR str)
254 {
255         char c;
256
257         /* Mask serial TX intr */
258         unsigned char irqsave;
259         KDBG_MASK_IRQ(irqsave);
260
261         while ((c = PGM_READ_CHAR(str++)))
262                 kputchar(c, 0);
263
264         KDBG_RESTORE_IRQ(irqsave);
265 }
266
267
268 int PGM_FUNC(__assert)(const char * PGM_ATTR cond, const char *file, int line)
269 {
270         PGM_FUNC(kputs)(file);
271         PGM_FUNC(kprintf)(PSTR(":%d: Assertion failed: "), line);
272         PGM_FUNC(kputs)(cond);
273         PGM_FUNC(kputs)(PSTR("\n"));
274         return 1;
275 }
276
277
278 int PGM_FUNC(__invalid_ptr)(void *value, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
279 {
280         PGM_FUNC(kputs)(file);
281         PGM_FUNC(kprintf)(PSTR(":%d: Invalid pointer: "), line);
282         PGM_FUNC(kputs)(name);
283         PGM_FUNC(kprintf)(PSTR(" = 0x%x\n"), value);
284         return 1;
285 }
286
287
288 void __init_wall(long *wall, int size)
289 {
290         while(size--)
291                 *wall++ = WALL_VALUE;
292 }
293
294
295 int __check_wall(long *wall, int size, const char *name, const char *file, int line)
296 {
297         int i, fail = 0;
298
299         for (i = 0; i < size; i++)
300         {
301                 if (wall[i] != WALL_VALUE)
302                 {
303                         kprintf("%s:%d: Wall broken: %s[%d] (0x%p) = 0x%lx\n",
304                                 file, line, name, i, wall + i, wall[i]);
305                         fail = 1;
306                 }
307         }
308
309         return fail;
310 }
311
312
313 /*!
314  * Dump binary data in hex
315  */
316 void kdump(const void *_buf, size_t len)
317 {
318         const unsigned char *buf = (const unsigned char *)_buf;
319
320         while (len--)
321                 kprintf("%02X", *buf++);
322         kputs("\n");
323 }
324
325 #endif /* _DEBUG */