📄 stm8l15x_usart.c
字号:
* @brief Sets the system clock prescaler.
* @note IrDA Low Power mode or smartcard mode should be enabled
* @note This function is related to SmartCard and IrDa mode.
* @param USARTx: Select the USARTx peripheral.
* @param USART_Prescaler: specifies the prescaler clock.
* @note In IrDA Low Power Mode the clock source is divided by the value given
* in the register (8 bits)
* - 0000 0000 Reserved
* - 0000 0001 divides the clock source by 1
* - 0000 0010 divides the clock source by 2
* - ...
* @note In Smart Card Mode the clock source is divided by the value given in the register
* (5 significant bits) multiplied by 2
* - 0 0000 Reserved
* - 0 0001 divides the clock source by 2
* - 0 0010 divides the clock source by 4
* - 0 0011 divides the clock source by 6
* - ...
* @retval None
*/
void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler)
{
/* Load the USART prescaler value*/
USARTx->PSCR = USART_Prescaler;
}
/**
* @brief Transmits break characters.
* @param USARTx: where x can be 1 to select the specified USART peripheral.
* @retval None
*/
void USART_SendBreak(USART_TypeDef* USARTx)
{
USARTx->CR2 |= USART_CR2_SBK;
}
/**
* @}
*/
/** @defgroup USART_Group2 Data transfers functions
* @brief Data transfers functions
*
@verbatim
===============================================================================
Data transfers functions
===============================================================================
This subsection provides a set of functions allowing to manage the USART data
transfers.
During an USART reception, data shifts in least significant bit first through
the RX pin. In this mode, the USART_DR register is similar to a buffer (RDR)
between the internal bus and the received shift register.
When a transmission is taking place, a write instruction to the USART_DR register
stores the data in the TDR register which is copied in the shift register
at the end of the current transmission.
The read access of the USART_DR register can be done using the USART_ReceiveData8()
or USART_ReceiveData9() functions and returns the RDR buffered value. Whereas a write
access to the USART_DR can be done using USART_SendData8() or USART_SendData9()
functions and stores the written data into TDR buffer.
@endverbatim
* @{
*/
/**
* @brief Returns the most recent received data by the USART peripheral.
* @param USARTx: where x can be 1 to select the specified USART peripheral.
* @retval The received data.
*/
uint8_t USART_ReceiveData8(USART_TypeDef* USARTx)
{
return USARTx->DR;
}
/**
* @brief Returns the most recent received data by the USART peripheral.
* @param USARTx: where x can be 1 to select the specified USART peripheral.
* @retval The received data.
*/
uint16_t USART_ReceiveData9(USART_TypeDef* USARTx)
{
uint16_t temp = 0;
temp = ((uint16_t)(((uint16_t)((uint16_t)USARTx->CR1 & (uint16_t)USART_CR1_R8)) << 1));
return (uint16_t)( ((uint16_t)((uint16_t)USARTx->DR) | temp) & ((uint16_t)0x01FF));
}
/**
* @brief Transmits 8 bit data through the USART peripheral.
* @param Data: The data to transmit.
* @retval None
*/
void USART_SendData8(USART_TypeDef* USARTx, uint8_t Data)
{
/* Transmit Data */
USARTx->DR = Data;
}
/**
* @brief Transmits 9 bit data through the USART peripheral.
* @param USARTx: Select the USARTx peripheral.
* @param Data: The data to transmit.
* This parameter should be lower than 0x1FF.
* @retval None
*/
void USART_SendData9(USART_TypeDef* USARTx, uint16_t Data)
{
assert_param(IS_USART_DATA_9BITS(Data));
/* Clear the transmit data bit 8 */
USARTx->CR1 &= ((uint8_t)~USART_CR1_T8);
/* Write the transmit data bit [8] */
USARTx->CR1 |= (uint8_t)(((uint8_t)(Data >> 2)) & USART_CR1_T8);
/* Write the transmit data bit [0:7] */
USARTx->DR = (uint8_t)(Data);
}
/**
* @}
*/
/** @defgroup USART_Group3 MultiProcessor Communication functions
* @brief Multi-Processor Communication functions
*
@verbatim
===============================================================================
Multi-Processor Communication functions
===============================================================================
This subsection provides a set of functions allowing to manage the USART
multiprocessor communication.
For instance one of the USARTs can be the master, its TX output is connected to
the RX input of the other USART. The others are slaves, their respective TX outputs
are logically ANDed together and connected to the RX input of the master.
USART multiprocessor communication is possible through the following procedure:
1. Program the Baud rate, Word length = 9 bits, Stop bits, Parity, Mode transmitter
or Mode receiver and hardware flow control values using the USART_Init()
function.
2. Configures the USART address using the USART_SetAddress() function.
3. Configures the wake up method (USART_WakeUp_IdleLine or USART_WakeUp_AddressMark)
using USART_WakeUpConfig() function only for the slaves.
4. Enable the USART using the USART_Cmd() function.
5. Enter the USART slaves in mute mode using USART_ReceiverWakeUpCmd() function.
The USART Slave exit from mute mode when receiving the wake up condition.
@endverbatim
* @{
*/
/**
* @brief Determines if the USART is in mute mode or not.
* @param USARTx: where x can be 1 to select the specified USART peripheral.
* @param NewState: The new state of the USART mode.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState)
{
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the mute mode USART by setting the RWU bit in the CR2 register */
USARTx->CR2 |= USART_CR2_RWU;
}
else
{
/* Disable the mute mode USART by clearing the RWU bit in the CR1 register */
USARTx->CR2 &= ((uint8_t)~USART_CR2_RWU);
}
}
/**
* @brief Sets the address of the USART node.
* @param USARTx: Select the USARTx peripheral.
* @param Address: Indicates the address of the USART node.
* This parameter should be lower than 16
* @retval None
*/
void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address)
{
/* assert_param for USART_Address */
assert_param(IS_USART_ADDRESS(USART_Address));
/* Clear the USART address */
USARTx->CR4 &= ((uint8_t)~USART_CR4_ADD);
/* Set the USART address node */
USARTx->CR4 |= USART_Address;
}
/**
* @brief Selects the USART WakeUp method.
* @param USART_WakeUp: Specifies the USART wakeup method.
* This parameter can be one of the following values:
* @arg USART_WakeUp_IdleLine: 0x01 Idle Line wake up
* @arg USART_WakeUp_AddressMark: 0x02 Address Mark wake up
* @retval None
*/
void USART_WakeUpConfig(USART_TypeDef* USARTx, USART_WakeUp_TypeDef USART_WakeUp)
{
assert_param(IS_USART_WAKEUP(USART_WakeUp));
USARTx->CR1 &= ((uint8_t)~USART_CR1_WAKE);
USARTx->CR1 |= (uint8_t)USART_WakeUp;
}
/**
* @}
*/
/** @defgroup USART_Group4 Halfduplex mode function
* @brief Half-duplex mode function
*
@verbatim
===============================================================================
Half-duplex mode function
===============================================================================
This subsection provides a function 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:
1. Program the Baud rate, Word length, Stop bits, Parity, Mode transmitter
or Mode receiver and hardware flow control values using the USART_Init()
function.
2. Configures the USART address using the USART_SetAddress() function.
3. Enable the USART using the USART_Cmd() function.
4. Enable the half duplex mode using USART_HalfDuplexCmd() function.
Note:
----
1. The RX pin is no longer used
2. In Half-duplex mode the following bits must be kept cleared:
- CLKEN bits in the USART_CR3 register.
- SCEN and IREN bits in the USART_CR5 register.
@endverbatim
* @{
*/
/**
* @brief Enables or disables the USART抯 Half Duplex communication.
* @param USARTx: where x can be 1 to select the specified 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)
{
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
USARTx->CR5 |= USART_CR5_HDSEL; /**< USART Half Duplex Enable */
}
else
{
USARTx->CR5 &= (uint8_t)~USART_CR5_HDSEL; /**< USART Half Duplex Disable */
}
}
/**
* @}
*/
/** @defgroup USART_Group5 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:
1. Configures the Smartcard Prsecaler using the USART_SetPrescaler() function.
2. Configures the Smartcard Guard Time using the USART_SetGuardTime() function.
3. 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
4. Program the Smartcard interface using the USART_Init() function as following:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -