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

# uart.c

    00001 /*! \file uart.c \brief UART driver for ARM LPC2000 16550. */
    00002 //*****************************************************************************
    00003 //
    00004 // File Name    : 'uart.c'
    00005 // Title        : UART driver for ARM LPC2000 16550
    00006 // Author       : Pascal Stang - Copyright (C) 2004
    00007 // Created      : 4/2/2004
    00008 // Revised      : 4/2/2004
    00009 // Version      : 0.1
    00010 // Target MCU   : Philips ARM LPC2000 Series
    00011 // Editor Tabs  : 4
    00012 //
    00013 // Thanks to Bill Knight for providing the excellent processor/uart
    00014 //  #defines used in this driver.
    00015 //
    00016 // This code is distributed under the GNU Public License
    00017 //      which can be found at http://www.gnu.org/licenses/gpl.txt
    00018 //
    00019 //*****************************************************************************
    00020
    00021 #include <limits.h>
    00022 #include "LPC2000.h"
    00023
    00024 #include "global.h"
    00025 #include "uart.h"
    00026
    00027 //! enable and initialize the uart
    00028 void uart0Init(uint16_t baud, uint8_t mode, uint8_t fifomode)
    00029 {
    00030     // set pin-select register to connect to UART0
    00031     PINSEL0 = (PINSEL0 & ~U0_PINMASK) | U0_PINSEL;
    00032     // reset and initialize the UART
    00033     U0IER = 0;      // disable all interrupt sources
    00034     U0IIR;          // clear any pending interrupts
    00035     U0FCR = (UFCR_TX_FIFO_RESET|UFCR_RX_FIFO_RESET);    // reset the Tx/Rx FIFOs
    00036     // set the baudrate divisor
    00037     U0LCR = ULCR_DLAB_ENABLE;   // set divisor access bit (permits modification of divisors)
    00038     U0DLL = (baud);             // set the baud division
    00039     U0DLM = (baud>>8);
    00040     U0LCR = 0;                  // reset DLAB (normal operation)
    00041     // set line parameters and fifo mode
    00042     U0LCR = mode;
    00043     U0FCR = fifomode;
    00044     // with uart configured, clear line status and receive registers
    00045     U0RBR; U0LSR;
    00046 }
    00047
    00048 void uart1Init(uint16_t baud, uint8_t mode, uint8_t fifomode)
    00049 {
    00050     // set pin-select register to connect to UART1
    00051     PINSEL0 = (PINSEL0 & ~U1_PINMASK) | U1_PINSEL;
    00052     // reset and initialize the UART
    00053     U1IER = 0;      // disable all interrupt sources
    00054     U1IIR;          // clear any pending interrupts
    00055     U1FCR = (UFCR_TX_FIFO_RESET|UFCR_RX_FIFO_RESET);    // reset the Tx/Rx FIFOs
    00056     // set the baudrate divisor
    00057     U1LCR = ULCR_DLAB_ENABLE;   // set divisor access bit (permits modification of divisors)
    00058     U1DLL = (baud);             // set the baud division
    00059     U1DLM = (baud>>8);
    00060     U1LCR = 0;                  // reset DLAB (normal operation)
    00061     // set line parameters and fifo mode
    00062     U1LCR = mode;
    00063     U1FCR = fifomode;
    00064     // with uart configured, clear line status and receive registers
    00065     U1RBR; U1LSR;
    00066 }
    00067
    00068 int uart0SendByte(int data)
    00069 {
    00070     // wait for tx buffer ready
    00071     while( !(U0LSR & ULSR_THRE) );
    00072     // send the character
    00073     return (U0THR = data);
    00074 }
    00075
    00076 int uart1SendByte(int data)
    00077 {
    00078     // wait for tx buffer ready
    00079     while( !(U1LSR & ULSR_THRE) );
    00080     // send the character
    00081     return (U1THR = data);
    00082 }
    00083
    00084 int uart0GetByte(void)
    00085 {
    00086     // if character is available, return it
    00087     if(U0LSR & ULSR_RDR)
    00088         return (U0RBR);
    00089     // otherwise, return failure
    00090     return -1;
    00091 }
    00092
    00093 int uart1GetByte(void)
    00094 {
    00095     // if character is available, return it
    00096     if(U1LSR & ULSR_RDR)
    00097         return (U1RBR);
    00098     // otherwise, return failure
    00099     return -1;
    00100 }