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

# uartintr.c

    00001 /*! \file uartintr.c \brief UART driver for AT91SAM7S with interrupts. */
    00002 //*****************************************************************************
    00003 //
    00004 // File Name    : 'uartintr.c'
    00005 // Title        : UART driver for AT91SAM7S with interrupts
    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
    00021 #include "global.h"
    00022 #include "processor.h"
    00023 #include "buffer.h"
    00024 #include "uartintr.h"
    00025
    00026 // receive and transmit buffers
    00027 cBuffer uartRxBuffer[3];            ///< uart receive buffer
    00028 static unsigned char uart0RxData[UART0_RX_BUFFER_SIZE];
    00029 static unsigned char uart1RxData[UART1_RX_BUFFER_SIZE];
    00030 static unsigned char uart2RxData[UART2_RX_BUFFER_SIZE];
    00031 //cBuffer uartTxBuffer[3];          ///< uart transmit buffer
    00032 //static unsigned char uart0TxData[UART0_TX_BUFFER_SIZE];
    00033 //static unsigned char uart1TxData[UART1_TX_BUFFER_SIZE];
    00034 //static unsigned char uart2TxData[UART2_TX_BUFFER_SIZE];
    00035
    00036 // Global Pointer to USARTs
    00037 //AT91S_USART * pUSART0 = AT91C_BASE_US0;
    00038 //AT91S_USART * pUSART1 = AT91C_BASE_US1;
    00039 //AT91S_USART * pUSART2 = AT91C_BASE_DBGU;
    00040 #define pUSART0  ((AT91S_USART*)AT91C_BASE_US0)
    00041 #define pUSART1  ((AT91S_USART*)AT91C_BASE_US1)
    00042 #define pUSART2  ((AT91S_USART*)AT91C_BASE_DBGU)
    00043
    00044 //! enable and initialize the uart
    00045 void uart0Init(uint16_t bauddiv, uint32_t mode)
    00046 {
    00047     // enable the clock of UART0
    00048     AT91C_BASE_PMC->PMC_PCER = (1<<AT91C_ID_US0);
    00049     // enable uart pins on PIO
    00050     *AT91C_PIOA_PDR = AT91C_PA5_RXD0 | AT91C_PA6_TXD0;
    00051     // select peripheral connection
    00052     *AT91C_PIOA_ASR = AT91C_PA5_RXD0 | AT91C_PA6_TXD0;
    00053     // disable I/O pullup
    00054     *AT91C_PIOA_PPUDR = AT91C_PA5_RXD0;
    00055     // reset the UART
    00056     pUSART2->US_CR = AT91C_US_RSTRX | AT91C_US_RSTTX | AT91C_US_RXDIS |AT91C_US_TXDIS;
    00057     // set serial line mode
    00058     pUSART0->US_MR =    AT91C_US_USMODE_NORMAL |// Normal Mode
    00059                         AT91C_US_CLKS_CLOCK |   // Clock = MCK
    00060                         mode;                   // user-defined data parameters
    00061     // set the baud rate
    00062     pUSART0->US_BRGR = bauddiv;
    00063     // enable the uart
    00064     pUSART0->US_CR = AT91C_US_RXEN | AT91C_US_TXEN;
    00065     // initialize buffers
    00066     bufferInit(&uartRxBuffer[0], uart0RxData, UART0_RX_BUFFER_SIZE);
    00067     //bufferInit(&uartTxBuffer[0], uart0TxData, UART0_TX_BUFFER_SIZE);
    00068     // attach interrupt handler
    00069     processorAicAttach(AT91C_ID_US0, (UART0_INTERRUPT_LEVEL|AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL), uart0Service);
    00070     // enable receive interrupt
    00071     pUSART0->US_IER = AT91C_US_RXRDY;
    00072 }
    00073
    00074 void uart1Init(uint16_t bauddiv, uint32_t mode)
    00075 {
    00076     // enable the clock of UART1
    00077     AT91C_BASE_PMC->PMC_PCER = (1<<AT91C_ID_US1);
    00078     // enable uart pins on PIO
    00079     *AT91C_PIOA_PDR = AT91C_PA21_RXD1 | AT91C_PA22_TXD1;
    00080     // select peripheral connection
    00081     *AT91C_PIOA_ASR = AT91C_PA21_RXD1 | AT91C_PA22_TXD1;
    00082     // disable I/O pullup
    00083     *AT91C_PIOA_PPUDR = AT91C_PA21_RXD1;
    00084     // reset the UART
    00085     pUSART2->US_CR = AT91C_US_RSTRX | AT91C_US_RSTTX | AT91C_US_RXDIS |AT91C_US_TXDIS;
    00086     // set serial line mode
    00087     pUSART1->US_MR =    AT91C_US_USMODE_NORMAL |// Normal Mode
    00088                         AT91C_US_CLKS_CLOCK |   // Clock = MCK
    00089                         mode;                   // user-defined data parameters
    00090     // set the baud rate
    00091     pUSART1->US_BRGR = bauddiv;
    00092     // enable the uart
    00093     pUSART1->US_CR = AT91C_US_RXEN | AT91C_US_TXEN;
    00094     // initialize buffers
    00095     bufferInit(&uartRxBuffer[1], uart1RxData, UART1_RX_BUFFER_SIZE);
    00096     //bufferInit(&uartTxBuffer[1], uart1TxData, UART1_TX_BUFFER_SIZE);
    00097     // attach interrupt handler
    00098     processorAicAttach(AT91C_ID_US1, (UART1_INTERRUPT_LEVEL|AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL), uart1Service);
    00099     // enable receive interrupt
    00100     pUSART1->US_IER = AT91C_US_RXRDY;
    00101 }
    00102
    00103 void uart2Init(uint16_t bauddiv, uint32_t mode)
    00104 {
    00105     // enable the clock of DBGU
    00106     AT91C_BASE_PMC->PMC_PCER = (1<<AT91C_ID_SYS);
    00107     // enable uart pins on PIO
    00108     *AT91C_PIOA_PDR = AT91C_PA9_DRXD | AT91C_PA10_DTXD;
    00109     // disable I/O pullup
    00110     *AT91C_PIOA_PPUDR = AT91C_PA9_DRXD;
    00111     // reset the UART
    00112     pUSART2->US_CR = AT91C_US_RSTRX | AT91C_US_RSTTX | AT91C_US_RXDIS |AT91C_US_TXDIS;
    00113     // set serial line mode
    00114     pUSART2->US_MR =    AT91C_US_USMODE_NORMAL |    // Normal Mode
    00115                         AT91C_US_CLKS_CLOCK |       // Clock = MCK
    00116                         AT91C_US_CHRL_8_BITS |      // 8-bit Data (no effect on DBGU)
    00117                         AT91C_US_PAR_NONE |         // No Parity
    00118                         AT91C_US_NBSTOP_1_BIT;      // 1 Stop Bit (no effect on DBGU)
    00119     // set the baud rate
    00120     pUSART2->US_BRGR = bauddiv;
    00121     // enable the uart
    00122     pUSART2->US_CR = AT91C_US_RXEN | AT91C_US_TXEN;
    00123     // initialize buffers
    00124     bufferInit(&uartRxBuffer[2], uart2RxData, UART2_RX_BUFFER_SIZE);
    00125     //bufferInit(&uartTxBuffer[2], uart2TxData, UART2_TX_BUFFER_SIZE);
    00126     // attach interrupt handler
    00127     processorAicAttachSys(SYSPID_DBGU, uart2Service);
    00128     // enable receive interrupt
    00129     pUSART2->US_IER = AT91C_US_RXRDY;
    00130 }
    00131
    00132 cBuffer* uart0GetRxBuffer(int dev)
    00133 {
    00134     // return rx buffer pointer
    00135     return &uartRxBuffer[dev];
    00136 }
    00137
    00138 int uart0SendByte(int data)
    00139 {
    00140     // wait for tx buffer ready
    00141     while( !(pUSART0->US_CSR & AT91C_US_TXRDY) );
    00142     // send the character
    00143     return (pUSART0->US_THR = data);
    00144 }
    00145
    00146 int uart1SendByte(int data)
    00147 {
    00148     // wait for tx buffer ready
    00149     while( !(pUSART1->US_CSR & AT91C_US_TXRDY) );
    00150     // send the character
    00151     return (pUSART1->US_THR = data);
    00152 }
    00153
    00154 int uart2SendByte(int data)
    00155 {
    00156     // wait for tx buffer ready
    00157     while( !(pUSART2->US_CSR & AT91C_US_TXRDY) );
    00158     // send the character
    00159     return (pUSART2->US_THR = data);
    00160 }
    00161
    00162 int uart0GetByte(void)
    00163 {
    00164     if(uartRxBuffer[0].datalength)      // check if character is available
    00165         return bufferGetFromFront(&uartRxBuffer[0]);    // return character
    00166     return -1;
    00167 }
    00168
    00169 int uart1GetByte(void)
    00170 {
    00171     if(uartRxBuffer[1].datalength)      // check if character is available
    00172         return bufferGetFromFront(&uartRxBuffer[1]);    // return character
    00173     return -1;
    00174 }
    00175
    00176 int uart2GetByte(void)
    00177 {
    00178     if(uartRxBuffer[2].datalength)      // check if character is available
    00179         return bufferGetFromFront(&uartRxBuffer[2]);    // return character
    00180     return -1;
    00181 }
    00182
    00183 void uart0Service(void)
    00184 {
    00185     unsigned int status;
    00186
    00187     // read the channel status register
    00188     status  = pUSART0->US_CSR;
    00189     status &= pUSART0->US_IMR;
    00190
    00191     if(status & AT91C_US_RXRDY)
    00192     {
    00193         bufferAddToEnd(&uartRxBuffer[0], pUSART0->US_RHR);
    00194     }
    00195
    00196     if(status & AT91C_US_TXRDY)
    00197     {
    00198     }
    00199
    00200     // reset error status bits
    00201     pUSART0->US_CR = AT91C_US_RSTSTA;
    00202     // clear AIC
    00203     AT91C_BASE_AIC->AIC_EOICR = 0;
    00204 }
    00205
    00206 void uart1Service(void)
    00207 {
    00208     unsigned int status;
    00209
    00210     // read the channel status register
    00211     status  = pUSART1->US_CSR;
    00212     status &= pUSART1->US_IMR;
    00213
    00214     if(status & AT91C_US_RXRDY)
    00215     {
    00216         bufferAddToEnd(&uartRxBuffer[1], pUSART1->US_RHR);
    00217     }
    00218
    00219     if(status & AT91C_US_TXRDY)
    00220     {
    00221     }
    00222
    00223     // reset error status bits
    00224     pUSART1->US_CR = AT91C_US_RSTSTA;
    00225     // clear AIC
    00226     AT91C_BASE_AIC->AIC_EOICR = 0;
    00227 }
    00228
    00229 void uart2Service(void)
    00230 {
    00231     unsigned int status;
    00232
    00233     // read the channel status register
    00234     status  = pUSART2->US_CSR;
    00235     status &= pUSART2->US_IMR;
    00236
    00237     if(status & AT91C_US_RXRDY)
    00238     {
    00239         bufferAddToEnd(&uartRxBuffer[2], pUSART2->US_RHR);
    00240     }
    00241
    00242     if(status & AT91C_US_TXRDY)
    00243     {
    00244     }
    00245
    00246     // reset error status bits
    00247     pUSART2->US_CR = AT91C_US_RSTSTA;
    00248     // clear AIC
    00249 //  AT91C_BASE_AIC->AIC_EOICR = 0;
    00250 }