User manual

LPCXpresso Experiment Kit - User’s Guide
Page 107
Copyright 2013 © Embedded Artists AB
Create a program that outputs a message, with the help of printf(), on the UART channel and
receive the message on a terminal program on a PC. Also let the program verify that scanf()
works.
Remember that the UART must still be initialized before printf()/scanf() are used.
7.15.3 Lab 14c: Interrupt driven UART handling and ring buffers
Blocking function calls can be problematic since it can block other activities in a system. A way to
handle this is to create circular buffers, both for received characters and transmission. An interrupt
handler place received characters in the receive buffer. The program can then peek into the circular
buffer to check if there are any received characters. If not, execution can continue with other tasks.
Below is code the implements UART receive functionality with the help of interrupts and circular
buffers. The interrupt routine (ISR) handle both receive and transmit interrupts. Study the code to
understand how it works.
#include "lpc11xx.h"
#include "uart.h"
//size of transmit buffer - size MUST be power of two
#define TX_BUFFER_SIZE 256
#define TX_BUFFER_MASK (TX_BUFFER_SIZE-1)
//size of receive buffer - size MUST be power of two
#define RX_BUFFER_SIZE 256
#define RX_BUFFER_MASK (RX_BUFFER_SIZE-1)
#define LSR_RDR 0x01
#define LSR_OE 0x02
#define LSR_PE 0x04
#define LSR_FE 0x08
#define LSR_BI 0x10
#define LSR_THRE 0x20
#define LSR_TEMT 0x40
#define LSR_RXFE 0x80
#define IER_RBR 0x01
#define IER_THRE 0x02
#define IER_RLS 0x04
#define IIR_PEND 0x01
#define IIR_RLS 0x03
#define IIR_RDA 0x02
#define IIR_CTI 0x06
#define IIR_THRE 0x01
static volatile uint8_t txBuf[TX_BUFFER_SIZE];
static volatile uint32_t txHead = 0;
static volatile uint32_t txTail = 0;
static volatile uint8_t txRunning = FALSE;
static volatile uint8_t rxBuf[RX_BUFFER_SIZE];
static volatile uint32_t rxHead = 0;
static volatile uint32_t rxTail = 0;
/*****************************************************************************
** Function name: UARTInit
**
** Descriptions: Initialize UART0 port, setup pin select,
** clock, parity, stop bits, FIFO, etc.
**
** parameters: UART baudrate
** Returned value: None
**
*****************************************************************************/
void UARTInit(uint32_t baudrate)