📄 usb_to_i2c.c
字号:
//printf("\r\nRead Buff1: %s", RxBuffer);
USB_I2C_ReqOut(RxBuffer, RxLen);
}else if (rxData->bCmd == I2C_CMD_WRITE){
USB_I2C_Revert(rxData);
}else{
printf("\r\n Command: Unknown I2c Command");
}
}
u8 USB_I2C_CheckTimerEvent(unsigned long Timer, I2C_TypeDef* I2Cx, unsigned long I2C_EVENT)
{
while((Timer--)&&(!I2C_CheckEvent(I2Cx, I2C_EVENT))); // 检查I2C的EV5状态并清除
if(Timer ==0)
{
return (1);
}
return (0);
}
u16 USB_I2C_Read(Usb_I2C_TRANSACTION* I2c_Data, u8* pBuffer)
{
u16 len;
u8 ErrorCount = 0;
len = I2c_Data->nBufferLength;
//#if I2cConfigDebug
printf("\r\nI2C Read Parameter:");
printf("\r\n Slave Address: 0x%x", I2c_Data->nSlaveDeviceAddress);
printf("\r\n MemAddr Length: %d bit", I2c_Data->nMemoryAddressLength);
printf("\r\n Memory Address: 0x%x", I2c_Data->nMemoryAddress);
printf("\r\n Buffer Length: %d Bytes", I2c_Data->nBufferLength);
///#endif
/* Send STRAT condition */
I2C_GenerateSTART(I2C_1, ENABLE); //I2C产生起始条件
/* Test on EV5 and clear it */
//while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_MODE_SELECT); //设置主机模式
if(USB_I2C_CheckTimerEvent(ulTimeOut_Time, I2C_1, I2C_EVENT_MASTER_MODE_SELECT))
{
ErrorCount++;
return 0;
}
/* Send EEPROM address for write */
I2C_Send7bitAddress(I2C_1, I2c_Data->nSlaveDeviceAddress, I2C_Direction_Transmitter); //发送要操作的器件地址
/* Test on EV6 and clear it */
//while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
if(USB_I2C_CheckTimerEvent(ulTimeOut_Time, I2C_1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
{
ErrorCount++;
return 0;
}
if(I2c_Data->nMemoryAddressLength == 8)
{
/* Send the EEPROM's internal address to write to : only one byte Address */
I2C_SendData(I2C_1, (u8)I2c_Data->nMemoryAddress); //发送被操作的地址,24C08的地址为一个8位
}
else
{
/* Send the EEPROM's internal address to write to : MSB of the address first */
I2C_SendData(I2C_1, (u8)((I2c_Data->nMemoryAddress & 0xFF00) >> 8)); //发送要写入的地址,因为24C32的地址为两个字节,所以先发送高8位的地址
/* 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++;
return 0;
}
/* Send the EEPROM's internal address to write to : LSB of the address */
I2C_SendData(I2C_1, (u8)(I2c_Data->nMemoryAddress & 0x00FF)); //因为24C32的地址为两个字节,后发送低8位的地址
}
/* 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++;
return 0;
}
/* 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++;
return 0;
}
/* Send EEPROM address for read */
I2C_Send7bitAddress(I2C_1, I2c_Data->nSlaveDeviceAddress, I2C_Direction_Receiver); //发送要操作的器件地址
/* Test on EV6 and clear it */
if(USB_I2C_CheckTimerEvent(ulTimeOut_Time,I2C_1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) //等待接收
{
ErrorCount++;
return 0;
}
/* While there is data to be read */
while(len) //每读到一个字节就减1,为0则读取完成
{
if(len == 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 */
len--; //每读到一个字节就减1,为0则读取完成
}
}
/* Enable Acknowledgement to be ready for another reception */
I2C_AcknowledgeConfig(I2C_1, ENABLE); //使能应答位ACK为有效
//printf("\r\n Buffer Length: %d Bytes", I2c_Data->nBufferLength);
printf("\r\n Read EEPROM: %d Bytes", I2c_Data->nBufferLength);
return I2c_Data->nBufferLength;
}
/**
* @brief Writes buffer of data 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.
* @param NumByteToWrite : number of bytes to write to the EEPROM.
* @retval None
*/
#if 0
void USB_I2C_Write(Usb_I2C_TRANSACTION* I2c_Data, uint8_t* pBuffer, uint16_t WriteAddr, uint16_t NumByteToWrite)
{
uint8_t NumOfPage = 0, NumOfSingle = 0, count = 0;
uint16_t Addr = 0;
#if I2cConfigDebug
printf("\r\nI2C Write Parameter:");
printf("\r\n Slave Address: 0x%x", I2c_Data->nSlaveDeviceAddress);
printf("\r\n MemAddr Length: %d bit", I2c_Data->nMemoryAddressLength);
printf("\r\n Memory Address: 0x%x", I2c_Data->nMemoryAddress);
printf("\r\n Buffer Length: %d Bytes", I2c_Data->nBufferLength);
#endif
Addr = WriteAddr % I2C_FLASH_PAGESIZE; //得到当前的起始地址所在页的地址
count = I2C_FLASH_PAGESIZE - Addr; //用当前页大小减去起始页地址,得到要在这个页里写入的字节数
NumOfPage = NumByteToWrite / I2C_FLASH_PAGESIZE; //需写入的整数页
NumOfSingle = NumByteToWrite % I2C_FLASH_PAGESIZE; //剩下的不到一页的字节数
/* If WriteAddr is I2C_FLASH_PAGESIZE aligned */
if(Addr == 0) //如果刚好起始地址为一页的开始
{
/* If NumByteToWrite < I2C_FLASH_PAGESIZE */
if(NumOfPage == 0)
{
I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
I2C_EE_WaitEepromStandbyState();
}
/* If NumByteToWrite > I2C_FLASH_PAGESIZE */
else
{
while(NumOfPage--)
{
I2C_EE_PageWrite(pBuffer, WriteAddr, I2C_FLASH_PAGESIZE);
I2C_EE_WaitEepromStandbyState();
WriteAddr += I2C_FLASH_PAGESIZE;
pBuffer += I2C_FLASH_PAGESIZE;
}
if(NumOfSingle!=0)
{
I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
I2C_EE_WaitEepromStandbyState();
}
}
}
/* If WriteAddr is not I2C_FLASH_PAGESIZE aligned */
else
{
/* If NumByteToWrite < I2C_FLASH_PAGESIZE */
if(NumOfPage== 0)
{
/* If the number of data to be written is more than the remaining space
in the current page: */
if (NumByteToWrite > count)
{
/* Write the data conained in same page */
I2C_EE_PageWrite(pBuffer, WriteAddr, count);
I2C_EE_WaitEepromStandbyState();
/* Write the remaining data in the following page */
I2C_EE_PageWrite((uint8_t*)(pBuffer + count), (WriteAddr + count), (NumByteToWrite - count));
I2C_EE_WaitEepromStandbyState();
}
else
{
I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
I2C_EE_WaitEepromStandbyState();
}
}
/* If NumByteToWrite > I2C_FLASH_PAGESIZE */
else
{
NumByteToWrite -= count;
NumOfPage = NumByteToWrite / I2C_FLASH_PAGESIZE;
NumOfSingle = NumByteToWrite % I2C_FLASH_PAGESIZE;
if(count != 0)
{
I2C_EE_PageWrite(pBuffer, WriteAddr, count);
I2C_EE_WaitEepromStandbyState();
WriteAddr += count;
pBuffer += count;
}
while(NumOfPage--)
{
I2C_EE_PageWrite(pBuffer, WriteAddr, I2C_FLASH_PAGESIZE);
I2C_EE_WaitEepromStandbyState();
WriteAddr += I2C_FLASH_PAGESIZE;
pBuffer += I2C_FLASH_PAGESIZE;
}
if(NumOfSingle != 0)
{
I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
I2C_EE_WaitEepromStandbyState();
}
}
}
}
#endif
/**
* @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.
* @retval None
*/
unsigned char USB_I2C_ByteWrite(unsigned char DevAddr, unsigned char * pBuffer, unsigned short WriteAddr)
{
unsigned char ErrorCount = 0;
/* Send STRAT condition */
I2C_GenerateSTART(I2C_1, ENABLE); //I2C产生起始条件
/* Test on EV5 and clear it */
//while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_MODE_SELECT); //设置主机模式
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 */
//while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
if(USB_I2C_CheckTimerEvent(ulTimeOut_Time, I2C_1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
{
ErrorCount++;
}
#ifdef EE_M24C08
/* Send the EEPROM's internal address to write to : only one byte Address */
I2C_SendData(I2C_1, WriteAddr); //发送被操作的地址,24C08的地址为一个8位
#elif defined(EE_M24C64_32)
/* Send the EEPROM's internal address to write to : MSB of the address first */
I2C_SendData(I2C_1, (uint8_t)((WriteAddr & 0xFF00) >> 8)); //发送要写入的地址,因为24C32的地址为两个字节,所以先发送高8位的地址
/* 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 the EEPROM's internal address to write to : LSB of the address */
I2C_SendData(I2C_1, (uint8_t)(WriteAddr & 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 the byte to be written */
I2C_SendData(I2C_1, *pBuffer); //发送被写数据
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -