/* UART receive-only interrupt handler for ring buffers */ void Chip_UART_RXIntHandlerRB(LPC_USART_T *pUART, RINGBUFF_T *pRB) { /* New data will be ignored if data not popped in time */ while ((Chip_UART_GetStatus(pUART) & UART_STAT_RXRDY) != 0) { uint8_t ch = Chip_UART_ReadByte(pUART); RingBuffer_Insert(pRB, &ch); } }
/* UART transmit-only interrupt handler for ring buffers */ void Chip_UART_TXIntHandlerRB(LPC_USART_T *pUART, RINGBUFF_T *pRB) { uint8_t ch; /* Fill FIFO until full or until TX ring buffer is empty */ while (((Chip_UART_GetStatus(pUART) & UART_STAT_TXRDY) != 0) && RingBuffer_Pop(pRB, &ch)) { Chip_UART_SendByte(pUART, ch); } }
/* Transmit a byte array through the UART peripheral (non-blocking) */ int Chip_UART_Send(LPC_USART_T *pUART, const void *data, int numBytes) { int sent = 0; uint8_t *p8 = (uint8_t *) data; /* Send until the transmit FIFO is full or out of bytes */ while ((sent < numBytes) && ((Chip_UART_GetStatus(pUART) & UART_STAT_TXRDY) != 0)) { Chip_UART_SendByte(pUART, *p8); p8++; sent++; } return sent; }
/* UART receive/transmit interrupt handler for ring buffers */ void Chip_UART_IRQRBHandler(LPC_USART_T *pUART, RINGBUFF_T *pRXRB, RINGBUFF_T *pTXRB) { /* Handle transmit interrupt if enabled */ if ((Chip_UART_GetStatus(pUART) & UART_STAT_TXRDY) != 0) { Chip_UART_TXIntHandlerRB(pUART, pTXRB); /* Disable transmit interrupt if the ring buffer is empty */ if (RingBuffer_IsEmpty(pTXRB)) { Chip_UART_IntDisable(pUART, UART_INTEN_TXRDY); } } /* Handle receive interrupt */ Chip_UART_RXIntHandlerRB(pUART, pRXRB); }
/* Read data through the UART peripheral (non-blocking) */ int Chip_UART_Read(LPC_USART_T *pUART, void *data, int numBytes) { int readBytes = 0; uint8_t *p8 = (uint8_t *) data; /* Send until the transmit FIFO is full or out of bytes */ while ((readBytes < numBytes) && ((Chip_UART_GetStatus(pUART) & UART_STAT_RXRDY) != 0)) { *p8 = Chip_UART_ReadByte(pUART); p8++; readBytes++; } return readBytes; }
/** * @brief UART interrupt handler sub-routine * @return Nothing */ void LPC_UARTHNDLR(void) { uint32_t count = 0; /* Handle transmit interrupt if enabled */ if ((Chip_UART_GetStatus(LPC_USART) & UART_STAT_TXRDY) != 0) { if (g_uCOM.txBuf_count > 0) { count = Chip_UART_Send(LPC_USART, &g_uCOM.txBuf[g_uCOM.txBuf_uartIndex], g_uCOM.txBuf_count); g_uCOM.txBuf_count -= count; g_uCOM.txBuf_uartIndex += count; } /* If usbRxBuf empty check if any packet pending USB EP RAM */ if (g_uCOM.txBuf_count < 1) { if ((g_uCOM.usbRxPending > 0) && USB_IsConfigured(g_uCOM.hUsb)) { g_uCOM.usbRxPending--; g_uCOM.txBuf_count = USBD_API->hw->ReadEP(g_uCOM.hUsb, USB_CDC_OUT_EP, g_uCOM.txBuf); g_uCOM.txBuf_uartIndex = 0; } else { /* all data transmitted on UART disable UART_INTEN_TXRDY */ Chip_UART_IntDisable(LPC_USART, UART_INTEN_TXRDY); } } } /* Handle receive interrupt */ count = Chip_UART_Read(LPC_USART, &g_uCOM.rxBuf[g_uCOM.rxBuf_uartIndex], UCOM_BUF_SZ - g_uCOM.rxBuf_uartIndex); if (count) { /* Note, following logic works if UCOM_BUF_SZ is 2^n size only. */ g_uCOM.rxBuf_uartIndex = (g_uCOM.rxBuf_uartIndex + count) & (UCOM_BUF_SZ - 1); /* If USB Tx is not busy kick start USB Tx */ if ((g_uCOM.usbTxBusy == 0) && USB_IsConfigured(g_uCOM.hUsb)) { g_uCOM.usbTxBusy = 1; count = USBD_API->hw->WriteEP(g_uCOM.hUsb, USB_CDC_IN_EP, &g_uCOM.rxBuf[g_uCOM.rxBuf_usbIndex], count); g_uCOM.rxBuf_usbIndex = (g_uCOM.rxBuf_usbIndex + count) & (UCOM_BUF_SZ - 1); } } }