📄 lpc177x_8x_uart.c
字号:
* - UART_2: UART2 peripheral
* - UART_3: UART3 peripheral
* - UART_4: UART4 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(UART_ID_Type UartID, UART_INT_Type UARTIntCfg, FunctionalState NewState)
{
uint32_t tmp;
__IO uint32_t *IER = NULL;
uint32_t IERMask = 0;
switch (UartID)
{
case UART_0:
IER = &LPC_UART0->IER;
IERMask = UART_IER_BITMASK;
break;
case UART_1:
IER = &LPC_UART1->IER;
IERMask = UART1_IER_BITMASK;
break;
case UART_2:
IER = &LPC_UART2->IER;
IERMask = UART_IER_BITMASK;
break;
case UART_3:
IER = &LPC_UART3->IER;
IERMask = UART_IER_BITMASK;
break;
case UART_4:
IER = &LPC_UART4->IER;
IERMask = UART_IER_BITMASK;
break;
}
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 UART_INTCFG_MS:
tmp = UART1_IER_MSINT_EN;
break;
case UART_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 (NewState == ENABLE)
{
*IER |= tmp& IERMask;
}
else
{
*IER &= (~tmp) & IERMask;
}
}
/********************************************************************//**
* @brief Get current value of Line Status register in UART peripheral.
* @param[in] UARTx UART peripheral selected, should be:
* - UART_0: UART0 peripheral
* - UART_1: UART1 peripheral
* - UART_2: UART2 peripheral
* - UART_3: UART3 peripheral
* - UART_4: UART4 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(UART_ID_Type UartID)
{
switch (UartID)
{
case UART_0:
return ((LPC_UART0->LSR) & UART_LSR_BITMASK);
case UART_1:
return ((LPC_UART1->LSR) & UART_LSR_BITMASK);
case UART_2:
return ((LPC_UART2->LSR) & UART_LSR_BITMASK);
case UART_3:
return ((LPC_UART3->LSR) & UART_LSR_BITMASK);
case UART_4:
return ((LPC_UART4->LSR) & UART_LSR_BITMASK);
}
return 0;
}
/********************************************************************//**
* @brief Get Interrupt Identification value
* @param[in] UARTx UART peripheral selected, should be:
* - UART_0: UART0 peripheral
* - UART_1: UART1 peripheral
* - UART_2: UART2 peripheral
* - UART_3: UART3 peripheral
* - UART_4: UART4 peripheral
* @return Current value of UART UIIR register in UART peripheral.
*********************************************************************/
uint32_t UART_GetIntId(UART_ID_Type UartID)
{
switch (UartID)
{
case UART_0:
return ((LPC_UART0->IIR) & UART_IIR_BITMASK);
case UART_1:
return ((LPC_UART1->IIR) & UART_IIR_BITMASK);
case UART_2:
return ((LPC_UART2->IIR) & UART_IIR_BITMASK);
case UART_3:
return ((LPC_UART3->IIR) & UART_IIR_BITMASK);
case UART_4:
return ((LPC_UART4->IIR) & UART_IIR_BITMASK);
}
return 0;
}
/*********************************************************************//**
* @brief Check whether if UART is busy or not
* @param[in] UARTx UART peripheral selected, should be:
* - UART_0: UART0 peripheral
* - UART_1: UART1 peripheral
* - UART_2: UART2 peripheral
* - UART_3: UART3 peripheral
* - UART_4: UART4 peripheral
* @return RESET if UART is not busy, otherwise return SET.
**********************************************************************/
FlagStatus UART_CheckBusy(UART_ID_Type UartID)
{
uint32_t LSR = 0;
switch (UartID)
{
case UART_0:
LSR = (LPC_UART0)->LSR & UART_LSR_TEMT;
break;
case UART_1:
LSR = (LPC_UART1)->LSR & UART_LSR_TEMT;
break;
case UART_2:
LSR = (LPC_UART2)->LSR & UART_LSR_TEMT;
break;
case UART_3:
LSR = (LPC_UART3)->LSR & UART_LSR_TEMT;
break;
case UART_4:
LSR = (LPC_UART4)->LSR & UART_LSR_TEMT;
break;
}
if (LSR & UART_LSR_TEMT)
{
return RESET;
}
return SET;
}
/*********************************************************************//**
* @brief Configure FIFO function on selected UART peripheral
* @param[in] UARTx UART peripheral selected, should be:
* - UART_0: UART0 peripheral
* - UART_1: UART1 peripheral
* - UART_2: UART2 peripheral
* - UART_3: UART3 peripheral
* - UART_4: UART4 peripheral
* @param[in] FIFOCfg Pointer to a UART_FIFO_CFG_Type Structure that
* contains specified information about FIFO configuration
* @return none
**********************************************************************/
void UART_FIFOConfig(UART_ID_Type UartID, UART_FIFO_CFG_Type *FIFOCfg)
{
uint8_t tmp = 0;
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
switch (UartID)
{
case UART_0:
LPC_UART0->FCR = tmp & UART_FCR_BITMASK;
break;
case UART_1:
LPC_UART1->FCR = tmp & UART_FCR_BITMASK;
break;
case UART_2:
LPC_UART2->FCR = tmp & UART_FCR_BITMASK;
break;
case UART_3:
LPC_UART3->FCR = tmp & UART_FCR_BITMASK;
break;
case UART_4:
LPC_UART4->FCR = tmp & UART_FCR_BITMASK;
break;
}
}
/*****************************************************************************//**
* @brief Fills each UART_FIFOInitStruct member with its default value:
* - FIFO_DMAMode = DISABLE
* - FIFO_Level = UART_FIFO_TRGLEV0
* - FIFO_ResetRxBuf = ENABLE
* - FIFO_ResetTxBuf = ENABLE
* - FIFO_State = ENABLE
* @param[in] UART_FIFOInitStruct Pointer to a UART_FIFO_CFG_Type structure
* which will be initialized.
* @return None
*******************************************************************************/
void UART_FIFOConfigStructInit(UART_FIFO_CFG_Type *UART_FIFOInitStruct)
{
UART_FIFOInitStruct->FIFO_DMAMode = DISABLE;
UART_FIFOInitStruct->FIFO_Level = UART_FIFO_TRGLEV0;
UART_FIFOInitStruct->FIFO_ResetRxBuf = ENABLE;
UART_FIFOInitStruct->FIFO_ResetTxBuf = ENABLE;
}
/*********************************************************************//**
* @brief Start/Stop Auto Baudrate activity
* @param[in] UARTx UART peripheral selected, should be
* - UART_0: UART0 peripheral
* - UART_1: UART1 peripheral
* - UART_2: UART2 peripheral
* - UART_3: UART3 peripheral
* - UART_4: UART4 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(UART_ID_Type UartID, UART_AB_CFG_Type *ABConfigStruct,
FunctionalState NewState)
{
uint32_t tmp;
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 (UartID == UART_1)
{
if (NewState == ENABLE)
{
// Clear DLL and DLM value
LPC_UART1->LCR |= UART_LCR_DLAB_EN;
LPC_UART1->DLL = 0;
LPC_UART1->DLM = 0;
LPC_UART1->LCR &= ~UART_LCR_DLAB_EN;
// FDR value must be reset to default value
LPC_UART1->FDR = 0x10;
LPC_UART1->ACR = UART_ACR_START | tmp;
}
else
{
LPC_UART1->ACR = 0;
}
}
else if (UartID == UART_4)
{
if (NewState == ENABLE)
{
// Clear DLL and DLM value
LPC_UART4->LCR |= UART_LCR_DLAB_EN;
LPC_UART4->DLL = 0;
LPC_UART4->DLM = 0;
LPC_UART4->LCR &= ~UART_LCR_DLAB_EN;
// FDR value must be reset to default value
LPC_UART4->FDR = 0x10;
LPC_UART4->ACR = UART_ACR_START | tmp;
}
else
{
LPC_UART4->ACR = 0;
}
}
else
{
LPC_UART_TypeDef *UARTx = uart_get_pointer(UartID);
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 Clear Autobaud Interrupt Pending
* @param[in] UARTx UART peripheral selected, should be
* - UART_0: UART0 peripheral
* - UART_1: UART1 peripheral
* - UART_2: UART2 peripheral
* - UART_3: UART3 peripheral
* - UART_4: UART4 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(UART_ID_Type UartID, UART_ABEO_Type ABIntType)
{
if (UartID == UART_1)
{
LPC_UART1->ACR |= ABIntType;
}
else if (UartID == UART_4)
{
LPC_UART4->ACR |= ABIntType;
}
else
{
LPC_UART_TypeDef *UARTx = uart_get_pointer(UartID);
UARTx->ACR |= ABIntType;
}
}
/*********************************************************************//**
* @brief Enable/Disable transmission on UART TxD pin
* @param[in] UARTx UART peripheral selected, should be:
* - UART_0: UART0 peripheral
* - UART_1: UART1 peripheral
* - UART_2: UART2 peripheral
* - UART_3: UART3 peripheral
* - UART_4: UART4 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(UART_ID_Type UartID, FunctionalState NewState)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -