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