stm8l15x_i2c.c
来自「STM8L的tim4定时器使用」· C语言 代码 · 共 1,141 行 · 第 1/3 页
C
1,141 行
}
/**
* @brief Enables or disables I2C software reset.
* @param I2Cx: where x can be 1 to select the specified I2C peripheral.
* @param NewState: Specifies the new state of the I2C software reset.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void I2C_SoftwareResetCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
{
/* Check function parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Peripheral under reset */
I2Cx->CR2 |= I2C_CR2_SWRST;
}
else /* NewState == DISABLE */
{
/* Peripheral not under reset */
I2Cx->CR2 &= (uint8_t)(~I2C_CR2_SWRST);
}
}
/**
* @brief Enables or disables the I2C clock stretching.
* @param I2Cx: where x can be 1 to select the specified I2C peripheral.
* @param NewState: Specifies the new state of the I2C Clock stretching.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void I2C_StretchClockCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
{
/* Check function parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Clock Stretching Enable */
I2Cx->CR1 &= (uint8_t)(~I2C_CR1_NOSTRETCH);
}
else /* NewState == DISABLE */
{
/* Clock Stretching Disable (Slave mode) */
I2Cx->CR1 |= I2C_CR1_NOSTRETCH;
}
}
/**
* @brief Enables or disables the I2C ARP.
* @param I2Cx: where x can be 1 to select the specified I2C peripheral.
* @param NewState: Specifies the new state of the I2C ARP
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void I2C_ARPCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
{
/* Check function parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* ARP Enable */
I2Cx->CR1 |= I2C_CR1_ARP;
}
else /* NewState == DISABLE */
{
/* ARP Disable */
I2Cx->CR1 &= (uint8_t)(~I2C_CR1_ARP);
}
}
/**
* @brief Enable or Disable the I2C acknowledge feature.
* @param I2Cx: where x can be 1 to select the specified I2C peripheral.
* @param NewState: Specifies the new state of the I2C acknowledge.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState)
{
/* Check function parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the acknowledgement */
I2Cx->CR2 |= I2C_CR2_ACK;
}
else
{
/* Disable the acknowledgement */
I2Cx->CR2 &= (uint8_t)(~I2C_CR2_ACK);
}
}
/**
* @brief Configures the specified I2C own address2.
* @param I2Cx: where x can be 1 to select the specified I2C peripheral.
* @param Address: specifies the 7bit I2C own address2.
* @retval None.
*/
void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint8_t Address)
{
uint8_t tmpreg = 0;
/* Get the old register value */
tmpreg = I2Cx->OAR2;
/* Reset I2Cx Own address2 bit [7:1] */
tmpreg &= (uint8_t)(~I2C_OAR2_ADD2);
/* Set I2Cx Own address2 */
tmpreg |= (uint8_t) ((uint8_t)Address & (uint8_t)0xFE);
/* Store the new register value */
I2Cx->OAR2 = tmpreg;
}
/**
* @brief Enables or disables the specified I2C dual addressing mode.
* @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
* @param NewState: new state of the I2C dual addressing mode.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable dual addressing mode */
I2Cx->OAR2 |= I2C_OAR2_ENDUAL;
}
else
{
/* Disable dual addressing mode */
I2Cx->OAR2 &= (uint8_t)(~I2C_OAR2_ENDUAL);
}
}
/**
* @brief Selects the specified I2C Ack position.
* @note This function must be called before data reception starts.
* @param I2Cx: where x can be 1 to select the specified I2C peripheral.
* @param I2C_AckPosition: specifies the Ack position.
* This parameter can be one of the following values:
* @arg I2C_AckPosition_Current: Acknowledge on the current byte
* @arg I2C_AckPosition_Next: Acknowledge on the next byte
* @retval None
*/
void I2C_AckPositionConfig(I2C_TypeDef* I2Cx, I2C_AckPosition_TypeDef I2C_AckPosition)
{
/* Check function parameters */
assert_param(IS_I2C_ACK_POSITION(I2C_AckPosition));
/* Clear the I2C Ack position */
I2Cx->CR2 &= (uint8_t)(~I2C_CR2_POS);
/* Configure the specified I2C Ack position*/
I2Cx->CR2 |= (uint8_t)I2C_AckPosition;
}
/**
* @brief Drives the SMBusAlert pin high or low.
* @param I2Cx: where x can be 1 to select the specified I2C peripheral.
* @param I2C_SMBusAlert: SMBusAlert pin state.
* This parameter can be one of the following values:
* @arg I2C_SMBusAlert_High: SMBAlert pin high
* @arg I2C_SMBusAlert_Low: SMBAlert pin Low
* @retval None
*/
void I2C_SMBusAlertConfig(I2C_TypeDef* I2Cx, I2C_SMBusAlert_TypeDef I2C_SMBusAlert)
{
/* Check functions parameters */
assert_param(IS_I2C_SMBUS_ALERT(I2C_SMBusAlert));
if (I2C_SMBusAlert != I2C_SMBusAlert_High)
{
/* SMBus Alert pin low */
I2Cx->CR2 |= (uint8_t)I2C_CR2_ALERT;
}
else /*I2C_SMBusAlert = I2C_SMBusAlert_High */
{
/* SMBus Alert pin high */
I2Cx->CR2 &= (uint8_t)(~I2C_CR2_ALERT);
}
}
/**
* @brief Selects I2C fast mode duty cycle.
* @param I2Cx: where x can be 1 to select the specified I2C peripheral.
* @param I2C_DutyCycle: specifies the fast mode duty cycle.
* This parameter can be one of the following values:
* @arg I2C_DutyCycle_2: I2C fast mode Tlow/Thigh = 2
* @arg I2C_DutyCycle_16_9: I2C fast mode Tlow/Thigh = 16/9
* @retval None
*/
void I2C_FastModeDutyCycleConfig(I2C_TypeDef* I2Cx, I2C_DutyCycle_TypeDef I2C_DutyCycle)
{
/* Check function parameters */
assert_param(IS_I2C_DUTY_CYCLE(I2C_DutyCycle));
if (I2C_DutyCycle == I2C_DutyCycle_16_9)
{
/* I2C fast mode Tlow/Thigh = 16/9 */
I2Cx->CCRH |= I2C_CCRH_DUTY;
}
else /* I2C_DUTYCYCLE_2 */
{
/* I2C fast mode Tlow/Thigh = 2 */
I2Cx->CCRH &= (uint8_t)(~I2C_CCRH_DUTY);
}
}
/**
* @brief Transmits the 7-bit address (to select the) slave device.
* @param I2Cx: where x can be 1 to select the specified I2C peripheral.
* @param Address: Specifies the slave address which will be transmitted.
* @param I2C_Direction: specifies whether the I2C device will be a Transmitter
* or a Receiver.
* This parameter can be one of the following values
* @arg I2C_Direction_Transmitter: Transmitter mode
* @arg I2C_Direction_Receiver: Receiver mode
* @retval None
*/
void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, uint8_t Address, I2C_Direction_TypeDef I2C_Direction)
{
/* Check function parameters */
assert_param(IS_I2C_ADDRESS(Address));
assert_param(IS_I2C_DIRECTION(I2C_Direction));
/* Test on the direction to set/reset the read/write bit */
if (I2C_Direction != I2C_Direction_Transmitter)
{
/* Set the address bit0 for read */
Address |= OAR1_ADD0_Set;
}
else
{
/* Reset the address bit0 for write */
Address &= OAR1_ADD0_Reset;
}
/* Send the address */
I2Cx->DR = Address;
}
/**
* @}
*/
/** @defgroup I2C_Group2 Data transfers functions
* @brief Data transfers functions
*
@verbatim
===============================================================================
Data transfers functions
===============================================================================
@endverbatim
* @{
*/
/**
* @brief Send a byte by writing in the DR register.
* @param I2Cx: where x can be 1 to select the specified I2C peripheral.
* @param Data: Byte to be sent.
* @retval None
*/
void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data)
{
/* Write in the DR register the data to be sent */
I2Cx->DR = Data;
}
/**
* @brief Returns the most recent received data.
* @param I2Cx: where x can be 1 to select the specified I2C peripheral.
* @param None
* @retval The value of the received byte data.
*/
uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx)
{
/* Return the data present in the DR register */
return ((uint8_t)I2Cx->DR);
}
/**
* @}
*/
/** @defgroup I2C_Group3 PEC management functions
* @brief PEC management functions
*
@verbatim
===============================================================================
PEC management functions
===============================================================================
@endverbatim
* @{
*/
/**
* @brief Enables or disables PEC transfer.
* @param I2Cx: where x can be 1 to select the specified I2C peripheral.
* @param NewState: indicates the PEC transfer state.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void I2C_TransmitPEC(I2C_TypeDef* I2Cx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the PEC transmission */
I2Cx->CR2 |= I2C_CR2_PEC;
}
else
{
/* Disable the PEC transmission */
I2Cx->CR2 &= (uint8_t)(~I2C_CR2_PEC);
}
}
/**
* @brief Enables or disables PEC calculation.
* @param I2Cx: where x can be 1 to select the specified I2C peripheral.
* @param NewState: indicates the PEC calculation state.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void I2C_CalculatePEC(I2C_TypeDef* I2Cx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable PEC calculation */
I2Cx->CR1 |= I2C_CR1_ENPEC;
}
else
{
/* Disable PEC calculation */
I2Cx->CR1 &= (uint8_t)(~I2C_CR1_ENPEC);
}
}
/**
* @brief Selects I2C PEC position..
* @param I2Cx: where x can be 1 to select the specified I2C peripheral.
* @param I2C_PECPosition:PEC position.
* This parameter can be one of the following values:
* @arg I2C_PECPosition_Current: Current byte in shift register is PEC
* @arg I2C_PECPosition_Next: Next byte in shift register is PEC
* @retval None
*/
void I2C_PECPositionConfig(I2C_TypeDef* I2Cx, I2C_PECPosition_TypeDef I2C_PECPosition)
{
/* Check the parameters */
assert_param(IS_I2C_PEC_POSITION(I2C_PECPosition));
/* Clear the I2C PEC position */
I2Cx->CR2 &= (uint8_t)(~I2C_CR2_POS);
/* Configure the specified I2C PEC position*/
I2Cx->CR2 |= (uint8_t)I2C_PECPosition;
}
/**
* @brief Returns PEC value.
* @param I2Cx: where x can be 1 to select the specified I2C peripheral.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?