/** * @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; }
/* 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; }
/** * @brief Main UART program body * @return Always returns -1 */ int main(void) { FlagStatus exitflag; uint8_t buffer[10]; int ret = 0; int len; SystemCoreClockUpdate(); Board_Init(); Board_UART_Init(LPC_UART); #if !((defined(CHIP_LPC43XX) && defined(BOARD_KEIL_MCB_18574357) && UARTNum==3) || ((!(defined(CHIP_LPC43XX) && defined(BOARD_KEIL_MCB_18574357))) && UARTNum==0)) Chip_UART_Init(LPC_UART); Chip_UART_SetBaud(LPC_UART, 115200); Chip_UART_ConfigData(LPC_UART, UART_LCR_WLEN8 | UART_LCR_SBS_1BIT); /* Default 8-N-1 */ /* Enable UART Transmit */ Chip_UART_TXEnable(LPC_UART); #endif /* Reset FIFOs, Enable FIFOs and DMA mode in UART */ Chip_UART_SetupFIFOS(LPC_UART, (UART_FCR_FIFO_EN | UART_FCR_RX_RS | UART_FCR_TX_RS | UART_FCR_DMAMODE_SEL | UART_FCR_TRG_LEV0)); /* Enable UART End of Auto baudrate & Auto baudrate timeout interrupts */ Chip_UART_IntEnable(LPC_UART, (UART_IER_ABEOINT | UART_IER_ABTOINT)); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(UARTx_IRQn, 1); /* Enable Interrupt for UART0 channel */ NVIC_EnableIRQ(UARTx_IRQn); /* Send UART Autobaud completed message */ Chip_UART_SendBlocking(LPC_UART, uartABStart, sizeof(uartABStart)); /* ---------------------- Auto baud rate section ----------------------- */ /* Start auto baudrate mode */ Chip_UART_ABCmd(LPC_UART, UART_ACR_MODE0, true, ENABLE); /* Loop until auto baudrate mode complete */ while (Chip_UART_GetABEOStatus(LPC_UART) == RESET) {} /* Send UART Autobaud completed message */ Chip_UART_SendBlocking(LPC_UART, uartABComplete, sizeof(uartABComplete)); /* 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_Read(LPC_UART, buffer, 1); } if (buffer[0] == 27) { /* ESC key, set exit flag */ Chip_UART_SendBlocking(LPC_UART, uartPolling_menu3, sizeof(uartPolling_menu3)); ret = -1; exitflag = SET; } else if (buffer[0] == 'c') { Chip_UART_SendBlocking(LPC_UART, uartPolling_menu4, sizeof(uartPolling_menu4)); len = 0; while (len == 0) { len = Chip_UART_Read(LPC_UART, buffer, sizeof(buffer)); if ((buffer[0] != '1') && (buffer[0] != '2') && (buffer[0] != '3')) { len = 0; } } switch (buffer[0]) { case '1': /* Polling Mode */ Chip_UART_SendBlocking(LPC_UART, uartPolling_menu5, sizeof(uartPolling_menu5)); 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; }
/* 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); } } }