16 #define SystemFrequency SystemCoreClock
18 volatile uint32_t UARTStatus;
19 volatile uint8_t UARTTxEmpty = 1;
20 volatile uint8_t UARTBuffer[BUFSIZE];
21 volatile uint32_t UARTCount = 0;
32 void UART_IRQHandler(
void)
34 uint8_t IIRValue, LSRValue;
35 uint8_t Dummy = Dummy;
37 IIRValue = LPC_UART->IIR;
41 if (IIRValue == IIR_RLS)
43 LSRValue = LPC_UART->LSR;
45 if (LSRValue & (LSR_OE | LSR_PE | LSR_FE | LSR_RXFE | LSR_BI))
49 UARTStatus = LSRValue;
50 Dummy = LPC_UART->RBR;
54 if (LSRValue & LSR_RDR)
58 UARTBuffer[UARTCount++] = LPC_UART->RBR;
59 if (UARTCount == BUFSIZE)
65 else if (IIRValue == IIR_RDA)
68 UARTBuffer[UARTCount++] = LPC_UART->RBR;
69 if (UARTCount == BUFSIZE)
74 else if (IIRValue == IIR_CTI)
79 else if (IIRValue == IIR_THRE)
82 LSRValue = LPC_UART->LSR;
84 if (LSRValue & LSR_THRE)
106 void UARTInit(uint32_t baudrate)
114 NVIC_DisableIRQ(UART_IRQn);
116 LPC_IOCON->PIO1_6 &= ~0x07;
117 LPC_IOCON->PIO1_6 |= 0x01;
118 LPC_IOCON->PIO1_7 &= ~0x07;
119 LPC_IOCON->PIO1_7 |= 0x01;
121 LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12);
122 LPC_SYSCON->UARTCLKDIV = 0x1;
124 LPC_UART->LCR = 0x83;
125 regVal = LPC_SYSCON->UARTCLKDIV;
126 Fdiv = (((SystemFrequency/LPC_SYSCON->SYSAHBCLKDIV)/regVal)/16)/baudrate ;
128 LPC_UART->DLM = Fdiv / 256;
129 LPC_UART->DLL = Fdiv % 256;
130 LPC_UART->LCR = 0x03;
131 LPC_UART->FCR = 0x07;
134 regVal = LPC_UART->LSR;
138 while (( LPC_UART->LSR & (LSR_THRE|LSR_TEMT)) != (LSR_THRE|LSR_TEMT) );
139 while ( LPC_UART->LSR & LSR_RDR )
141 regVal = LPC_UART->RBR;
145 NVIC_EnableIRQ(UART_IRQn);
148 LPC_UART->IER = IER_RBR | IER_THRE | IER_RLS;
150 LPC_UART->IER = IER_RBR | IER_RLS;
165 void UARTSend(uint8_t *BufferPtr, uint32_t Length)
167 LPC_UART->IER = IER_THRE | IER_RLS;
169 while ( Length != 0 )
173 while ( !(LPC_UART->LSR & LSR_THRE) );
174 LPC_UART->THR = *BufferPtr;
177 while ( !(UARTTxEmpty & 0x01) );
178 LPC_UART->THR = *BufferPtr;
185 LPC_UART->IER = IER_THRE | IER_RLS | IER_RBR;