Here is some UART code I use:
Code: Select all//------------------------ UART1 ISR ------------------------//
static void uart1ISR(void)
{
/* Read IIR to clear interrupt and find out the cause */
unsigned iir = U1IIR;
/* Handle UART1 interrupt */
switch ((iir >> 1) & 0x7)
{
case 1:
/* THRE interrupt */
break;
case 2:
/* RDA interrupt */
do
{
uint16_t temp;
// calc next insert index & store character
temp = (uart1_rx_insert_idx + 1) % UART1_RX_BUFFER_SIZE;
uart1_rx_buffer[uart1_rx_insert_idx] = U1RBR;
// check for more room in queue
if (temp != uart1_rx_extract_idx)
uart1_rx_insert_idx = temp; // update insert index
}
while (U1LSR & 0x01);
break;
case 3:
/* RLS interrupt */
break;
case 6:
/* CTI interrupt */
break;
}
}
/* Description:
* This function gets a character from the UART1 receive queue
*
* Calling Sequence:
* void
*
* Returns:
* character on success, -1 if no character is available
*
*****************************************************************************/
int uart1Getch(void)
{
uint8_t ch;
if (uart1_rx_insert_idx == uart1_rx_extract_idx) // check if character is available
return -1;
ch = uart1_rx_buffer[uart1_rx_extract_idx++]; // get character, bump pointer
uart1_rx_extract_idx %= UART1_RX_BUFFER_SIZE; // limit the pointer
return ch;
}
//put this in the init. function
// initialise PLL
PLL0CFG=0x24; // Cclk = 60Mhz
PLL0CON=0x01;
PLL0FEED=0xAA;
PLL0FEED=0x55;
while(!(PLL0STAT & 0x0400))
;
PLL0CON=0x3;
PLL0FEED=0xAA;
PLL0FEED=0x55;
// initialise MAM and VPB
MAMTIM=0x3; // 3 cycles to read from FLASH
MAMCR=0x2; // MAM functions fully enabled
APBDIV=0x02; // Pclk = 30MHz
// initialise pclk (30 MHz)
unsigned long pclk = liblpc2000_get_pclk(liblpc2000_get_cclk(OSCILLATOR_CLOCK_FREQUENCY));
// initialise UART1 (RxD1 P0.9, TxD1 P0.8
U1LCR = 0x83; // 8 bit, 1 stop bit, no parity, enable DLAB
U1DLL = 0xC3; // set for 9600 baud
U1DLM = 0x00;
U1LCR &= ~0x80; // disable DLAB
U1FCR = 1; // enable FIFO
PINSEL0 = PINSEL0 & ~(0xFFFF << 16) | (0x5555 << 16);
// initialise Rx circular buffer
uart1_rx_extract_idx = uart1_rx_insert_idx = 0;
You probably don't need the circular buffer, so you can remove the relevant code.
Leon