📄 i2c.c
字号:
/*******************************************************************************
* Function Name : I2C_AddressSend
* Description : Transmits the address byte to select the slave device.
* Input : I2Cx ( I2C0 or I2C1 )
* Address: an u16 parameter indicating the slave address
* Mode (I2C_Mode10,I2C_Mode7).
* Direction (I2C_RX,I2C_TX).
* Return : None.
********************************************************************************/
void I2C_AddressSend (I2C_TypeDef *I2Cx, u16 Address, I2C_Addressing Mode, I2C_Direction Direction)
{
if (Mode == I2C_Mode10 )
/*10 bit addressing mode */
{
/* Update the DR register by generated header */
I2Cx->DR = ((Address>>7)|0xa0)&0xfe;
/* Wait till I2C_ADD10 flag is set */
while ((I2Cx->SR1&0x40)==0);
/* clear I2C_ADD10 flag */
(void)I2Cx->SR2;
I2Cx->DR=(u8)Address;
/* Test on the direction to define the read/write bit */
if (Direction == I2C_RX)
{
/* Wait till I2C_ENDAD flag is set */
while ((I2Cx->SR2&0x20)==0);
I2Cx->CR|=0x20;
/* Repeated START Generate */
I2C_STARTGenerate (I2Cx, ENABLE);
/* Test on SB flag status */
while ((I2Cx->SR1&0x01)==0);
I2Cx->DR = ((Address>>7)|0xa1);
}
}
else
/* 7 bit addressing mode */
{
if (Direction == I2C_RX) Address|=0x01; else Address&=~0x01;
I2Cx->DR=(u8)Address;
}
}
/*******************************************************************************
* Function Name : I2C_ByteSend
* Description : Send a single byte of data.
* Input : I2Cx ( I2C0 or I2C1 )
* Data : the byte to be sent to the slave
* Return : None.
*******************************************************************************/
void I2C_ByteSend (I2C_TypeDef *I2Cx, u8 Data)
{
/* Write in the DR register the byte to be sent */
I2Cx->DR = Data;
}
/*******************************************************************************
* Function Name : I2C_TransmissionStatus
* Description : Report the NewState of the transmission
* Input : I2Cx ( I2C0 or I2C1 )
* Return : I2C_Tx_Status :transmission status (I2C_TX_NO, I2C_TX_SB,
* I2C_TX_AF, I2C_TX_ARLO, I2C_TX_BERR,I2C_TX_ADD_OK,
* I2C_TX_DATA_OK,I2C_TX_ONGOING)
*******************************************************************************/
I2C_Tx_Status I2C_TransmissionStatus (I2C_TypeDef *I2Cx)
{
u8 SR1value=0;
u8 SR2value=0;
I2C_Tx_Status NewState = I2C_TX_NO;
SR1value = I2Cx->SR1;
SR2value = I2Cx->SR2;
if ((I2Cx->SR1&0x10)==0)
NewState=I2C_TX_NO;
else if (I2Cx->SR1&0x01)
/* I2C_SB bit is set */
NewState=I2C_TX_SB;
else if ((SR2value & 0x10)&&(I2Cx->CR&0x04))
/* I2C_ACK &I2C_AF are both set */
NewState=I2C_TX_AF;
else if (SR2value & 0x04)
/* I2C_ARLO is set in multimaster mode */
NewState=I2C_TX_ARLO;
else if (SR2value & 0x02)
/* I2C_BERR bit is set */
NewState=I2C_TX_BERR;
else if ((SR1value & 0x80)&& (I2Cx->SR2&0x20))
/* I2C_EVF and I2C_ENDAD are both set */
NewState=I2C_TX_ADD_OK;
else if ((I2Cx->SR1&0x20)&& (I2Cx->SR1&0x08))
/* I2C_TRA and I2C_BTF are both set */
NewState=I2C_TX_DATA_OK;
else
NewState=I2C_TX_ONGOING;
return NewState;
}
/*******************************************************************************
* Function Name : I2C_ByteReceive
* Description : Returns the received byte.
* Input : I2Cx ( I2C0 or I2C1 )
* Return : the byte received
*******************************************************************************/
u8 I2C_ByteReceive (I2C_TypeDef *I2Cx)
{
/* Wait till I2C_BTF bit is set */
while ((I2Cx->SR1 & 0x08)==0);
return I2Cx->DR;
}
/*******************************************************************************
* Function Name :I2C_ReceptionStatus
* Description : Report the reception NewState.
* Input : I2Cx ( I2C0 or I2C1 )
* Return : I2C_Rx_Status:the NewState of the reception ( I2C_RX_NO,
* I2C_RX_SB,I2C_RX_AF,I2C_RX_ARLO,I2C_RX_BERR,I2C_RX_ADD_OK,
* I2C_RX_DATA_OK, I2C_RX_ONGOING)
*******************************************************************************/
I2C_Rx_Status I2C_ReceptionStatus (I2C_TypeDef *I2Cx)
{
u8 SR1value=0;
u8 SR2value=0;
I2C_Rx_Status NewState = I2C_RX_NO;
SR1value= I2Cx->SR1;
SR2value= I2Cx->SR2;
if ((I2Cx->SR1&0x10) == 0)
NewState=I2C_RX_NO;
else if (I2Cx->SR1&0x01)
/* I2C_SB bit is set */
NewState=I2C_RX_SB;
else if ((SR2value & 0x10) && (I2Cx->CR&0x04))
/* I2C_ACK &I2C_AF are both set */
NewState=I2C_RX_AF;
else if (SR2value & 0x04)
/* I2C_ARLO is set */
NewState=I2C_RX_ARLO;
else if (SR2value & 0x02)
/* I2C_BERR bit is set */
NewState=I2C_RX_BERR;
else if ((SR1value & 0x80) && (I2Cx->SR1&0x08)==0)
/* I2C_EVF is set & I2C_BTF is not set */
NewState=I2C_RX_ADD_OK;
else if ((I2Cx->SR1&0x20)==0 && (I2Cx->SR1&0x08))
/* I2C_TRA is cleared & I2C_BTF is set */
NewState=I2C_RX_DATA_OK;
else
NewState=I2C_RX_ONGOING;
return NewState;
}
/******************* (C) COPYRIGHT 2003 STMicroelectronics *****END OF FILE****/
/*******************************************************************************
* Function Name : I2C_GetLastEvent
* Description : Gets the last I2Cx event that has occurred.
* Input : - I2Cx: specifies the I2C to read its flags, it can be:
* I2C0, I2C1.
* Output : None.
* Return : The Last happened Event.
*******************************************************************************/
u16 I2C_GetLastEvent(I2C_TypeDef *I2Cx)
{
u16 Flag1 = 0, Flag2 = 0, LastEvent = 0;
Flag1 = I2Cx->SR1;
Flag2 = I2Cx->SR2;
Flag2 = Flag2<<8;
/* Get the last event value from I2C status register */
LastEvent = (((Flag1 | (Flag2)) & I2C_Event_Mask));
/* Return the last event */
return LastEvent;
}
/*******************************************************************************
* Function Name : I2C_CheckEvent
* Description : Checks whether the Last I2C Event is equal to the one passed
* as parameter.
* Input : - I2Cx: specifies the I2C to read its flags, it can be:
* I2C0, I2C1
- I2C_EVENT: specifies the event to be checked. This parameter
* can be one of the following values:
* - I2C_EVENT_SLAVE_ADDRESS_MATCHED
* - I2C_EVENT_SLAVE_BYTE_RECEIVED
* - I2C_EVENT_SLAVE_BYTE_TRANSMITTED
* - I2C_EVENT_SLAVE_ACK_FAILURE
* - I2C_EVENT_MASTER_MODE_SELECT
* - I2C_EVENT_MASTER_MODE_SELECTED
* - I2C_EVENT_MASTER_BYTE_RECEIVED
* - I2C_EVENT_MASTER_BYTE_TRANSMITTED
* - I2C_EVENT_MASTER_MODE_ADDRESS10
* - I2C_EVENT_SLAVE_STOP_DETECTED
* Output : None
* Return : An FlagStatus enumuration value:
* - SET: Last event is equal to the I2C_Event
* - RESET: Last event is different from the I2C_Event
*******************************************************************************/
FlagStatus I2C_CheckEvent(I2C_TypeDef *I2Cx, u16 I2C_EVENT)
{
u16 LastEvent = I2C_GetLastEvent(I2Cx);
/* Check whether the last event is equal to I2C_EVENT */
if (LastEvent == I2C_EVENT)
{
/* Return SUCCESS when last event is equal to I2C_EVENT */
return SET;
}
else
{
/* Return ERROR when last event is different from I2C_EVENT */
return RESET;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -