/** * @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 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); } } }