📄 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); //产生结束条件,一个字节操作结束
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产生起始条件
/* 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); //发送要操作的器件地址
/* Test on EV6 and clear it */
if(USB_I2C_CheckTimerEvent(ulTimeOut_Time, I2C_1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) //等待结束
{
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); //发送要写入的地址
#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)); //发送要写入的地址,因为24C32的地址为两个字节,所以先发送高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)) //等待结束
{
ErrorCount++;
}
/* Send the EEPROM's internal address to read from: LSB of the address */
I2C_SendData(I2C_1, (uint8_t)(ReadAddr & 0x00FF)); //因为24C32的地址为两个字节,后发送低8位的地址
#endif /* EE_M24C08 */
/* 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 STRAT condition a second time */
I2C_GenerateSTART(I2C_1, ENABLE); //重新产生起始条件
/* 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); //发送要操作的器件地址
/* 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) //每读到一个字节就减1,为0则读取完成
{
if(NumByteToRead == 1)
{
/* Disable Acknowledgement */
I2C_AcknowledgeConfig(I2C_1, DISABLE); //设置应答位ACK为无效
/* Send STOP Condition */
I2C_GenerateSTOP(I2C_1, ENABLE); //产生停止条件
}
/* 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--; //每读到一个字节就减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,开始发送过程
//not recommanded, stupid way
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT)); //设置主机模式
I2C_Send7bitAddress(I2Cx,EE24cBlockSelect,I2C_Direction_Transmitter); //发送片选,选择哪一片区域写。i2C地址区分
// when get ACK, means Set Success
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); //等待这次选择过程完成
I2C_SendData(I2Cx, WriteAddr); //发送要写入的地址码
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); //等待字节发送完成
I2C_SendData(I2Cx, Byte); //发送要写的字节
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); //等待直到字节发送完成
I2C_GenerateSTOP(I2Cx, ENABLE); //发送过程结束。
}
#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 + -