}
/**
* @brief Writes one byte to the I2C EEPROM.
* @param pBuffer : pointer to the buffer containing the data to be
* written to the EEPROM.
* @param WriteAddr : EEPROM's internal address to write to.
zhgmdh2009
* @retval None
*/
void I2C_EE_ByteWrite(uint8_t i2c_data, uint8_t WriteAddr)
{
/* Send STRAT condition */
I2C_GenerateSTART(I2C_EE, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_MODE_SELECT));
/* Send EEPROM address for write */
I2C_Send7bitAddress(I2C_EE, EEPROM_ADDRESS, I2C_Direction_Transmitter);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C_EE, WriteAddr);
/* Test on EV8 and clear it */
while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/* Send the byte to be written */
I2C_SendData(I2C_EE, i2c_data);
/* Test on EV8 and clear it */
while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/* Send STOP condition */
I2C_GenerateSTOP(I2C_EE, ENABLE);
}
/**
* @brief Reads a block of data from the EEPROM.
* @param pBuffer : pointer to the buffer that receives the data read
* from the EEPROM.
* @param ReadAddr : EEPROM's internal address to read from.
* @param NumByteToRead : number of bytes to read from the EEPROM.
* @retval None
*/
uint8_t I2C_EE_BufferRead(uint8_t ReadAddr,uint8_t NumByteToRead)
{
uint8_t I2CData;
/* While the bus is busy */
while(I2C_GetFlagStatus(I2C_EE, I2C_FLAG_BUSY));
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_MODE_SELECT));
/* Send EEPROM address for write */
I2C_Send7bitAddress(I2C_EE, EEPROM_ADDRESS, I2C_Direction_Transmitter);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
/* Clear EV6 by setting again the PE bit */
I2C_Cmd(I2C_EE, ENABLE);
/* Send the EEPROM's internal address to read from: Only one byte address */
I2C_SendData(I2C_EE, ReadAddr);
/* Test on EV8 and clear it */
while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/* Send STRAT condition a second time */
I2C_GenerateSTART(I2C_EE, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_MODE_SELECT));
/* Send EEPROM address for read */
I2C_Send7bitAddress(I2C_EE, EEPROM_ADDRESS, I2C_Direction_Receiver);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
/* While there is data to be read */
while(NumByteToRead)
{
if(NumByteToRead == 1)
{
/* Disable Acknowledgement */
I2C_AcknowledgeConfig(I2C_EE, DISABLE);
/* Send STOP Condition */
I2C_GenerateSTOP(I2C_EE, ENABLE);
}
/* Test on EV7 and clear it */
if(I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_BYTE_RECEIVED))
{
/* Read a byte from the EEPROM */
I2CData = I2C_ReceiveData(I2C_EE);
/* Point to the next location where the byte read will be saved */
/* Decrement the read bytes counter */
NumByteToRead--;
}
}
/* Enable Acknowledgement to be ready for another reception */
I2C_AcknowledgeConfig(I2C_EE, ENABLE);
return I2CData;
}