Embedded Freaks..

August 16, 2009

LPC1766 Not So Reliable UART0 Loopback

Filed under: Cortex-M — Tags: — kunilkuda @ 1:35 pm

Let’s continue the last post with the real interrupt based program: UART0 interrupt-based loopback. The program example will echoing anything that is sent to the serial port, but since it’s not checking whether the TX FIFO is empty or not during the loopback, some data may be lost if you sent a file (or sending a lot of data).

The whole application can be downloaded here.

The UART peripheral on LPC17xx works similar to the other LPC2000 series. The initialisation steps are:

 // Change P0.2 and P0.3 mode to TXD0 and RXD0, assuming the each pins are pulled up (PINMODE = 00),
 // and open drain mode is off (PINMODE_OD = 0).
 // If you have to set the PINMODE, don't pull down the TXD0 pin.
 PINCON->PINSEL0 = (1 << 4) | (1 << 6);

 // Set 8N1 mode
 UART0->LCR &= ~STOP_1_BIT & ~PARITY_NONE;
 UART0->LCR |= CHAR_8_BIT;

 // Set the baud rate (ie. fill up the DLM, DLL, and FDR)
 uart_set_baudrate(baudrate);

 // Enable TX and RX FIFO. This step is optional, but since UART's FIFO is there, and we've paid
 // for it, I'd like to enable it.
 UART0->FCR |= FIFO_ENABLE;

 // Set FIFO to trigger when at least 14 characters available (only needed if FIFO is enabled)
 UART0->FCR |= (3 << 6);

 // Enable UART RX interrupt (for LPC17xx UART). Note, there's 3 different interrupt under UART0
 // interrupt (TX buffer empty/ THRE, RX buffer full / RDA, or character received timeout / CTI)
 // Read LPC17xx user manual for the details
 UART0->IER = RBR_IRQ_ENABLE;

 // Enable the UART interrupt (for Cortex-CM3 NVIC)
 NVIC_EnableIRQ(UART0_IRQn);

If you have work with LPC2000 series before, the whole initialisation code is similar, except for the last part ‘NVIC_EnableIRQ(UART0_IRQn)’. The NVIC_EnableIRQ() is used to enable the UART0 interrupt on NVIC.

The next step, after the UART initialisation, is to enable the global interrupt, like this:

int main(void) {
 uart_init(115200);
 __enable_irq();

 while (1) {
  __WFI();
 }

 return 0;
}

The __enable_irq() is a macro from ‘core_cm3.h’, which will be expanded to ‘asm volatile(“cpsie i”)’. The __WFI() is also a macro for ‘asm volatile(“wfi”)’, to enter the sleep mode until the interrupt comes.

Now, how to handle the UART0 interrupt ? Take a look at your startup code:

void __attribute__((weak)) UART0_IRQHandler(void);      // 21: UART0
// ....
__attribute__ ((section(".cs3.interrupt_vector")))
void (* const g_pfnVectors[])(void) = {
 //..
 UART0_IRQHandler, // 21: UART0
};
//..
#pragma weak UART0_IRQHandler       = Default_Handler // 21: UART0

The startup code said that UART0 interrupt handler is named as UART0_IRQHandler, and it’s temporarily linked to Default_Handler(). It means that if you declare a function named: ‘void UART0_IRQHandler(void)’, the temporary link to Default_Handler() will be overridden. That’s how you’re handling the interrupt, by creating the same function name as in the startup file:

/*
 * UART0 interrupt handler
 */
void UART0_IRQHandler(void) {
 switch(UART0->IIR & 0x0E) {
 case RDA_INTERRUPT:
 case CTI_INTERRUPT:
  // Loopback to send
  UART0->THR = UART0->RBR;
  break;
 }
}

In the interrupt handler, we still need to read IIR register, since LPC17xx implementation multiplexed several interrupts into a single interrupt.

Advertisements

4 Comments »

  1. It seems that the download link for the whole application doesn’t work anymore.

    Comment by Guest — December 23, 2010 @ 11:30 pm

  2. Seems like you’re right..Either they’ve deleted my file, or it’s pointing to the wrong link. Will check it once I got a chance. Busy holiday for me..

    Merry Christmas !!

    Comment by kunilkuda — December 24, 2010 @ 12:33 am

  3. Ok..Uploaded my archive and fixed the link

    Comment by kunilkuda — March 24, 2011 @ 7:33 am

  4. […] LPC1766 Not So Reliable UART0 Loopback (via Embedded Freaks..) 2011/04/13 bygreencn Leave a comment Go to comments Let's continue the last post with the real interrupt based program: UART0 interrupt-based loopback. The program example will echoing anything that is sent to the serial port, but since it's not checking whether the TX FIFO is empty or not during the loopback, some data may be lost if you sent a file (or sending a lot of data). The whole application can be downloaded here. The UART peripheral on LPC17xx works similar to the other LPC2000 series. T … Read More […]

    Pingback by LPC1766 Not So Reliable UART0 Loopback (via Embedded Freaks..) « Green World — April 13, 2011 @ 12:27 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: