Split debug by cpu type.
[bertos.git] / 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 Bernardo Innocenti <bernie@codewiz.org>
31  *
32  * -->
33  *
34  * \brief AVR debug support (implementation).
35  *
36  * \version $Id$
37  * \author Bernardo Innocenti <bernie@develer.com>
38  * \author Stefano Fedrigo <aleph@develer.com>
39  * \author Francesco Sacchi <batt@develer.com>
40  */
41
42 #include <cpu/cpu.h>
43 #include <cfg/macros.h> /* for BV() */
44 #include <appconfig.h>
45 #include <hw_cpu.h>     /* for CLOCK_FREQ */
46 #include <hw_ser.h>     /* Required for bus macros overrides */
47
48 #include <avr/io.h>
49
50 #if CONFIG_KDEBUG_PORT == 0
51
52         /*
53          * Support for special bus policies or external transceivers
54          * on UART0 (to be overridden in "hw_ser.h").
55          *
56          * HACK: if we don't set TXEN, kdbg disables the transmitter
57          * after each output statement until the serial driver
58          * is initialized.  These glitches confuse the debug
59          * terminal that ends up printing some trash.
60          */
61         #ifndef KDBG_UART0_BUS_INIT
62         #define KDBG_UART0_BUS_INIT  do { \
63                         UCSR0B = BV(TXEN0); \
64                 } while (0)
65         #endif
66         #ifndef KDBG_UART0_BUS_RX
67         #define KDBG_UART0_BUS_RX    do {} while (0)
68         #endif
69         #ifndef KDBG_UART0_BUS_TX
70         #define KDBG_UART0_BUS_TX    do {} while (0)
71         #endif
72
73         #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA168
74                 #define UCR UCSR0B
75                 #define UDR UDR0
76                 #define USR UCSR0A
77         #elif CPU_AVR_ATMEGA8
78                 #define UCR UCSRB
79                 #define USR UCSRA
80         #else
81                 #error Unknown CPU
82         #endif
83
84         #define KDBG_WAIT_READY()     do { loop_until_bit_is_set(USR, UDRE0); } while(0)
85         #define KDBG_WAIT_TXDONE()    do { loop_until_bit_is_set(USR, TXC0); } while(0)
86
87         /*
88          * We must clear the TXC flag before sending a new character to allow
89          * KDBG_WAIT_TXDONE() to work properly.
90          *
91          * BUG: if KDBG_WRITE_CHAR() is called after the TXC flag is set by hardware,
92          * a new TXC could be generated after we've cleared it and before the new
93          * character is written to UDR.  On a 485 bus, the transceiver will be put
94          * in RX mode while still transmitting the last char.
95          */
96         #define KDBG_WRITE_CHAR(c)    do { USR |= BV(TXC0); UDR = (c); } while(0)
97
98         #define KDBG_MASK_IRQ(old)    do { \
99                 (old) = UCR; \
100                 UCR |= BV(TXEN0); \
101                 UCR &= ~(BV(TXCIE0) | BV(UDRIE0)); \
102                 KDBG_UART0_BUS_TX; \
103         } while(0)
104
105         #define KDBG_RESTORE_IRQ(old) do { \
106                 KDBG_WAIT_TXDONE(); \
107                 KDBG_UART0_BUS_RX; \
108                 UCR = (old); \
109         } while(0)
110
111         typedef uint8_t kdbg_irqsave_t;
112
113 #elif CONFIG_KDEBUG_PORT == 1
114
115         /*
116                 * Support for special bus policies or external transceivers
117                 * on UART1 (to be overridden in "hw_ser.h").
118                 *
119                 * HACK: if we don't set TXEN, kdbg disables the transmitter
120                 * after each output statement until the serial driver
121                 * is initialized.  These glitches confuse the debug
122                 * terminal that ends up printing some trash.
123                 */
124         #ifndef KDBG_UART1_BUS_INIT
125         #define KDBG_UART1_BUS_INIT  do { \
126                         UCSR1B = BV(TXEN1); \
127                 } while (0)
128         #endif
129         #ifndef KDBG_UART1_BUS_RX
130         #define KDBG_UART1_BUS_RX    do {} while (0)
131         #endif
132         #ifndef KDBG_UART1_BUS_TX
133         #define KDBG_UART1_BUS_TX    do {} while (0)
134         #endif
135
136         #define KDBG_WAIT_READY()     do { loop_until_bit_is_set(UCSR1A, UDRE1); } while(0)
137         #define KDBG_WAIT_TXDONE()    do { loop_until_bit_is_set(UCSR1A, TXC1); } while(0)
138         #define KDBG_WRITE_CHAR(c)    do { UCSR1A |= BV(TXC1); UDR1 = (c); } while(0)
139
140         #define KDBG_MASK_IRQ(old)    do { \
141                 (old) = UCSR1B; \
142                 UCSR1B |= BV(TXEN1); \
143                 UCSR1B &= ~(BV(TXCIE1) | BV(UDRIE1)); \
144                 KDBG_UART1_BUS_TX; \
145         } while(0)
146
147         #define KDBG_RESTORE_IRQ(old) do { \
148                 KDBG_WAIT_TXDONE(); \
149                 KDBG_UART1_BUS_RX; \
150                 UCSR1B = (old); \
151         } while(0)
152
153         typedef uint8_t kdbg_irqsave_t;
154
155 /*
156         * Special debug port for BitBanged Serial see below for details...
157         */
158 #elif CONFIG_KDEBUG_PORT == 666
159         #include "hw_ser.h"
160         #define KDBG_WAIT_READY()      do { /*nop*/ } while(0)
161         #define KDBG_WRITE_CHAR(c)     _kdebug_bitbang_putchar((c))
162         #define KDBG_MASK_IRQ(old)     do { IRQ_SAVE_DISABLE((old)); } while(0)
163         #define KDBG_RESTORE_IRQ(old)  do { IRQ_RESTORE((old)); } while(0)
164         typedef cpuflags_t kdbg_irqsave_t;
165
166         #define KDBG_DELAY (((CLOCK_FREQ + CONFIG_KDEBUG_BAUDRATE / 2) / CONFIG_KDEBUG_BAUDRATE) + 7) / 14
167
168         static void _kdebug_bitbang_delay(void)
169         {
170                 unsigned long i;
171
172                 for (i = 0; i < KDBG_DELAY; i++)
173                 {
174                         NOP;
175                         NOP;
176                         NOP;
177                         NOP;
178                         NOP;
179                 }
180         }
181
182         /**
183          * Putchar for BITBANG serial debug console.
184          * Sometimes, we can't permit to use a whole serial for debugging purpose.
185          * Since debug console is in output only it is usefull to use a single generic I/O pin for debug.
186          * This is achieved by this simple function, that shift out the data like a UART, but
187          * in software :)
188          * The only requirement is that SER_BITBANG_* macros will be defined somewhere (usually hw_ser.h)
189          * \note All interrupts are disabled during debug prints!
190          */
191         static void _kdebug_bitbang_putchar(char c)
192         {
193                 int i;
194                 uint16_t data = c;
195
196                 /* Add stop bit */
197                 data |= 0x0100;
198
199                 /* Add start bit*/
200                 data <<= 1;
201
202                 /* Shift out data */
203                 uint16_t shift = 1;
204                 for (i = 0; i < 10; i++)
205                 {
206                         if (data & shift)
207                                 SER_BITBANG_HIGH;
208                         else
209                                 SER_BITBANG_LOW;
210                         _kdebug_bitbang_delay();
211                         shift <<= 1;
212                 }
213         }
214 #else
215         #error CONFIG_KDEBUG_PORT should be either 0, 1 or 666
216 #endif
217
218
219 INLINE void kdbg_hw_init(void)
220 {
221         #if CONFIG_KDEBUG_PORT == 666
222                 SER_BITBANG_INIT;
223         #else /* CONFIG_KDEBUG_PORT != 666 */
224                 /* Compute the baud rate */
225                 uint16_t period = (((CLOCK_FREQ / 16UL) + (CONFIG_KDEBUG_BAUDRATE / 2)) / CONFIG_KDEBUG_BAUDRATE) - 1;
226
227                 #if (CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128)
228                         #if CONFIG_KDEBUG_PORT == 0
229                                 UBRR0H = (uint8_t)(period>>8);
230                                 UBRR0L = (uint8_t)period;
231                                 KDBG_UART0_BUS_INIT;
232                         #elif CONFIG_KDEBUG_PORT == 1
233                                 UBRR1H = (uint8_t)(period>>8);
234                                 UBRR1L = (uint8_t)period;
235                                 KDBG_UART1_BUS_INIT;
236                         #else
237                                 #error CONFIG_KDEBUG_PORT must be either 0 or 1
238                         #endif
239
240                 #elif CPU_AVR_ATMEGA168
241                         UBRR0H = (uint8_t)(period>>8);
242                         UBRR0L = (uint8_t)period;
243                         KDBG_UART0_BUS_INIT;
244                 #elif CPU_AVR_ATMEGA8
245                         UBRRH = (uint8_t)(period>>8);
246                         UBRRL = (uint8_t)period;
247                 #elif CPU_AVR_ATMEGA103
248                         UBRR = (uint8_t)period;
249                         KDBG_UART0_BUS_INIT;
250                 #else
251                         #error Unknown CPU
252                 #endif
253         #endif /* CONFIG_KDEBUG_PORT == 666 */
254 }