@ -22,391 +22,297 @@
# ifdef TARGET_LPC1768
# ifdef TARGET_LPC1768
# include "../../core/macros.h"
# include "../../inc/MarlinConfig.h"
# include "../HAL.h"
# include "HardwareSerial.h"
# include "HardwareSerial.h"
# define UART3 3
HardwareSerial Serial3 = HardwareSerial ( UART3 ) ;
volatile uint32_t UART0Status , UART1Status , UART2Status , UART3Status ;
volatile uint8_t UART0TxEmpty = 1 , UART1TxEmpty = 1 , UART2TxEmpty = 1 , UART3TxEmpty = 1 ;
volatile uint8_t UART0Buffer [ UARTRXQUEUESIZE ] , UART1Buffer [ UARTRXQUEUESIZE ] , UART2Buffer [ UARTRXQUEUESIZE ] , UART3Buffer [ UARTRXQUEUESIZE ] ;
volatile uint32_t UART0RxQueueWritePos = 0 , UART1RxQueueWritePos = 0 , UART2RxQueueWritePos = 0 , UART3RxQueueWritePos = 0 ;
volatile uint32_t UART0RxQueueReadPos = 0 , UART1RxQueueReadPos = 0 , UART2RxQueueReadPos = 0 , UART3RxQueueReadPos = 0 ;
volatile uint8_t dummy ;
void HardwareSerial : : begin ( uint32_t baudrate ) {
uint32_t Fdiv ;
uint32_t pclkdiv , pclk ;
if ( PortNum = = 0 )
{
LPC_PINCON - > PINSEL0 & = ~ 0x000000F0 ;
LPC_PINCON - > PINSEL0 | = 0x00000050 ; /* RxD0 is P0.3 and TxD0 is P0.2 */
/* By default, the PCLKSELx value is zero, thus, the PCLK for
all the peripherals is 1 / 4 of the SystemFrequency . */
/* Bit 6~7 is for UART0 */
pclkdiv = ( LPC_SC - > PCLKSEL0 > > 6 ) & 0x03 ;
switch ( pclkdiv )
{
case 0x00 :
default :
pclk = SystemCoreClock / 4 ;
break ;
case 0x01 :
pclk = SystemCoreClock ;
break ;
case 0x02 :
pclk = SystemCoreClock / 2 ;
break ;
case 0x03 :
pclk = SystemCoreClock / 8 ;
break ;
}
LPC_UART0 - > LCR = 0x83 ; /* 8 bits, no Parity, 1 Stop bit */
Fdiv = ( pclk / 16 ) / baudrate ; /*baud rate */
LPC_UART0 - > DLM = Fdiv / 256 ;
LPC_UART0 - > DLL = Fdiv % 256 ;
LPC_UART0 - > LCR = 0x03 ; /* DLAB = 0 */
LPC_UART0 - > FCR = 0x07 ; /* Enable and reset TX and RX FIFO. */
NVIC_EnableIRQ ( UART0_IRQn ) ;
LPC_UART0 - > IER = IER_RBR | IER_THRE | IER_RLS ; /* Enable UART0 interrupt */
}
else if ( PortNum = = 1 )
{
LPC_PINCON - > PINSEL4 & = ~ 0x0000000F ;
LPC_PINCON - > PINSEL4 | = 0x0000000A ; /* Enable RxD1 P2.1, TxD1 P2.0 */
/* By default, the PCLKSELx value is zero, thus, the PCLK for
all the peripherals is 1 / 4 of the SystemFrequency . */
/* Bit 8,9 are for UART1 */
pclkdiv = ( LPC_SC - > PCLKSEL0 > > 8 ) & 0x03 ;
switch ( pclkdiv )
{
case 0x00 :
default :
pclk = SystemCoreClock / 4 ;
break ;
case 0x01 :
pclk = SystemCoreClock ;
break ;
case 0x02 :
pclk = SystemCoreClock / 2 ;
break ;
case 0x03 :
pclk = SystemCoreClock / 8 ;
break ;
}
LPC_UART1 - > LCR = 0x83 ; /* 8 bits, no Parity, 1 Stop bit */
Fdiv = ( pclk / 16 ) / baudrate ; /*baud rate */
LPC_UART1 - > DLM = Fdiv / 256 ;
LPC_UART1 - > DLL = Fdiv % 256 ;
LPC_UART1 - > LCR = 0x03 ; /* DLAB = 0 */
LPC_UART1 - > FCR = 0x07 ; /* Enable and reset TX and RX FIFO. */
NVIC_EnableIRQ ( UART1_IRQn ) ;
LPC_UART1 - > IER = IER_RBR | IER_THRE | IER_RLS ; /* Enable UART1 interrupt */
}
else if ( PortNum = = 2 )
{
//LPC_PINCON->PINSEL4 &= ~0x000F0000; /*Pinsel4 Bits 16-19*/
//LPC_PINCON->PINSEL4 |= 0x000A0000; /* RxD2 is P2.9 and TxD2 is P2.8, value 10*/
LPC_PINCON - > PINSEL0 & = ~ 0x00F00000 ; /*Pinsel0 Bits 20-23*/
LPC_PINCON - > PINSEL0 | = 0x00500000 ; /* RxD2 is P0.11 and TxD2 is P0.10, value 01*/
LPC_SC - > PCONP | = 1 < < 24 ; //Enable PCUART2
/* By default, the PCLKSELx value is zero, thus, the PCLK for
all the peripherals is 1 / 4 of the SystemFrequency . */
/* Bit 6~7 is for UART3 */
pclkdiv = ( LPC_SC - > PCLKSEL1 > > 16 ) & 0x03 ;
switch ( pclkdiv )
{
case 0x00 :
default :
pclk = SystemCoreClock / 4 ;
break ;
case 0x01 :
pclk = SystemCoreClock ;
break ;
case 0x02 :
pclk = SystemCoreClock / 2 ;
break ;
case 0x03 :
pclk = SystemCoreClock / 8 ;
break ;
}
LPC_UART2 - > LCR = 0x83 ; /* 8 bits, no Parity, 1 Stop bit */
Fdiv = ( pclk / 16 ) / baudrate ; /*baud rate */
LPC_UART2 - > DLM = Fdiv / 256 ;
LPC_UART2 - > DLL = Fdiv % 256 ;
LPC_UART2 - > LCR = 0x03 ; /* DLAB = 0 */
LPC_UART2 - > FCR = 0x07 ; /* Enable and reset TX and RX FIFO. */
NVIC_EnableIRQ ( UART2_IRQn ) ;
LPC_UART2 - > IER = IER_RBR | IER_THRE | IER_RLS ; /* Enable UART3 interrupt */
}
else if ( PortNum = = 3 )
{
LPC_PINCON - > PINSEL0 & = ~ 0x0000000F ;
LPC_PINCON - > PINSEL0 | = 0x0000000A ; /* RxD3 is P0.1 and TxD3 is P0.0 */
LPC_SC - > PCONP | = 1 < < 4 | 1 < < 25 ; //Enable PCUART1
/* By default, the PCLKSELx value is zero, thus, the PCLK for
all the peripherals is 1 / 4 of the SystemFrequency . */
/* Bit 6~7 is for UART3 */
pclkdiv = ( LPC_SC - > PCLKSEL1 > > 18 ) & 0x03 ;
switch ( pclkdiv )
{
case 0x00 :
default :
pclk = SystemCoreClock / 4 ;
break ;
case 0x01 :
pclk = SystemCoreClock ;
break ;
case 0x02 :
pclk = SystemCoreClock / 2 ;
break ;
case 0x03 :
pclk = SystemCoreClock / 8 ;
break ;
}
LPC_UART3 - > LCR = 0x83 ; /* 8 bits, no Parity, 1 Stop bit */
Fdiv = ( pclk / 16 ) / baudrate ; /*baud rate */
LPC_UART3 - > DLM = Fdiv / 256 ;
LPC_UART3 - > DLL = Fdiv % 256 ;
LPC_UART3 - > LCR = 0x03 ; /* DLAB = 0 */
LPC_UART3 - > FCR = 0x07 ; /* Enable and reset TX and RX FIFO. */
NVIC_EnableIRQ ( UART3_IRQn ) ;
LPC_UART3 - > IER = IER_RBR | IER_THRE | IER_RLS ; /* Enable UART3 interrupt */
}
}
int HardwareSerial : : read ( ) {
HardwareSerial Serial = HardwareSerial ( LPC_UART0 ) ;
uint8_t rx ;
HardwareSerial Serial1 = HardwareSerial ( ( LPC_UART_TypeDef * ) LPC_UART1 ) ;
if ( PortNum = = 0 )
HardwareSerial Serial2 = HardwareSerial ( LPC_UART2 ) ;
{
HardwareSerial Serial3 = HardwareSerial ( LPC_UART3 ) ;
if ( UART0RxQueueReadPos = = UART0RxQueueWritePos )
return - 1 ;
void HardwareSerial : : begin ( uint32_t baudrate ) {
// Read from "head"
UART_CFG_Type UARTConfigStruct ;
rx = UART0Buffer [ UART0RxQueueReadPos ] ; // grab next byte
PINSEL_CFG_Type PinCfg ;
UART0RxQueueReadPos = ( UART0RxQueueReadPos + 1 ) % UARTRXQUEUESIZE ;
UART_FIFO_CFG_Type FIFOConfig ;
return rx ;
}
if ( UARTx = = LPC_UART0 ) {
if ( PortNum = = 1 )
/*
{
* Initialize UART0 pin connect
if ( UART1RxQueueReadPos = = UART1RxQueueWritePos )
*/
return - 1 ;
PinCfg . Funcnum = 1 ;
PinCfg . OpenDrain = 0 ;
// Read from "head"
PinCfg . Pinmode = 0 ;
rx = UART1Buffer [ UART1RxQueueReadPos ] ; // grab next byte
PinCfg . Pinnum = 2 ;
UART1RxQueueReadPos = ( UART1RxQueueReadPos + 1 ) % UARTRXQUEUESIZE ;
PinCfg . Portnum = 0 ;
return rx ;
PINSEL_ConfigPin ( & PinCfg ) ;
}
PinCfg . Pinnum = 3 ;
if ( PortNum = = 2 )
PINSEL_ConfigPin ( & PinCfg ) ;
{
if ( UART2RxQueueReadPos = = UART2RxQueueWritePos )
return - 1 ;
// Read from "head"
rx = UART2Buffer [ UART2RxQueueReadPos ] ; // grab next byte
UART2RxQueueReadPos = ( UART2RxQueueReadPos + 1 ) % UARTRXQUEUESIZE ;
return rx ;
}
if ( PortNum = = 3 )
{
if ( UART3RxQueueReadPos = = UART3RxQueueWritePos )
return - 1 ;
// Read from "head"
rx = UART3Buffer [ UART3RxQueueReadPos ] ; // grab next byte
UART3RxQueueReadPos = ( UART3RxQueueReadPos + 1 ) % UARTRXQUEUESIZE ;
return rx ;
}
return 0 ;
}
}
else if ( ( LPC_UART1_TypeDef * ) UARTx = = LPC_UART1 ) {
size_t HardwareSerial : : write ( uint8_t send ) {
/*
if ( PortNum = = 0 )
* Initialize UART1 pin connect
{
*/
/* THRE status, contain valid data */
PinCfg . Funcnum = 1 ;
while ( ! ( UART0TxEmpty & 0x01 ) ) ;
PinCfg . OpenDrain = 0 ;
LPC_UART0 - > THR = send ;
PinCfg . Pinmode = 0 ;
UART0TxEmpty = 0 ; /* not empty in the THR until it shifts out */
PinCfg . Pinnum = 15 ;
}
PinCfg . Portnum = 0 ;
else if ( PortNum = = 1 )
PINSEL_ConfigPin ( & PinCfg ) ;
{
PinCfg . Pinnum = 16 ;
PINSEL_ConfigPin ( & PinCfg ) ;
/* THRE status, contain valid data */
}
while ( ! ( UART1TxEmpty & 0x01 ) ) ;
else if ( UARTx = = LPC_UART2 ) {
LPC_UART1 - > THR = send ;
/*
UART1TxEmpty = 0 ; /* not empty in the THR until it shifts out */
* Initialize UART2 pin connect
*/
PinCfg . Funcnum = 1 ;
}
PinCfg . OpenDrain = 0 ;
else if ( PortNum = = 2 )
PinCfg . Pinmode = 0 ;
{
PinCfg . Pinnum = 10 ;
/* THRE status, contain valid data */
PinCfg . Portnum = 0 ;
while ( ! ( UART2TxEmpty & 0x01 ) ) ;
PINSEL_ConfigPin ( & PinCfg ) ;
LPC_UART2 - > THR = send ;
PinCfg . Pinnum = 11 ;
UART2TxEmpty = 0 ; /* not empty in the THR until it shifts out */
PINSEL_ConfigPin ( & PinCfg ) ;
}
}
else if ( UARTx = = LPC_UART3 ) {
else if ( PortNum = = 3 )
/*
{
* Initialize UART2 pin connect
/* THRE status, contain valid data */
*/
while ( ! ( UART3TxEmpty & 0x01 ) ) ;
PinCfg . Funcnum = 1 ;
LPC_UART3 - > THR = send ;
PinCfg . OpenDrain = 0 ;
UART3TxEmpty = 0 ; /* not empty in the THR until it shifts out */
PinCfg . Pinmode = 0 ;
PinCfg . Pinnum = 0 ;
}
PinCfg . Portnum = 0 ;
return 0 ;
PINSEL_ConfigPin ( & PinCfg ) ;
PinCfg . Pinnum = 1 ;
PINSEL_ConfigPin ( & PinCfg ) ;
}
}
int HardwareSerial : : available ( ) {
/* Initialize UART Configuration parameter structure to default state:
if ( PortNum = = 0 )
* Baudrate = 9600 bps
{
* 8 data bit
return ( UART0RxQueueWritePos + UARTRXQUEUESIZE - UART0RxQueueReadPos ) % UARTRXQUEUESIZE ;
* 1 Stop bit
* None parity
*/
UART_ConfigStructInit ( & UARTConfigStruct ) ;
// Re-configure baudrate
UARTConfigStruct . Baud_rate = baudrate ;
// Initialize eripheral with given to corresponding parameter
UART_Init ( UARTx , & UARTConfigStruct ) ;
// Enable and reset the TX and RX FIFOs
UART_FIFOConfigStructInit ( & FIFOConfig ) ;
UART_FIFOConfig ( UARTx , & FIFOConfig ) ;
// Enable UART Transmit
UART_TxCmd ( UARTx , ENABLE ) ;
// Configure Interrupts
UART_IntConfig ( UARTx , UART_INTCFG_RBR , ENABLE ) ;
UART_IntConfig ( UARTx , UART_INTCFG_RLS , ENABLE ) ;
if ( UARTx = = LPC_UART0 )
NVIC_EnableIRQ ( UART0_IRQn ) ;
else if ( ( LPC_UART1_TypeDef * ) UARTx = = LPC_UART1 )
NVIC_EnableIRQ ( UART1_IRQn ) ;
else if ( UARTx = = LPC_UART2 )
NVIC_EnableIRQ ( UART2_IRQn ) ;
else if ( UARTx = = LPC_UART3 )
NVIC_EnableIRQ ( UART3_IRQn ) ;
RxQueueWritePos = RxQueueReadPos = 0 ;
# if TX_BUFFER_SIZE > 0
TxQueueWritePos = TxQueueReadPos = 0 ;
# endif
}
}
if ( PortNum = = 1 )
{
int HardwareSerial : : peek ( ) {
return ( UART1RxQueueWritePos + UARTRXQUEUESIZE - UART1RxQueueReadPos ) % UARTRXQUEUESIZE ;
int byte = - 1 ;
}
if ( PortNum = = 2 )
/* Temporarily lock out UART receive interrupts during this read so the UART receive
{
interrupt won ' t cause problems with the index values */
return ( UART2RxQueueWritePos + UARTRXQUEUESIZE - UART2RxQueueReadPos ) % UARTRXQUEUESIZE ;
UART_IntConfig ( UARTx , UART_INTCFG_RBR , DISABLE ) ;
}
if ( PortNum = = 3 )
if ( RxQueueReadPos ! = RxQueueWritePos )
{
byte = RxBuffer [ RxQueueReadPos ] ;
return ( UART3RxQueueWritePos + UARTRXQUEUESIZE - UART3RxQueueReadPos ) % UARTRXQUEUESIZE ;
/* Re-enable UART interrupts */
UART_IntConfig ( UARTx , UART_INTCFG_RBR , ENABLE ) ;
return byte ;
}
}
return 0 ;
int HardwareSerial : : read ( ) {
int byte = - 1 ;
/* Temporarily lock out UART receive interrupts during this read so the UART receive
interrupt won ' t cause problems with the index values */
UART_IntConfig ( UARTx , UART_INTCFG_RBR , DISABLE ) ;
if ( RxQueueReadPos ! = RxQueueWritePos ) {
byte = RxBuffer [ RxQueueReadPos ] ;
RxQueueReadPos = ( RxQueueReadPos + 1 ) % RX_BUFFER_SIZE ;
}
}
void HardwareSerial : : flush ( ) {
/* Re-enable UART interrupts */
if ( PortNum = = 0 )
UART_IntConfig ( UARTx , UART_INTCFG_RBR , ENABLE ) ;
{
UART0RxQueueWritePos = 0 ;
UART0RxQueueReadPos = 0 ;
return byte ;
}
}
if ( PortNum = = 1 )
{
UART1RxQueueWritePos = 0 ;
UART1RxQueueReadPos = 0 ;
}
if ( PortNum = = 2 )
{
UART2RxQueueWritePos = 0 ;
UART2RxQueueReadPos = 0 ;
}
if ( PortNum = = 3 )
{
UART3RxQueueWritePos = 0 ;
UART3RxQueueReadPos = 0 ;
}
return ;
}
void HardwareSerial : : printf ( const char * format , . . . ) {
size_t HardwareSerial : : write ( uint8_t send ) {
static char buffer [ 256 ] ;
# if TX_BUFFER_SIZE > 0
va_list vArgs ;
size_t bytes = 0 ;
va_start ( vArgs , format ) ;
uint32_t fifolvl = 0 ;
int length = vsnprintf ( ( char * ) buffer , 256 , ( char const * ) format , vArgs ) ;
va_end ( vArgs ) ;
/* If the Tx Buffer is full, wait for space to clear */
if ( length > 0 & & length < 256 ) {
if ( ( TxQueueWritePos + 1 ) % TX_BUFFER_SIZE = = TxQueueReadPos ) flushTX ( ) ;
for ( int i = 0 ; i < length ; ) {
write ( buffer [ i ] ) ;
/* Temporarily lock out UART transmit interrupts during this read so the UART transmit interrupt won't
+ + i ;
cause problems with the index values */
}
UART_IntConfig ( UARTx , UART_INTCFG_THRE , DISABLE ) ;
}
/* LPC17xx.h incorrectly defines FIFOLVL as a uint8_t, when it's actually a 32-bit register */
if ( ( LPC_UART1_TypeDef * ) UARTx = = LPC_UART1 )
fifolvl = * ( reinterpret_cast < volatile uint32_t * > ( & ( ( LPC_UART1_TypeDef * ) UARTx ) - > FIFOLVL ) ) ;
else
fifolvl = * ( reinterpret_cast < volatile uint32_t * > ( & UARTx - > FIFOLVL ) ) ;
/* If the queue is empty and there's space in the FIFO, immediately send the byte */
if ( TxQueueWritePos = = TxQueueReadPos & & fifolvl < UART_TX_FIFO_SIZE ) {
bytes = UART_Send ( UARTx , & send , 1 , BLOCKING ) ;
}
/* Otherwiise, write the byte to the transmit buffer */
else if ( ( TxQueueWritePos + 1 ) % TX_BUFFER_SIZE ! = TxQueueReadPos ) {
TxBuffer [ TxQueueWritePos ] = send ;
TxQueueWritePos = ( TxQueueWritePos + 1 ) % TX_BUFFER_SIZE ;
bytes + + ;
}
}
# ifdef __cplusplus
/* Re-enable the TX Interrupt */
extern " C " {
UART_IntConfig ( UARTx , UART_INTCFG_THRE , ENABLE ) ;
return bytes ;
# else
return UART_Send ( UARTx , & send , 1 , BLOCKING ) ;
# endif
}
# if TX_BUFFER_SIZE > 0
void HardwareSerial : : flushTX ( ) {
/* Wait for the tx buffer and FIFO to drain */
while ( TxQueueWritePos ! = TxQueueReadPos & & UART_CheckBusy ( UARTx ) = = SET ) ;
}
# endif
# endif
/*****************************************************************************
int HardwareSerial : : available ( ) {
* * Function name : UART0_IRQHandler
return ( RxQueueWritePos + RX_BUFFER_SIZE - RxQueueReadPos ) % RX_BUFFER_SIZE ;
* *
}
* * Descriptions : UART0 interrupt handler
* *
void HardwareSerial : : flush ( ) {
* * parameters : None
RxQueueWritePos = 0 ;
* * Returned value : None
RxQueueReadPos = 0 ;
* *
}
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void UART0_IRQHandler ( void )
{
uint8_t IIRValue , LSRValue ;
IIRValue = LPC_UART0 - > IIR ;
void HardwareSerial : : printf ( const char * format , . . . ) {
char RxBuffer [ 256 ] ;
va_list vArgs ;
va_start ( vArgs , format ) ;
int length = vsnprintf ( RxBuffer , 256 , format , vArgs ) ;
va_end ( vArgs ) ;
if ( length > 0 & & length < 256 ) {
for ( int i = 0 ; i < length ; + + i )
write ( RxBuffer [ i ] ) ;
}
}
IIRValue > > = 1 ; /* skip pending bit in IIR */
void HardwareSerial : : IRQHandler ( ) {
IIRValue & = 0x07 ; /* check bit 1~3, interrupt identification */
uint32_t IIRValue ;
if ( IIRValue = = IIR_RLS ) /* Receive Line Status */
uint8_t LSRValue , byte ;
IIRValue = UART_GetIntId ( UARTx ) ;
IIRValue & = UART_IIR_INTID_MASK ; /* check bit 1~3, interrupt identification */
if ( IIRValue = = UART_IIR_INTID_RLS ) /* Receive Line Status */
{
{
LSRValue = LPC_UART0 - > LSR ;
LSRValue = UART_GetLineStatus ( UARTx ) ;
/* Receive Line Status */
/* Receive Line Status */
if ( LSRValue & ( LSR_OE | LSR_PE | LSR_FE | LSR_RXFE | LSR_BI ) )
if ( LSRValue & ( UART_ LSR_OE| UART_ LSR_PE| UART_ LSR_FE| UART_ LSR_RXFE| UART_ LSR_BI) )
{
{
/* There are errors or break interrupt */
/* There are errors or break interrupt */
/* Read LSR will clear the interrupt */
/* Read LSR will clear the interrupt */
UART0 Status = LSRValue ;
Status = LSRValue ;
dummy = LPC_UART0 - > RBR ; /* Dummy read on RX to clear
byte = UART_ReceiveByte ( UARTx ) ; /* Dummy read on RX to clear
interrupt , then bail out */
interrupt , then bail out */
return ;
return ;
}
}
if ( LSRValue & LSR_RDR ) /* Receive Data Ready */
}
{
/* If no error on RLS, normal ready, save into the data buffer. */
if ( IIRValue = = UART_IIR_INTID_RDA ) /* Receive Data Available */
/* Note: read RBR will clear the interrupt */
{
if ( ( UART0RxQueueWritePos + 1 ) % UARTRXQUEUESIZE ! = UART0RxQueueReadPos )
/* Clear the FIFO */
while ( UART_Receive ( UARTx , & byte , 1 , NONE_BLOCKING ) ) {
if ( ( RxQueueWritePos + 1 ) % RX_BUFFER_SIZE ! = RxQueueReadPos )
{
{
UART0Buffer [ UART0RxQueueWritePos ] = LPC_UART0 - > RBR ;
RxBuffer[ RxQueueWritePos ] = byte ;
UART0 RxQueueWritePos = ( UART0 RxQueueWritePos+ 1 ) % UARTRXQUEUE SIZE;
RxQueueWritePos = ( RxQueueWritePos+ 1 ) % RX_BUFFER_ SIZE;
}
}
else
else
dummy = LPC_UART0 - > RBR ;
break ;
}
}
}
}
else if ( IIRValue = = IIR_RDA ) /* Receive Data Available */
else if ( IIRValue = = UART_IIR_INTID_CTI ) /* Character timeout indicator */
{
/* Receive Data Available */
if ( ( UART0RxQueueWritePos + 1 ) % UARTRXQUEUESIZE ! = UART0RxQueueReadPos )
{
UART0Buffer [ UART0RxQueueWritePos ] = LPC_UART0 - > RBR ;
UART0RxQueueWritePos = ( UART0RxQueueWritePos + 1 ) % UARTRXQUEUESIZE ;
}
else
dummy = LPC_UART1 - > RBR ;
}
else if ( IIRValue = = IIR_CTI ) /* Character timeout indicator */
{
{
/* Character Time-out indicator */
/* Character Time-out indicator */
UART0 Status | = 0x100 ; /* Bit 9 as the CTI error */
Status | = 0x100 ; /* Bit 9 as the CTI error */
}
}
else if ( IIRValue = = IIR_THRE ) /* THRE, transmit holding register empty */
{
# if TX_BUFFER_SIZE > 0
/* THRE interrupt */
if ( IIRValue = = UART_IIR_INTID_THRE ) {
LSRValue = LPC_UART0 - > LSR ; /* Check status in the LSR to see if
/* Disable THRE interrupt */
valid data in U0THR or not */
UART_IntConfig ( UARTx , UART_INTCFG_THRE , DISABLE ) ;
if ( LSRValue & LSR_THRE )
{
/* Wait for FIFO buffer empty */
UART0TxEmpty = 1 ;
while ( UART_CheckBusy ( UARTx ) = = SET ) ;
}
else
/* Transfer up to UART_TX_FIFO_SIZE bytes of data */
{
for ( int i = 0 ; i < UART_TX_FIFO_SIZE & & TxQueueWritePos ! = TxQueueReadPos ; i + + ) {
UART0TxEmpty = 0 ;
/* Move a piece of data into the transmit FIFO */
if ( UART_Send ( UARTx , & TxBuffer [ TxQueueReadPos ] , 1 , NONE_BLOCKING ) )
TxQueueReadPos = ( TxQueueReadPos + 1 ) % TX_BUFFER_SIZE ;
else
break ;
}
/* If there is no more data to send, disable the transmit interrupt - else enable it or keep it enabled */
if ( TxQueueWritePos = = TxQueueReadPos )
UART_IntConfig ( UARTx , UART_INTCFG_THRE , DISABLE ) ;
else
UART_IntConfig ( UARTx , UART_INTCFG_THRE , ENABLE ) ;
}
}
}
# endif
}
# ifdef __cplusplus
extern " C " {
# endif
/*****************************************************************************
* * Function name : UART0_IRQHandler
* *
* * Descriptions : UART0 interrupt handler
* *
* * parameters : None
* * Returned value : None
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void UART0_IRQHandler ( void )
{
Serial . IRQHandler ( ) ;
}
}
/*****************************************************************************
/*****************************************************************************
@ -420,69 +326,9 @@ void UART0_IRQHandler (void)
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void UART1_IRQHandler ( void )
void UART1_IRQHandler ( void )
{
{
uint8_t IIRValue , LSRValue ;
Serial1 . IRQHandler ( ) ;
IIRValue = LPC_UART1 - > IIR ;
IIRValue > > = 1 ; /* skip pending bit in IIR */
IIRValue & = 0x07 ; /* check bit 1~3, interrupt identification */
if ( IIRValue = = IIR_RLS ) /* Receive Line Status */
{
LSRValue = LPC_UART1 - > LSR ;
/* Receive Line Status */
if ( LSRValue & ( LSR_OE | LSR_PE | LSR_FE | LSR_RXFE | LSR_BI ) )
{
/* There are errors or break interrupt */
/* Read LSR will clear the interrupt */
UART1Status = LSRValue ;
dummy = LPC_UART1 - > RBR ; /* Dummy read on RX to clear
interrupt , then bail out */
return ;
}
if ( LSRValue & LSR_RDR ) /* Receive Data Ready */
{
/* If no error on RLS, normal ready, save into the data buffer. */
/* Note: read RBR will clear the interrupt */
if ( ( UART1RxQueueWritePos + 1 ) % UARTRXQUEUESIZE ! = UART1RxQueueReadPos )
{
UART1Buffer [ UART1RxQueueWritePos ] = LPC_UART1 - > RBR ;
UART1RxQueueWritePos = ( UART1RxQueueWritePos + 1 ) % UARTRXQUEUESIZE ;
}
else
dummy = LPC_UART1 - > RBR ;
}
}
else if ( IIRValue = = IIR_RDA ) /* Receive Data Available */
{
/* Receive Data Available */
if ( ( UART1RxQueueWritePos + 1 ) % UARTRXQUEUESIZE ! = UART1RxQueueReadPos )
{
UART1Buffer [ UART1RxQueueWritePos ] = LPC_UART1 - > RBR ;
UART1RxQueueWritePos = ( UART1RxQueueWritePos + 1 ) % UARTRXQUEUESIZE ;
}
else
dummy = LPC_UART1 - > RBR ;
}
else if ( IIRValue = = IIR_CTI ) /* Character timeout indicator */
{
/* Character Time-out indicator */
UART1Status | = 0x100 ; /* Bit 9 as the CTI error */
}
else if ( IIRValue = = IIR_THRE ) /* THRE, transmit holding register empty */
{
/* THRE interrupt */
LSRValue = LPC_UART1 - > LSR ; /* Check status in the LSR to see if
valid data in U0THR or not */
if ( LSRValue & LSR_THRE )
{
UART1TxEmpty = 1 ;
}
else
{
UART1TxEmpty = 0 ;
}
}
}
}
/*****************************************************************************
/*****************************************************************************
* * Function name : UART2_IRQHandler
* * Function name : UART2_IRQHandler
* *
* *
@ -494,71 +340,13 @@ void UART1_IRQHandler (void)
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void UART2_IRQHandler ( void )
void UART2_IRQHandler ( void )
{
{
uint8_t IIRValue , LSRValue ;
Serial2 . IRQHandler ( ) ;
IIRValue = LPC_UART2 - > IIR ;
IIRValue > > = 1 ; /* skip pending bit in IIR */
IIRValue & = 0x07 ; /* check bit 1~3, interrupt identification */
if ( IIRValue = = IIR_RLS ) /* Receive Line Status */
{
LSRValue = LPC_UART2 - > LSR ;
/* Receive Line Status */
if ( LSRValue & ( LSR_OE | LSR_PE | LSR_FE | LSR_RXFE | LSR_BI ) )
{
/* There are errors or break interrupt */
/* Read LSR will clear the interrupt */
UART2Status = LSRValue ;
dummy = LPC_UART2 - > RBR ; /* Dummy read on RX to clear
interrupt , then bail out */
return ;
}
if ( LSRValue & LSR_RDR ) /* Receive Data Ready */
{
/* If no error on RLS, normal ready, save into the data buffer. */
/* Note: read RBR will clear the interrupt */
if ( ( UART2RxQueueWritePos + 1 ) % UARTRXQUEUESIZE ! = UART2RxQueueReadPos )
{
UART2Buffer [ UART2RxQueueWritePos ] = LPC_UART2 - > RBR ;
UART2RxQueueWritePos = ( UART2RxQueueWritePos + 1 ) % UARTRXQUEUESIZE ;
}
}
}
else if ( IIRValue = = IIR_RDA ) /* Receive Data Available */
{
/* Receive Data Available */
if ( ( UART2RxQueueWritePos + 1 ) % UARTRXQUEUESIZE ! = UART2RxQueueReadPos )
{
UART2Buffer [ UART2RxQueueWritePos ] = LPC_UART2 - > RBR ;
UART2RxQueueWritePos = ( UART2RxQueueWritePos + 1 ) % UARTRXQUEUESIZE ;
}
else
dummy = LPC_UART2 - > RBR ;
}
else if ( IIRValue = = IIR_CTI ) /* Character timeout indicator */
{
/* Character Time-out indicator */
UART2Status | = 0x100 ; /* Bit 9 as the CTI error */
}
else if ( IIRValue = = IIR_THRE ) /* THRE, transmit holding register empty */
{
/* THRE interrupt */
LSRValue = LPC_UART2 - > LSR ; /* Check status in the LSR to see if
valid data in U0THR or not */
if ( LSRValue & LSR_THRE )
{
UART2TxEmpty = 1 ;
}
else
{
UART2TxEmpty = 0 ;
}
}
}
}
/*****************************************************************************
/*****************************************************************************
* * Function name : UART3_IRQHandler
* * Function name : UART3_IRQHandler
* *
* *
* * Descriptions : UART 0 interrupt handler
* * Descriptions : UART3 interrupt handler
* *
* *
* * parameters : None
* * parameters : None
* * Returned value : None
* * Returned value : None
@ -566,66 +354,7 @@ void UART2_IRQHandler (void)
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void UART3_IRQHandler ( void )
void UART3_IRQHandler ( void )
{
{
uint8_t IIRValue , LSRValue ;
Serial3 . IRQHandler ( ) ;
IIRValue = LPC_UART3 - > IIR ;
IIRValue > > = 1 ; /* skip pending bit in IIR */
IIRValue & = 0x07 ; /* check bit 1~3, interrupt identification */
if ( IIRValue = = IIR_RLS ) /* Receive Line Status */
{
LSRValue = LPC_UART3 - > LSR ;
/* Receive Line Status */
if ( LSRValue & ( LSR_OE | LSR_PE | LSR_FE | LSR_RXFE | LSR_BI ) )
{
/* There are errors or break interrupt */
/* Read LSR will clear the interrupt */
UART3Status = LSRValue ;
dummy = LPC_UART3 - > RBR ; /* Dummy read on RX to clear
interrupt , then bail out */
return ;
}
if ( LSRValue & LSR_RDR ) /* Receive Data Ready */
{
/* If no error on RLS, normal ready, save into the data buffer. */
/* Note: read RBR will clear the interrupt */
if ( ( UART3RxQueueWritePos + 1 ) % UARTRXQUEUESIZE ! = UART3RxQueueReadPos )
{
UART3Buffer [ UART3RxQueueWritePos ] = LPC_UART3 - > RBR ;
UART3RxQueueWritePos = ( UART3RxQueueWritePos + 1 ) % UARTRXQUEUESIZE ;
}
}
}
else if ( IIRValue = = IIR_RDA ) /* Receive Data Available */
{
/* Receive Data Available */
if ( ( UART3RxQueueWritePos + 1 ) % UARTRXQUEUESIZE ! = UART3RxQueueReadPos )
{
UART3Buffer [ UART3RxQueueWritePos ] = LPC_UART3 - > RBR ;
UART3RxQueueWritePos = ( UART3RxQueueWritePos + 1 ) % UARTRXQUEUESIZE ;
}
else
dummy = LPC_UART3 - > RBR ;
}
else if ( IIRValue = = IIR_CTI ) /* Character timeout indicator */
{
/* Character Time-out indicator */
UART3Status | = 0x100 ; /* Bit 9 as the CTI error */
}
else if ( IIRValue = = IIR_THRE ) /* THRE, transmit holding register empty */
{
/* THRE interrupt */
LSRValue = LPC_UART3 - > LSR ; /* Check status in the LSR to see if
valid data in U0THR or not */
if ( LSRValue & LSR_THRE )
{
UART3TxEmpty = 1 ;
}
else
{
UART3TxEmpty = 0 ;
}
}
}
}
# ifdef __cplusplus
# ifdef __cplusplus