4 * This file is part of BeRTOS.
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.
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.
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
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.
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>
35 * \brief AVR XMega debug support (implementation).
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.
41 * \author Onno <developer@gorgoz.org>
45 #include <hw/hw_cpufreq.h> /* for CPU_FREQ */
46 #include "hw/hw_ser.h" /* Required for bus macros overrides */
48 #include "cfg/cfg_debug.h" /* for debugging configuration settings */
49 #include <cfg/macros.h> /* for BV(), DIV_ROUND */
51 #include <cpu/types.h>
56 /* Set KDBG_USART, KDBG_USART_PORT and KDBG_USART_TX_PIN_bm
57 * according to the CONFIG_KDEBUG_PORT setting
58 * The Xmega A and D families support at least 2 UARTS
60 #if CONFIG_KDEBUG_PORT == 0
61 #define KDBG_USART USARTC0
62 #define KDBG_USART_PORT PORTC
63 #define KDBG_USART_TX_PIN_bm PIN3_bm
64 #elif CONFIG_KDEBUG_PORT == 1
65 #define KDBG_USART USARTD0
66 #define KDBG_USART_PORT PORTD
67 #define KDBG_USART_TX_PIN_bm PIN3_bm
69 /* Allow the configuration of the extra 3 UARTS for the
72 #ifdef CPU_AVR_XMEGA_A
73 #if CONFIG_KDEBUG_PORT == 2
74 #define KDBG_USART USARTC1
75 #define KDBG_USART_PORT PORTC
76 #define KDBG_USART_TX_PIN_bm PIN7_bm
77 #elif CONFIG_KDEBUG_PORT == 3
78 #define KDBG_USART USARTD1
79 #define KDBG_USART_PORT PORTD
80 #define KDBG_USART_TX_PIN_bm PIN7_bm
81 #elif CONFIG_KDEBUG_PORT == 4
82 #define KDBG_USART USARTE0
83 #define KDBG_USART_PORT PORTE
84 #define KDBG_USART_TX_PIN_bm PIN3_bm
87 /* Check if all required KDBG_ macros are defined
91 #error CONFIG_KDEBUG_PORT should be either 0 or 1
93 #error CONFIG_KDEBUG_PORT should be either 0, 1, 2, 3 or 4
98 * Scalefactor to use for computing the baudrate
99 * this scalefactor should be an integer value between -7
102 #ifndef KDBG_USART_SCALE_FACTOR
103 #define KDBG_USART_SCALE_FACTOR (-7)
105 #if KDBG_USART_SCALE_FACTOR > 7 || KDBG_USART_SCALE_FACTOR < -7
106 #error KDBG_USART_SCALE_FACTOR should be an integer between -7 and 7
113 * Used to set or alter the KDB_USART operation,
114 * enable the usart or send a byte.
115 * Some of these methods are called/included from kdbg_hw_init()
116 * others are called/included from the cpu independ kdebug implementation
117 * These macros are heavily imspired by the examples provided by atmel
121 #define KDBG_SET_FORMAT(_charSize, _parityMode, _twoStopBits) \
122 (KDBG_USART).CTRLC = (uint8_t) _charSize | _parityMode | \
123 (_twoStopBits ? USART_SBMODE_bm : 0)
125 #define KDBG_SET_BAUDRATE(_bselValue, _bScaleFactor) \
126 (KDBG_USART).BAUDCTRLA =(uint8_t)_bselValue; \
127 (KDBG_USART).BAUDCTRLB =(_bScaleFactor << USART_BSCALE0_bp)|(_bselValue >> 8)
129 #define KDBG_TX_ENABLE() ((KDBG_USART).CTRLB |= USART_TXEN_bm)
131 #define KDBG_SET_MODE(_usartMode) \
132 ((KDBG_USART).CTRLC = ((KDBG_USART).CTRLC & (~USART_CMODE_gm)) | _usartMode)
134 #define KDBG_WAIT_READY() do{ loop_until_bit_is_set((KDBG_USART).STATUS, USART_DREIF_bp); } while(0)
135 #define KDBG_WAIT_TXDONE() do { loop_until_bit_is_set((KDBG_USART).STATUS, USART_TXCIF_bp); } while(0)
136 #define KDBG_WRITE_CHAR(c) do { (KDBG_USART).DATA = (c); } while(0)
138 #define KDBG_SET_TX_INTERRUPTLEVEL(_txdIntLevel) \
139 (KDBG_USART).CTRLA = ((KDBG_USART).CTRLA & ~USART_TXCINTLVL_gm) | _txdIntLevel
141 #define KDBG_SET_DRE_INTERRUPTLEVEL(_dreIntLevel) \
142 (KDBG_USART).CTRLA = ((KDBG_USART).CTRLA & ~USART_DREINTLVL_gm) | _dreIntLevel
147 * To restore the USART state, to registers need to be restored
148 * These registers (CTRLA and CTRLB) can be saved to the
149 * kdbg_avr_xmaga_irqsave structure
151 struct kdbg_avr_xmega_irqsave
156 typedef struct kdbg_avr_xmega_irqsave kdbg_irqsave_t;
159 * param is the kdbg_irqsave_t structure
161 * * Stores the current state of the USART.CTRLA and
162 * the USART.CTRLB registers
163 * * Disables Transmit Complete and Date Register Empty interrupts
164 * * Enabled the transmitter
166 #define KDBG_MASK_IRQ(old) do { \
167 (old).ctrlb = KDBG_USART.CTRLB; \
168 (old).ctrla = KDBG_USART.CTRLA; \
169 KDBG_SET_TX_INTERRUPTLEVEL(USART_TXCINTLVL_OFF_gc); \
170 KDBG_SET_DRE_INTERRUPTLEVEL(USART_DREINTLVL_OFF_gc); \
175 * param is the kdbg_irqsave_t structure
177 * * waits until all data has been transmitted
178 * * restores the USART.CTRLA and USART.CTRLB registers
180 #define KDBG_RESTORE_IRQ(old) do { \
181 KDBG_WAIT_TXDONE(); \
182 KDBG_USART.CTRLB = (old).ctrlb; \
183 KDBG_USART.CTRLA = (old).ctrla; \
188 * method included from the cpu independent kdebug.c file.
189 * it initializes KDBG_USART by:
190 * * Setting the KDBG_USART_TX_PIN_bm as an outputpin
191 * * Setting KDBG_USART to use 8 bits, No parity, 1 stopbit
192 * * Setting the baudrate to 115200
193 * * Enabeling the transmitter
195 INLINE void kdbg_hw_init(void)
197 //set transmit pin as output
198 KDBG_USART_PORT.OUT = KDBG_USART_PORT.OUT & ~KDBG_USART_TX_PIN_bm;
199 KDBG_USART_PORT.DIRSET = KDBG_USART_TX_PIN_bm;
200 //set 8 bits, no parity, 1 stop bit
201 KDBG_SET_FORMAT(USART_CHSIZE_8BIT_gc, USART_PMODE_DISABLED_gc, false);
202 //compute and set the baud rate
203 /* Compute baud-rate period, this requires a valid USART_SCALE_FACTOR */
204 #if KDBG_USART_SCALE_FACTOR < 0
205 uint16_t bsel = DIV_ROUND((1 << (-(KDBG_USART_SCALE_FACTOR))) * (CPU_FREQ - (16 * CONFIG_KDEBUG_BAUDRATE)), 16 * CONFIG_KDEBUG_BAUDRATE);
207 uint16_t bsel = DIV_ROUND(CPU_FREQ, (1 << (KDBG_USART_SCALE_FACTOR)) * 16 * CONFIG_KDEBUG_BAUDRATE) - 1;
209 KDBG_SET_BAUDRATE(bsel, KDBG_USART_SCALE_FACTOR);
210 //enable the Transmitter