📄 stm32l1xx_usart.c
字号:
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the LIN mode by setting the LINEN bit in the CR2 register */
USARTx->CR2 |= USART_CR2_LINEN;
}
else
{
/* Disable the LIN mode by clearing the LINEN bit in the CR2 register */
USARTx->CR2 &= (uint16_t)~((uint16_t)USART_CR2_LINEN);
}
}
/**
* @brief Transmits break characters.
* @param USARTx: Select the USART peripheral.
* This parameter can be one of the following values:
* USART1, USART2, USART3, UART4 or UART5.
* @retval None.
*/
void USART_SendBreak(USART_TypeDef* USARTx)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
/* Send break characters */
USARTx->CR1 |= USART_CR1_SBK;
}
/**
* @}
*/
/** @defgroup USART_Group5 Halfduplex mode function
* @brief Half-duplex mode function
*
@verbatim
===============================================================================
##### Half-duplex mode function #####
===============================================================================
[..] This subsection provides a set of functions allowing to manage the USART
Half-duplex communication.
[..] The USART can be configured to follow a single-wire half-duplex protocol
where the TX and RX lines are internally connected.
[..] USART Half duplex communication is possible through the following procedure:
(#) Program the Baud rate, Word length, Stop bits, Parity, Mode transmitter
or Mode receiver and hardware flow control values using the USART_Init()
function.
(#) Configures the USART address using the USART_SetAddress() function.
(#) Enable the USART using the USART_Cmd() function.
(#) Enable the half duplex mode using USART_HalfDuplexCmd() function.
-@- The RX pin is no longer used.
-@- In Half-duplex mode the following bits must be kept cleared:
(+@) LINEN and CLKEN bits in the USART_CR2 register.
(+@) SCEN and IREN bits in the USART_CR3 register.
@endverbatim
* @{
*/
/**
* @brief Enables or disables the USART's Half Duplex communication.
* @param USARTx: Select the USART peripheral.
* This parameter can be one of the following values:
* USART1, USART2, USART3, UART4 or UART5.
* @param NewState: new state of the USART Communication.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
USARTx->CR3 |= USART_CR3_HDSEL;
}
else
{
/* Disable the Half-Duplex mode by clearing the HDSEL bit in the CR3 register */
USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_HDSEL);
}
}
/**
* @}
*/
/** @defgroup USART_Group6 Smartcard mode functions
* @brief Smartcard mode functions
*
@verbatim
===============================================================================
##### Smartcard mode functions #####
===============================================================================
[..] This subsection provides a set of functions allowing to manage the USART
Smartcard communication.
[..] The Smartcard interface is designed to support asynchronous protocol
Smartcards as defined in the ISO 7816-3 standard. The USART can provide
a clock to the smartcard through the SCLK output. In smartcard mode,
SCLK is not associated to the communication but is simply derived from
the internal peripheral input clock through a 5-bit prescaler.
[..] Smartcard communication is possible through the following procedure:
(#) Configures the Smartcard Prsecaler using the USART_SetPrescaler()
function.
(#) Configures the Smartcard Guard Time using the USART_SetGuardTime()
function.
(#) Program the USART clock using the USART_ClockInit() function as following:
(++) USART Clock enabled.
(++) USART CPOL Low.
(++) USART CPHA on first edge.
(++) USART Last Bit Clock Enabled.
(#) Program the Smartcard interface using the USART_Init() function as
following:
(++) Word Length = 9 Bits.
(++) 1.5 Stop Bit.
(++) Even parity.
(++) BaudRate = 12096 baud.
(++) Hardware flow control disabled (RTS and CTS signals).
(++) Tx and Rx enabled
(#) Optionally you can enable the parity error interrupt using
the USART_ITConfig() function.
(#) Enable the USART using the USART_Cmd() function.
(#) Enable the Smartcard NACK using the USART_SmartCardNACKCmd() function.
(#) Enable the Smartcard interface using the USART_SmartCardCmd() function.
[..]
Please refer to the ISO 7816-3 specification for more details.
[..]
(@) It is also possible to choose 0.5 stop bit for receiving but it is
recommended to use 1.5 stop bits for both transmitting and receiving
to avoid switching between the two configurations.
(@) In smartcard mode, the following bits must be kept cleared:
(+@) LINEN bit in the USART_CR2 register.
(+@) HDSEL and IREN bits in the USART_CR3 register.
@endverbatim
* @{
*/
/**
* @brief Sets the specified USART guard time.
* @param USARTx: Select the USART peripheral.
* This parameter can be one of the following values:
* USART1, USART2 or USART3.
* @param USART_GuardTime: specifies the guard time.
* @retval None.
*/
void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime)
{
/* Check the parameters */
assert_param(IS_USART_123_PERIPH(USARTx));
/* Clear the USART Guard time */
USARTx->GTPR &= USART_GTPR_PSC;
/* Set the USART guard time */
USARTx->GTPR |= (uint16_t)((uint16_t)USART_GuardTime << 0x08);
}
/**
* @brief Enables or disables the USART's Smart Card mode.
* @param USARTx: Select the USART peripheral.
* This parameter can be one of the following values:
* USART1, USART2 or USART3.
* @param NewState: new state of the Smart Card mode.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_USART_123_PERIPH(USARTx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the SC mode by setting the SCEN bit in the CR3 register */
USARTx->CR3 |= USART_CR3_SCEN;
}
else
{
/* Disable the SC mode by clearing the SCEN bit in the CR3 register */
USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_SCEN);
}
}
/**
* @brief Enables or disables NACK transmission.
* @param USARTx: Select the USART peripheral.
* This parameter can be one of the following values:
* USART1, USART2 or USART3.
* @param NewState: new state of the NACK transmission.
* This parameter can be: ENABLE or DISABLE.
* @retval None.
*/
void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_USART_123_PERIPH(USARTx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the NACK transmission by setting the NACK bit in the CR3 register */
USARTx->CR3 |= USART_CR3_NACK;
}
else
{
/* Disable the NACK transmission by clearing the NACK bit in the CR3 register */
USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_NACK);
}
}
/**
* @}
*/
/** @defgroup USART_Group7 IrDA mode functions
* @brief IrDA mode functions
*
@verbatim
===============================================================================
##### IrDA mode functions #####
===============================================================================
[..] This subsection provides a set of functions allowing to manage the USART
IrDA communication.
[..] IrDA is a half duplex communication protocol. If the Transmitter is busy,
any data on the IrDA receive line will be ignored by the IrDA decoder
and if the Receiver is busy, data on the TX from the USART to IrDA will
not be encoded by IrDA. While receiving data, transmission should be
avoided as the data to be transmitted could be corrupted.
[..] IrDA communication is possible through the following procedure:
(#) Program the Baud rate, Word length = 8 bits, Stop bits, Parity,
Transmitter/Receiver modes and hardware flow control values using
the USART_Init() function.
(#) Enable the USART using the USART_Cmd() function.
(#) Configures the IrDA pulse width by configuring the prescaler using
the USART_SetPrescaler() function.
(#) Configures the IrDA USART_IrDAMode_LowPower or USART_IrDAMode_Normal
mode using the USART_IrDAConfig() function.
(#) Enable the IrDA using the USART_IrDACmd() function.
[..]
(@) A pulse of width less than two and greater than one PSC period(s) may or
may not be rejected.
(@) The receiver set up time should be managed by software. The IrDA physical
layer specification specifies a minimum of 10 ms delay between
transmission and reception (IrDA is a half duplex protocol).
(@) In IrDA mode, the following bits must be kept cleared:
(+@) LINEN, STOP and CLKEN bits in the USART_CR2 register.
(+@) SCEN and HDSEL bits in the USART_CR3 register.
@endverbatim
* @{
*/
/**
* @brief Configures the USART's IrDA interface.
* @param USARTx: Select the USART peripheral.
* This parameter can be one of the following values:
* USART1, USART2, USART3, UART4 or UART5.
* @param USART_IrDAMode: specifies the IrDA mode.
* This parameter can be one of the following values:
* @arg USART_IrDAMode_LowPower: USART IrDA Low Power mode selected.
* @arg USART_IrDAMode_Normal: USART IrDA Normal mode selected.
* @retval None
*/
void USART_IrDAConfig(USART_TypeDef* USARTx, uint16_t USART_IrDAMode)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_USART_IRDA_MODE(USART_IrDAMode));
USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_IRLP);
USARTx->CR3 |= USART_IrDAMode;
}
/**
* @brief Enables or disables the USART's IrDA interface.
* @param USARTx: Select the USART peripheral.
* This parameter can be one of the following values:
* USART1, USART2, USART3, UART4 or UART5.
* @param NewState: new state of the IrDA mode.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the IrDA mode by setting the IREN bit in the CR3 register */
USARTx->CR3 |= USART_CR3_IREN;
}
else
{
/* Disable the IrDA mode by clearing the IREN bit in the CR3 register */
USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_IREN);
}
}
/**
* @}
*/
/** @defgroup USART_Group8 DMA transfers management functions
* @brief DMA transfers management functions
*
@verbatim
===============================================================================
##### DMA transfers management functions #####
===============================================================================
@endverbatim
* @{
*/
/**
* @brief Enables or disables the USART's DMA interface.
* @param USARTx: Select the USART peripheral.
* This parameter can be one of the following values:
* USART1, USART2, USART3, UART4 or UART5.
* @param USART_DMAReq: specifies the DMA request.
* This parameter can be any combination of the following values:
* @arg USART_DMAReq_Tx: USART DMA transmit request.
* @arg USART_DMAReq_Rx: USART DMA receive request.
* @param NewState: new state of the DMA Request sources.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_USART_DMAREQ(USART_DMAReq));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the DMA transfer for selected requests by setting the DMAT and/or
DMAR bits in the USART CR3 register */
USARTx->CR3 |= USART_DMAReq;
}
else
{
/* Disable the DMA transfer for selected requests by clearing the DMAT and/or
DMAR bits in the USART CR3 register */
USARTx->CR3 &= (uint16_t)~USART_DMAReq;
}
}
/**
* @}
*/
/** @defgroup USART_Group9 Interrupts and flags management functions
* @brief Interrupts and flags management functions
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -