📄 stm32f10x_i2c.c
字号:
/*******************************************************************************
* Function Name : I2C_DMALastTransferCmd
* Description : Specifies that the next DMA transfer is the last one.
* Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
* - NewState: new state of the I2C DMA last transfer.
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : None
*******************************************************************************/
void I2C_DMALastTransferCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_I2C_ALL_PERIPH(I2Cx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Next DMA transfer is the last transfer */
I2Cx->CR2 |= CR2_LAST_Set;
}
else
{
/* Next DMA transfer is not the last transfer */
I2Cx->CR2 &= CR2_LAST_Reset;
}
}
/*******************************************************************************
* Function Name : I2C_GenerateSTART
* Description : Generates I2Cx communication START condition.
* Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
* - NewState: new state of the I2C START condition generation.
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : None.
*******************************************************************************/
void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_I2C_ALL_PERIPH(I2Cx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Generate a START condition */
I2Cx->CR1 |= CR1_START_Set;
}
else
{
/* Disable the START condition generation */
I2Cx->CR1 &= CR1_START_Reset;
}
}
/*******************************************************************************
* Function Name : I2C_GenerateSTOP
* Description : Generates I2Cx communication STOP condition.
* Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
* - NewState: new state of the I2C STOP condition generation.
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : None.
*******************************************************************************/
void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_I2C_ALL_PERIPH(I2Cx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Generate a STOP condition */
I2Cx->CR1 |= CR1_STOP_Set;
}
else
{
/* Disable the STOP condition generation */
I2Cx->CR1 &= CR1_STOP_Reset;
}
}
/*******************************************************************************
* Function Name : I2C_AcknowledgeConfig
* Description : Enables or disables the specified I2C acknowledge feature.
* Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
* - NewState: new state of the I2C Acknowledgement.
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : None.
*******************************************************************************/
void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_I2C_ALL_PERIPH(I2Cx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the acknowledgement */
I2Cx->CR1 |= CR1_ACK_Set;
}
else
{
/* Disable the acknowledgement */
I2Cx->CR1 &= CR1_ACK_Reset;
}
}
/*******************************************************************************
* Function Name : I2C_OwnAddress2Config
* Description : Configures the specified I2C own address2.
* Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
* - Address: specifies the 7bit I2C own address2.
* Output : None
* Return : None.
*******************************************************************************/
void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, u8 Address)
{
u16 tmpreg = 0;
/* Check the parameters */
assert_param(IS_I2C_ALL_PERIPH(I2Cx));
/* Get the old register value */
tmpreg = I2Cx->OAR2;
/* Reset I2Cx Own address2 bit [7:1] */
tmpreg &= OAR2_ADD2_Reset;
/* Set I2Cx Own address2 */
tmpreg |= (u16)(Address & (u16)0x00FE);
/* Store the new register value */
I2Cx->OAR2 = tmpreg;
}
/*******************************************************************************
* Function Name : I2C_DualAddressCmd
* Description : Enables or disables the specified I2C dual addressing mode.
* Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
* - NewState: new state of the I2C dual addressing mode.
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : None
*******************************************************************************/
void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_I2C_ALL_PERIPH(I2Cx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable dual addressing mode */
I2Cx->OAR2 |= OAR2_ENDUAL_Set;
}
else
{
/* Disable dual addressing mode */
I2Cx->OAR2 &= OAR2_ENDUAL_Reset;
}
}
/*******************************************************************************
* Function Name : I2C_GeneralCallCmd
* Description : Enables or disables the specified I2C general call feature.
* Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
* - NewState: new state of the I2C General call.
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : None
*******************************************************************************/
void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_I2C_ALL_PERIPH(I2Cx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable generall call */
I2Cx->CR1 |= CR1_ENGC_Set;
}
else
{
/* Disable generall call */
I2Cx->CR1 &= CR1_ENGC_Reset;
}
}
/*******************************************************************************
* Function Name : I2C_ITConfig
* Description : Enables or disables the specified I2C interrupts.
* Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
* - I2C_IT: specifies the I2C interrupts sources to be enabled
* or disabled.
* This parameter can be any combination of the following values:
* - I2C_IT_BUF: Buffer interrupt mask
* - I2C_IT_EVT: Event interrupt mask
* - I2C_IT_ERR: Error interrupt mask
* - NewState: new state of the specified I2C interrupts.
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : None
*******************************************************************************/
void I2C_ITConfig(I2C_TypeDef* I2Cx, u16 I2C_IT, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_I2C_ALL_PERIPH(I2Cx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
assert_param(IS_I2C_CONFIG_IT(I2C_IT));
if (NewState != DISABLE)
{
/* Enable the selected I2C interrupts */
I2Cx->CR2 |= I2C_IT;
}
else
{
/* Disable the selected I2C interrupts */
I2Cx->CR2 &= (u16)~I2C_IT;
}
}
/*******************************************************************************
* Function Name : I2C_SendData
* Description : Sends a data byte through the I2Cx peripheral.
* Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
* - Data: Byte to be transmitted..
* Output : None
* Return : None
*******************************************************************************/
void I2C_SendData(I2C_TypeDef* I2Cx, u8 Data)
{
/* Check the parameters */
assert_param(IS_I2C_ALL_PERIPH(I2Cx));
/* Write in the DR register the data to be sent */
I2Cx->DR = Data;
}
/*******************************************************************************
* Function Name : I2C_ReceiveData
* Description : Returns the most recent received data by the I2Cx peripheral.
* Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
* Output : None
* Return : The value of the received data.
*******************************************************************************/
u8 I2C_ReceiveData(I2C_TypeDef* I2Cx)
{
/* Check the parameters */
assert_param(IS_I2C_ALL_PERIPH(I2Cx));
/* Return the data in the DR register */
return (u8)I2Cx->DR;
}
/*******************************************************************************
* Function Name : I2C_Send7bitAddress
* Description : Transmits the address byte to select the slave device.
* Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
* - Address: specifies the slave address which will be transmitted
* - I2C_Direction: specifies whether the I2C device will be a
* Transmitter or a Receiver.
* This parameter can be one of the following values
* - I2C_Direction_Transmitter: Transmitter mode
* - I2C_Direction_Receiver: Receiver mode
* Output : None
* Return : None.
*******************************************************************************/
void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, u8 Address, u8 I2C_Direction)
{
/* Check the parameters */
assert_param(IS_I2C_ALL_PERIPH(I2Cx));
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;
}
/*******************************************************************************
* Function Name : I2C_ReadRegister
* Description : Reads the specified I2C register and returns its value.
* Input1 : - I2C_Register: specifies the register to read.
* This parameter can be one of the following values:
* - I2C_Register_CR1: CR1 register.
* - I2C_Register_CR2: CR2 register.
* - I2C_Register_OAR1: OAR1 register.
* - I2C_Register_OAR2: OAR2 register.
* - I2C_Register_DR: DR register.
* - I2C_Register_SR1: SR1 register.
* - I2C_Register_SR2: SR2 register.
* - I2C_Register_CCR: CCR register.
* - I2C_Register_TRISE: TRISE register.
* Output : None
* Return : The value of the read register.
*******************************************************************************/
u16 I2C_ReadRegister(I2C_TypeDef* I2Cx, u8 I2C_Register)
{
/* Check the parameters */
assert_param(IS_I2C_ALL_PERIPH(I2Cx));
assert_param(IS_I2C_REGISTER(I2C_Register));
/* Return the selected register value */
return (*(vu16 *)(*((vu32 *)&I2Cx) + I2C_Register));
}
/*******************************************************************************
* Function Name : I2C_SoftwareResetCmd
* Description : Enables or disables the specified I2C software reset.
* Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
* - NewState: new state of the I2C software reset.
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : None
*******************************************************************************/
void I2C_SoftwareResetCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_I2C_ALL_PERIPH(I2Cx));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Peripheral under reset */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -