Semplificato l'output dell'ASSERT
[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.8  2004/07/30 14:26:33  rasky
20  * Semplificato l'output dell'ASSERT
21  * Aggiunta ASSERT2 con stringa di help opzionalmente disattivabile
22  *
23  * Revision 1.7  2004/07/30 14:15:53  rasky
24  * Nuovo supporto unificato per detect della CPU
25  *
26  * Revision 1.6  2004/07/18 21:49:28  bernie
27  * Add ATmega8 support.
28  *
29  * Revision 1.5  2004/06/27 15:20:26  aleph
30  * Change UNUSED() macro to accept two arguments: type and name;
31  * Add macro GNUC_PREREQ to detect GCC version during build;
32  * Some spacing cleanups and typo fix
33  *
34  * Revision 1.4  2004/06/06 18:09:51  bernie
35  * Import DSP56800 changes; Print broken wall bricks in hex.
36  *
37  * Revision 1.3  2004/06/03 11:27:09  bernie
38  * Add dual-license information.
39  *
40  * Revision 1.2  2004/05/23 18:21:53  bernie
41  * Trim CVS logs and cleanup header info.
42  *
43  */
44
45 #include "kdebug.h"
46 #include "hw.h"
47 #include "config.h"
48
49 #include <mware/formatwr.h> /* for _formatted_write() */
50
51 #ifdef _DEBUG
52
53 #if defined(_EMUL)
54         #include <stdio.h>
55         #define KDBG_WAIT_READY()      do {/*nop*/} while(0)
56         #define KDBG_WRITE_CHAR(c)     putchar((c))
57         #define KDBG_MASK_IRQ(old)     do {/*nop*/} while(0)
58         #define KDBG_RESTORE_IRQ()     do {/*nop*/} while(0)
59 #elif CPU_I196
60         #include "Util196.h"
61         #define KDBG_WAIT_READY()      do {} while (!(SP_STAT & (SPSF_TX_EMPTY | SPSF_TX_INT)))
62         #define KDBG_WRITE_CHAR(c)     do { SBUF = (c); } while(0)
63         #define KDBG_MASK_IRQ(old) \
64                 do { \
65                         (old) = INT_MASK1 & INT1F_TI; \
66                         INT_MASK1 &= ~INT1F_TI; \
67                 } while(0)
68         #define KDBG_RESTORE_IRQ(old)  do { INT_MASK1 |= (old); }
69 #elif CPU_AVR
70         #include <avr/io.h>
71         #if CONFIG_KDEBUG_PORT == 0
72                 #if defined(__AVR_ATmega64__)
73                         #define UCR UCSR0B
74                         #define UDR UDR0
75                         #define USR UCSR0A
76                 #elif defined(__AVR_ATmega8__)
77                         #define UCR UCSRB
78                         #define USR UCSRA
79                 #endif
80                 #define KDBG_WAIT_READY()     do { loop_until_bit_is_set(USR, UDRE); } while(0)
81                 #define KDBG_WRITE_CHAR(c)    do { UCR |= BV(TXEN); UDR = (c); } while(0)
82                 #define KDBG_MASK_IRQ(old)    do { (old) = UCR & BV(TXCIE); cbi(UCR, TXCIE); } while(0)
83                 #define KDBG_RESTORE_IRQ(old) do { UCR |= (old); } while(0)
84         #elif CONFIG_KDEBUG_PORT == 1
85                 #define KDBG_WAIT_READY()     do { loop_until_bit_is_set(UCSR1A, UDRE); } while(0)
86                 #define KDBG_WRITE_CHAR(c)    do { UCSR1B |= BV(TXEN); UDR1 = (c); } while(0)
87                 #define KDBG_MASK_IRQ(old)    do { (old) = UCSR1B & BV(TXCIE); cbi(UCSR1B, TXCIE); } while(0)
88                 #define KDBG_RESTORE_IRQ(old) do { UCSR1B |= (old); } while(0)
89         #else
90                 #error CONFIG_KDEBUG_PORT should be either 0 or 1
91         #endif
92 #elif defined(__MWERKS__) && CPU_DSP56K
93         /* Debugging go through the JTAG interface. The MSL library already
94            implements the console I/O correctly. */
95         #include <stdio.h>
96         #define KDBG_WAIT_READY()
97         #define KDBG_WRITE_CHAR(c)        do { char ch=c; fwrite(&ch,1,1,stdout); } while (0)
98         #define KDBG_MASK_IRQ(old)
99         #define KDBG_RESTORE_IRQ(old)
100 #else
101         #error Unknown architecture
102 #endif
103
104
105 void kdbg_init(void)
106 {
107 #if CPU_I196
108
109         /* Set serial port for 19200bps 8N1 */
110         INT_MASK1 &= ~(INT1F_TI | INT1F_RI);
111         SP_CON = SPCF_RECEIVE_ENABLE | SPCF_MODE1;
112         ioc1_img |= IOC1F_TXD_SEL | IOC1F_EXTINT_SRC;
113         IOC1 = ioc1_img;
114         BAUD_RATE = 0x33;
115         BAUD_RATE = 0x80;
116
117 #elif CPU_AVR
118
119         /* Compute the baud rate */
120         uint16_t period = (((CLOCK_FREQ / 16UL) + (CONFIG_KDEBUG_BAUDRATE / 2)) / CONFIG_KDEBUG_BAUDRATE) - 1;
121
122         #if defined(__AVR_ATmega64__)
123                 #if CONFIG_KDEBUG_PORT == 0
124                         UBRR0H = (uint8_t)(period>>8);
125                         UBRR0L = (uint8_t)period;
126                 #elif CONFIG_KDEBUG_PORT == 1
127                         UBRR1H = (uint8_t)(period>>8);
128                         UBRR1L = (uint8_t)period;
129                 #else
130                         #error CONFIG_KDEBUG_PORT must be either 0 or 1
131                 #endif
132         #elif defined(__AVR_ATmega8__)
133                 UBRRH = (uint8_t)(period>>8);
134                 UBRRL = (uint8_t)period;
135         #elif defined(__AVR_ATmega103__)
136                 UBRR = (uint8_t)period;
137         #else
138                 #error Unknown arch
139         #endif
140
141 #endif /* !CPU_I196 && !CPU_AVR */
142
143         kputs("\n\n*** DBG START ***\n");
144 }
145
146
147 /*!
148  * Output one character to the debug console
149  */
150 static void kputchar(char c, UNUSED(void *, unused))
151 {
152         /* Poll while serial buffer is still busy */
153         KDBG_WAIT_READY();
154
155         /* Send '\n' as '\r\n' for dumb terminals */
156         if (c == '\n')
157         {
158                 KDBG_WRITE_CHAR('\r');
159                 KDBG_WAIT_READY();
160         }
161
162         KDBG_WRITE_CHAR(c);
163 }
164
165
166 void PGM_FUNC(kprintf)(const char * PGM_ATTR fmt, ...)
167 {
168         va_list ap;
169
170         /* Mask serial TX intr */
171         unsigned char irqsave;
172         KDBG_MASK_IRQ(irqsave);
173
174         va_start(ap, fmt);
175         PGM_FUNC(_formatted_write)(fmt, kputchar, 0, ap);
176         va_end(ap);
177
178         /* Restore serial TX intr */
179         KDBG_RESTORE_IRQ(irqsave);
180 }
181
182
183 void PGM_FUNC(kputs)(const char * PGM_ATTR str)
184 {
185         char c;
186
187         /* Mask serial TX intr */
188         unsigned char irqsave;
189         KDBG_MASK_IRQ(irqsave);
190
191         while ((c = PGM_READ_CHAR(str++)))
192                 kputchar(c, 0);
193
194         KDBG_RESTORE_IRQ(irqsave);
195 }
196
197
198 int PGM_FUNC(__assert)(const char * PGM_ATTR cond, const char *file, int line)
199 {
200         PGM_FUNC(kputs)(file);
201         PGM_FUNC(kprintf)(PSTR(":%d: Assertion failed: "), line);
202         PGM_FUNC(kputs)(cond);
203         PGM_FUNC(kputs)(PSTR("\n"));
204         return 1;
205 }
206
207
208 int PGM_FUNC(__invalid_ptr)(void *value, const char * PGM_ATTR name, const char * PGM_ATTR file, int line)
209 {
210         PGM_FUNC(kputs)(file);
211         PGM_FUNC(kprintf)(PSTR(":%d: Invalid pointer: "), line);
212         PGM_FUNC(kputs)(name);
213         PGM_FUNC(kprintf)(PSTR(" = 0x%x\n"), value);
214         return 1;
215 }
216
217
218 void __init_wall(long *wall, int size)
219 {
220         while(size--)
221                 *wall++ = WALL_VALUE;
222 }
223
224
225 int __check_wall(long *wall, int size, const char *name, const char *file, int line)
226 {
227         int i, fail = 0;
228
229         for (i = 0; i < size; i++)
230         {
231                 if (wall[i] != WALL_VALUE)
232                 {
233                         kprintf("%s:%d: Wall broken: %s[%d] (0x%p) = 0x%lx\n",
234                                 file, line, name, i, wall + i, wall[i]);
235                         fail = 1;
236                 }
237         }
238
239         return fail;
240 }
241
242
243 /*!
244  * Dump binary data in hex
245  */
246 void kdump(const void *_buf, size_t len)
247 {
248         const unsigned char *buf = (const unsigned char *)_buf;
249
250         while (len--)
251                 kprintf("%02X", *buf++);
252         kputs("\n");
253 }
254
255 #endif /* _DEBUG */