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