Add support for ATmega2560.
[bertos.git] / bertos / cpu / avr / drv / kdebug_avr.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2003, 2004, 2005, 2006, 2007 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 2000, 2001, 2002 Bernie Innocenti <bernie@codewiz.org>
31  *
32  * -->
33  *
34  * \brief AVR debug support (implementation).
35  *
36  * \author Bernie Innocenti <bernie@codewiz.org>
37  * \author Stefano Fedrigo <aleph@develer.com>
38  * \author Francesco Sacchi <batt@develer.com>
39  */
40
41 #include <hw/hw_cpufreq.h>     /* for CPU_FREQ */
42 #include "hw/hw_ser.h"     /* Required for bus macros overrides */
43
44 #include "cfg/cfg_debug.h"
45 #include <cfg/macros.h> /* for BV(), DIV_ROUND */
46
47 #include <cpu/types.h>
48 #include <cpu/attr.h>
49
50 #include <avr/io.h>
51
52 #if CONFIG_KDEBUG_PORT == 0
53
54         /*
55          * Support for special bus policies or external transceivers
56          * on UART0 (to be overridden in "hw/hw_ser.h").
57          *
58          * HACK: if we don't set TXEN, kdbg disables the transmitter
59          * after each output statement until the serial driver
60          * is initialized.  These glitches confuse the debug
61          * terminal that ends up printing some trash.
62          */
63         #ifndef KDBG_UART0_BUS_INIT
64         #define KDBG_UART0_BUS_INIT  do { \
65                         UCR = BV(TXEN0); \
66                 } while (0)
67         #endif
68         #ifndef KDBG_UART0_BUS_RX
69         #define KDBG_UART0_BUS_RX    do {} while (0)
70         #endif
71         #ifndef KDBG_UART0_BUS_TX
72         #define KDBG_UART0_BUS_TX    do {} while (0)
73         #endif
74
75         #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA1281 || CPU_AVR_ATMEGA1280 \
76             || CPU_AVR_ATMEGA168 || CPU_AVR_ATMEGA328P || CPU_AVR_ATMEGA2560
77                 #define UCR UCSR0B
78                 #define UDR UDR0
79                 #define USR UCSR0A
80         #elif CPU_AVR_ATMEGA8 || CPU_AVR_ATMEGA32
81                 #define UCR    UCSRB
82                 #define USR    UCSRA
83                 #define TXEN0  TXEN
84                 #define UDRE0  UDRE
85                 #define TXC0   TXC
86                 #define TXCIE0 TXCIE
87                 #define UDRIE0 UDRIE
88         #else
89                 #error Unknown CPU
90         #endif
91
92         #define KDBG_WAIT_READY()     do { loop_until_bit_is_set(USR, UDRE0); } while(0)
93         #define KDBG_WAIT_TXDONE()    do { loop_until_bit_is_set(USR, TXC0); } while(0)
94
95         /*
96          * We must clear the TXC flag before sending a new character to allow
97          * KDBG_WAIT_TXDONE() to work properly.
98          *
99          * BUG: if KDBG_WRITE_CHAR() is called after the TXC flag is set by hardware,
100          * a new TXC could be generated after we've cleared it and before the new
101          * character is written to UDR.  On a 485 bus, the transceiver will be put
102          * in RX mode while still transmitting the last char.
103          */
104         #define KDBG_WRITE_CHAR(c)    do { USR |= BV(TXC0); UDR = (c); } while(0)
105
106         #define KDBG_MASK_IRQ(old)    do { \
107                 (old) = UCR; \
108                 UCR |= BV(TXEN0); \
109                 UCR &= ~(BV(TXCIE0) | BV(UDRIE0)); \
110                 KDBG_UART0_BUS_TX; \
111         } while(0)
112
113         #define KDBG_RESTORE_IRQ(old) do { \
114                 KDBG_WAIT_TXDONE(); \
115                 KDBG_UART0_BUS_RX; \
116                 UCR = (old); \
117         } while(0)
118
119         typedef uint8_t kdbg_irqsave_t;
120
121 #elif CONFIG_KDEBUG_PORT == 1
122
123         /*
124          * Support for special bus policies or external transceivers
125          * on UART1 (to be overridden in "hw/hw_ser.h").
126          *
127          * HACK: if we don't set TXEN, kdbg disables the transmitter
128          * after each output statement until the serial driver
129          * is initialized.  These glitches confuse the debug
130          * terminal that ends up printing some trash.
131          */
132         #ifndef KDBG_UART1_BUS_INIT
133         #define KDBG_UART1_BUS_INIT  do { \
134                         UCSR1B = BV(TXEN1); \
135                 } while (0)
136         #endif
137         #ifndef KDBG_UART1_BUS_RX
138         #define KDBG_UART1_BUS_RX    do {} while (0)
139         #endif
140         #ifndef KDBG_UART1_BUS_TX
141         #define KDBG_UART1_BUS_TX    do {} while (0)
142         #endif
143
144         #define KDBG_WAIT_READY()     do { loop_until_bit_is_set(UCSR1A, UDRE1); } while(0)
145         #define KDBG_WAIT_TXDONE()    do { loop_until_bit_is_set(UCSR1A, TXC1); } while(0)
146         #define KDBG_WRITE_CHAR(c)    do { UCSR1A |= BV(TXC1); UDR1 = (c); } while(0)
147
148         #define KDBG_MASK_IRQ(old)    do { \
149                 (old) = UCSR1B; \
150                 UCSR1B |= BV(TXEN1); \
151                 UCSR1B &= ~(BV(TXCIE1) | BV(UDRIE1)); \
152                 KDBG_UART1_BUS_TX; \
153         } while(0)
154
155         #define KDBG_RESTORE_IRQ(old) do { \
156                 KDBG_WAIT_TXDONE(); \
157                 KDBG_UART1_BUS_RX; \
158                 UCSR1B = (old); \
159         } while(0)
160
161         typedef uint8_t kdbg_irqsave_t;
162
163 #elif CONFIG_KDEBUG_PORT == 2
164
165         /*
166          * Support for special bus policies or external transceivers
167          * on UART2 (to be overridden in "hw/hw_ser.h").
168          *
169          * HACK: if we don't set TXEN, kdbg disables the transmitter
170          * after each output statement until the serial driver
171          * is initialized.  These glitches confuse the debug
172          * terminal that ends up printing some trash.
173          */
174         #ifndef KDBG_UART2_BUS_INIT
175         #define KDBG_UART2_BUS_INIT  do { \
176                         UCSR2B = BV(TXEN2); \
177                 } while (0)
178         #endif
179         #ifndef KDBG_UART2_BUS_RX
180         #define KDBG_UART2_BUS_RX    do {} while (0)
181         #endif
182         #ifndef KDBG_UART2_BUS_TX
183         #define KDBG_UART2_BUS_TX    do {} while (0)
184         #endif
185
186         #define KDBG_WAIT_READY()     do { loop_until_bit_is_set(UCSR2A, UDRE2); } while(0)
187         #define KDBG_WAIT_TXDONE()    do { loop_until_bit_is_set(UCSR2A, TXC2); } while(0)
188         #define KDBG_WRITE_CHAR(c)    do { UCSR2A |= BV(TXC2); UDR2 = (c); } while(0)
189
190         #define KDBG_MASK_IRQ(old)    do { \
191                 (old) = UCSR2B; \
192                 UCSR2B |= BV(TXEN2); \
193                 UCSR2B &= ~(BV(TXCIE2) | BV(UDRIE2)); \
194                 KDBG_UART2_BUS_TX; \
195         } while(0)
196
197         #define KDBG_RESTORE_IRQ(old) do { \
198                 KDBG_WAIT_TXDONE(); \
199                 KDBG_UART2_BUS_RX; \
200                 UCSR2B = (old); \
201         } while(0)
202
203         typedef uint8_t kdbg_irqsave_t;
204
205 #elif CONFIG_KDEBUG_PORT == 3
206
207         /*
208          * Support for special bus policies or external transceivers
209          * on UART3 (to be overridden in "hw/hw_ser.h").
210          *
211          * HACK: if we don't set TXEN, kdbg disables the transmitter
212          * after each output statement until the serial driver
213          * is initialized.  These glitches confuse the debug
214          * terminal that ends up printing some trash.
215          */
216         #ifndef KDBG_UART3_BUS_INIT
217         #define KDBG_UART3_BUS_INIT  do { \
218                         UCSR3B = BV(TXEN3); \
219                 } while (0)
220         #endif
221         #ifndef KDBG_UART3_BUS_RX
222         #define KDBG_UART3_BUS_RX    do {} while (0)
223         #endif
224         #ifndef KDBG_UART3_BUS_TX
225         #define KDBG_UART3_BUS_TX    do {} while (0)
226         #endif
227
228         #define KDBG_WAIT_READY()     do { loop_until_bit_is_set(UCSR3A, UDRE3); } while(0)
229         #define KDBG_WAIT_TXDONE()    do { loop_until_bit_is_set(UCSR3A, TXC3); } while(0)
230         #define KDBG_WRITE_CHAR(c)    do { UCSR3A |= BV(TXC3); UDR3 = (c); } while(0)
231
232         #define KDBG_MASK_IRQ(old)    do { \
233                 (old) = UCSR3B; \
234                 UCSR3B |= BV(TXEN3); \
235                 UCSR3B &= ~(BV(TXCIE3) | BV(UDRIE3)); \
236                 KDBG_UART3_BUS_TX; \
237         } while(0)
238
239         #define KDBG_RESTORE_IRQ(old) do { \
240                 KDBG_WAIT_TXDONE(); \
241                 KDBG_UART3_BUS_RX; \
242                 UCSR3B = (old); \
243         } while(0)
244
245         typedef uint8_t kdbg_irqsave_t;
246
247
248 /*
249  * Special debug port for BitBanged Serial see below for details...
250  */
251 #elif CONFIG_KDEBUG_PORT == 666
252         #include "hw/hw_ser.h"
253         #define KDBG_WAIT_READY()      do { /*nop*/ } while(0)
254         #define KDBG_WRITE_CHAR(c)     _kdebug_bitbang_putchar((c))
255         #define KDBG_MASK_IRQ(old)     do { IRQ_SAVE_DISABLE((old)); } while(0)
256         #define KDBG_RESTORE_IRQ(old)  do { IRQ_RESTORE((old)); } while(0)
257         typedef cpu_flags_t kdbg_irqsave_t;
258
259         #define KDBG_DELAY (((CPU_FREQ + CONFIG_KDEBUG_BAUDRATE / 2) / CONFIG_KDEBUG_BAUDRATE) + 7) / 14
260
261         static void _kdebug_bitbang_delay(void)
262         {
263                 unsigned long i;
264
265                 for (i = 0; i < KDBG_DELAY; i++)
266                 {
267                         NOP;
268                         NOP;
269                         NOP;
270                         NOP;
271                         NOP;
272                 }
273         }
274
275         /**
276          * Putchar for BITBANG serial debug console.
277          * Sometimes, we can't permit to use a whole serial for debugging purpose.
278          * Since debug console is in output only it is useful to use a single generic I/O pin for debug.
279          * This is achieved by this simple function, that shift out the data like a UART, but
280          * in software :)
281          * The only requirement is that SER_BITBANG_* macros will be defined somewhere (usually hw_ser.h)
282          * \note All interrupts are disabled during debug prints!
283          */
284         static void _kdebug_bitbang_putchar(char c)
285         {
286                 int i;
287                 uint16_t data = c;
288
289                 /* Add stop bit */
290                 data |= 0x0100;
291
292                 /* Add start bit*/
293                 data <<= 1;
294
295                 /* Shift out data */
296                 uint16_t shift = 1;
297                 for (i = 0; i < 10; i++)
298                 {
299                         if (data & shift)
300                                 SER_BITBANG_HIGH;
301                         else
302                                 SER_BITBANG_LOW;
303                         _kdebug_bitbang_delay();
304                         shift <<= 1;
305                 }
306         }
307 #else
308         #error CONFIG_KDEBUG_PORT should be either 0, 1, 2, 3 or 666
309 #endif
310
311
312 INLINE void kdbg_hw_init(void)
313 {
314         #if CONFIG_KDEBUG_PORT == 666
315                 SER_BITBANG_INIT;
316         #else /* CONFIG_KDEBUG_PORT != 666 */
317                 /* Compute the baud rate */
318                 uint16_t period = DIV_ROUND(CPU_FREQ / 16UL, CONFIG_KDEBUG_BAUDRATE) - 1;
319
320                 #if (CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA1281)
321                         #if CONFIG_KDEBUG_PORT == 0
322                                 UBRR0H = (uint8_t)(period>>8);
323                                 UBRR0L = (uint8_t)period;
324                                 KDBG_UART0_BUS_INIT;
325                         #elif CONFIG_KDEBUG_PORT == 1
326                                 UBRR1H = (uint8_t)(period>>8);
327                                 UBRR1L = (uint8_t)period;
328                                 KDBG_UART1_BUS_INIT;
329                         #else
330                                 #error CONFIG_KDEBUG_PORT must be either 0 or 1
331                         #endif
332
333                 #elif CPU_AVR_ATMEGA1280 || CPU_AVR_ATMEGA2560
334                         #if CONFIG_KDEBUG_PORT == 0
335                                 UBRR0H = (uint8_t)(period>>8);
336                                 UBRR0L = (uint8_t)period;
337                                 KDBG_UART0_BUS_INIT;
338                         #elif CONFIG_KDEBUG_PORT == 1
339                                 UBRR1H = (uint8_t)(period>>8);
340                                 UBRR1L = (uint8_t)period;
341                                 KDBG_UART1_BUS_INIT;
342                         #elif CONFIG_KDEBUG_PORT == 2
343                                 UBRR2H = (uint8_t)(period>>8);
344                                 UBRR2L = (uint8_t)period;
345                                 KDBG_UART2_BUS_INIT;
346                         #elif CONFIG_KDEBUG_PORT == 3
347                                 UBRR3H = (uint8_t)(period>>8);
348                                 UBRR3L = (uint8_t)period;
349                                 KDBG_UART3_BUS_INIT;
350                         #else
351                                 #error CONFIG_KDEBUG_PORT must be either 0 or 1 or 2 or 3
352                         #endif
353
354                 #elif CPU_AVR_ATMEGA168 || CPU_AVR_ATMEGA328P
355                         #if CONFIG_KDEBUG_PORT == 0
356                                 UBRR0H = (uint8_t)(period>>8);
357                                 UBRR0L = (uint8_t)period;
358                                 KDBG_UART0_BUS_INIT;
359                         #else
360                                 #error Only CONFIG_KDEBUG_PORT 0 is supported for this cpu
361                         #endif
362
363                 #elif CPU_AVR_ATMEGA8 || CPU_AVR_ATMEGA32
364                         #if CONFIG_KDEBUG_PORT == 0
365                                 UBRRH = (uint8_t)(period>>8);
366                                 UBRRL = (uint8_t)period;
367                                 KDBG_UART0_BUS_INIT;
368                         #else
369                                 #error Only CONFIG_KDEBUG_PORT 0 is supported for this cpu
370                         #endif
371                 #elif CPU_AVR_ATMEGA103
372                         #if CONFIG_KDEBUG_PORT == 0
373                                 UBRR = (uint8_t)period;
374                                 KDBG_UART0_BUS_INIT;
375                         #else
376                                 #error Only CONFIG_KDEBUG_PORT 0 is supported for this cpu
377                         #endif
378                 #else
379                         #error Unknown CPU
380                 #endif
381         #endif /* CONFIG_KDEBUG_PORT == 666 */
382 }
383