Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

# uart.c

    00001 /*! \file uart.c \brief UART driver for AT91SAM7S. */
    00002 //*****************************************************************************
    00003 //
    00004 // File Name    : 'uart.c'
    00005 // Title        : UART driver for AT91SAM7S
    00006 // Author       : Pascal Stang - Copyright (C) 2004-2006
    00007 // Created      : 4/3/2004
    00008 // Revised      : 7/4/2006
    00009 // Version      : 0.1
    00010 // Target MCU   : Atmel ARM AT91SAM7S Series
    00011 // Editor Tabs  : 4
    00012 //
    00013 // This code is distributed under the GNU Public License
    00014 //      which can be found at http://www.gnu.org/licenses/gpl.txt
    00015 //
    00016 //*****************************************************************************
    00017
    00018 // AT91SAM7S definitions
    00019 #include "at91sam7s64.h"
    00020 #include "global.h"
    00021
    00022 #include "uart.h"
    00023
    00024 // Global Pointer to USARTs
    00025 //AT91S_USART * pUSART0 = AT91C_BASE_US0;
    00026 //AT91S_USART * pUSART1 = AT91C_BASE_US1;
    00027 #define pUSART0  ((AT91S_USART*)AT91C_BASE_US0)
    00028 #define pUSART1  ((AT91S_USART*)AT91C_BASE_US1)
    00029 #define pUSART2  ((AT91S_USART*)AT91C_BASE_DBGU)
    00030
    00031 //! enable and initialize the uart
    00032 void uart0Init(uint16_t bauddiv, uint32_t mode)
    00033 {
    00034     // enable the clock of UART0
    00035     AT91C_BASE_PMC->PMC_PCER = (1<<AT91C_ID_US0);
    00036     // enable uart pins on PIO
    00037     *AT91C_PIOA_PDR = AT91C_PA5_RXD0 | AT91C_PA6_TXD0;
    00038     // select peripheral connection
    00039     *AT91C_PIOA_ASR = AT91C_PA5_RXD0 | AT91C_PA6_TXD0;
    00040     // disable I/O pullup
    00041      *AT91C_PIOA_PPUDR = AT91C_PA5_RXD0;
    00042     // reset the UART
    00043     pUSART2->US_CR = AT91C_US_RSTRX | AT91C_US_RSTTX | AT91C_US_RXDIS |AT91C_US_TXDIS;
    00044     // set serial line mode
    00045     pUSART0->US_MR =    AT91C_US_USMODE_NORMAL |// Normal Mode
    00046                         AT91C_US_CLKS_CLOCK |   // Clock = MCK
    00047                         mode;                   // user-defined data parameters
    00048     // set the baud rate
    00049     pUSART0->US_BRGR = bauddiv;
    00050     // enable the uart
    00051     pUSART0->US_CR = AT91C_US_RXEN | AT91C_US_TXEN;
    00052 }
    00053
    00054 void uart1Init(uint16_t bauddiv, uint32_t mode)
    00055 {
    00056     // enable the clock of UART1
    00057     AT91C_BASE_PMC->PMC_PCER = (1<<AT91C_ID_US1);
    00058     // enable uart pins on PIO
    00059     *AT91C_PIOA_PDR = AT91C_PA21_RXD1 | AT91C_PA22_TXD1;
    00060     // select peripheral connection
    00061     *AT91C_PIOA_ASR = AT91C_PA21_RXD1 | AT91C_PA22_TXD1;
    00062     // disable I/O pullup
    00063     *AT91C_PIOA_PPUDR = AT91C_PA21_RXD1;
    00064     // reset the UART
    00065     pUSART2->US_CR = AT91C_US_RSTRX | AT91C_US_RSTTX | AT91C_US_RXDIS |AT91C_US_TXDIS;
    00066     // set serial line mode
    00067     pUSART1->US_MR =    AT91C_US_USMODE_NORMAL |// Normal Mode
    00068                         AT91C_US_CLKS_CLOCK |   // Clock = MCK
    00069                         mode;                   // user-defined data parameters
    00070     // set the baud rate
    00071     pUSART1->US_BRGR = bauddiv;
    00072     // enable the uart
    00073     pUSART1->US_CR = AT91C_US_RXEN | AT91C_US_TXEN;
    00074 }
    00075
    00076 void uart2Init(uint16_t bauddiv, uint32_t mode)
    00077 {
    00078     // enable the clock of DBGU
    00079     AT91C_BASE_PMC->PMC_PCER = (1<<AT91C_ID_SYS);
    00080     // enable uart pins on PIO
    00081     *AT91C_PIOA_PDR = AT91C_PA9_DRXD | AT91C_PA10_DTXD;
    00082     // reset the UART
    00083     pUSART2->US_CR = AT91C_US_RSTRX | AT91C_US_RSTTX | AT91C_US_RXDIS |AT91C_US_TXDIS;
    00084     // set serial line mode
    00085     pUSART2->US_MR =    AT91C_US_USMODE_NORMAL |    // Normal Mode
    00086                         AT91C_US_CLKS_CLOCK |       // Clock = MCK
    00087                         AT91C_US_CHRL_8_BITS |      // 8-bit Data (no effect on DBGU)
    00088                         AT91C_US_PAR_NONE |         // No Parity
    00089                         AT91C_US_NBSTOP_1_BIT;      // 1 Stop Bit (no effect on DBGU)
    00090     // set the baud rate
    00091     pUSART2->US_BRGR = bauddiv;
    00092     // enable the uart
    00093     pUSART2->US_CR = AT91C_US_RXEN | AT91C_US_TXEN;
    00094 }
    00095
    00096 int uart0SendByte(int data)
    00097 {
    00098     // wait for tx buffer ready
    00099     while( !(pUSART0->US_CSR & AT91C_US_TXRDY) );
    00100     // send the character
    00101     return (pUSART0->US_THR = data);
    00102 }
    00103
    00104 int uart1SendByte(int data)
    00105 {
    00106     // wait for tx buffer ready
    00107     while( !(pUSART1->US_CSR & AT91C_US_TXRDY) );
    00108     // send the character
    00109     return (pUSART1->US_THR = data);
    00110 }
    00111
    00112 int uart2SendByte(int data)
    00113 {
    00114     // wait for tx buffer ready
    00115     while( !(pUSART2->US_CSR & AT91C_US_TXRDY) );
    00116     // send the character
    00117     return (pUSART2->US_THR = data);
    00118 }
    00119
    00120 int uart0GetByte(void)
    00121 {
    00122     // if character is available, return it
    00123     if(pUSART0->US_CSR & AT91C_US_RXRDY)
    00124         return (pUSART0->US_RHR);
    00125     // otherwise, return failure
    00126     return -1;
    00127 }
    00128
    00129 int uart1GetByte(void)
    00130 {
    00131     // if character is available, return it
    00132     if(pUSART1->US_CSR & AT91C_US_RXRDY)
    00133         return (pUSART1->US_RHR);
    00134     // otherwise, return failure
    00135     return -1;
    00136 }
    00137
    00138 int uart2GetByte(void)
    00139 {
    00140     // if character is available, return it
    00141     if(pUSART2->US_CSR & AT91C_US_RXRDY)
    00142         return (pUSART2->US_RHR);
    00143     // otherwise, return failure
    00144     return -1;
    00145 }
    00146