📄 lpc177x_8x_uart.c
字号:
{
if (NewState == ENABLE)
{
if (UartID == UART_1)
{
LPC_UART1->TER |= UART_TER_TXEN;
}
else if (UartID == UART_4)
{
LPC_UART4->TER |= UART4_TER_TXEN;
}
else
{
LPC_UART_TypeDef *UARTx = uart_get_pointer(UartID);
UARTx->TER |= UART_TER_TXEN;
}
}
else
{
if (UartID == UART_1)
{
LPC_UART1->TER &= (~UART_TER_TXEN) & UART_TER_BITMASK;
}
else if (UartID == UART_4)
{
LPC_UART4->TER &= (~UART4_TER_TXEN) & UART4_TER_BITMASK;
}
else
{
LPC_UART_TypeDef *UARTx = uart_get_pointer(UartID);
UARTx->TER &= (~UART_TER_TXEN) & UART_TER_BITMASK;
}
}
}
/* UART IrDA functions ---------------------------------------------------*/
/*********************************************************************//**
* @brief Enable or disable inverting serial input function of IrDA
* on UART peripheral.
* @param[in] UARTx UART peripheral selected, should be LPC_UART4 (only)
* @param[in] NewState New state of inverting serial input, should be:
* - ENABLE: Enable this function.
* - DISABLE: Disable this function.
* @return none
**********************************************************************/
void UART_IrDAInvtInputCmd(UART_ID_Type UartID, FunctionalState NewState)
{
if (UartID != UART_4)
return;
if (NewState == ENABLE)
{
LPC_UART4->ICR |= UART_ICR_IRDAINV;
}
else if (NewState == DISABLE)
{
LPC_UART4->ICR &= (~UART_ICR_IRDAINV) & UART_ICR_BITMASK;
}
}
/*********************************************************************//**
* @brief Enable or disable IrDA function on UART peripheral.
* @param[in] UARTx UART peripheral selected, should be LPC_UART4 (only)
* @param[in] NewState New state of IrDA function, should be:
* - ENABLE: Enable this function.
* - DISABLE: Disable this function.
* @return none
**********************************************************************/
void UART_IrDACmd(UART_ID_Type UartID, FunctionalState NewState)
{
if (UartID != UART_4)
return;
if (NewState == ENABLE)
{
LPC_UART4->ICR |= UART_ICR_IRDAEN;
}
else
{
LPC_UART4->ICR &= (~UART_ICR_IRDAEN) & UART_ICR_BITMASK;
}
}
/*********************************************************************//**
* @brief Configure Pulse divider for IrDA function on UART peripheral.
* @param[in] UARTx UART peripheral selected, should be LPC_UART4 (only)
* @param[in] PulseDiv Pulse Divider value from Peripheral clock,
* should be one of the following:
- UART_IrDA_PULSEDIV2 : Pulse width = 2 * Tpclk
- UART_IrDA_PULSEDIV4 : Pulse width = 4 * Tpclk
- UART_IrDA_PULSEDIV8 : Pulse width = 8 * Tpclk
- UART_IrDA_PULSEDIV16 : Pulse width = 16 * Tpclk
- UART_IrDA_PULSEDIV32 : Pulse width = 32 * Tpclk
- UART_IrDA_PULSEDIV64 : Pulse width = 64 * Tpclk
- UART_IrDA_PULSEDIV128 : Pulse width = 128 * Tpclk
- UART_IrDA_PULSEDIV256 : Pulse width = 256 * Tpclk
* @return none
**********************************************************************/
void UART_IrDAPulseDivConfig(UART_ID_Type UartID, UART_IrDA_PULSE_Type PulseDiv)
{
uint32_t tmp, tmp1;
if (UartID != UART_4)
return;
tmp1 = UART_ICR_PULSEDIV(PulseDiv);
tmp = LPC_UART4->ICR & (~ UART_ICR_PULSEDIV(7));
tmp |= tmp1 | UART_ICR_FIXPULSE_EN;
LPC_UART4->ICR = tmp & UART_ICR_BITMASK;
}
/* UART1 FullModem function ---------------------------------------------*/
/*********************************************************************//**
* @brief Force pin DTR/RTS corresponding to given state (Full modem mode)
* @param[in] UARTx LPC_UART1 (only)
* @param[in] Pin Pin that NewState will be applied to, should be:
* - UART1_MODEM_PIN_DTR: DTR pin.
* - UART1_MODEM_PIN_RTS: RTS pin.
* @param[in] NewState New State of DTR/RTS pin, should be:
* - INACTIVE: Force the pin to inactive signal.
- ACTIVE: Force the pin to active signal.
* @return none
**********************************************************************/
void UART_FullModemForcePinState(UART_ID_Type UartID,
UART_MODEM_PIN_Type Pin,
UART1_SignalState NewState)
{
uint8_t tmp = 0;
if (UartID != UART_1)
return;
switch (Pin)
{
case UART1_MODEM_PIN_DTR:
tmp = UART1_MCR_DTR_CTRL;
break;
case UART1_MODEM_PIN_RTS:
tmp = UART1_MCR_RTS_CTRL;
break;
default:
break;
}
if (NewState == ACTIVE)
{
LPC_UART1->MCR |= tmp;
}
else
{
LPC_UART1->MCR &= (~tmp) & UART1_MCR_BITMASK;
}
}
/*********************************************************************//**
* @brief Configure Full Modem mode for UART peripheral
* @param[in] UARTx LPC_UART1 (only)
* @param[in] Mode Full Modem mode, should be:
* - UART1_MODEM_MODE_LOOPBACK: Loop back mode.
* - UART1_MODEM_MODE_AUTO_RTS: Auto-RTS mode.
* - UART1_MODEM_MODE_AUTO_CTS: Auto-CTS mode.
* @param[in] NewState New State of this mode, should be:
* - ENABLE: Enable this mode.
- DISABLE: Disable this mode.
* @return none
**********************************************************************/
void UART_FullModemConfigMode(UART_ID_Type UartID, UART_MODEM_MODE_Type Mode,
FunctionalState NewState)
{
uint8_t tmp;
if(UartID != UART_1)
return;
switch(Mode)
{
case UART1_MODEM_MODE_LOOPBACK:
tmp = UART1_MCR_LOOPB_EN;
break;
case UART1_MODEM_MODE_AUTO_RTS:
tmp = UART1_MCR_AUTO_RTS_EN;
break;
case UART1_MODEM_MODE_AUTO_CTS:
tmp = UART1_MCR_AUTO_CTS_EN;
break;
default:
break;
}
if (NewState == ENABLE)
{
LPC_UART1->MCR |= tmp;
}
else
{
LPC_UART1->MCR &= (~tmp) & UART1_MCR_BITMASK;
}
}
/*********************************************************************//**
* @brief Get current status of modem status register
* @param[in] UARTx LPC_UART1 (only)
* @return Current value of modem status register
* Note: The return value of this function must be ANDed with each member
* UART_MODEM_STAT_type enumeration to determine current flag status
* corresponding to each modem flag status. Because some flags in
* modem status register will be cleared after reading, the next reading
* modem register could not be correct. So this function used to
* read modem status register in one time only, then the return value
* used to check all flags.
**********************************************************************/
uint8_t UART_FullModemGetStatus(UART_ID_Type UartID)
{
if(UartID != UART_1)
return 0;
return ((LPC_UART1->MSR) & UART1_MSR_BITMASK);
}
/* UART RS485 functions --------------------------------------------------------------*/
/*********************************************************************//**
* @brief Configure UART peripheral in RS485 mode according to the specified
* parameters in the RS485ConfigStruct.
* @param[in] UARTx LPC_UART0 ~LPC_UART4
* @param[in] RS485ConfigStruct Pointer to a UART1_RS485_CTRLCFG_Type structure
* that contains the configuration information for specified UART
* in RS485 mode.
* @return None
**********************************************************************/
void UART_RS485Config(UART_ID_Type UartID, UART1_RS485_CTRLCFG_Type *RS485ConfigStruct)
{
uint32_t tmp;
__IO uint32_t *RS485DLY, *ADRMATCH, *RS485CTRL, *LCR;
tmp = 0;
if (UartID == UART_1)
{
RS485DLY = (__IO uint32_t *)&LPC_UART1->RS485DLY;
ADRMATCH = (__IO uint32_t *)&LPC_UART1->ADRMATCH;
LCR = (__IO uint32_t *)&LPC_UART1->LCR;
RS485CTRL = (__IO uint32_t *)&LPC_UART1->RS485CTRL;
}
else if (UartID == UART_4)
{
RS485DLY = (__IO uint32_t *)&LPC_UART4->RS485DLY;
ADRMATCH = (__IO uint32_t *)&LPC_UART4->ADRMATCH;
LCR = (__IO uint32_t *)&LPC_UART4->LCR;
RS485CTRL = (__IO uint32_t *)&LPC_UART4->RS485CTRL;
}
else
{
LPC_UART_TypeDef *UARTx = uart_get_pointer(UartID);
RS485DLY = (__IO uint32_t *)&UARTx->RS485DLY;
ADRMATCH = (__IO uint32_t *)&UARTx->ADRMATCH;
LCR = (__IO uint32_t *)&UARTx->LCR;
RS485CTRL = (__IO uint32_t *)&UARTx->RS485CTRL;
}
// If Auto Direction Control is enabled - This function is used in Master mode
if (RS485ConfigStruct->AutoDirCtrl_State == ENABLE)
{
tmp |= UART_RS485CTRL_DCTRL_EN;
// Set polar
if (RS485ConfigStruct->DirCtrlPol_Level == SET)
{
tmp |= UART_RS485CTRL_OINV_1;
}
// Set pin according to. This condition is only with UART1. The others are used
// OE pin as default for control the direction of RS485 buffer IC
if ((RS485ConfigStruct->DirCtrlPin == UART_RS485_DIRCTRL_DTR) &&
(UartID == UART_1))
{
tmp |= UART_RS485CTRL_SEL_DTR;
}
// Fill delay time
*RS485DLY = RS485ConfigStruct->DelayValue & UART_RS485DLY_BITMASK;
}
// MultiDrop mode is enable
if (RS485ConfigStruct->NormalMultiDropMode_State == ENABLE)
{
tmp |= UART_RS485CTRL_NMM_EN;
}
// Auto Address Detect function
if (RS485ConfigStruct->AutoAddrDetect_State == ENABLE)
{
tmp |= UART_RS485CTRL_AADEN;
// Fill Match Address
*ADRMATCH = RS485ConfigStruct->MatchAddrValue & UART_RS485ADRMATCH_BITMASK;
}
// Receiver is disable
if (RS485ConfigStruct->Rx_State == DISABLE)
{
tmp |= UART_RS485CTRL_RX_DIS;
}
// write back to RS485 control register
*RS485CTRL = tmp & UART_RS485CTRL_BITMASK;
// Enable Parity function and leave parity in stick '0' parity as default
*LCR |= (UART_LCR_PARITY_F_0 | UART_LCR_PARITY_EN);
}
/*********************************************************************//**
* @brief Enable/Disable receiver in RS485 module in UART1
* @param[in] UARTx LPC_UART1 (only)
* @param[in] NewState New State of command, should be:
* - ENABLE: Enable this function.
* - DISABLE: Disable this function.
* @return None
**********************************************************************/
void UART_RS485ReceiverCmd(UART_ID_Type UartID, FunctionalState NewState)
{
__IO uint32_t *RS485CTRL;
if (UartID == UART_1)
{
RS485CTRL = (__IO uint32_t *)&LPC_UART1->RS485DLY;
}
else if (UartID == UART_4)
{
RS485CTRL = (__IO uint32_t *)&LPC_UART4->RS485DLY;
}
else
{
LPC_UART_TypeDef *UARTx = uart_get_pointer(UartID);
RS485CTRL = (__IO uint32_t *)&UARTx->RS485DLY;
}
if (NewState == ENABLE)
{
*RS485CTRL &= ~UART_RS485CTRL_RX_DIS;
}
else
{
*RS485CTRL |= UART_RS485CTRL_RX_DIS;
}
}
/*********************************************************************//**
* @brief Send data on RS485 bus with specified parity stick value (9-bit mode).
* @param[in] UARTx LPC_UART1 (only)
* @param[in] pDatFrm Pointer to data frame.
* @param[in] size Size of data.
* @param[in] ParityStick Parity Stick value, should be 0 or 1.
* @return None
**********************************************************************/
uint32_t UART_RS485Send(UART_ID_Type UartID, uint8_t *pDatFrm,
uint32_t size, uint8_t ParityStick)
{
uint8_t tmp, save;
uint32_t cnt;
__IO uint32_t *LCR, *LSR;
if (UartID == UART_1)
{
LCR = (__IO uint32_t *)&LPC_UART1->LCR;
LSR = (__IO uint32_t *)&LPC_UART1->LSR;
}
else if (UartID == UART_4)
{
LCR = (__IO uint32_t *)&LPC_UART4->LCR;
LSR = (__IO uint32_t *)&LPC_UART4->LSR;
}
else
{
LPC_UART_TypeDef *UARTx = uart_get_pointer(UartID);
LCR = (__IO uint32_t *)&UARTx->LCR;
LSR = (__IO uint32_t *)&UARTx->LSR;
}
if (ParityStick)
{
save = tmp = *LCR & UART_LCR_BITMASK;
tmp &= ~(UART_LCR_PARITY_EVEN);
*LCR = tmp;
cnt = UART_Send(UartID, pDatFrm, size, BLOCKING);
while (!(*LSR & UART_LSR_TEMT));
*LCR = save;
}
else
{
cnt = UART_Send(UartID, pDatFrm, size, BLOCKING);
while (!(*LSR & UART_LSR_TEMT));
}
return cnt;
}
/*********************************************************************//**
* @brief Send Slave address frames on RS485 bus.
* @param[in] UARTx LPC_UART1 (only)
* @param[in] SlvAddr Slave Address.
* @return None
**********************************************************************/
void UART_RS485SendSlvAddr(UART_ID_Type UartID, uint8_t SlvAddr)
{
UART_RS485Send(UartID, &SlvAddr, 1, 1);
}
/*********************************************************************//**
* @brief Send Data frames on RS485 bus.
* @param[in] UARTx LPC_UART1 (only)
* @param[in] pData Pointer to data to be sent.
* @param[in] size Size of data frame to be sent.
* @return None
**********************************************************************/
uint32_t UART_RS485SendData(UART_ID_Type UartID, uint8_t *pData, uint32_t size)
{
return (UART_RS485Send(UartID, pData, size, 0));
}
/**
* @}
*/
#endif /*_UART*/
/**
* @}
*/
/* --------------------------------- End Of File ------------------------------ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -