Ejemplo n.º 1
0
/**
 * @brief	Handle interrupt from UART
 * @return	Nothing
 */
void UART1_IRQHandler(void)
{
	uint8_t ch;
	uint32_t IntStatus = Chip_UART_GetIntStatus(LPC_USART1);

	if (IntStatus & RXRDY_INT) {
		if(receiveCompleted == false) {
			Chip_UART_ReceiveByte(LPC_USART1, &RxBuf1[RxBufCnt1++]);
			if (RxBufCnt1 == BUFFER_SIZE) {
				Chip_UART_IntEnable(LPC_USART1, RXRDY_INT, DISABLE);
				receiveCompleted = true;
				RxBufCnt1 = 0;
			}
		}
		else {
			Chip_UART_ReceiveByte(LPC_USART1, &ch);
		}
	}

	if (IntStatus & TXRDY_INT) {
		if (sendCompleted == false) {
			Chip_UART_SendByte(LPC_USART1, RxBuf1[TxBufCnt1++]);
			if (TxBufCnt1 == BUFFER_SIZE) {
				Chip_UART_IntEnable(LPC_USART1, TXRDY_INT, DISABLE);
				sendCompleted = true;
				TxBufCnt1 = 0;
			}
		}
		else {
			return;
		}

	}
}
/* UART interrupt service routine */
void Chip_UART_Interrupt_Handler(LPC_USART_Type *UARTx)
{
	uint8_t tmpc;
	uint32_t rLen;
	UART_INT_STATUS_Type Sts = Chip_UART_GetIntStatus(UARTx);
	if (Sts == UART_INTSTS_ERROR) {
		return;	/* error */
	}
	if (Sts & UART_INTSTS_RTR) {	/* ready for Read Data */
		while (1) {
			/* Call UART read function in UART driver */
			rLen = Chip_UART_Receive(UARTx, &tmpc, 1, NONE_BLOCKING);
			/* If data received */
			if (rLen) {
				/* Check if buffer is more space
				 * If no more space, remaining character will be trimmed out
				 */
				if (!__BUF_IS_FULL(rb.rx_head, rb.rx_tail)) {
					rb.rx[rb.rx_head] = tmpc;
					__BUF_INCR(rb.rx_head);
				}
			}
			/* no more data */
			else {
				break;
			}
		}
	}

	if (Sts & UART_INTSTS_RTS) {	/* ready for Write Data */
		/* Disable THRE interrupt */
		Chip_UART_IntConfig(UARTx, UART_INTCFG_THRE, DISABLE);

		/* Wait for FIFO buffer empty, transfer UART_TX_FIFO_SIZE bytes
		 * of data or break whenever ring buffers are empty */
		/* Wait until THR empty */
		while (Chip_UART_CheckBusy(UARTx) == SET) ;

		while (!__BUF_IS_EMPTY(rb.tx_head, rb.tx_tail)) {
			/* Move a piece of data into the transmit FIFO */
			if (Chip_UART_Send(UARTx, (uint8_t *) &rb.tx[rb.tx_tail], 1, NONE_BLOCKING)) {
				/* Update transmit ring FIFO tail pointer */
				__BUF_INCR(rb.tx_tail);
			}
			else {
				break;
			}
		}

		/* If there is no more data to send, disable the transmit
		   interrupt - else enable it or keep it enabled */
		if (__BUF_IS_EMPTY(rb.tx_head, rb.tx_tail)) {
			Chip_UART_IntConfig(UARTx, UART_INTCFG_THRE, DISABLE);
			// Reset Tx Interrupt state
			TxIntStat = RESET;
		}
		else {
			/* Set Tx Interrupt state */
			TxIntStat = SET;
			Chip_UART_IntConfig(UARTx, UART_INTCFG_THRE, ENABLE);
		}
	}

	if(Sts & UART_INTSTS_ABEO)
		Chip_UART_ABClearIntPending(UARTx, UART_INTSTS_ABEO);
	if (Sts & UART_INTSTS_ABTO)
		Chip_UART_ABClearIntPending(UARTx, UART_INTSTS_ABTO);
	if (ABsyncSts == RESET)
	{
		/* Interrupt caused by End of auto-baud */
		if (Sts & UART_INTSTS_ABEO){
			// Disable AB interrupt
			Chip_UART_IntConfig(UARTx, UART_INTCFG_ABEO, DISABLE);
			// Set Sync flag
			ABsyncSts = SET;
		}

		/* Auto-Baudrate Time-Out interrupt (not implemented) */
		if (Sts & UART_INTSTS_ABTO) {
			/* Disable this interrupt - Add your code here */
			Chip_UART_IntConfig(UARTx, UART_INTCFG_ABTO, DISABLE);
		}
	}
}