?? usb_to_i2c.c
字號:
/* Test on EV8 and clear it */
//while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
if(USB_I2C_CheckTimerEvent(ulTimeOut_Time, I2C_1, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
{
ErrorCount++;
}
/* Send STOP condition */
I2C_GenerateSTOP(I2C_1, ENABLE); //產(chǎn)生結(jié)束條件,一個字節(jié)操作結(jié)束
return ErrorCount;
}
/**
* @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
*/
u8 USB_I2C_BufferRead(unsigned char DevAddr, uint8_t* pBuffer, uint16_t ReadAddr, uint16_t NumByteToRead)
{
unsigned char ErrorCount = 0;
/* While the bus is busy */
while(I2C_GetFlagStatus(I2C_1, I2C_FLAG_BUSY));
/* Send START condition */
I2C_GenerateSTART(I2C_1, ENABLE); //I2C產(chǎn)生起始條件
/* Test on EV5 and clear it */
if(USB_I2C_CheckTimerEvent(ulTimeOut_Time, I2C_1, I2C_EVENT_MASTER_MODE_SELECT)) //設置主機模式
{
ErrorCount++;
}
/* Send EEPROM address for write */
I2C_Send7bitAddress(I2C_1, DevAddr, I2C_Direction_Transmitter); //發(fā)送要操作的器件地址
/* Test on EV6 and clear it */
if(USB_I2C_CheckTimerEvent(ulTimeOut_Time, I2C_1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) //等待結(jié)束
{
ErrorCount++;
}
/* Clear EV6 by setting again the PE bit */
I2C_Cmd(I2C_1, ENABLE);
#ifdef EE_M24C08
/* Send the EEPROM's internal address to read from: Only one byte address */
I2C_SendData(I2C_1, ReadAddr); //發(fā)送要寫入的地址
#elif defined (EE_M24C64_32)
/* Send the EEPROM's internal address to read from: MSB of the address first */
I2C_SendData(I2C_1, (uint8_t)((ReadAddr & 0xFF00) >> 8)); //發(fā)送要寫入的地址,因為24C32的地址為兩個字節(jié),所以先發(fā)送高8位的地址
/* Test on EV8 and clear it */
if(USB_I2C_CheckTimerEvent(ulTimeOut_Time, I2C_1, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
//if(USB_I2C_CheckTimerEvent(ulTimeOut_Time, I2C_1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) //等待結(jié)束
{
ErrorCount++;
}
/* Send the EEPROM's internal address to read from: LSB of the address */
I2C_SendData(I2C_1, (uint8_t)(ReadAddr & 0x00FF)); //因為24C32的地址為兩個字節(jié),后發(fā)送低8位的地址
#endif /* EE_M24C08 */
/* Test on EV8 and clear it */
//while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); //等待發(fā)送結(jié)束
if(USB_I2C_CheckTimerEvent(ulTimeOut_Time, I2C_1, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
{
ErrorCount++;
}
/* Send STRAT condition a second time */
I2C_GenerateSTART(I2C_1, ENABLE); //重新產(chǎn)生起始條件
/* Test on EV5 and clear it */
if(USB_I2C_CheckTimerEvent(ulTimeOut_Time, I2C_1, I2C_EVENT_MASTER_MODE_SELECT))
{
ErrorCount++;
}
/* Send EEPROM address for read */
I2C_Send7bitAddress(I2C_1, DevAddr, I2C_Direction_Receiver); //發(fā)送要操作的器件地址
/* Test on EV6 and clear it */
if(USB_I2C_CheckTimerEvent(ulTimeOut_Time,I2C_1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) //等待接收
{
ErrorCount++;
}
/* While there is data to be read */
while(NumByteToRead) //每讀到一個字節(jié)就減1,為0則讀取完成
{
if(NumByteToRead == 1)
{
/* Disable Acknowledgement */
I2C_AcknowledgeConfig(I2C_1, DISABLE); //設置應答位ACK為無效
/* Send STOP Condition */
I2C_GenerateSTOP(I2C_1, ENABLE); //產(chǎn)生停止條件
}
/* Test on EV7 and clear it */
if(I2C_CheckEvent(I2C_1, I2C_EVENT_MASTER_BYTE_RECEIVED))
{
/* Read a byte from the EEPROM */
*pBuffer = I2C_ReceiveData(I2C_1); //按長度讀取
/* Point to the next location where the byte read will be saved */
pBuffer++;
/* Decrement the read bytes counter */
NumByteToRead--; //每讀到一個字節(jié)就減1,為0則讀取完成
}
}
/* Enable Acknowledgement to be ready for another reception */
I2C_AcknowledgeConfig(I2C_1, ENABLE); //使能應答位ACK為有效
return ErrorCount;
}
void USB_I2C_ScanDev(unsigned char DevAddr)
{
__IO uint16_t SR1_Tmp = 0;
do
{
/* Send START condition */
I2C_GenerateSTART(I2C_1, ENABLE);
/* Read I2C_EE SR1 register to clear pending flags */
SR1_Tmp = I2C_ReadRegister(I2C_1, I2C_Register_SR1);
/* Send EEPROM address for write */
I2C_Send7bitAddress(I2C_1, DevAddr, I2C_Direction_Transmitter);
}while(!(I2C_ReadRegister(I2C_1, I2C_Register_SR1) & 0x0002));
/* Clear AF flag */
I2C_ClearFlag(I2C_1, I2C_FLAG_AF);
/* STOP condition */
I2C_GenerateSTOP(I2C_1, ENABLE);
}
/**
* @brief Wait for EEPROM Standby state
* @param None
* @retval None
*/
void USB_I2C_WaitEepromStandbyState(unsigned char DevAddr)
{
__IO uint16_t SR1_Tmp = 0;
do
{
/* Send START condition */
I2C_GenerateSTART(I2C_1, ENABLE);
/* Read I2C_EE SR1 register to clear pending flags */
SR1_Tmp = I2C_ReadRegister(I2C_1, I2C_Register_SR1);
/* Send EEPROM address for write */
I2C_Send7bitAddress(I2C_1, DevAddr, I2C_Direction_Transmitter);
}while(!(I2C_ReadRegister(I2C_1, I2C_Register_SR1) & 0x0002));
/* Clear AF flag */
I2C_ClearFlag(I2C_1, I2C_FLAG_AF);
/* STOP condition */
I2C_GenerateSTOP(I2C_1, ENABLE);
}
void i2c_24c_byte_write(unsigned char Byte, unsigned char WriteAddr, unsigned int ByteToWrite, unsigned char EE24cBlockSelect,I2C_TypeDef *I2Cx)
{
// Start the I2C
I2C_GenerateSTART(I2Cx,ENABLE); //打開I2C,開始發(fā)送過程
//not recommanded, stupid way
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT)); //設置主機模式
I2C_Send7bitAddress(I2Cx,EE24cBlockSelect,I2C_Direction_Transmitter); //發(fā)送片選,選擇哪一片區(qū)域?qū)憽2C地址區(qū)分
// when get ACK, means Set Success
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); //等待這次選擇過程完成
I2C_SendData(I2Cx, WriteAddr); //發(fā)送要寫入的地址碼
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); //等待字節(jié)發(fā)送完成
I2C_SendData(I2Cx, Byte); //發(fā)送要寫的字節(jié)
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); //等待直到字節(jié)發(fā)送完成
I2C_GenerateSTOP(I2Cx, ENABLE); //發(fā)送過程結(jié)束。
}
#if 0
void USB_I2C_ReadTest(void)
{
u8 temp;
/* Initialize the I2C EEPROM driver ----------------------------------------*/
//I2C_EE_Init();
/* First write in the memory followed by a read of the written data --------*/
/* Write on I2C EEPROM from EEPROM_WriteAddress1 */
I2C_EE_BufferWrite(Tx1_Buffer, EEPROM_WriteAddress1, BufferSize1);
/* Read from I2C EEPROM from EEPROM_ReadAddress1 */
temp = USB_I2C_BufferRead(0xa0, Rx1_Buffer, EEPROM_ReadAddress1, BufferSize1);
/* Check if the data written to the memory is read correctly */
//TransferStatus1 = Buffercmp(Tx1_Buffer, Rx1_Buffer, BufferSize1);
printf("\r\n");
if (temp == 0)
{
//printf("\r\nRead Buff1: OK!");
printf("\r\nRead Buff1: %s", Rx1_Buffer);
}
else
{
printf("\r\nRead Buff1: FAILED! Error: %d", temp);
printf("\r\nRead Buff1: %s", Rx1_Buffer);
}
/* Wait for EEPROM standby state */
//I2C_EE_WaitEepromStandbyState();
USB_I2C_WaitEepromStandbyState(0xa0);
/* Read from I2C EEPROM from EEPROM_ReadAddress1 */
temp = USB_I2C_BufferRead(0xa0, Rx2_Buffer, EEPROM_ReadAddress2, BufferSize2);
/* Check if the data written to the memory is read correctly */
//TransferStatus1 = Buffercmp(Tx1_Buffer, Rx1_Buffer, BufferSize1);
printf("\r\n");
if (temp == 0)
{
//printf("\r\nRead Buff1: OK!");
printf("\r\nRead Buff2: %s", Rx2_Buffer);
}
else
{
printf("\r\nRead Buff2: FAILED! Error: %d", temp);
printf("\r\nRead Buff2: %s", Rx2_Buffer);
}
}
void USB_I2C_TEST(void)
{
/* Initialize the I2C EEPROM driver ----------------------------------------*/
I2C_EE_Init();
/* First write in the memory followed by a read of the written data --------*/
/* Write on I2C EEPROM from EEPROM_WriteAddress1 */
I2C_EE_BufferWrite(Tx1_Buffer, EEPROM_WriteAddress1, BufferSize1);
/* Read from I2C EEPROM from EEPROM_ReadAddress1 */
I2C_EE_BufferRead(Rx1_Buffer, EEPROM_ReadAddress1, BufferSize1);
/* Check if the data written to the memory is read correctly */
TransferStatus1 = Buffercmp(Tx1_Buffer, Rx1_Buffer, BufferSize1);
printf("\r\n");
if (TransferStatus1 == PASSED)
{
//printf("\r\nRead Buff1: OK!");
printf("\r\nRead Buff1: %s", Rx1_Buffer);
}
else
{
printf("\r\nRead Buff1: FAILED!");
printf("\r\nRead Buff1: %s", Rx1_Buffer);
}
/* Wait for EEPROM standby state */
I2C_EE_WaitEepromStandbyState();
/* Second write in the memory followed by a read of the written data -------*/
/* Write on I2C EEPROM from EEPROM_WriteAddress2 */
I2C_EE_BufferWrite(Tx2_Buffer, EEPROM_WriteAddress2, BufferSize2);
/* Read from I2C EEPROM from EEPROM_ReadAddress2 */
I2C_EE_BufferRead(Rx2_Buffer, EEPROM_ReadAddress2, BufferSize2);
/* Check if the data written to the memory is read correctly */
TransferStatus2 = Buffercmp(Tx2_Buffer, Rx2_Buffer, BufferSize2);
if (TransferStatus2 == PASSED)
{
//printf("\r\nWrite/Read Buff2: OK!");
printf("\r\nRead Buff2: %s", Rx2_Buffer);
}
else{
printf("\r\nWrite/Read Buff2: FAILED!");
printf("\r\nRead Buff2: %s", Rx2_Buffer);
}
}
#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -