lpcware

Using UART on LPC12xx

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by tbutuza on Sat May 14 02:24:32 MST 2011
I opened this thread to discuss about use UART on LPC12xx mcu.

Previously I used uart on LPC1769 without problems. Now I'd like to do this on LPC1227 but after several hours (days?) fighting, I did not have success.

I can send data on uart but receive is not working.
After initializing uart I receive only one character, but no further receive is possible.
I tried it with interrupts and polling. I also tried Uart0 and Uart1.
All 4 tests had the same result: Only one character can be received right after the initialization, and no more.

Similar code runs well on LPC1769 without problem. As I know the Uart  module is exactly the same in LPC1768 and LPC1227. Just the base  addresses are different.

I can send characters, it indicates that the power, clock are right.
Something wrong with the receiver side but what?

It is my code:

Initialization:
// =======================================
static void UART1Init (uint32_t baudrate) {
    uint32_t Fdiv;
    uint32_t pclk;

    // Assign txd, rxd lines to pins.
    LPC_IOCON->PIO0_8 = 0x02;
    LPC_IOCON->PIO0_9 = 0x02;

    // SYSAHBCLKCTRL  12. bit should be set to 1.

    pclk = UART_INPUT_FREQ;

    LPC_UART1->LCR = 0x83;        // 8 bits, no Parity, 1 Stop bit
    Fdiv = ( pclk / 16 ) / baudrate ;    // baud rate

    LPC_UART1->DLM = Fdiv / 256;                           
    LPC_UART1->DLL = Fdiv % 256;
    LPC_UART1->LCR = 0x03;        // DLAB = 0
    LPC_UART1->FCR = 0x07;        // Enable and reset TX and RX FIFO.


    //LPC_UART1->IER = IER_RBR | IER_THRE | IER_RLS;    // Enable UART0 interrupt
    //LPC_UART0->IER = IER_RBR;    // Enable UART0 interrupt (Only the receive data available interrupt is enabled.)

    //NVIC_EnableIRQ (UART1_IRQn);
}
The content of the main function:

    unsigned char lsrValue;

    UART0RcvSignal = 0;
    ledState = 0;
    counter = 0;

    // Disable watchdog
    LPC_WDT->MOD = 0x00;
    LPC_WDT->FEED = 0xAA;
    LPC_WDT->FEED = 0x55;

//    UART0Init (9600);
    UART1Init (9600);

    // enable all interrupts (Is this necessary ???)
    //__asm volatile ("cpsie i");

    LPC_GPIO0->DIR = LEDPIN;

    LPC_GPIO0->OUT = LEDPIN;    // Initially the led is on.

    // Enter an infinite loop
    while (1) {
        counter++;
        if (counter >= COUNTER_MAX) {
            counter = 0;
            LPC_UART1->THR = 'A';
        }

        lsrValue = LPC_UART1->LSR;
        if (lsrValue & 0x01) {
            UART0RcvSignal = 1;
            UART0RcvData = LPC_UART1->RBR;
        }

        if (UART0RcvSignal) {
            // Echo back the character
            LPC_UART1->THR = (UART0RcvData + 1);

            // Blink the led
            if (ledState) {
                ledState = 0;
                LPC_GPIO0->OUT = LEDPIN;
            } else {
                ledState = 1;
                LPC_GPIO0->OUT = 0;
            }
            UART0RcvSignal = 0;
        }
    }
As you see it is a polling-based program.
It sends characters periodiccaly. When a character received, it echoes (received + 1) and blinks the led.

Led blinks only once and character echoed only immediatelly after the initialization.
Subsequent receive is not possible, apparently the receiver part of the uart freezes.

What did I wrong?
Did I forget something?

Outcomes