/* Transmit a byte array through the UART peripheral (blocking) */ int Chip_UART_SendBlocking(LPC_USART_T *pUART, const void *data, int numBytes) { int pass, sent = 0; uint8_t *p8 = (uint8_t *) data; while (numBytes > 0) { pass = Chip_UART_Send(pUART, p8, numBytes); numBytes -= pass; sent += pass; p8 += pass; } return sent; }
/* UCOM bulk EP_IN and EP_OUT endpoints handler */ static ErrorCode_t UCOM_bulk_hdlr(USBD_HANDLE_T hUsb, void *data, uint32_t event) { UCOM_DATA_T *pUcom = (UCOM_DATA_T *) data; uint32_t count = 0; switch (event) { /* A transfer from us to the USB host that we queued has completed. */ case USB_EVT_IN: /* check if UART had more data to send */ if (pUcom->rxBuf_uartIndex < pUcom->rxBuf_usbIndex) { count = UCOM_BUF_SZ - pUcom->rxBuf_usbIndex; } else { count = pUcom->rxBuf_uartIndex - pUcom->rxBuf_usbIndex; } if (count) { pUcom->usbTxBusy = 1; count = USBD_API->hw->WriteEP(pUcom->hUsb, USB_CDC_IN_EP, &pUcom->rxBuf[g_uCOM.rxBuf_usbIndex], count); g_uCOM.rxBuf_usbIndex = (g_uCOM.rxBuf_usbIndex + count) & (UCOM_BUF_SZ - 1); } else { pUcom->usbTxBusy = 0; } break; /* We received a transfer from the USB host . */ case USB_EVT_OUT: if ((Chip_UART_GetIntsEnabled(LPC_USART) & UART_IER_THREINT) == 0) { pUcom->txBuf_count = USBD_API->hw->ReadEP(hUsb, USB_CDC_OUT_EP, pUcom->txBuf); pUcom->txBuf_uartIndex = 0; /* kick start UART tranmission */ pUcom->txBuf_uartIndex = Chip_UART_Send(LPC_USART, &pUcom->txBuf[g_uCOM.txBuf_uartIndex], pUcom->txBuf_count); pUcom->txBuf_count -= pUcom->txBuf_uartIndex; /* Enable UART transmit interrupt */ Chip_UART_IntEnable(LPC_USART, UART_IER_THREINT); } else { pUcom->usbRxPending++; } break; default: break; } return LPC_OK; }
/** * @brief UART interrupt handler sub-routine * @return Nothing */ void UART_IRQHandler(void) { uint32_t count = 0; /* Handle transmit interrupt if enabled */ if (LPC_USART->IER & UART_IER_THREINT) { 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) { 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_IER_THREINT */ Chip_UART_IntDisable(LPC_USART, UART_IER_THREINT); } } } /* 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) { 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); } } }
void UART_IRQHandler(UCOM_DATA_T *pUcom ) { uint32_t count = 0; /* Handle transmit interrupt if enabled */ if (pUcom->selected->IER & UART_IER_THREINT) { if (pUcom->txBuf_count > 0) { count = Chip_UART_Send(pUcom->selected, &pUcom->txBuf[pUcom->txBuf_uartIndex], pUcom->txBuf_count); pUcom->txBuf_count -= count; pUcom->txBuf_uartIndex += count; } /* If usbRxBuf empty check if any packet pending USB EP RAM */ if (pUcom->txBuf_count < 1) { if ((pUcom->usbRxPending > 0) && USB_IsConfigured(pUcom->hUsb)) { pUcom->usbRxPending--; pUcom->txBuf_count = USBD_API->hw->ReadEP(pUcom->hUsb, pUcom->outEndpoint, pUcom->txBuf); pUcom->txBuf_uartIndex = 0; } else { /* all data transmitted on UART disable UART_IER_THREINT */ Chip_UART_IntDisable(pUcom->selected, UART_IER_THREINT); } } } /* Handle receive interrupt */ count = Chip_UART_Read(pUcom->selected, &pUcom->rxBuf[pUcom->rxBuf_uartIndex], UCOM_BUF_SZ - pUcom->rxBuf_uartIndex); if (count) { /* Note, following logic works if UCOM_BUF_SZ is 2^n size only. */ pUcom->rxBuf_uartIndex = (pUcom->rxBuf_uartIndex + count) & (UCOM_BUF_SZ - 1); /* If USB Tx is not busy kick start USB Tx */ if ((pUcom->usbTxBusy == 0) && USB_IsConfigured(pUcom->hUsb)) { pUcom->usbTxBusy = 1; count = USBD_API->hw->WriteEP(pUcom->hUsb, pUcom->inEndpoint, &pUcom->rxBuf[pUcom->rxBuf_usbIndex], count); pUcom->rxBuf_usbIndex = (pUcom->rxBuf_usbIndex + count) & (UCOM_BUF_SZ - 1); } } }
static ErrorCode_t UCOM_bulk_hdlrOutGeneric(USBD_HANDLE_T hUsb, void *data, uint32_t event) { UCOM_DATA_T *pUcom = (UCOM_DATA_T *) data; switch (event) { case USB_EVT_OUT: if(pUcom->selected!=0){ if ((Chip_UART_GetIntsEnabled(pUcom->selected) & UART_IER_THREINT) == 0) { pUcom->txBuf_count = USBD_API->hw->ReadEP(hUsb, pUcom->outEndpoint, pUcom->txBuf); pUcom->txBuf_uartIndex = 0; /* kick start UART tranmission */ pUcom->txBuf_uartIndex = Chip_UART_Send(pUcom->selected, &pUcom->txBuf[pUcom->txBuf_uartIndex], pUcom->txBuf_count); pUcom->txBuf_count -= pUcom->txBuf_uartIndex; /* Enable UART transmit interrupt */ Chip_UART_IntEnable(pUcom->selected, UART_IER_THREINT); } else { pUcom->usbRxPending++; } return LPC_OK; } break; break; default: break; } return ERR_USBD_UNHANDLED; }
/** * @brief Main UART program body * @return Always returns -1 */ int main(void) { FlagStatus exitflag; uint8_t buffer[10]; int ret = 0; uint32_t len; /* UART FIFO configuration Struct variable */ UART_FIFO_CFG_T UARTFIFOConfigStruct; /* Auto baudrate configuration structure */ UART_AB_CFG_T ABConfig; Board_Init(); Board_UART_Init(LPC_UART); //#if (UARTNum != 0) Chip_UART_Init(LPC_UART); Chip_UART_SetBaud(LPC_UART, 115200); Chip_UART_ConfigData(LPC_UART, UART_DATABIT_8, UART_PARITY_NONE, UART_STOPBIT_1); /* Default 8-N-1 */ /* Enable UART Transmit */ Chip_UART_TxCmd(LPC_UART, ENABLE); //#endif Chip_UART_Send(LPC_UART, "F**K OFF\n",10, BLOCKING); Chip_UART_FIFOConfigStructInit(LPC_UART, &UARTFIFOConfigStruct); /* Enable DMA mode in UART */ UARTFIFOConfigStruct.FIFO_DMAMode = ENABLE; /* Initialize FIFO for UART0 peripheral */ Chip_UART_FIFOConfig(LPC_UART, &UARTFIFOConfigStruct); /* Enable UART End of Auto baudrate interrupt */ Chip_UART_IntConfig(LPC_UART, UART_INTCFG_ABEO, ENABLE); /* Enable UART Auto baudrate timeout interrupt */ Chip_UART_IntConfig(LPC_UART, UART_INTCFG_ABTO, ENABLE); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(UARTx_IRQn, 1); /* Enable Interrupt for UART0 channel */ NVIC_EnableIRQ(UARTx_IRQn); // /* ---------------------- Auto baud rate section ----------------------- */ // /* Configure Auto baud rate mode */ // ABConfig.ABMode = UART_AUTOBAUD_MODE0; // ABConfig.AutoRestart = ENABLE; // // /* Start auto baudrate mode */ // Chip_UART_ABCmd(LPC_UART, &ABConfig, ENABLE); // // // /* Loop until auto baudrate mode complete */ // while (Chip_UART_GetABEOStatus(LPC_UART) == RESET) {} // // Chip_UART_Send(LPC_UART, uartABComplete, sizeof(uartABComplete), BLOCKING); /* Disable UART Interrupt */ NVIC_DisableIRQ(UARTx_IRQn); /* Print welcome screen */ Print_Menu_Polling(); exitflag = RESET; /* Read some data from the buffer */ while (exitflag == RESET) { len = 0; while (len == 0) { len = Chip_UART_Receive(LPC_UART, buffer, 1, NONE_BLOCKING); } if (buffer[0] == 27) { /* ESC key, set exit flag */ Chip_UART_Send(LPC_UART, uartPolling_menu3, sizeof(uartPolling_menu3), BLOCKING); ret = -1; exitflag = SET; } else if (buffer[0] == 'c') { Chip_UART_Send(LPC_UART, uartPolling_menu4, sizeof(uartPolling_menu4), BLOCKING); len = 0; while (len == 0) { len = Chip_UART_Receive(LPC_UART, buffer, sizeof(buffer), NONE_BLOCKING); if ((buffer[0] != '1') && (buffer[0] != '2') && (buffer[0] != '3')) { len = 0; } } switch (buffer[0]) { case '1': /* Polling Mode */ Chip_UART_Send(LPC_UART, uartPolling_menu5, sizeof(uartPolling_menu5), BLOCKING); break; case '2': /* Interrupt Mode */ ret = 2; /* Exitflag = SET; */ App_Interrupt_Test(); Print_Menu_Polling(); break; case '3': /* DMA mode */ ret = 3; App_DMA_Test(); Print_Menu_Polling(); break; } } } /* Wait for current transmission complete - THR must be empty */ while (Chip_UART_CheckBusy(LPC_UART) == SET) {} /* DeInitialize UART0 peripheral */ Chip_UART_DeInit(LPC_UART); return ret; }
/* Print Welcome menu by Polling mode */ static void Print_Menu_Polling(void) { Chip_UART_Send(LPC_UART, uartPolling_menu1, sizeof(uartPolling_menu1), BLOCKING); Chip_UART_Send(LPC_UART, uartPolling_menu2, sizeof(uartPolling_menu2), BLOCKING); Chip_UART_Send(LPC_UART, uartPolling_menu5, sizeof(uartPolling_menu5), BLOCKING); }
platform_result_t platform_uart_transmit_bytes( platform_uart_driver_t* driver, const uint8_t* data_out, uint32_t size ) { Chip_UART_Send(driver->interface->uart_base, (uint8_t *)data_out,(int)size, BLOCKING); return WICED_SUCCESS; }
/* UART transmit function for interrupt mode (using ring buffers) */ uint32_t Chip_UART_Interrupt_Transmit(LPC_USART_Type *UARTx, uint8_t *txbuf, uint8_t buflen) { uint8_t *data = (uint8_t *) txbuf; uint32_t bytes = 0; /* Temporarily lock out UART transmit interrupts during this read so the UART transmit interrupt won't cause problems with the index values */ Chip_UART_IntConfig(UARTx, UART_INTCFG_THRE, DISABLE); /* Loop until transmit run buffer is full or until n_bytes expires */ while ((buflen > 0) && (!__BUF_IS_FULL(rb.tx_head, rb.tx_tail))) { /* Write data from buffer into ring buffer */ rb.tx[rb.tx_head] = *data; data++; /* Increment head pointer */ __BUF_INCR(rb.tx_head); /* Increment data count and decrement buffer size count */ bytes++; buflen--; } /* * Check if current Tx interrupt enable is reset, * that means the Tx interrupt must be re-enabled * due to call UART_IntTransmit() function to trigger * this interrupt type */ if (TxIntStat == RESET) { // 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); } } /* * Otherwise, re-enables Tx Interrupt */ else { Chip_UART_IntConfig(UARTx, UART_INTCFG_THRE, ENABLE); } return bytes; }
/* 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); } } }