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