stm32f0xx_usart.c
来自「stm32f0固件库」· C语言 代码 · 共 1,641 行 · 第 1/5 页
C
1,641 行
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_USART_MUTEMODE_WAKEUP(USART_WakeUp));
USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_WAKE);
USARTx->CR1 |= USART_WakeUp;
}
/**
* @brief Configure the the USART Address detection length.
* @param USARTx: where x can be 1 or 2 to select the USART peripheral.
* @param USART_AddressLength: specifies the USART address length detection.
* This parameter can be one of the following values:
* @arg USART_AddressLength_4b: 4-bit address length detection
* @arg USART_AddressLength_7b: 7-bit address length detection
* @retval None
*/
void USART_AddressDetectionConfig(USART_TypeDef* USARTx, uint32_t USART_AddressLength)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_USART_ADDRESS_DETECTION(USART_AddressLength));
USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_ADDM7);
USARTx->CR2 |= USART_AddressLength;
}
/**
* @}
*/
/** @defgroup USART_Group6 LIN mode functions
* @brief LIN mode functions
*
@verbatim
===============================================================================
##### LIN mode functions #####
===============================================================================
[..] This subsection provides a set of functions allowing to manage the USART
LIN Mode communication.
[..] In LIN mode, 8-bit data format with 1 stop bit is required in accordance
with the LIN standard.
[..] Only this LIN Feature is supported by the USART IP:
(+) LIN Master Synchronous Break send capability and LIN slave break
detection capability : 13-bit break generation and 10/11 bit break
detection.
[..] USART LIN Master transmitter communication is possible through the
following procedure:
(#) Program the Baud rate, Word length = 8bits, Stop bits = 1bit, Parity,
Mode transmitter or Mode receiver and hardware flow control values
using the USART_Init() function.
(#) Enable the LIN mode using the USART_LINCmd() function.
(#) Enable the USART using the USART_Cmd() function.
(#) Send the break character using USART_SendBreak() function.
[..] USART LIN Master receiver communication is possible through the
following procedure:
(#) Program the Baud rate, Word length = 8bits, Stop bits = 1bit, Parity,
Mode transmitter or Mode receiver and hardware flow control values
using the USART_Init() function.
(#) Configures the break detection length
using the USART_LINBreakDetectLengthConfig() function.
(#) Enable the LIN mode using the USART_LINCmd() function.
-@- In LIN mode, the following bits must be kept cleared:
(+@) CLKEN in the USART_CR2 register.
(+@) STOP[1:0], SCEN, HDSEL and IREN in the USART_CR3 register.
(#) Enable the USART using the USART_Cmd() function.
@endverbatim
* @{
*/
/**
* @brief Sets the USART LIN Break detection length.
* @param USARTx: where x can be 1 to select the USART peripheral.
* @param USART_LINBreakDetectLength: specifies the LIN break detection length.
* This parameter can be one of the following values:
* @arg USART_LINBreakDetectLength_10b: 10-bit break detection
* @arg USART_LINBreakDetectLength_11b: 11-bit break detection
* @retval None
*/
void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint32_t USART_LINBreakDetectLength)
{
/* Check the parameters */
assert_param(IS_USART_1_PERIPH(USARTx));
assert_param(IS_USART_LIN_BREAK_DETECT_LENGTH(USART_LINBreakDetectLength));
USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_LBDL);
USARTx->CR2 |= USART_LINBreakDetectLength;
}
/**
* @brief Enables or disables the USART's LIN mode.
* @param USARTx: where x can be 1 to select the USART peripheral.
* @param NewState: new state of the USART LIN mode.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_USART_1_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 &= (uint32_t)~((uint32_t)USART_CR2_LINEN);
}
}
/**
* @}
*/
/** @defgroup USART_Group7 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 half duplex mode using USART_HalfDuplexCmd() function.
(#) Enable the USART using the USART_Cmd() 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: where x can be 1 or 2 to select the USART peripheral.
* @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 &= (uint32_t)~((uint32_t)USART_CR3_HDSEL);
}
}
/**
* @}
*/
/** @defgroup USART_Group8 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 Smartcard NACK using the USART_SmartCardNACKCmd() function.
(#) Enable the Smartcard interface using the USART_SmartCardCmd() function.
(#) Enable the USART using the USART_Cmd() 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: where x can be 1 to select the USART peripheral.
* @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_1_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: where x can be 1 to select the USART peripheral.
* @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_1_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 &= (uint32_t)~((uint32_t)USART_CR3_SCEN);
}
}
/**
* @brief Enables or disables NACK transmission.
* @param USARTx: where x can be 1 to select the USART peripheral.
* @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_1_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 &= (uint32_t)~((uint32_t)USART_CR3_NACK);
}
}
/**
* @brief Sets the Smart Card number of retries in transmit and receive.
* @param USARTx: where x can be 1 to select the USART peripheral.
* @param USART_AutoCount: specifies the Smart Card auto retry count.
* @retval None
*/
void USART_SetAutoRetryCount(USART_TypeDef* USARTx, uint8_t USART_AutoCount)
{
/* Check the parameters */
assert_param(IS_USART_1_PERIPH(USARTx));
assert_param(IS_USART_AUTO_RETRY_COUNTER(USART_AutoCount));
/* Clear the USART auto retry count */
USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_SCARCNT);
/* Set the USART auto retry count*/
USARTx->CR3 |= (uint32_t)((uint32_t)USART_AutoCount << 0x11);
}
/**
* @brief Sets the Smart Card Block length.
* @param USARTx: where x can be 1 to select the USART peripheral.
* @param USART_BlockLength: specifies the Smart Card block length.
* @retval None
*/
void USART_SetBlockLength(USART_TypeDef* USARTx, uint8_t USART_BlockLength)
{
/* Check the parameters */
assert_param(IS_USART_1_PERIPH(USARTx));
/* Clear the Smart card block length */
USARTx->RTOR &= (uint32_t)~((uint32_t)USART_RTOR_BLEN);
/* Set the Smart Card block length */
USARTx->RTOR |= (uint32_t)((uint32_t)USART_BlockLength << 0x18);
}
/**
* @}
*/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?