📄 stm32f10x_i2c.c
字号:
I2Cx->CR1 |= CR1_SWRST_Set;
}
else
{
/* Peripheral not under reset */
I2Cx->CR1 &= CR1_SWRST_Reset;
}
}
/*******************************************************************************
* Function Name : I2C_SMBusAlertConfig
* Description : Drives the SMBusAlert pin high or low for the specified I2C.
* Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
* - I2C_SMBusAlert: specifies SMBAlert pin level.
* This parameter can be one of the following values:
* - I2C_SMBusAlert_Low: SMBAlert pin driven low
* - I2C_SMBusAlert_High: SMBAlert pin driven high
* Output : None
* Return : None
*******************************************************************************/
void I2C_SMBusAlertConfig(I2C_TypeDef* I2Cx, u16 I2C_SMBusAlert)
{
/* Check the parameters */
assert_param(IS_I2C_ALL_PERIPH(I2Cx));
assert_param(IS_I2C_SMBUS_ALERT(I2C_SMBusAlert));
if (I2C_SMBusAlert == I2C_SMBusAlert_Low)
{
/* Drive the SMBusAlert pin Low */
I2Cx->CR1 |= I2C_SMBusAlert_Low;
}
else
{
/* Drive the SMBusAlert pin High */
I2Cx->CR1 &= I2C_SMBusAlert_High;
}
}
/*******************************************************************************
* Function Name : I2C_TransmitPEC
* Description : Enables or disables the specified I2C PEC transfer.
* Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
* - NewState: new state of the I2C PEC transmission.
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : None
*******************************************************************************/
void I2C_TransmitPEC(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 selected I2C PEC transmission */
I2Cx->CR1 |= CR1_PEC_Set;
}
else
{
/* Disable the selected I2C PEC transmission */
I2Cx->CR1 &= CR1_PEC_Reset;
}
}
/*******************************************************************************
* Function Name : I2C_PECPositionConfig
* Description : Selects the specified I2C PEC position.
* Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
* - I2C_PECPosition: specifies the PEC position.
* This parameter can be one of the following values:
* - I2C_PECPosition_Next: indicates that the next
* byte is PEC
* - I2C_PECPosition_Current: indicates that current
* byte is PEC
* Output : None
* Return : None
*******************************************************************************/
void I2C_PECPositionConfig(I2C_TypeDef* I2Cx, u16 I2C_PECPosition)
{
/* Check the parameters */
assert_param(IS_I2C_ALL_PERIPH(I2Cx));
assert_param(IS_I2C_PEC_POSITION(I2C_PECPosition));
if (I2C_PECPosition == I2C_PECPosition_Next)
{
/* Next byte in shift register is PEC */
I2Cx->CR1 |= I2C_PECPosition_Next;
}
else
{
/* Current byte in shift register is PEC */
I2Cx->CR1 &= I2C_PECPosition_Current;
}
}
/*******************************************************************************
* Function Name : I2C_CalculatePEC
* Description : Enables or disables the PEC value calculation of the
* transfered bytes.
* Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
* - NewState: new state of the I2Cx PEC value calculation.
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : None
*******************************************************************************/
void I2C_CalculatePEC(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 selected I2C PEC calculation */
I2Cx->CR1 |= CR1_ENPEC_Set;
}
else
{
/* Disable the selected I2C PEC calculation */
I2Cx->CR1 &= CR1_ENPEC_Reset;
}
}
/*******************************************************************************
* Function Name : I2C_GetPEC
* Description : Returns the PEC value for the specified I2C.
* Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
* Output : None
* Return : The PEC value.
*******************************************************************************/
u8 I2C_GetPEC(I2C_TypeDef* I2Cx)
{
/* Check the parameters */
assert_param(IS_I2C_ALL_PERIPH(I2Cx));
/* Return the selected I2C PEC value */
return ((I2Cx->SR2) >> 8);
}
/*******************************************************************************
* Function Name : I2C_ARPCmd
* Description : Enables or disables the specified I2C ARP.
* Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
* - NewState: new state of the I2Cx ARP.
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : None
*******************************************************************************/
void I2C_ARPCmd(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 selected I2C ARP */
I2Cx->CR1 |= CR1_ENARP_Set;
}
else
{
/* Disable the selected I2C ARP */
I2Cx->CR1 &= CR1_ENARP_Reset;
}
}
/*******************************************************************************
* Function Name : I2C_StretchClockCmd
* Description : Enables or disables the specified I2C Clock stretching.
* Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
* - NewState: new state of the I2Cx Clock stretching.
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : None
*******************************************************************************/
void I2C_StretchClockCmd(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 selected I2C Clock stretching */
I2Cx->CR1 |= CR1_NOSTRETCH_Set;
}
else
{
/* Disable the selected I2C Clock stretching */
I2Cx->CR1 &= CR1_NOSTRETCH_Reset;
}
}
/*******************************************************************************
* Function Name : I2C_FastModeDutyCycleConfig
* Description : Selects the specified I2C fast mode duty cycle.
* Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
* - I2C_DutyCycle: specifies the fast mode duty cycle.
* This parameter can be one of the following values:
* - I2C_DutyCycle_2: I2C fast mode Tlow/Thigh = 2
* - I2C_DutyCycle_16_9: I2C fast mode Tlow/Thigh = 16/9
* Output : None
* Return : None
*******************************************************************************/
void I2C_FastModeDutyCycleConfig(I2C_TypeDef* I2Cx, u16 I2C_DutyCycle)
{
/* Check the parameters */
assert_param(IS_I2C_ALL_PERIPH(I2Cx));
assert_param(IS_I2C_DUTY_CYCLE(I2C_DutyCycle));
if (I2C_DutyCycle != I2C_DutyCycle_16_9)
{
/* I2C fast mode Tlow/Thigh=2 */
I2Cx->CCR &= I2C_DutyCycle_2;
}
else
{
/* I2C fast mode Tlow/Thigh=16/9 */
I2Cx->CCR |= I2C_DutyCycle_16_9;
}
}
/*******************************************************************************
* Function Name : I2C_GetLastEvent
* Description : Returns the last I2Cx Event.
* Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
* Output : None
* Return : The last event
*******************************************************************************/
u32 I2C_GetLastEvent(I2C_TypeDef* I2Cx)
{
u32 lastevent = 0;
u32 flag1 = 0, flag2 = 0;
/* Check the parameters */
assert_param(IS_I2C_ALL_PERIPH(I2Cx));
/* Read the I2Cx status register */
flag1 = I2Cx->SR1;
flag2 = I2Cx->SR2;
flag2 = flag2 << 16;
/* Get the last event value from I2C status register */
lastevent = (flag1 | flag2) & FLAG_Mask;
/* Return status */
return lastevent;
}
/*******************************************************************************
* Function Name : I2C_CheckEvent
* Description : Checks whether the last I2Cx Event is equal to the one passed
* as parameter.
* Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
* - I2C_EVENT: specifies the event to be checked.
* This parameter can be one of the following values:
* - I2C_EVENT_SLAVE_ADDRESS_MATCHED : EV1
* - I2C_EVENT_SLAVE_BYTE_RECEIVED : EV2
* - I2C_EVENT_SLAVE_BYTE_TRANSMITTED : EV3
* - I2C_EVENT_SLAVE_ACK_FAILURE : EV3-2
* - I2C_EVENT_MASTER_MODE_SELECT : EV5
* - I2C_EVENT_MASTER_MODE_SELECTED : EV6
* - I2C_EVENT_MASTER_BYTE_RECEIVED : EV7
* - I2C_EVENT_MASTER_BYTE_TRANSMITTED : EV8
* - I2C_EVENT_MASTER_MODE_ADDRESS10 : EV9
* - I2C_EVENT_SLAVE_STOP_DETECTED : EV4
* Output : None
* Return : An ErrorStatus enumuration value:
* - SUCCESS: Last event is equal to the I2C_EVENT
* - ERROR: Last event is different from the I2C_EVENT
*******************************************************************************/
ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, u32 I2C_EVENT)
{
u32 lastevent = 0;
u32 flag1 = 0, flag2 = 0;
ErrorStatus status = ERROR;
/* Check the parameters */
assert_param(IS_I2C_ALL_PERIPH(I2Cx));
assert_param(IS_I2C_EVENT(I2C_EVENT));
/* Read the I2Cx status register */
flag1 = I2Cx->SR1;
flag2 = I2Cx->SR2;
flag2 = flag2 << 16;
/* Get the last event value from I2C status register */
lastevent = (flag1 | flag2) & FLAG_Mask;
/* Check whether the last event is equal to I2C_EVENT */
if (lastevent == I2C_EVENT )
{
/* SUCCESS: last event is equal to I2C_EVENT */
status = SUCCESS;
}
else
{
/* ERROR: last event is different from I2C_EVENT */
status = ERROR;
}
/* Return status */
return status;
}
/*******************************************************************************
* Function Name : I2C_GetFlagStatus
* Description : Checks whether the specified I2C flag is set or not.
* Input : - I2Cx: where x can be 1 or 2 to select the I2C peripheral.
* - I2C_FLAG: specifies the flag to check.
* This parameter can be one of the following values:
* - I2C_FLAG_DUALF: Dual flag (Slave mode)
* - I2C_FLAG_SMBHOST: SMBus host header (Slave mode)
* - I2C_FLAG_SMBDEFAULT: SMBus default header (Slave mode)
* - I2C_FLAG_GENCALL: General call header flag (Slave mode)
* - I2C_FLAG_TRA: Transmitter/Receiver flag
* - I2C_FLAG_BUSY: Bus busy flag
* - I2C_FLAG_MSL: Master/Slave flag
* - I2C_FLAG_SMBALERT: SMBus Alert flag
* - I2C_FLAG_TIMEOUT: Timeout or Tlow error flag
* - I2C_FLAG_PECERR: PEC error in reception flag
* - I2C_FLAG_OVR: Overrun/Underrun flag (Slave mode)
* - I2C_FLAG_AF: Acknowledge failure flag
* - I2C_FLAG_ARLO: Arbitration lost flag (Master mode)
* - I2C_FLAG_BERR: Bus error flag
* - I2C_FLAG_TXE: Data register empty flag (Transmitter)
* - I2C_FLAG_RXNE: Data register not empty (Receiver) flag
* - I2C_FLAG_STOPF: Stop detection flag (Slave mode)
* - I2C_FLAG_ADD10: 10-bit header sent flag (Master mode)
* - I2C_FLAG_BTF: Byte transfer finished flag
* - I2C_FLAG_ADDR: Address sent flag (Master mode) 揂DSL
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -