Content originally posted in LPCWare by paganborn25 on Tue Jul 31 19:15:21 MST 2012
Here's the code. The UART interrupt triggers on reset (???) and never again no matter how many times I send serial data. Trying to blink an LED once serial data is received. Is there a mistake in assuming the clock default as 12MHz??? Do I need to configure SYSREMAP to RAM??? Or is there some assembly code I need to write before this...
//Code
#include "stdint.h"
#define __PTR *(volatile uint32_t*)
#define ISER1 __PTR 0xE000E104
#define ICER1 __PTR 0xE000E184
#define SYSAHBCLKCTRL __PTR 0x40048080
#define SYSAHBCLKDIV __PTR 0x40048078
#define U0IER __PTR 0x40008004
#define U0RBR __PTR 0x40008000
#define U0THR __PTR 0x40008000
#define U0IIR __PTR 0x40008008 //Some places in the manual suggest //40048008.
#define U0FCR __PTR 0x40008008
#define U0LSR __PTR 0x40008014
#define U0LCR __PTR 0x4000800C
#define GPIO0DIR __PTR 0x50008000
#define GPIO0DATA __PTR 0x50003FFC
#define U0DLL __PTR 0x40008000
#define U0DLM __PTR 0x40008004
#define IOCON_PIO1_6 __PTR 0x400440A4
#define IOCON_PIO1_7 __PTR 0x400440A8
#define UARTCLKDIV __PTR 0x40048098
#define SystemFrequency 12000000 //The default RC oscillator is 12MHz
#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
#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 BUFSIZE 0x40
void UARTInit(uint32_t Baudrate);
void UART_IRQHandler(void);
void UARTSend(uint8_t *BufferPtr, uint32_t Length);
void blink_led(void);
void UART_IRQHandler(void)
{
blink_led();
}
void UARTInit(uint32_t baudrate)
{
//The default RC oscillator is used at 12MHz
uint32_t Fdiv;
uint32_t regVal;
ICER1|=1<<14;
IOCON_PIO1_6 &= ~0x07; /* UART I/O config */
IOCON_PIO1_6 |= 0x01; /* UART RXD */
IOCON_PIO1_7 &= ~0x07;
IOCON_PIO1_7 |= 0x01; /* UART TXD */
/* Enable UART clock */
SYSAHBCLKCTRL |= (1<<12);
UARTCLKDIV = 0x1; /* divided by 1 */
U0LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */
regVal = UARTCLKDIV;
Fdiv = (((SystemFrequency/SYSAHBCLKDIV)/regVal)/16)/baudrate ; /*baud rate */
U0DLM = Fdiv / 256;
U0DLL = Fdiv % 256;
U0LCR = 0x03; /* DLAB = 0 */
U0FCR = 0x07; /* Enable and reset TX and RX FIFO. */
/* Read to clear the line status. */
regVal = U0LSR;
while (( U0LSR & (LSR_THRE|LSR_TEMT)) != (LSR_THRE|LSR_TEMT) );
while ( U0LSR & LSR_RDR )
{
regVal = U0RBR; /* Dump data from RX FIFO */
}
/* Enable the UART Interrupt */
ISER1|=1<<14;
U0IER = IER_RBR | IER_RLS; /* Enable UART receive interrupt */
return;
}
void blink_led()
{
int i;
i=U0IIR;
i=U0LSR;
i=U0RBR;
SYSAHBCLKCTRL|=1<<6;
GPIO0DIR|=1<<7;
GPIO0DATA&=~1<<7;
for(i=0;i<350000;i++);
GPIO0DATA|=1<<7;
return;
}
int main (void)
{
UARTInit(115200);
while(1);
}