EEPROM不能直接跨页写,要跨页的话,得重新指定一次新的页地址,同时得有一次停止位。
理解一下这段代码,其中NumOfPage、NumOfSingle两个变量,以及I2C_FLASH_PAGESIZE。
- /**
- * @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
- */
- void I2C_EE_BufferWrite(uint8_t* pBuffer, uint16_t WriteAddr, uint16_t NumByteToWrite)
- {
- uint8_t NumOfPage = 0, NumOfSingle = 0, count = 0;
- uint16_t Addr = 0;
- 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();
- }
- }
- }
- }
- /**
- * @brief Writes more than one byte to the EEPROM with a single WRITE cycle.
- * @note The number of byte can't exceed the EEPROM page size.
- * @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
- */
- void I2C_EE_PageWrite(uint8_t* pBuffer, uint16_t WriteAddr, uint8_t NumByteToWrite)
- {
- /* While the bus is busy */
- while(I2C_GetFlagStatus(I2C_EE, I2C_FLAG_BUSY));
- /* Send START 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));
- #ifdef EE_M24C08
- /* Send the EEPROM's internal address to write to : only one byte Address */
- I2C_SendData(I2C_EE, WriteAddr);
- #elif defined(EE_M24C64_32)
- /* Send the EEPROM's internal address to write to : MSB of the address first */
- I2C_SendData(I2C_EE, (uint8_t)((WriteAddr 0xFF00) >> 8));
- /* Test on EV8 and clear it */
- while(!I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
- /* Send the EEPROM's internal address to write to : LSB of the address */
- I2C_SendData(I2C_EE, (uint8_t)(WriteAddr 0x00FF));
- #endif /* EE_M24C08 */
- /* Test on EV8 and clear it */
- while(! I2C_CheckEvent(I2C_EE, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
- /* While there is data to be written */
- while(NumByteToWrite--)
- {
- /* Send the current byte */
- I2C_SendData(I2C_EE, *pBuffer);
- /* Point to the next byte to be written */
- pBuffer++;
- /* 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 Wait for EEPROM Standby state
- * @param None
- * @retval None
- */
- void I2C_EE_WaitEepromStandbyState(void)
- {
- __IO uint16_t SR1_Tmp = 0;
- do
- {
- /* Send START condition */
- I2C_GenerateSTART(I2C_EE, ENABLE);
- /* Read I2C_EE SR1 register to clear pending flags */
- SR1_Tmp = I2C_ReadRegister(I2C_EE, I2C_Register_SR1);
- /* Send EEPROM address for write */
- I2C_Send7bitAddress(I2C_EE, EEPROM_ADDRESS, I2C_Direction_Transmitter);
- }while(!(I2C_ReadRegister(I2C_EE, I2C_Register_SR1) 0x0002));
- /* Clear AF flag */
- I2C_ClearFlag(I2C_EE, I2C_FLAG_AF);
- /* STOP condition */
- I2C_GenerateSTOP(I2C_EE, ENABLE);
- }
|
|
2024-4-15 14:19:05
评论
举报
|
|
|