/*********************************************************************//** * @brief Enable/Disable transmission on UART TxD pin * @param[in] UARTx UART peripheral selected, should be: * - LPC_UART0: UART0 peripheral * - LPC_UART1: UART1 peripheral * - LPC_UART2: UART2 peripheral * - LPC_UART3: UART3 peripheral * @param[in] NewState New State of Tx transmission function, should be: * - ENABLE: Enable this function - DISABLE: Disable this function * @return none **********************************************************************/ void UART_TxCmd(LPC_UART_TypeDef *UARTx, FunctionalState NewState) { CHECK_PARAM(PARAM_UARTx(UARTx)); CHECK_PARAM(PARAM_FUNCTIONALSTATE(NewState)); if (NewState == ENABLE) { if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1) { ((LPC_UART1_TypeDef *)UARTx)->TER |= UART_TER_TXEN; } else { UARTx->TER |= UART_TER_TXEN; } } else { if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1) { ((LPC_UART1_TypeDef *)UARTx)->TER &= (~UART_TER_TXEN) & UART_TER_BITMASK; } else { UARTx->TER &= (~UART_TER_TXEN) & UART_TER_BITMASK; } } }
/*********************************************************************//** * @brief Clear Autobaud Interrupt Pending * @param[in] UARTx UART peripheral selected, should be * - LPC_UART0: UART0 peripheral * - LPC_UART1: UART1 peripheral * - LPC_UART2: UART2 peripheral * - LPC_UART3: UART3 peripheral * @param[in] ABIntType type of auto-baud interrupt, should be: * - UART_AUTOBAUD_INTSTAT_ABEO: End of Auto-baud interrupt * - UART_AUTOBAUD_INTSTAT_ABTO: Auto-baud time out interrupt * @return none **********************************************************************/ void UART_ABClearIntPending(LPC_UART_TypeDef *UARTx, UART_ABEO_Type ABIntType) { CHECK_PARAM(PARAM_UARTx(UARTx)); if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1) { UARTx->ACR |= ABIntType; } else UARTx->ACR |= ABIntType; }
/*********************************************************************//** * @brief Start/Stop Auto Baudrate activity * @param[in] UARTx UART peripheral selected, should be * - LPC_UART0: UART0 peripheral * - LPC_UART1: UART1 peripheral * - LPC_UART2: UART2 peripheral * - LPC_UART3: UART3 peripheral * @param[in] ABConfigStruct A pointer to UART_AB_CFG_Type structure that * contains specified information about UART * auto baudrate configuration * @param[in] NewState New State of Auto baudrate activity, should be: * - ENABLE: Start this activity * - DISABLE: Stop this activity * Note: Auto-baudrate mode enable bit will be cleared once this mode * completed. * @return none **********************************************************************/ void UART_ABCmd(LPC_UART_TypeDef *UARTx, UART_AB_CFG_Type *ABConfigStruct, \ FunctionalState NewState) { uint32_t tmp; CHECK_PARAM(PARAM_UARTx(UARTx)); CHECK_PARAM(PARAM_FUNCTIONALSTATE(NewState)); tmp = 0; if (NewState == ENABLE) { if (ABConfigStruct->ABMode == UART_AUTOBAUD_MODE1){ tmp |= UART_ACR_MODE; } if (ABConfigStruct->AutoRestart == ENABLE){ tmp |= UART_ACR_AUTO_RESTART; } } if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1) { if (NewState == ENABLE) { // Clear DLL and DLM value ((LPC_UART1_TypeDef *)UARTx)->LCR |= UART_LCR_DLAB_EN; ((LPC_UART1_TypeDef *)UARTx)->DLL = 0; ((LPC_UART1_TypeDef *)UARTx)->DLM = 0; ((LPC_UART1_TypeDef *)UARTx)->LCR &= ~UART_LCR_DLAB_EN; // FDR value must be reset to default value ((LPC_UART1_TypeDef *)UARTx)->FDR = 0x10; ((LPC_UART1_TypeDef *)UARTx)->ACR = UART_ACR_START | tmp; } else { ((LPC_UART1_TypeDef *)UARTx)->ACR = 0; } } else { if (NewState == ENABLE) { // Clear DLL and DLM value UARTx->LCR |= UART_LCR_DLAB_EN; UARTx->DLL = 0; UARTx->DLM = 0; UARTx->LCR &= ~UART_LCR_DLAB_EN; // FDR value must be reset to default value UARTx->FDR = 0x10; UARTx->ACR = UART_ACR_START | tmp; } else { UARTx->ACR = 0; } } }
/*********************************************************************//** * @brief Configure FIFO function on selected UART peripheral * @param[in] UARTx UART peripheral selected, should be UART0, UART1, * UART2 or UART3. * @param[in] FIFOCfg Pointer to a UART_FIFO_CFG_Type Structure that * contains specified information about FIFO configuration * @return none **********************************************************************/ void UART_FIFOConfig(UART_TypeDef *UARTx, UART_FIFO_CFG_Type *FIFOCfg) { uint8_t tmp = 0; CHECK_PARAM(PARAM_UARTx(UARTx)); CHECK_PARAM(PARAM_UART_FIFO_LEVEL(FIFOCfg->FIFO_Level)); CHECK_PARAM(PARAM_FUNCTIONALSTATE(FIFOCfg->FIFO_DMAMode)); CHECK_PARAM(PARAM_FUNCTIONALSTATE(FIFOCfg->FIFO_ResetRxBuf)); CHECK_PARAM(PARAM_FUNCTIONALSTATE(FIFOCfg->FIFO_ResetTxBuf)); tmp |= UART_FCR_FIFO_EN; switch (FIFOCfg->FIFO_Level){ case UART_FIFO_TRGLEV0: tmp |= UART_FCR_TRG_LEV0; break; case UART_FIFO_TRGLEV1: tmp |= UART_FCR_TRG_LEV1; break; case UART_FIFO_TRGLEV2: tmp |= UART_FCR_TRG_LEV2; break; case UART_FIFO_TRGLEV3: default: tmp |= UART_FCR_TRG_LEV3; break; } if (FIFOCfg->FIFO_ResetTxBuf == ENABLE) { tmp |= UART_FCR_TX_RS; } if (FIFOCfg->FIFO_ResetRxBuf == ENABLE) { tmp |= UART_FCR_RX_RS; } if (FIFOCfg->FIFO_DMAMode == ENABLE) { tmp |= UART_FCR_DMAMODE_SEL; } //write to FIFO control register if (((UART1_TypeDef *)UARTx) == UART1) { ((UART1_TypeDef *)UARTx)->/*IIFCR.*/FCR = tmp & UART_FCR_BITMASK; } else { UARTx->/*IIFCR.*/FCR = tmp & UART_FCR_BITMASK; } }
/********************************************************************//** * @brief Get current value of Line Status register in UART peripheral. * @param[in] UARTx UART peripheral selected, should be: * - LPC_UART0: UART0 peripheral * - LPC_UART1: UART1 peripheral * - LPC_UART2: UART2 peripheral * - LPC_UART3: UART3 peripheral * @return Current value of Line Status register in UART peripheral. * Note: The return value of this function must be ANDed with each member in * UART_LS_Type enumeration to determine current flag status * corresponding to each Line status type. Because some flags in * Line Status register will be cleared after reading, the next reading * Line Status register could not be correct. So this function used to * read Line status register in one time only, then the return value * used to check all flags. *********************************************************************/ uint8_t UART_GetLineStatus(LPC_UART_TypeDef* UARTx) { CHECK_PARAM(PARAM_UARTx(UARTx)); if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1) { return ((((LPC_UART1_TypeDef *)LPC_UART1)->LSR) & UART_LSR_BITMASK); } else { return ((UARTx->LSR) & UART_LSR_BITMASK); } }
/*********************************************************************//** * @brief Force BREAK character on UART line, output pin UARTx TXD is forced to logic 0. * @param[in] UARTx UART peripheral selected, should be: * - LPC_UART0: UART0 peripheral * - LPC_UART1: UART1 peripheral * - LPC_UART2: UART2 peripheral * - LPC_UART3: UART3 peripheral * @return None **********************************************************************/ void UART_ForceBreak(LPC_UART_TypeDef* UARTx) { CHECK_PARAM(PARAM_UARTx(UARTx)); if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1) { ((LPC_UART1_TypeDef *)UARTx)->LCR |= UART_LCR_BREAK_EN; } else { UARTx->LCR |= UART_LCR_BREAK_EN; } }
/*********************************************************************//** * @brief Receive a single data from UART peripheral * @param[in] UARTx UART peripheral selected, should be: * - LPC_UART0: UART0 peripheral * - LPC_UART1: UART1 peripheral * - LPC_UART2: UART2 peripheral * - LPC_UART3: UART3 peripheral * @return Data received **********************************************************************/ uint8_t UART_ReceiveByte(LPC_UART_TypeDef* UARTx) { CHECK_PARAM(PARAM_UARTx(UARTx)); if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1) { return (((LPC_UART1_TypeDef *)UARTx)->/*RBTHDLR.*/RBR & UART_RBR_MASKBIT); } else { return (UARTx->/*RBTHDLR.*/RBR & UART_RBR_MASKBIT); } }
/*********************************************************************//** * @brief Transmit a single data through UART peripheral * @param[in] UARTx UART peripheral selected, should be: * - LPC_UART0: UART0 peripheral * - LPC_UART1: UART1 peripheral * - LPC_UART2: UART2 peripheral * - LPC_UART3: UART3 peripheral * @param[in] Data Data to transmit (must be 8-bit long) * @return None **********************************************************************/ void UART_SendByte(LPC_UART_TypeDef* UARTx, uint8_t Data) { CHECK_PARAM(PARAM_UARTx(UARTx)); if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1) { ((LPC_UART1_TypeDef *)UARTx)->/*RBTHDLR.*/THR = Data & UART_THR_MASKBIT; } else { UARTx->/*RBTHDLR.*/THR = Data & UART_THR_MASKBIT; } }
/*********************************************************************//** * @brief De-initializes the UARTx peripheral registers to their * default reset values. * @param[in] UARTx UART peripheral selected, should be: * - LPC_UART0: UART0 peripheral * - LPC_UART1: UART1 peripheral * - LPC_UART2: UART2 peripheral * - LPC_UART3: UART3 peripheral * @return None **********************************************************************/ void UART_DeInit(LPC_UART_TypeDef* UARTx) { // For debug mode CHECK_PARAM(PARAM_UARTx(UARTx)); UART_TxCmd(UARTx, DISABLE); #ifdef _UART0 if (UARTx == LPC_UART0) { /* Set up clock and power for UART module */ CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART0, DISABLE); } #endif #ifdef _UART1 if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1) { /* Set up clock and power for UART module */ CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART1, DISABLE); } #endif #ifdef _UART2 if (UARTx == LPC_UART2) { /* Set up clock and power for UART module */ CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART2, DISABLE); } #endif #ifdef _UART3 if (UARTx == LPC_UART3) { /* Set up clock and power for UART module */ CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART3, DISABLE); } #endif }
/********************************************************************//** * @brief Get Interrupt Identification value * @param[in] UARTx UART peripheral selected, should be: * - LPC_UART0: UART0 peripheral * - LPC_UART1: UART1 peripheral * - LPC_UART2: UART2 peripheral * - LPC_UART3: UART3 peripheral * @return Current value of UART UIIR register in UART peripheral. *********************************************************************/ uint32_t UART_GetIntId(LPC_UART_TypeDef* UARTx) { CHECK_PARAM(PARAM_UARTx(UARTx)); return (UARTx->IIR & 0x03CF); }
/********************************************************************//** * @brief Enable or disable specified UART interrupt. * @param[in] UARTx UART peripheral selected, should be * - LPC_UART0: UART0 peripheral * - LPC_UART1: UART1 peripheral * - LPC_UART2: UART2 peripheral * - LPC_UART3: UART3 peripheral * @param[in] UARTIntCfg Specifies the interrupt flag, * should be one of the following: - UART_INTCFG_RBR : RBR Interrupt enable - UART_INTCFG_THRE : THR Interrupt enable - UART_INTCFG_RLS : RX line status interrupt enable - UART1_INTCFG_MS : Modem status interrupt enable (UART1 only) - UART1_INTCFG_CTS : CTS1 signal transition interrupt enable (UART1 only) - UART_INTCFG_ABEO : Enables the end of auto-baud interrupt - UART_INTCFG_ABTO : Enables the auto-baud time-out interrupt * @param[in] NewState New state of specified UART interrupt type, * should be: * - ENALBE: Enable this UART interrupt type. * - DISALBE: Disable this UART interrupt type. * @return None *********************************************************************/ void UART_IntConfig(LPC_UART_TypeDef *UARTx, UART_INT_Type UARTIntCfg, FunctionalState NewState) { uint32_t tmp; CHECK_PARAM(PARAM_UARTx(UARTx)); CHECK_PARAM(PARAM_FUNCTIONALSTATE(NewState)); switch(UARTIntCfg){ case UART_INTCFG_RBR: tmp = UART_IER_RBRINT_EN; break; case UART_INTCFG_THRE: tmp = UART_IER_THREINT_EN; break; case UART_INTCFG_RLS: tmp = UART_IER_RLSINT_EN; break; case UART1_INTCFG_MS: tmp = UART1_IER_MSINT_EN; break; case UART1_INTCFG_CTS: tmp = UART1_IER_CTSINT_EN; break; case UART_INTCFG_ABEO: tmp = UART_IER_ABEOINT_EN; break; case UART_INTCFG_ABTO: tmp = UART_IER_ABTOINT_EN; break; } if ((LPC_UART1_TypeDef *) UARTx == LPC_UART1) { CHECK_PARAM((PARAM_UART_INTCFG(UARTIntCfg)) || (PARAM_UART1_INTCFG(UARTIntCfg))); } else { CHECK_PARAM(PARAM_UART_INTCFG(UARTIntCfg)); } if (NewState == ENABLE) { if ((LPC_UART1_TypeDef *) UARTx == LPC_UART1) { ((LPC_UART1_TypeDef *)UARTx)->/*DLIER.*/IER |= tmp; } else { UARTx->/*DLIER.*/IER |= tmp; } } else { if ((LPC_UART1_TypeDef *) UARTx == LPC_UART1) { ((LPC_UART1_TypeDef *)UARTx)->/*DLIER.*/IER &= (~tmp) & UART1_IER_BITMASK; } else { UARTx->/*DLIER.*/IER &= (~tmp) & UART_IER_BITMASK; } } }
/********************************************************************//** * @brief Initializes the UARTx peripheral according to the specified * parameters in the UART_ConfigStruct. * @param[in] UARTx UART peripheral selected, should be: * - LPC_UART0: UART0 peripheral * - LPC_UART1: UART1 peripheral * - LPC_UART2: UART2 peripheral * - LPC_UART3: UART3 peripheral * @param[in] UART_ConfigStruct Pointer to a UART_CFG_Type structure * that contains the configuration information for the * specified UART peripheral. * @return None *********************************************************************/ void UART_Init(LPC_UART_TypeDef *UARTx, UART_CFG_Type *UART_ConfigStruct) { uint32_t tmp; // For debug mode CHECK_PARAM(PARAM_UARTx(UARTx)); CHECK_PARAM(PARAM_UART_DATABIT(UART_ConfigStruct->Databits)); CHECK_PARAM(PARAM_UART_STOPBIT(UART_ConfigStruct->Stopbits)); CHECK_PARAM(PARAM_UART_PARITY(UART_ConfigStruct->Parity)); #ifdef _UART0 if(UARTx == LPC_UART0) { /* Set up clock and power for UART module */ CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART0, ENABLE); } #endif #ifdef _UART1 if(((LPC_UART1_TypeDef *)UARTx) == LPC_UART1) { /* Set up clock and power for UART module */ CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART1, ENABLE); } #endif #ifdef _UART2 if(UARTx == LPC_UART2) { /* Set up clock and power for UART module */ CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART2, ENABLE); } #endif #ifdef _UART3 if(UARTx == LPC_UART3) { /* Set up clock and power for UART module */ CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCUART3, ENABLE); } #endif if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1) { /* FIFOs are empty */ ((LPC_UART1_TypeDef *)UARTx)->/*IIFCR.*/FCR = ( UART_FCR_FIFO_EN \ | UART_FCR_RX_RS | UART_FCR_TX_RS); // Disable FIFO ((LPC_UART1_TypeDef *)UARTx)->/*IIFCR.*/FCR = 0; // Dummy reading while (((LPC_UART1_TypeDef *)UARTx)->LSR & UART_LSR_RDR) { tmp = ((LPC_UART1_TypeDef *)UARTx)->/*RBTHDLR.*/RBR; } ((LPC_UART1_TypeDef *)UARTx)->TER = UART_TER_TXEN; // Wait for current transmit complete while (!(((LPC_UART1_TypeDef *)UARTx)->LSR & UART_LSR_THRE)); // Disable Tx ((LPC_UART1_TypeDef *)UARTx)->TER = 0; // Disable interrupt ((LPC_UART1_TypeDef *)UARTx)->/*DLIER.*/IER = 0; // Set LCR to default state ((LPC_UART1_TypeDef *)UARTx)->LCR = 0; // Set ACR to default state ((LPC_UART1_TypeDef *)UARTx)->ACR = 0; // Set Modem Control to default state ((LPC_UART1_TypeDef *)UARTx)->MCR = 0; // Set RS485 control to default state ((LPC_UART1_TypeDef *)UARTx)->RS485CTRL = 0; // Set RS485 delay timer to default state ((LPC_UART1_TypeDef *)UARTx)->RS485DLY = 0; // Set RS485 addr match to default state ((LPC_UART1_TypeDef *)UARTx)->ADRMATCH = 0; //Dummy Reading to Clear Status tmp = ((LPC_UART1_TypeDef *)UARTx)->MSR; tmp = ((LPC_UART1_TypeDef *)UARTx)->LSR; } else { /* FIFOs are empty */ UARTx->/*IIFCR.*/FCR = ( UART_FCR_FIFO_EN | UART_FCR_RX_RS | UART_FCR_TX_RS); // Disable FIFO UARTx->/*IIFCR.*/FCR = 0; // Dummy reading while (UARTx->LSR & UART_LSR_RDR) { tmp = UARTx->/*RBTHDLR.*/RBR; } UARTx->TER = UART_TER_TXEN; // Wait for current transmit complete while (!(UARTx->LSR & UART_LSR_THRE)); // Disable Tx UARTx->TER = 0; // Disable interrupt UARTx->/*DLIER.*/IER = 0; // Set LCR to default state UARTx->LCR = 0; // Set ACR to default state UARTx->ACR = 0; // Dummy reading tmp = UARTx->LSR; } if (UARTx == LPC_UART3) { // Set IrDA to default state UARTx->ICR = 0; } // Set Line Control register ---------------------------- uart_set_divisors(UARTx, (UART_ConfigStruct->Baud_rate)); if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1) { tmp = (((LPC_UART1_TypeDef *)UARTx)->LCR & (UART_LCR_DLAB_EN | UART_LCR_BREAK_EN)) \ & UART_LCR_BITMASK; } else { tmp = (UARTx->LCR & (UART_LCR_DLAB_EN | UART_LCR_BREAK_EN)) & UART_LCR_BITMASK; } switch (UART_ConfigStruct->Databits){ case UART_DATABIT_5: tmp |= UART_LCR_WLEN5; break; case UART_DATABIT_6: tmp |= UART_LCR_WLEN6; break; case UART_DATABIT_7: tmp |= UART_LCR_WLEN7; break; case UART_DATABIT_8: default: tmp |= UART_LCR_WLEN8; break; } if (UART_ConfigStruct->Parity == UART_PARITY_NONE) { // Do nothing... } else { tmp |= UART_LCR_PARITY_EN; switch (UART_ConfigStruct->Parity) { case UART_PARITY_ODD: tmp |= UART_LCR_PARITY_ODD; break; case UART_PARITY_EVEN: tmp |= UART_LCR_PARITY_EVEN; break; case UART_PARITY_SP_1: tmp |= UART_LCR_PARITY_F_1; break; case UART_PARITY_SP_0: tmp |= UART_LCR_PARITY_F_0; break; default: break; } } switch (UART_ConfigStruct->Stopbits){ case UART_STOPBIT_2: tmp |= UART_LCR_STOPBIT_SEL; break; case UART_STOPBIT_1: default: // Do no thing break; } // Write back to LCR, configure FIFO and Disable Tx if (((LPC_UART1_TypeDef *)UARTx) == LPC_UART1) { ((LPC_UART1_TypeDef *)UARTx)->LCR = (uint8_t)(tmp & UART_LCR_BITMASK); } else { UARTx->LCR = (uint8_t)(tmp & UART_LCR_BITMASK); } }