📄 stm8l15x_usart.c
字号:
- 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
5. Optionally you can enable the parity error interrupt using the USART_ITConfig()
function
6. Enable the USART using the USART_Cmd() function.
7. Enable the Smartcard NACK using the USART_SmartCardNACKCmd() function.
8. Enable the Smartcard interface using the USART_SmartCardCmd() function.
Please refer to the ISO 7816-3 specification for more details.
Note:
-----
1. 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.
2. In smartcard mode, the following bits must be kept cleared:
- HDSEL and IREN bits in the USART_CR5 register.
@endverbatim
* @{
*/
/**
* @brief Enables or disables the USART Smart Card mode.
* @param USARTx: Select the USARTx 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)
{
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the SC mode by setting the SCEN bit in the CR5 register */
USARTx->CR5 |= USART_CR5_SCEN;
}
else
{
/* Disable the SC mode by clearing the SCEN bit in the CR5 register */
USARTx->CR5 &= ((uint8_t)(~USART_CR5_SCEN));
}
}
/**
* @brief Enables or disables NACK transmission.
* @param USARTx: where x can be 1 to select the specified USART peripheral.
* @param NewState: new state of the Smart Card mode.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState)
{
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the NACK transmission by setting the NACK bit in the CR5 register */
USARTx->CR5 |= USART_CR5_NACK;
}
else
{
/* Disable the NACK transmission by clearing the NACK bit in the CR5 register */
USARTx->CR5 &= ((uint8_t)~(USART_CR5_NACK));
}
}
/**
* @brief Sets the specified USART guard time.
* @note SmartCard Mode should be Enabled
* @param USARTx: where x can be 1 to select the specified USART peripheral.
* @param USART_GuardTime: specifies the guard time.
* @retval None
*/
void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime)
{
/* Set the USART guard time */
USARTx->GTR = USART_GuardTime;
}
/**
* @}
*/
/** @defgroup USART_Group6 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:
1. Program the Baud rate, Word length = 8 bits, Stop bits, Parity, Transmitter/Receiver
modes and hardware flow control values using the USART_Init() function.
2. Enable the USART using the USART_Cmd() function.
3. Configures the IrDA pulse width by configuring the prescaler using
the USART_SetPrescaler() function.
4. Configures the IrDA USART_IrDAMode_LowPower or USART_IrDAMode_Normal mode
using the USART_IrDAConfig() function.
5. Enable the IrDA using the USART_IrDACmd() function.
Note:
-----
1. A pulse of width less than two and greater than one PSC period(s) may or may
not be rejected.
2. 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).
3. In IrDA mode, the following bits must be kept cleared:
- STOP and CLKEN bits in the USART_CR3 register.
- SCEN and HDSEL bits in the USART_CR5 register.
@endverbatim
* @{
*/
/**
* @brief Configures the USART抯 IrDA interface.
* @param USARTx: where x can be 1 to select the specified USART peripheral.
* @param USART_IrDAMode specifies the IrDA mode.
* This parameter can be one of the following values:
* @arg USART_IrDAMode_Normal: IrDA Normal Mode
* @arg USART_IrDAMode_LowPower: IrDA Low Power Mode
* @retval None
*/
void USART_IrDAConfig(USART_TypeDef* USARTx, USART_IrDAMode_TypeDef USART_IrDAMode)
{
assert_param(IS_USART_IRDAMODE(USART_IrDAMode));
if (USART_IrDAMode != USART_IrDAMode_Normal)
{
USARTx->CR5 |= USART_CR5_IRLP;
}
else
{
USARTx->CR5 &= ((uint8_t)~USART_CR5_IRLP);
}
}
/**
* @brief Enables or disables the USART抯 IrDA interface.
* @param USARTx: where x can be 1 to select the specified USART peripheral.
* @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 parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the IrDA mode by setting the IREN bit in the CR3 register */
USARTx->CR5 |= USART_CR5_IREN;
}
else
{
/* Disable the IrDA mode by clearing the IREN bit in the CR3 register */
USARTx->CR5 &= ((uint8_t)~USART_CR5_IREN);
}
}
/**
* @}
*/
/** @defgroup USART_Group7 DMA transfers management functions
* @brief DMA transfers management functions
*
@verbatim
===============================================================================
DMA transfers management functions
===============================================================================
@endverbatim
* @{
*/
/**
* @brief Enables or disables the USART DMA interface.
* @param USARTx: where x can be 1 to select the specified USART peripheral.
* @param USART_DMAReq Specifies the USART DMA transfer request to be enabled or disabled.
* This parameter can be one of the following values:
* @arg USART_DMAReq_TX: Receive DMA request Enable
* @arg USART_DMAReq_RX: Transmit DMA request Enable
* @param NewState Indicates the new state of the USART DMA request.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void USART_DMACmd(USART_TypeDef* USARTx, USART_DMAReq_TypeDef USART_DMAReq,
FunctionalState NewState)
{
/* Check the parameters */
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 CR5 register */
USARTx->CR5 |= (uint8_t) USART_DMAReq;
}
else
{
/* Disable the DMA transfer for selected requests by clearing the DMAT and/or
DMAR bits in the USART CR5 register */
USARTx->CR5 &= (uint8_t)~USART_DMAReq;
}
}
/**
* @}
*/
/** @defgroup USART_Group8 Interrupts and flags management functions
* @brief Interrupts and flags management functions
*
@verbatim
===============================================================================
Interrupts and flags management functions
===============================================================================
This subsection provides a set of functions allowing to configure the USART
Interrupts sources, DMA channels requests and check or clear the flags or
pending bits status.
The user should identify which mode will be used in his application to manage
the communication: Polling mode, Interrupt mode or DMA mode.
Polling Mode
=============
In Polling Mode, the USART communication can be managed by 9 flags:
1. USART_FLAG_TXE: to indicate the status of the transmit buffer register
2. USART_FLAG_RXNE: to indicate the status of the receive buffer register
3. USART_FLAG_TC: to indicate the status of the transmit operation
4. USART_FLAG_IDLE: to indicate the status of the Idle Line
5. USART_FLAG_SBK: to indicate the status of the Send Break characters
6. USART_FLAG_NE: to indicate if a noise error occur
7. USART_FLAG_FE: to indicate if a frame error occur
8. USART_FLAG_PE: to indicate if a parity error occur
9. USART_FLAG_ORE: to indicate if an Overrun error occur
In this Mode it is advised to use the following functions:
- FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, USART_FLAG_TypeDef USART_FLAG);
- void USART_ClearFlag(USART_TypeDef* USARTx, USART_FLAG_TypeDef USART_FLAG);
Interrupt Mode
===============
In Interrupt Mode, the USART communication can be managed by 7 interrupt sources
and 6 pending bits:
Pending Bits:
-------------
1. USART_IT_TXE: to indicate the status of the transmit buffer register
2. USART_IT_RXNE: to indicate the status of the receive buffer register
3. USART_IT_TC: to indicate the status of the transmit operation
4. USART_IT_IDLE: to indicate the status of the Idle Line
5. USART_IT_PE: to indicate if a parity error occur
6. USART_IT_OR: to indicate if an Noise flag, overrun error and framing error in
multibuffer communication error occur
Interrupt Source:
-----------------
1. USART_IT_TXE: specifies the interrupt source for the Tx buffer empty
interrupt.
2. USART_IT_RXNE: specifies the interrupt source for the Rx buffer not
empty interrupt.
3. USART_IT_TC: specifies the interrupt source for the Transmit complete
interrupt.
4. USART_IT_IDLE: specifies the interrupt source for the Idle Line interrupt.
5. USART_IT_PE: specifies the interrupt source for the parity error interrupt.
6. USART_IT_ERR: specifies the interrupt source for the errors interrupt.
7. USART_IT_OR: specifies the interrupt source for the overrun error interrupt.
Note: Some parameters are coded in order to use them as interrupt source or
---- as pending bits.
In this Mode it is advised to use the following functions:
- void USART_ITConfig(USART_TypeDef* USARTx, USART_IT_TypeDef USART_IT, FunctionalState
NewState);
- ITStatus USART_GetITStatus(USART_TypeDef* USARTx, USART_IT_TypeDef USART_IT);
- void USART_ClearITPendingBit(USART_TypeDef* USARTx, USART_IT_TypeDef USART_IT);
DMA Mode
========
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -