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