d8e146372352c158242b41499194990788a63342
[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  * \version $Id$
37  * \author Bernie Innocenti <bernie@codewiz.org>
38  * \author Stefano Fedrigo <aleph@develer.com>
39  * \author Francesco Sacchi <batt@develer.com>
40  */
41
42 #include "hw/hw_cpu.h"     /* for CLOCK_FREQ */
43 #include "hw/hw_ser.h"     /* Required for bus macros overrides */
44
45 #include "cfg/cfg_debug.h"
46 #include <cfg/macros.h> /* for BV(), DIV_ROUND */
47
48 #include <cpu/types.h>
49 #include <cpu/attr.h>
50
51 #include <avr/io.h>
52
53 #if CONFIG_KDEBUG_PORT == 0
54
55         /*
56          * Support for special bus policies or external transceivers
57          * on UART0 (to be overridden in "hw/hw_ser.h").
58          *
59          * HACK: if we don't set TXEN, kdbg disables the transmitter
60          * after each output statement until the serial driver
61          * is initialized.  These glitches confuse the debug
62          * terminal that ends up printing some trash.
63          */
64         #ifndef KDBG_UART0_BUS_INIT
65         #define KDBG_UART0_BUS_INIT  do { \
66                         UCR = BV(TXEN0); \
67                 } while (0)
68         #endif
69         #ifndef KDBG_UART0_BUS_RX
70         #define KDBG_UART0_BUS_RX    do {} while (0)
71         #endif
72         #ifndef KDBG_UART0_BUS_TX
73         #define KDBG_UART0_BUS_TX    do {} while (0)
74         #endif
75
76         #if CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA1281 || CPU_AVR_ATMEGA168
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 /*
164  * Special debug port for BitBanged Serial see below for details...
165  */
166 #elif CONFIG_KDEBUG_PORT == 666
167         #include "hw/hw_ser.h"
168         #define KDBG_WAIT_READY()      do { /*nop*/ } while(0)
169         #define KDBG_WRITE_CHAR(c)     _kdebug_bitbang_putchar((c))
170         #define KDBG_MASK_IRQ(old)     do { IRQ_SAVE_DISABLE((old)); } while(0)
171         #define KDBG_RESTORE_IRQ(old)  do { IRQ_RESTORE((old)); } while(0)
172         typedef cpu_flags_t kdbg_irqsave_t;
173
174         #define KDBG_DELAY (((CLOCK_FREQ + CONFIG_KDEBUG_BAUDRATE / 2) / CONFIG_KDEBUG_BAUDRATE) + 7) / 14
175
176         static void _kdebug_bitbang_delay(void)
177         {
178                 unsigned long i;
179
180                 for (i = 0; i < KDBG_DELAY; i++)
181                 {
182                         NOP;
183                         NOP;
184                         NOP;
185                         NOP;
186                         NOP;
187                 }
188         }
189
190         /**
191          * Putchar for BITBANG serial debug console.
192          * Sometimes, we can't permit to use a whole serial for debugging purpose.
193          * Since debug console is in output only it is useful to use a single generic I/O pin for debug.
194          * This is achieved by this simple function, that shift out the data like a UART, but
195          * in software :)
196          * The only requirement is that SER_BITBANG_* macros will be defined somewhere (usually hw_ser.h)
197          * \note All interrupts are disabled during debug prints!
198          */
199         static void _kdebug_bitbang_putchar(char c)
200         {
201                 int i;
202                 uint16_t data = c;
203
204                 /* Add stop bit */
205                 data |= 0x0100;
206
207                 /* Add start bit*/
208                 data <<= 1;
209
210                 /* Shift out data */
211                 uint16_t shift = 1;
212                 for (i = 0; i < 10; i++)
213                 {
214                         if (data & shift)
215                                 SER_BITBANG_HIGH;
216                         else
217                                 SER_BITBANG_LOW;
218                         _kdebug_bitbang_delay();
219                         shift <<= 1;
220                 }
221         }
222 #else
223         #error CONFIG_KDEBUG_PORT should be either 0, 1 or 666
224 #endif
225
226
227 INLINE void kdbg_hw_init(void)
228 {
229         #if CONFIG_KDEBUG_PORT == 666
230                 SER_BITBANG_INIT;
231         #else /* CONFIG_KDEBUG_PORT != 666 */
232                 /* Compute the baud rate */
233                 uint16_t period = DIV_ROUND(CLOCK_FREQ / 16UL, CONFIG_KDEBUG_BAUDRATE) - 1;
234
235                 #if (CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA1281)
236                         #if CONFIG_KDEBUG_PORT == 0
237                                 UBRR0H = (uint8_t)(period>>8);
238                                 UBRR0L = (uint8_t)period;
239                                 KDBG_UART0_BUS_INIT;
240                         #elif CONFIG_KDEBUG_PORT == 1
241                                 UBRR1H = (uint8_t)(period>>8);
242                                 UBRR1L = (uint8_t)period;
243                                 KDBG_UART1_BUS_INIT;
244                         #else
245                                 #error CONFIG_KDEBUG_PORT must be either 0 or 1
246                         #endif
247
248                 #elif CPU_AVR_ATMEGA168
249                         UBRR0H = (uint8_t)(period>>8);
250                         UBRR0L = (uint8_t)period;
251                         KDBG_UART0_BUS_INIT;
252                 #elif CPU_AVR_ATMEGA8 || CPU_AVR_ATMEGA32
253                         UBRRH = (uint8_t)(period>>8);
254                         UBRRL = (uint8_t)period;
255                         KDBG_UART0_BUS_INIT;
256                 #elif CPU_AVR_ATMEGA103
257                         UBRR = (uint8_t)period;
258                         KDBG_UART0_BUS_INIT;
259                 #else
260                         #error Unknown CPU
261                 #endif
262         #endif /* CONFIG_KDEBUG_PORT == 666 */
263 }
264