void UART1_IRQHandler() { uint32_t interruptSource = UART_GetIntId(UART1_DEVICE) & UART_IIR_INTID_MASK; if(interruptSource == 0) { // Check Modem status uint8_t modemStatus = UART_FullModemGetStatus(LPC_UART1); // Check CTS status change flag if (modemStatus & UART1_MODEM_STAT_DELTA_CTS) { // if CTS status is active, continue to send data if (modemStatus & UART1_MODEM_STAT_CTS) { CTS_STATE = ACTIVE; UART_TxCmd(UART1_DEVICE, ENABLE); } else { // Otherwise, Stop current transmission immediately CTS_STATE = INACTIVE; UART_TxCmd(UART1_DEVICE, DISABLE); } } } switch(interruptSource) { case UART_IIR_INTID_RDA: case UART_IIR_INTID_CTI: handleReceiveInterrupt(); break; case UART_IIR_INTID_THRE: handleTransmitInterrupt(); break; } }
/*********************************************************************//** * @brief UART1 interrupt handler sub-routine * @param[in] None * @return None **********************************************************************/ void UART1_IRQHandler(void) { uint8_t modemsts; uint32_t intsrc, tmp, tmp1; /* Determine the interrupt source */ intsrc = UART_GetIntId((LPC_UART_TypeDef *)LPC_UART1); tmp = intsrc & UART_IIR_INTID_MASK; /* * In case of using UART1 with full modem, * interrupt ID = 0 that means modem status interrupt has been detected */ if (tmp == 0){ // Check Modem status modemsts = UART_FullModemGetStatus(LPC_UART1); #if (AUTO_RTS_CTS_USE == 0) // Check CTS status change flag if (modemsts & UART1_MODEM_STAT_DELTA_CTS) { // if CTS status is active, continue to send data if (modemsts & UART1_MODEM_STAT_CTS) { // Re-Enable Tx UART_TxCmd((LPC_UART_TypeDef *)LPC_UART1, ENABLE); } // Otherwise, Stop current transmission immediately else{ // Disable Tx UART_TxCmd((LPC_UART_TypeDef *)LPC_UART1, DISABLE); } } #endif } // Receive Line Status if (tmp == UART_IIR_INTID_RLS){ // Check line status tmp1 = UART_GetLineStatus((LPC_UART_TypeDef *)LPC_UART1); // Mask out the Receive Ready and Transmit Holding empty status tmp1 &= (UART_LSR_OE | UART_LSR_PE | UART_LSR_FE \ | UART_LSR_BI | UART_LSR_RXFE); // If any error exist if (tmp1) { UART1_IntErr(tmp1); } } // Receive Data Available or Character time-out if ((tmp == UART_IIR_INTID_RDA) || (tmp == UART_IIR_INTID_CTI)){ UART1_IntReceive(); } // Transmit Holding Empty if (tmp == UART_IIR_INTID_THRE){ UART1_IntTransmit(); } }
/* Auto flow control does work, but it turns the serial write functions into * blocking functions, which drags USB down. Instead we handle it manually so we * can make them asynchronous and let USB run at full speed. */ void configureFlowControl() { if (UART_FullModemGetStatus(LPC_UART1) & UART1_MODEM_STAT_CTS) { // Enable UART Transmit UART_TxCmd(UART1_DEVICE, ENABLE); } // Enable Modem status interrupt UART_IntConfig(UART1_DEVICE, UART1_INTCFG_MS, ENABLE); // Enable CTS1 signal transition interrupt UART_IntConfig(UART1_DEVICE, UART1_INTCFG_CTS, ENABLE); resumeReceive(); }
/*********************************************************************//** * @brief Main UART testing example sub-routine * Print welcome screen first, then press any key to have it * read in from the terminal and returned back to the terminal. * - Press ESC to exit * - Press 'r' to print welcome screen menu again **********************************************************************/ int c_entry(void) { // UART Configuration structure variable UART_CFG_Type UARTConfigStruct; // UART FIFO configuration Struct variable UART_FIFO_CFG_Type UARTFIFOConfigStruct; // Pin configuration for UART0 PINSEL_CFG_Type PinCfg; uint32_t idx, len; __IO FlagStatus exitflag; uint8_t buffer[10]; // DeInit NVIC and SCBNVIC NVIC_DeInit(); NVIC_SCBDeInit(); /* Configure the NVIC Preemption Priority Bits: * two (2) bits of preemption priority, six (6) bits of sub-priority. * Since the Number of Bits used for Priority Levels is five (5), so the * actual bit number of sub-priority is three (3) */ NVIC_SetPriorityGrouping(0x05); // Set Vector table offset value #if (__RAM_MODE__==1) NVIC_SetVTOR(0x10000000); #else NVIC_SetVTOR(0x00000000); #endif /* * Initialize UART1 pin connect */ PinCfg.Funcnum = 1; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = 0; for (idx = 15; idx <= 22; idx++){ PinCfg.Pinnum = idx; PINSEL_ConfigPin(&PinCfg); } /* Initialize UART Configuration parameter structure to default state: * Baudrate = 9600bps * 8 data bit * 1 Stop bit * None parity */ UART_ConfigStructInit(&UARTConfigStruct); // Initialize UART1 peripheral with given to corresponding parameter UART_Init((LPC_UART_TypeDef *)LPC_UART1, &UARTConfigStruct); /* Initialize FIFOConfigStruct to default state: * - FIFO_DMAMode = DISABLE * - FIFO_Level = UART_FIFO_TRGLEV0 * - FIFO_ResetRxBuf = ENABLE * - FIFO_ResetTxBuf = ENABLE * - FIFO_State = ENABLE */ UART_FIFOConfigStructInit(&UARTFIFOConfigStruct); // Initialize FIFO for UART1 peripheral UART_FIFOConfig((LPC_UART_TypeDef *)LPC_UART1, &UARTFIFOConfigStruct); #if (AUTO_RTS_CTS_USE==0) /* * Determine current state of CTS pin to enable Tx * activity */ if (UART_FullModemGetStatus(LPC_UART1) & UART1_MODEM_STAT_CTS) { // Enable UART Transmit UART_TxCmd((LPC_UART_TypeDef *)LPC_UART1, ENABLE); } #else // Enable UART Transmit UART_TxCmd((UART_TypeDef *)UART1, ENABLE); #endif // Reset ring buf head and tail idx __BUF_RESET(rb.rx_head); __BUF_RESET(rb.rx_tail); __BUF_RESET(rb.tx_head); __BUF_RESET(rb.tx_tail); #if AUTO_RTS_CTS_USE UART_FullModemConfigMode(UART1, UART1_MODEM_MODE_AUTO_RTS, ENABLE); UART_FullModemConfigMode(UART1, UART1_MODEM_MODE_AUTO_CTS, ENABLE); #else // Enable Modem status interrupt UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART1_INTCFG_MS, ENABLE); // Enable CTS1 signal transition interrupt UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART1_INTCFG_CTS, ENABLE); // Modem Status interrupt call back UART_SetupCbs((LPC_UART_TypeDef *)LPC_UART1, 4, (void *)UART1_ModemCallBack); // Force RTS pin state to ACTIVE UART_FullModemForcePinState(LPC_UART1, UART1_MODEM_PIN_RTS, ACTIVE); //RESET RTS State flag RTS_State = ACTIVE; #endif // Setup callback --------------- // Receive callback UART_SetupCbs((LPC_UART_TypeDef *)LPC_UART1, 0, (void *)UART1_IntReceive); // Transmit callback UART_SetupCbs((LPC_UART_TypeDef *)LPC_UART1, 1, (void *)UART1_IntTransmit); // Line Status Error callback UART_SetupCbs((LPC_UART_TypeDef *)LPC_UART1, 3, (void *)UART1_IntErr); /* Enable UART Rx interrupt */ UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART_INTCFG_RBR, ENABLE); /* Enable UART line status interrupt */ UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART_INTCFG_RLS, ENABLE); /* * Do not enable transmit interrupt here, since it is handled by * UART_Send() function, just to reset Tx Interrupt state for the * first time */ TxIntStat = RESET; /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(UART1_IRQn, ((0x01<<3)|0x01)); /* Enable Interrupt for UART1 channel */ NVIC_EnableIRQ(UART1_IRQn); // print welcome screen print_menu(); // reset exit flag exitflag = RESET; /* Read some data from the buffer */ while (exitflag == RESET) { len = 0; while (len == 0) { len = UARTReceive((LPC_UART_TypeDef *)LPC_UART1, buffer, sizeof(buffer)); } /* Got some data */ idx = 0; while (idx < len) { if (buffer[idx] == 27) { /* ESC key, set exit flag */ UARTSend((LPC_UART_TypeDef *)LPC_UART1, menu3, sizeof(menu3)); exitflag = SET; } else if (buffer[idx] == 'r') { print_menu(); } else { /* Echo it back */ UARTSend((LPC_UART_TypeDef *)LPC_UART1, &buffer[idx], 1); } idx++; } } // wait for current transmission complete - THR must be empty while (UART_CheckBusy((LPC_UART_TypeDef *)LPC_UART1) == SET); // DeInitialize UART1 peripheral UART_DeInit((LPC_UART_TypeDef *)LPC_UART1); /* Loop forever */ while(1); return 1; }
/*********************************************************************//** * @brief c_entry: Main UART-FULLMODEM program body * @param[in] None * @return int **********************************************************************/ int c_entry(void) { // UART Configuration structure variable UART_CFG_Type UARTConfigStruct; // UART FIFO configuration Struct variable UART_FIFO_CFG_Type UARTFIFOConfigStruct; // Pin configuration for UART1 PINSEL_CFG_Type PinCfg; uint32_t idx, len; __IO FlagStatus exitflag; uint8_t buffer[10]; /* * Initialize UART1 pin connect * If using MCB1700 eval board, assign pin P2.0 - P2.7 * If using IAR 1768 KS board, assign pin P0.7 - P0.15 */ #ifdef MCB_LPC_1768 PinCfg.Funcnum = 2; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = 2; for (idx = 0; idx <= 7; idx++){ PinCfg.Pinnum = idx; PINSEL_ConfigPin(&PinCfg); } #elif defined(IAR_LPC_1768) PinCfg.Funcnum = 1; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = 0; for (idx = 15; idx <= 22; idx++){ PinCfg.Pinnum = idx; PINSEL_ConfigPin(&PinCfg); } #endif /* Initialize UART Configuration parameter structure to default state: * Baudrate = 9600bps * 8 data bit * 1 Stop bit * None parity */ UART_ConfigStructInit(&UARTConfigStruct); // Initialize UART1 peripheral with given to corresponding parameter UART_Init((LPC_UART_TypeDef *)LPC_UART1, &UARTConfigStruct); /* Initialize FIFOConfigStruct to default state: * - FIFO_DMAMode = DISABLE * - FIFO_Level = UART_FIFO_TRGLEV0 * - FIFO_ResetRxBuf = ENABLE * - FIFO_ResetTxBuf = ENABLE * - FIFO_State = ENABLE */ UART_FIFOConfigStructInit(&UARTFIFOConfigStruct); // Initialize FIFO for UART1 peripheral UART_FIFOConfig((LPC_UART_TypeDef *)LPC_UART1, &UARTFIFOConfigStruct); #if (AUTO_RTS_CTS_USE==0) /* * Determine current state of CTS pin to enable Tx * activity */ if (UART_FullModemGetStatus(LPC_UART1) & UART1_MODEM_STAT_CTS) { // Enable UART Transmit UART_TxCmd((LPC_UART_TypeDef *)LPC_UART1, ENABLE); } #else // Enable UART Transmit UART_TxCmd((LPC_UART_TypeDef *)LPC_UART1, ENABLE); #endif // Reset ring buf head and tail idx __BUF_RESET(rb.rx_head); __BUF_RESET(rb.rx_tail); __BUF_RESET(rb.tx_head); __BUF_RESET(rb.tx_tail); #if AUTO_RTS_CTS_USE UART_FullModemConfigMode(LPC_UART1, UART1_MODEM_MODE_AUTO_RTS, ENABLE); UART_FullModemConfigMode(LPC_UART1, UART1_MODEM_MODE_AUTO_CTS, ENABLE); #else // Enable Modem status interrupt UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART1_INTCFG_MS, ENABLE); // Enable CTS1 signal transition interrupt UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART1_INTCFG_CTS, ENABLE); // Force RTS pin state to ACTIVE UART_FullModemForcePinState(LPC_UART1, UART1_MODEM_PIN_RTS, ACTIVE); //RESET RTS State flag RTS_State = ACTIVE; #endif /* Enable UART Rx interrupt */ UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART_INTCFG_RBR, ENABLE); /* Enable UART line status interrupt */ UART_IntConfig((LPC_UART_TypeDef *)LPC_UART1, UART_INTCFG_RLS, ENABLE); /* * Do not enable transmit interrupt here, since it is handled by * UART_Send() function, just to reset Tx Interrupt state for the * first time */ TxIntStat = RESET; /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(UART1_IRQn, ((0x01<<3)|0x01)); /* Enable Interrupt for UART1 channel */ NVIC_EnableIRQ(UART1_IRQn); // print welcome screen print_menu(); // reset exit flag exitflag = RESET; /* Read some data from the buffer */ while (exitflag == RESET) { len = 0; while (len == 0) { len = UARTReceive((LPC_UART_TypeDef *)LPC_UART1, buffer, sizeof(buffer)); } /* Got some data */ idx = 0; while (idx < len) { if (buffer[idx] == 27) { /* ESC key, set exit flag */ UARTSend((LPC_UART_TypeDef *)LPC_UART1, menu3, sizeof(menu3)); exitflag = SET; } else if (buffer[idx] == 'r') { print_menu(); } else { /* Echo it back */ UARTSend((LPC_UART_TypeDef *)LPC_UART1, &buffer[idx], 1); } idx++; } } // wait for current transmission complete - THR must be empty while (UART_CheckBusy((LPC_UART_TypeDef *)LPC_UART1) == SET); // DeInitialize UART1 peripheral UART_DeInit((LPC_UART_TypeDef *)LPC_UART1); /* Loop forever */ while(1); return 1; }