diff --git a/arch/avr/src/at91uc3/at91uc3_config.h b/arch/avr/src/at91uc3/at91uc3_config.h index 426057cb11..feac188b85 100755 --- a/arch/avr/src/at91uc3/at91uc3_config.h +++ b/arch/avr/src/at91uc3/at91uc3_config.h @@ -109,6 +109,16 @@ # undef HAVE_SERIAL_CONSOLE #endif +/* Determine which (if any) console driver to use */ + +#if CONFIG_NFILE_DESCRIPTORS == 0 || defined(CONFIG_DEV_LOWCONSOLE) || !defined(HAVE_RS232_DEVICE) +# undef CONFIG_USE_SERIALDRIVER +# undef CONFIG_USE_EARLYSERIALINIT +#elif defined(CONFIG_DEV_CONSOLE) && CONFIG_NFILE_DESCRIPTORS > 0 +# define CONFIG_USE_SERIALDRIVER 1 +# define CONFIG_USE_EARLYSERIALINIT 1 +#endif + /************************************************************************************ * Public Types ************************************************************************************/ diff --git a/arch/avr/src/at91uc3/at91uc3_internal.h b/arch/avr/src/at91uc3/at91uc3_internal.h index 54c0d2c43c..23cfc6632c 100755 --- a/arch/avr/src/at91uc3/at91uc3_internal.h +++ b/arch/avr/src/at91uc3/at91uc3_internal.h @@ -42,6 +42,9 @@ #include +#include +#include + /************************************************************************************ * Pre-processor Definitions ************************************************************************************/ @@ -68,11 +71,34 @@ extern void up_clkinitialize(void); +/****************************************************************************** + * Name: usart_reset + * + * Description: + * Reset a USART. + * + ******************************************************************************/ + +extern void usart_reset(uintptr_t usart_base); + +/****************************************************************************** + * Name: usart_configure + * + * Description: + * Configure a USART as a RS-232 UART. + * + ******************************************************************************/ + +void usart_configure(uintptr_t usart_base, uint32_t baud, unsigned int parity, + unsigned int nbits, bool stop2); + /************************************************************************************ * Name: up_consoleinit * * Description: - * Initialize a console for debug output. + * Initialize a console for debug output. This function is called very + * early in the intialization sequence to configure the serial console uart + * (only). * ************************************************************************************/ diff --git a/arch/avr/src/at91uc3/at91uc3_lowconsole.c b/arch/avr/src/at91uc3/at91uc3_lowconsole.c index e12ee4cc75..3a05973ff0 100644 --- a/arch/avr/src/at91uc3/at91uc3_lowconsole.c +++ b/arch/avr/src/at91uc3/at91uc3_lowconsole.c @@ -1,4 +1,4 @@ -/************************************************************************** +/****************************************************************************** * arch/avr/src/at91uc3/at91uc3_lowconsole.c * * Copyright (C) 2010 Gregory Nutt. All rights reserved. @@ -31,75 +31,300 @@ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - **************************************************************************/ + ******************************************************************************/ -/************************************************************************** +/****************************************************************************** * Included Files - **************************************************************************/ + ******************************************************************************/ #include - #include "at91uc3_config.h" + +#include +#include + +#include +#include + +#include "up_arch.h" #include "up_internal.h" #include "at91uc3_internal.h" +#include "at91uc3_usart.h" -/************************************************************************** +/****************************************************************************** * Private Definitions - **************************************************************************/ + ******************************************************************************/ -/************************************************************************** +/* Select USART parameters for the selected console */ + +#if defined(CONFIG_USART0_SERIAL_CONSOLE) +# define AVR32_CONSOLE_BASE AVR32_USART0_BASE +# define AVR32_CONSOLE_BAUD CONFIG_USART0_BAUD +# define AVR32_CONSOLE_BITS CONFIG_USART0_BITS +# define AVR32_CONSOLE_PARITY CONFIG_USART0_PARITY +# define AVR32_CONSOLE_2STOP CONFIG_USART0_2STOP +#elif defined(CONFIG_USART1_SERIAL_CONSOLE) +# define AVR32_CONSOLE_BASE AVR32_USART1_BASE +# define AVR32_CONSOLE_BAUD CONFIG_USART1_BAUD +# define AVR32_CONSOLE_BITS CONFIG_USART1_BITS +# define AVR32_CONSOLE_PARITY CONFIG_USART1_PARITY +# define AVR32_CONSOLE_2STOP CONFIG_USART1_2STOP +#elif defined(CONFIG_USART2_SERIAL_CONSOLE) +# define AVR32_CONSOLE_BASE AVR32_USART2_BASE +# define AVR32_CONSOLE_BAUD CONFIG_USART2_BAUD +# define AVR32_CONSOLE_BITS CONFIG_USART2_BITS +# define AVR32_CONSOLE_PARITY CONFIG_USART2_PARITY +# define AVR32_CONSOLE_2STOP CONFIG_USART2_2STOP +#else +# error "No CONFIG_USARTn_SERIAL_CONSOLE Setting" +#endif + +/****************************************************************************** * Private Types - **************************************************************************/ + ******************************************************************************/ -/************************************************************************** +/****************************************************************************** * Private Function Prototypes - **************************************************************************/ + ******************************************************************************/ -/************************************************************************** +/****************************************************************************** * Global Variables - **************************************************************************/ + ******************************************************************************/ -/************************************************************************** +/****************************************************************************** * Private Variables - **************************************************************************/ + ******************************************************************************/ -/************************************************************************** +/****************************************************************************** * Private Functions - **************************************************************************/ + ******************************************************************************/ -/************************************************************************** +/****************************************************************************** + * Name: usart_putreg + * + * Description: + * Write a value to a USART register + * + ******************************************************************************/ + +#ifdef HAVE_RS232_DEVICE +static inline void usart_putreg(uintptr_t usart_base, unsigned int offset, uint32_t value) +{ + putreg32(value, usart_base + offset); +} +#endif + +/****************************************************************************** + * Name: usart_getreg + * + * Description: + * Get a value from a USART register + * + ******************************************************************************/ + +#ifdef HAVE_RS232_DEVICE +static inline uint32_t usart_getreg(uintptr_t usart_base, unsigned int offset) +{ + return getreg32(usart_base + offset); +} +#endif + +/****************************************************************************** + * Name: usart_setbaudrate + * + * Description: + * Configure the UART baud rate. + * + ******************************************************************************/ + +#ifdef HAVE_RS232_DEVICE +static void usart_setbaudrate(uintptr_t usart_base, uint32_t baudrate) +{ + uint32_t cd; + uint32_t mr; + + /* Select 16x or 8x oversampling mode or go to synchronous mode */ + + mr = usart_getreg(usart_base, AVR32_USART_MR_OFFSET); + if (baudrate < AVR32_PBA_CLOCK / 16) + { + /* Select 16x oversampling mode and clear the SYNC mode bit */ + + mr &= ~(USART_MR_OVER|USART_MR_SYNC); + + /* Calculate the clock divider assuming 16x oversampling */ + + cd = (AVR32_PBA_CLOCK + (baudrate << 3)) / (baudrate << 4); + } + else if (baudrate < AVR32_PBA_CLOCK / 8) + { + /* Select 8x oversampling mode and clear the SYNC mode bit */ + + mr &= ~USART_MR_SYNC; + mr |= USART_MR_OVER; + + /* Calculate the clock divider assuming 16x oversampling */ + + cd = (AVR32_PBA_CLOCK + (baudrate << 2)) / (baudrate << 3); + } + else + { + /* Set the SYNC mode bit */ + + mr |= USART_MR_SYNC; + + /* Use the undivided PBA clock */ + + cd = AVR32_PBA_CLOCK / baudrate; + } + + DEBUGASSERT(cd > 0 && cd < 65536); + usart_putreg(usart_base, AVR32_USART_MR_OFFSET, mr); + usart_putreg(usart_base, AVR32_USART_BRGR_OFFSET, cd); +} +#endif + +/****************************************************************************** * Public Functions - **************************************************************************/ + ******************************************************************************/ -/************************************************************************************ +/****************************************************************************** + * Name: usart_reset + * + * Description: + * Reset a USART. + * + ******************************************************************************/ + +#ifdef HAVE_RS232_DEVICE +void usart_reset(uintptr_t usart_base) +{ + irqstate_t flags; + + /* Disable all USART interrupts */ + + flags = irqsave(); + usart_putreg(usart_base, AVR32_USART_IDR_OFFSET, 0xffffffff); + irqrestore(flags); + + /* Reset mode and other registers */ + + usart_putreg(usart_base, AVR32_USART_MR_OFFSET, 0); /* Reset mode register */ + usart_putreg(usart_base, AVR32_USART_RTOR_OFFSET, 0); /* Reset receiver timeout register */ + usart_putreg(usart_base, AVR32_USART_TTGR_OFFSET, 0); /* Reset transmitter timeguard reg */ + + /* Disable RX and TX, put USART in reset, disable handshaking signals */ + + usart_putreg(usart_base, AVR32_USART_CR_OFFSET, + USART_CR_RSTRX|USART_CR_RSTTX|USART_CR_RSTSTA|USART_CR_RSTIT| + USART_CR_RSTNACK|USART_CR_DTRDIS|USART_CR_RTSDIS); +} +#endif + +/****************************************************************************** + * Name: usart_configure + * + * Description: + * Configure a USART as a RS-232 UART. + * + ******************************************************************************/ + +#ifdef HAVE_RS232_DEVICE +void usart_configure(uintptr_t usart_base, uint32_t baud, unsigned int parity, + unsigned int nbits, bool stop2) +{ + uint32_t regval; + + /* Reset the USART and disable RX and TX */ + + usart_reset(usart_base); + + /* Set the baud rate generation register */ + + usart_setbaudrate(usart_base, baud); + + /* Configure STOP bits */ + + regval = USART_MR_MODE_NORMAL|USART_MR_CHMODE_NORMAL; /* Normal RS-232 mode */ + regval |= stop2 ? USART_MR_NBSTOP_2 : USART_MR_NBSTOP_1; + + /* Configure parity */ + + switch (parity) + { + case 0: + default: + regval |= USART_MR_PAR_NONE; + break; + + case 1: + regval |= USART_MR_PAR_ODD; + break; + + case 2: + regval |= USART_MR_PAR_EVEN; + break; + } + + /* Configure the number of bits per word */ + + DEBUGASSERT(nbits >= 5 && nbits <= 9); + if (nbits == 9) + { + regval |= USART_MR_MODE9; + } + else + { + regval |= USART_MR_CHRL_BITS(nbits); + } + + usart_putreg(usart_base, AVR32_USART_MR_OFFSET, regval); + + /* Enable RX and TX */ + + regval = usart_getreg(usart_base, AVR32_USART_CR_OFFSET); + regval |= (USART_CR_RXEN|USART_CR_TXEN); + usart_putreg(usart_base, AVR32_USART_CR_OFFSET, regval); +} +#endif + +/****************************************************************************** * Name: up_consoleinit * * Description: - * Initialize a console for debug output. + * Initialize a console for debug output. This function is called very + * early in the intialization sequence to configure the serial console uart + * (only). * - ************************************************************************************/ + ******************************************************************************/ void up_consoleinit(void) { -#warning "Not Implemented" +#ifdef HAVE_SERIAL_CONSOLE + usart_configure(AVR32_CONSOLE_BASE, AVR32_CONSOLE_BAUD, AVR32_CONSOLE_PARITY, + AVR32_CONSOLE_BITS, (bool)AVR32_CONSOLE_2STOP); +# warning "Probably not all Implemented" +#endif } -/************************************************************************************ +/****************************************************************************** * Name: up_lowputc * * Description: * Output one byte on the serial console * - ************************************************************************************/ + ******************************************************************************/ void up_lowputc(char ch) { #ifdef HAVE_SERIAL_CONSOLE - /* Wait until the TX data register is empty */ -#warning "Not Implemented" + /* Wait until the TX to become ready */ + + while (usart_getreg(AVR32_CONSOLE_BASE, USART_CSR_TXRDY) == 0); /* Then send the character */ -#warning "Not Implemented" + + usart_putreg(AVR32_CONSOLE_BASE, AVR32_USART_THR_OFFSET, (uint32_t)ch); #endif } diff --git a/arch/avr/src/at91uc3/at91uc3_lowinit.c b/arch/avr/src/at91uc3/at91uc3_lowinit.c index 38aa67937c..071e86690f 100644 --- a/arch/avr/src/at91uc3/at91uc3_lowinit.c +++ b/arch/avr/src/at91uc3/at91uc3_lowinit.c @@ -87,7 +87,7 @@ void up_lowinit(void) up_clkinitialize(); - /* Initialize a console */ + /* Initialize a console (probably a serial console) */ up_consoleinit(); @@ -95,7 +95,7 @@ void up_lowinit(void) * available as soon as possible). */ -#ifdef CONFIG_USE_SERIALDRIVER +#ifdef CONFIG_USE_EARLYSERIALINIT up_earlyserialinit(); #endif diff --git a/arch/avr/src/at91uc3/at91uc3_serial.c b/arch/avr/src/at91uc3/at91uc3_serial.c index 8936298d29..9cd550384c 100644 --- a/arch/avr/src/at91uc3/at91uc3_serial.c +++ b/arch/avr/src/at91uc3/at91uc3_serial.c @@ -143,14 +143,14 @@ struct up_dev_s { - uint32_t usartbase; /* Base address of USART registers */ - uint32_t baud; /* Configured baud */ - uint32_t ie; /* Saved interrupt mask bits value */ - uint32_t sr; /* Saved status bits */ - uint8_t irq; /* IRQ associated with this USART */ - uint8_t parity; /* 0=none, 1=odd, 2=even */ - uint8_t bits; /* Number of bits (7 or 8) */ - bool stopbits2; /* true: Configure with 2 stop bits instead of 1 */ + uintptr_t usartbase; /* Base address of USART registers */ + uint32_t baud; /* Configured baud */ + uint32_t ie; /* Saved interrupt mask bits value */ + uint32_t sr; /* Saved status bits */ + uint8_t irq; /* IRQ associated with this USART */ + uint8_t parity; /* 0=none, 1=odd, 2=even */ + uint8_t bits; /* Number of bits (7 or 8) */ + bool stopbits2; /* true: Configure with 2 stop bits instead of 1 */ }; /**************************************************************************** @@ -361,78 +361,12 @@ static inline void up_disableusartint(struct up_dev_s *priv, uint16_t *ie) static int up_setup(struct uart_dev_s *dev) { struct up_dev_s *priv = (struct up_dev_s*)dev->priv; + #ifdef CONFIG_SUPPRESS_UART_CONFIG - uint32_t regval; + /* Configure the USART as an RS-232 UART */ - /* Configure STOP bits */ - - regval = uUSART_MR_MODE_NORMAL; - if (priv->stopbits2) - { - regval |= USART_MR_NBSTOP_2; - } - else - { - regval |= USART_MR_NBSTOP_1; - } - - /* Configure parity */ - - switch (priv->parity) - { - case 0: - default: - regval |= USART_MR_PAR_NONE; - break; - - case 1: - regval |= USART_MR_PAR_ODD; - break; - - case 2: - regval |= USART_MR_PAR_EVEN; - break; - } - - /* Configure the number of bits per word */ - - switch (priv->bits) - { - case 5: - regval |= USART_MR_CHRL_5BITS; - break; - - case 6: - regval |= USART_MR_CHRL_6BITS; - break; - - case 7: - regval |= USART_MR_CHRL_7BITS; - break; - - case 8: - default: - regval |= USART_MR_CHRL_8BITS; - break; - - case 9: - regval |= USART_MR_MODE9; - break; - } - - regval = up_serialout(priv, AVR32_USART_MR_OFFSET, regval); - - /* Disable interrupts at the UART */ -#warning "Not Implemented" - - /* Configure hardware flow control -- Not yet supported */ -#warning "Not Implemented" - - /* Configure the USART Baud Rate */ -#warning "Not Implemented" - - /* Enable Rx, Tx, and the USART */ -#warning "Not Implemented" + usart_configure(priv->usartbase, priv->baud, priv->parity, + priv->bits, priv->stopbits2); #endif /* Initialize the IMR shadow register */ @@ -453,15 +387,10 @@ static int up_setup(struct uart_dev_s *dev) static void up_shutdown(struct uart_dev_s *dev) { struct up_dev_s *priv = (struct up_dev_s*)dev->priv; - uint32_t regval; - /* Disable all interrupts */ + /* Reset, disable interrupts, and disable Rx and Tx */ - up_disableusartint(priv, NULL); - - /* Disable Rx, Tx, and the UART */ - -#warning "Not Implemented" + usart_reset(priv->usartbase); } /**************************************************************************** diff --git a/arch/avr/src/at91uc3/at91uc3_usart.h b/arch/avr/src/at91uc3/at91uc3_usart.h index 56c4e864da..846d5c8338 100755 --- a/arch/avr/src/at91uc3/at91uc3_usart.h +++ b/arch/avr/src/at91uc3/at91uc3_usart.h @@ -147,13 +147,13 @@ #define USART_MR_MODE_SHIFT (0) #define USART_MR_MODE_MASK (15 << USART_MR_MODE_SHIFT) -# define USART_MR_MODE_NORMAL (0 << USART_MR_MODE_SHIFT) /* Normal */ -# define USART_MR_MODE_RS485 (1 << USART_MR_MODE_SHIFT) /* RS485 */ -# define USART_MR_MODE_HW (2 << USART_MR_MODE_SHIFT) /* Hardware Handshaking */ -# define USART_MR_MODE_MODEM (3 << USART_MR_MODE_SHIFT) /* Modem */ -# define USART_MR_MODE_T0 (4 << USART_MR_MODE_SHIFT) /* IS07816 Protocol: T = 0 */ -# define USART_MR_MODE_T1 (6 << USART_MR_MODE_SHIFT) /* IS07816 Protocol: T = 1 */ -# define USART_MR_MODE_IRDA (8 << USART_MR_MODE_SHIFT) /* IrDA */ +# define USART_MR_MODE_NORMAL (0 << USART_MR_MODE_SHIFT) /* Normal */ +# define USART_MR_MODE_RS485 (1 << USART_MR_MODE_SHIFT) /* RS485 */ +# define USART_MR_MODE_HW (2 << USART_MR_MODE_SHIFT) /* Hardware Handshaking */ +# define USART_MR_MODE_MODEM (3 << USART_MR_MODE_SHIFT) /* Modem */ +# define USART_MR_MODE_T0 (4 << USART_MR_MODE_SHIFT) /* IS07816 Protocol: T = 0 */ +# define USART_MR_MODE_T1 (6 << USART_MR_MODE_SHIFT) /* IS07816 Protocol: T = 1 */ +# define USART_MR_MODE_IRDA (8 << USART_MR_MODE_SHIFT) /* IrDA */ # define USART_MR_MODE_MASTER (14 << USART_MR_MODE_SHIFT) /* SPI Master */ # define USART_MR_MODE_SLAVE (15 << USART_MR_MODE_SHIFT) /* SPI Slave */ #define USART_MR_USCLKS_SHIFT (4) /* Bits 4-5: Clock Selection */ @@ -163,6 +163,7 @@ # define USART_MR_USCLKS_CLK (3 << USART_MR_USCLKS_SHIFT) /* CLK */ #define USART_MR_CHRL_SHIFT (6) /* Bit 6-7: Character Length */ #define USART_MR_CHRL_MASK (3 << USART_MR_CHRL_SHIFT) +# define USART_MR_CHRL_BITS(n) (((n) - 5) << USART_MR_CHRL_SHIFT) # define USART_MR_CHRL_5BITS (0 << USART_MR_CHRL_SHIFT) /* 5 bits */ # define USART_MR_CHRL_6BITS (1 << USART_MR_CHRL_SHIFT) /* 6 bits */ # define USART_MR_CHRL_7BITS (2 << USART_MR_CHRL_SHIFT) /* 7 bits */ diff --git a/arch/avr/src/common/up_internal.h b/arch/avr/src/common/up_internal.h index 417ffeb601..e5aabc6c74 100644 --- a/arch/avr/src/common/up_internal.h +++ b/arch/avr/src/common/up_internal.h @@ -59,16 +59,6 @@ #undef CONFIG_SUPPRESS_UART_CONFIG /* DEFINED: Do not reconfig UART */ #undef CONFIG_DUMP_ON_EXIT /* DEFINED: Dump task state on exit */ -/* Determine which (if any) console driver to use */ - -#if CONFIG_NFILE_DESCRIPTORS == 0 || defined(CONFIG_DEV_LOWCONSOLE) -# undef CONFIG_USE_SERIALDRIVER -# undef CONFIG_USE_EARLYSERIALINIT -#elif defined(CONFIG_DEV_CONSOLE) && CONFIG_NFILE_DESCRIPTORS > 0 -# define CONFIG_USE_SERIALDRIVER 1 -# define CONFIG_USE_EARLYSERIALINIT 1 -#endif - /* Check if an interrupt stack size is configured */ #ifndef CONFIG_ARCH_INTERRUPTSTACK diff --git a/configs/avr32dev1/include/board.h b/configs/avr32dev1/include/board.h index 6361071985..8f38b2e4f4 100755 --- a/configs/avr32dev1/include/board.h +++ b/configs/avr32dev1/include/board.h @@ -48,6 +48,20 @@ ************************************************************************************/ /* Clocking *************************************************************************/ +/* #define AVR32_FRCOSC 15200 RCOsc frequency in Hz */ + +#define AVR32_FOSC32 32768 /* Osc32 frequency in Hz */ +#define AVR32_OSC32STARTUP 3 /* Osc32 startup time in RCOsc periods */ + +#define AVR32_FOSC0 12000000 /* Osc0 frequency in Hz */ +#define AVR32_OSC0STARTUP 3 /* Osc0 startup time in RCOsc periods. + +/* #define AVR32_FOSC1 12000000 Osc1 frequency: Hz. + * #define AVR32_OSC1STARTUP 3 Osc1 startup time in RCOsc periods. + */ + +#define AVR32_CPU_CLOCK AVR32_FOSC0 +#define AVR32_PBA_CLOCK AVR32_FOSC0 /* LED definitions ******************************************************************/