Fix debug port select.
[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_ATMEGA168 || CPU_AVR_ATMEGA328P
76                 #define UCR UCSR0B
77                 #define UDR UDR0
78                 #define USR UCSR0A
79         #elif CPU_AVR_ATMEGA8 || CPU_AVR_ATMEGA32
80                 #define UCR    UCSRB
81                 #define USR    UCSRA
82                 #define TXEN0  TXEN
83                 #define UDRE0  UDRE
84                 #define TXC0   TXC
85                 #define TXCIE0 TXCIE
86                 #define UDRIE0 UDRIE
87         #else
88                 #error Unknown CPU
89         #endif
90
91         #define KDBG_WAIT_READY()     do { loop_until_bit_is_set(USR, UDRE0); } while(0)
92         #define KDBG_WAIT_TXDONE()    do { loop_until_bit_is_set(USR, TXC0); } while(0)
93
94         /*
95          * We must clear the TXC flag before sending a new character to allow
96          * KDBG_WAIT_TXDONE() to work properly.
97          *
98          * BUG: if KDBG_WRITE_CHAR() is called after the TXC flag is set by hardware,
99          * a new TXC could be generated after we've cleared it and before the new
100          * character is written to UDR.  On a 485 bus, the transceiver will be put
101          * in RX mode while still transmitting the last char.
102          */
103         #define KDBG_WRITE_CHAR(c)    do { USR |= BV(TXC0); UDR = (c); } while(0)
104
105         #define KDBG_MASK_IRQ(old)    do { \
106                 (old) = UCR; \
107                 UCR |= BV(TXEN0); \
108                 UCR &= ~(BV(TXCIE0) | BV(UDRIE0)); \
109                 KDBG_UART0_BUS_TX; \
110         } while(0)
111
112         #define KDBG_RESTORE_IRQ(old) do { \
113                 KDBG_WAIT_TXDONE(); \
114                 KDBG_UART0_BUS_RX; \
115                 UCR = (old); \
116         } while(0)
117
118         typedef uint8_t kdbg_irqsave_t;
119
120 #elif CONFIG_KDEBUG_PORT == 1
121
122         /*
123                 * Support for special bus policies or external transceivers
124                 * on UART1 (to be overridden in "hw/hw_ser.h").
125                 *
126                 * HACK: if we don't set TXEN, kdbg disables the transmitter
127                 * after each output statement until the serial driver
128                 * is initialized.  These glitches confuse the debug
129                 * terminal that ends up printing some trash.
130                 */
131         #ifndef KDBG_UART1_BUS_INIT
132         #define KDBG_UART1_BUS_INIT  do { \
133                         UCSR1B = BV(TXEN1); \
134                 } while (0)
135         #endif
136         #ifndef KDBG_UART1_BUS_RX
137         #define KDBG_UART1_BUS_RX    do {} while (0)
138         #endif
139         #ifndef KDBG_UART1_BUS_TX
140         #define KDBG_UART1_BUS_TX    do {} while (0)
141         #endif
142
143         #define KDBG_WAIT_READY()     do { loop_until_bit_is_set(UCSR1A, UDRE1); } while(0)
144         #define KDBG_WAIT_TXDONE()    do { loop_until_bit_is_set(UCSR1A, TXC1); } while(0)
145         #define KDBG_WRITE_CHAR(c)    do { UCSR1A |= BV(TXC1); UDR1 = (c); } while(0)
146
147         #define KDBG_MASK_IRQ(old)    do { \
148                 (old) = UCSR1B; \
149                 UCSR1B |= BV(TXEN1); \
150                 UCSR1B &= ~(BV(TXCIE1) | BV(UDRIE1)); \
151                 KDBG_UART1_BUS_TX; \
152         } while(0)
153
154         #define KDBG_RESTORE_IRQ(old) do { \
155                 KDBG_WAIT_TXDONE(); \
156                 KDBG_UART1_BUS_RX; \
157                 UCSR1B = (old); \
158         } while(0)
159
160         typedef uint8_t kdbg_irqsave_t;
161
162 /*
163  * Special debug port for BitBanged Serial see below for details...
164  */
165 #elif CONFIG_KDEBUG_PORT == 666
166         #include "hw/hw_ser.h"
167         #define KDBG_WAIT_READY()      do { /*nop*/ } while(0)
168         #define KDBG_WRITE_CHAR(c)     _kdebug_bitbang_putchar((c))
169         #define KDBG_MASK_IRQ(old)     do { IRQ_SAVE_DISABLE((old)); } while(0)
170         #define KDBG_RESTORE_IRQ(old)  do { IRQ_RESTORE((old)); } while(0)
171         typedef cpu_flags_t kdbg_irqsave_t;
172
173         #define KDBG_DELAY (((CPU_FREQ + CONFIG_KDEBUG_BAUDRATE / 2) / CONFIG_KDEBUG_BAUDRATE) + 7) / 14
174
175         static void _kdebug_bitbang_delay(void)
176         {
177                 unsigned long i;
178
179                 for (i = 0; i < KDBG_DELAY; i++)
180                 {
181                         NOP;
182                         NOP;
183                         NOP;
184                         NOP;
185                         NOP;
186                 }
187         }
188
189         /**
190          * Putchar for BITBANG serial debug console.
191          * Sometimes, we can't permit to use a whole serial for debugging purpose.
192          * Since debug console is in output only it is useful to use a single generic I/O pin for debug.
193          * This is achieved by this simple function, that shift out the data like a UART, but
194          * in software :)
195          * The only requirement is that SER_BITBANG_* macros will be defined somewhere (usually hw_ser.h)
196          * \note All interrupts are disabled during debug prints!
197          */
198         static void _kdebug_bitbang_putchar(char c)
199         {
200                 int i;
201                 uint16_t data = c;
202
203                 /* Add stop bit */
204                 data |= 0x0100;
205
206                 /* Add start bit*/
207                 data <<= 1;
208
209                 /* Shift out data */
210                 uint16_t shift = 1;
211                 for (i = 0; i < 10; i++)
212                 {
213                         if (data & shift)
214                                 SER_BITBANG_HIGH;
215                         else
216                                 SER_BITBANG_LOW;
217                         _kdebug_bitbang_delay();
218                         shift <<= 1;
219                 }
220         }
221 #else
222         #error CONFIG_KDEBUG_PORT should be either 0, 1 or 666
223 #endif
224
225
226 INLINE void kdbg_hw_init(void)
227 {
228         #if CONFIG_KDEBUG_PORT == 666
229                 SER_BITBANG_INIT;
230         #else /* CONFIG_KDEBUG_PORT != 666 */
231                 /* Compute the baud rate */
232                 uint16_t period = DIV_ROUND(CPU_FREQ / 16UL, CONFIG_KDEBUG_BAUDRATE) - 1;
233
234                 #if (CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA1281)
235                         #if CONFIG_KDEBUG_PORT == 0
236                                 UBRR0H = (uint8_t)(period>>8);
237                                 UBRR0L = (uint8_t)period;
238                                 KDBG_UART0_BUS_INIT;
239                         #elif CONFIG_KDEBUG_PORT == 1
240                                 UBRR1H = (uint8_t)(period>>8);
241                                 UBRR1L = (uint8_t)period;
242                                 KDBG_UART1_BUS_INIT;
243                         #else
244                                 #error CONFIG_KDEBUG_PORT must be either 0 or 1
245                         #endif
246
247                 #elif CPU_AVR_ATMEGA168 || CPU_AVR_ATMEGA328P
248                         #if CONFIG_KDEBUG_PORT == 0
249                                 UBRR0H = (uint8_t)(period>>8);
250                                 UBRR0L = (uint8_t)period;
251                                 KDBG_UART0_BUS_INIT;
252                         #else
253                                 #error Only CONFIG_KDEBUG_PORT 0 is supported for this cpu
254                         #endif
255
256                 #elif CPU_AVR_ATMEGA8 || CPU_AVR_ATMEGA32
257                         #if CONFIG_KDEBUG_PORT == 0
258                                 UBRRH = (uint8_t)(period>>8);
259                                 UBRRL = (uint8_t)period;
260                                 KDBG_UART0_BUS_INIT;
261                         #else
262                                 #error Only CONFIG_KDEBUG_PORT 0 is supported for this cpu
263                         #endif
264                 #elif CPU_AVR_ATMEGA103
265                         #if CONFIG_KDEBUG_PORT == 0
266                                 UBRR = (uint8_t)period;
267                                 KDBG_UART0_BUS_INIT;
268                         #else
269                                 #error Only CONFIG_KDEBUG_PORT 0 is supported for this cpu
270                         #endif
271                 #else
272                         #error Unknown CPU
273                 #endif
274         #endif /* CONFIG_KDEBUG_PORT == 666 */
275 }
276