?? usb_to_i2c.c
字號(hào):
//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狀態(tài)并清除
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產(chǎn)生起始條件
/* Test on EV5 and clear it */
//while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_MODE_SELECT); //設(shè)置主機(jī)模式
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); //發(fā)送要操作的器件地址
/* 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); //發(fā)送被操作的地址,24C08的地址為一個(gè)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)); //發(fā)送要寫入的地址,因?yàn)?4C32的地址為兩個(gè)字節(jié),所以先發(fā)送高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)); //因?yàn)?4C32的地址為兩個(gè)字節(jié),后發(fā)送低8位的地址
}
/* 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++;
return 0;
}
/* 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++;
return 0;
}
/* Send EEPROM address for read */
I2C_Send7bitAddress(I2C_1, I2c_Data->nSlaveDeviceAddress, 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++;
return 0;
}
/* While there is data to be read */
while(len) //每讀到一個(gè)字節(jié)就減1,為0則讀取完成
{
if(len == 1)
{
/* Disable Acknowledgement */
I2C_AcknowledgeConfig(I2C_1, DISABLE); //設(shè)置應(yīng)答位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 */
len--; //每讀到一個(gè)字節(jié)就減1,為0則讀取完成
}
}
/* Enable Acknowledgement to be ready for another reception */
I2C_AcknowledgeConfig(I2C_1, ENABLE); //使能應(yīng)答位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; //得到當(dāng)前的起始地址所在頁的地址
count = I2C_FLASH_PAGESIZE - Addr; //用當(dāng)前頁大小減去起始頁地址,得到要在這個(gè)頁里寫入的字節(jié)數(shù)
NumOfPage = NumByteToWrite / I2C_FLASH_PAGESIZE; //需寫入的整數(shù)頁
NumOfSingle = NumByteToWrite % I2C_FLASH_PAGESIZE; //剩下的不到一頁的字節(jié)數(shù)
/* 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產(chǎn)生起始條件
/* Test on EV5 and clear it */
//while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_MODE_SELECT); //設(shè)置主機(jī)模式
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 */
//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); //發(fā)送被操作的地址,24C08的地址為一個(gè)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)); //發(fā)送要寫入的地址,因?yàn)?4C32的地址為兩個(gè)字節(jié),所以先發(fā)送高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)); //因?yàn)?4C32的地址為兩個(gè)字節(jié),后發(fā)送低8位的地址
#endif /* EE_M24C08 */
/* Test on EV8 and clear it */
//while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); //等待發(fā)送字節(jié)結(jié)束
if(USB_I2C_CheckTimerEvent(ulTimeOut_Time, I2C_1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) //等待發(fā)送字節(jié)結(jié)束
{
ErrorCount++;
}
/* Send the byte to be written */
I2C_SendData(I2C_1, *pBuffer); //發(fā)送被寫數(shù)據(jù)
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -