Merge contributed patch to extend support of atxmega.
[bertos.git] / bertos / cpu / avr / drv / kdebug_xmega.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  * Copyright 2011 Onno <developer@gorgoz.org>
32  *
33  * -->
34  *
35  * \brief AVR XMega debug support (implementation).
36  *
37  * This file is heavily inspired by the AVR implementation for BeRTOS,
38  * but uses a different approach for implementing the different debug
39  * ports, by using the USART_t structs.
40  *
41  * \author Onno <developer@gorgoz.org>
42  * notest:all
43  */
44
45 #include <hw/hw_cpufreq.h>      /* for CPU_FREQ */
46 #include "hw/hw_ser.h"          /* Required for bus macros overrides */
47
48 #include "cfg/cfg_debug.h"      /* for debugging configuration settings */
49 #include <cfg/macros.h>         /* for BV(), DIV_ROUND */
50
51 #include <cpu/types.h>
52 #include <cpu/attr.h>
53
54 #include <avr/io.h>
55
56 /* Set KDBG_USART, KDBG_USART_PORT and KDBG_USART_TX_PIN_bm
57  * according to the CONFIG_KDEBUG_PORT setting
58  * All Xmega families support at least 2 UARTS
59  * Some Xmega families suport more (D3->3, A4->5, A3->7, A1->8)
60  */
61 #if CONFIG_KDEBUG_PORT == 0
62         #define KDBG_USART              USARTC0
63         #define KDBG_USART_PORT         PORTC
64         #define KDBG_USART_TX_PIN_bm    PIN3_bm
65 #elif CONFIG_KDEBUG_PORT == 1
66         #define KDBG_USART              USARTD0
67         #define KDBG_USART_PORT         PORTD
68         #define KDBG_USART_TX_PIN_bm    PIN3_bm
69 #endif
70 #if CPU_AVR_XMEGA_D3 || CPU_AVR_XMEGA_A4 || CPU_AVR_XMEGA_A3 || CPU_AVR_XMEGA_A1
71         #if CONFIG_KDEBUG_PORT == 2
72                 #define KDBG_USART              USARTE0
73                 #define KDBG_USART_PORT         PORTE
74                 #define KDBG_USART_TX_PIN_bm    PIN3_bm
75         #endif
76 #endif
77 #if CPU_AVR_XMEGA_A4 || CPU_AVR_XMEGA_A3 || CPU_AVR_XMEGA_A1
78         #if CONFIG_KDEBUG_PORT == 3
79                 #define KDBG_USART              USARTC1
80                 #define KDBG_USART_PORT         PORTC
81                 #define KDBG_USART_TX_PIN_bm    PIN7_bm
82         #elif CONFIG_KDEBUG_PORT == 4
83                 #define KDBG_USART              USARTD1
84                 #define KDBG_USART_PORT         PORTD
85                 #define KDBG_USART_TX_PIN_bm    PIN7_bm
86         #endif
87 #endif
88 #if CPU_AVR_XMEGA_A3 || CPU_AVR_XMEGA_A1
89         #if CONFIG_KDEBUG_PORT == 5
90                 #define KDBG_USART              USARTE1
91                 #define KDBG_USART_PORT         PORTE
92                 #define KDBG_USART_TX_PIN_bm    PIN7_bm
93         #elif CONFIG_KDEBUG_PORT == 6
94                 #define KDBG_USART              USARTF0
95                 #define KDBG_USART_PORT         PORTF
96                 #define KDBG_USART_TX_PIN_bm    PIN3_bm
97         #endif
98 #endif
99 #if CPU_AVR_XMEGA_A1
100         #if CONFIG_KDEBUG_PORT == 7
101                 #define KDBG_USART              USARTF1
102                 #define KDBG_USART_PORT         PORTF
103                 #define KDBG_USART_TX_PIN_bm    PIN7_bm
104         #endif
105 #endif
106
107
108 /* Check if all required KDBG_ macros are defined
109  */
110 #ifndef KDBG_USART
111         #if CPU_AVR_XMEGA_D4
112                 #error CONFIG_KDEBUG_PORT should be either 0 or 1
113         #elif CPU_AVR_XMEGA_D3
114                 #error CONFIG_KDEBUG_PORT should be either 0, 1 or 2
115         #elif CPU_AVR_XMEGA_A4
116                 #error CONFIG_KDEBUG_PORT should be either 0, 1, 2, 3 or 4
117         #elif CPU_AVR_XMEGA_A3
118                 #error CONFIG_KDEBUG_PORT should be either 0, 1, 2, 3, 4, 5 or 6
119         #elif CPU_AVR_XMEGA_A1
120                 #error CONFIG_KDEBUG_PORT should be either 0, 1, 2, 3, 4, 5, 6 or 7
121         #endif
122 #endif
123
124 /*
125  * Scalefactor to use for computing the baudrate
126  * this scalefactor should be an integer value between -7
127  * and 7
128  */
129 #ifndef KDBG_USART_SCALE_FACTOR
130         #define KDBG_USART_SCALE_FACTOR (-7)
131 #else
132         #if KDBG_USART_SCALE_FACTOR > 7 || KDBG_USART_SCALE_FACTOR < -7
133                 #error KDBG_USART_SCALE_FACTOR should be an integer between -7 and 7
134         #endif
135 #endif
136
137 /*
138  * \name KDBG macros
139  *
140  * Used to set or alter the KDB_USART operation,
141  * enable the usart or send a byte.
142  * Some of these methods are called/included from kdbg_hw_init()
143  * others are called/included from the cpu independ kdebug implementation
144  * These macros are heavily imspired by the examples provided by atmel
145  *
146  * \{
147  */
148 #define KDBG_SET_FORMAT(_charSize, _parityMode, _twoStopBits)         \
149         (KDBG_USART).CTRLC = (uint8_t) _charSize | _parityMode |                      \
150                           (_twoStopBits ? USART_SBMODE_bm : 0)
151
152 #define KDBG_SET_BAUDRATE(_bselValue, _bScaleFactor)                  \
153         (KDBG_USART).BAUDCTRLA =(uint8_t)_bselValue;                                           \
154         (KDBG_USART).BAUDCTRLB =(_bScaleFactor << USART_BSCALE0_bp)|(_bselValue >> 8)
155
156 #define KDBG_TX_ENABLE()        ((KDBG_USART).CTRLB |= USART_TXEN_bm)
157
158 #define KDBG_SET_MODE(_usartMode)                                      \
159         ((KDBG_USART).CTRLC = ((KDBG_USART).CTRLC & (~USART_CMODE_gm)) | _usartMode)
160
161 #define KDBG_WAIT_READY()   do{ loop_until_bit_is_set((KDBG_USART).STATUS, USART_DREIF_bp); } while(0)
162 #define KDBG_WAIT_TXDONE()  do { loop_until_bit_is_set((KDBG_USART).STATUS, USART_TXCIF_bp); } while(0)
163 #define KDBG_WRITE_CHAR(c)  do { (KDBG_USART).DATA = (c); } while(0)
164
165 #define KDBG_SET_TX_INTERRUPTLEVEL(_txdIntLevel)                      \
166         (KDBG_USART).CTRLA = ((KDBG_USART).CTRLA & ~USART_TXCINTLVL_gm) | _txdIntLevel
167
168 #define KDBG_SET_DRE_INTERRUPTLEVEL(_dreIntLevel)                      \
169         (KDBG_USART).CTRLA = ((KDBG_USART).CTRLA & ~USART_DREINTLVL_gm) | _dreIntLevel
170
171 /*\}*/
172
173 /*
174  * To restore the USART state, to registers need to be restored
175  * These registers (CTRLA and CTRLB) can be saved to the
176  * kdbg_avr_xmaga_irqsave structure
177  */
178 struct kdbg_avr_xmega_irqsave
179 {
180         uint8_t ctrlb;
181         uint8_t ctrla;
182 };
183 typedef struct kdbg_avr_xmega_irqsave kdbg_irqsave_t;
184
185 /*
186  * param is the kdbg_irqsave_t structure
187  *
188  * * Stores the current state of the USART.CTRLA and
189  * the USART.CTRLB registers
190  * * Disables Transmit Complete and Date Register Empty interrupts
191  * * Enabled the transmitter
192  */
193 #define KDBG_MASK_IRQ(old)    do { \
194         (old).ctrlb = KDBG_USART.CTRLB; \
195         (old).ctrla = KDBG_USART.CTRLA; \
196         KDBG_SET_TX_INTERRUPTLEVEL(USART_TXCINTLVL_OFF_gc); \
197         KDBG_SET_DRE_INTERRUPTLEVEL(USART_DREINTLVL_OFF_gc); \
198         KDBG_TX_ENABLE(); \
199 } while(0)
200
201 /*
202  * param is the kdbg_irqsave_t structure
203  *
204  * * waits until all data has been transmitted
205  * * restores the USART.CTRLA and USART.CTRLB registers
206  */
207 #define KDBG_RESTORE_IRQ(old) do { \
208         KDBG_WAIT_TXDONE(); \
209         KDBG_USART.CTRLB = (old).ctrlb; \
210         KDBG_USART.CTRLA = (old).ctrla; \
211 } while(0)
212
213
214 /*
215  * method included from the cpu independent kdebug.c file.
216  * it initializes KDBG_USART by:
217  * * Setting the KDBG_USART_TX_PIN_bm as an outputpin
218  * * Setting KDBG_USART to use 8 bits, No parity, 1 stopbit
219  * * Setting the baudrate to 115200
220  * * Enabeling the transmitter
221  */
222 INLINE void kdbg_hw_init(void)
223 {
224                 //set transmit pin as output
225                 //KDBG_USART_PORT.OUTCLR = KDBG_USART_TX_PIN_bm;
226                 KDBG_USART_PORT.OUTSET = KDBG_USART_TX_PIN_bm;
227                 KDBG_USART_PORT.DIRSET = KDBG_USART_TX_PIN_bm;
228                 //set 8 bits, no parity, 1 stop bit
229                 KDBG_SET_FORMAT(USART_CHSIZE_8BIT_gc, USART_PMODE_DISABLED_gc, false);
230                 //compute and set the baud rate
231                 /* Compute baud-rate period, this requires a valid USART_SCALE_FACTOR */
232                 #if KDBG_USART_SCALE_FACTOR < 0
233                         uint16_t bsel = DIV_ROUND((1 << (-(KDBG_USART_SCALE_FACTOR))) * (CPU_FREQ - (16 * CONFIG_KDEBUG_BAUDRATE)), 16 * CONFIG_KDEBUG_BAUDRATE);
234                 #else
235                         uint16_t bsel = DIV_ROUND(CPU_FREQ, (1 << (KDBG_USART_SCALE_FACTOR)) * 16 * CONFIG_KDEBUG_BAUDRATE) - 1;
236                 #endif
237                 KDBG_SET_BAUDRATE(bsel, KDBG_USART_SCALE_FACTOR);
238                 //enable the Transmitter
239                 KDBG_TX_ENABLE();
240 }