/** * @brief How to make a write followed by a read in the E²PROM. * @par Examples description * - Write 8 bytes on the devive (E²PROM) 0xA0. * - Read back the 8 bytes written on the devive (E²PROM) 0xA0. * - Check the coherency between the written and read Data. * @par Parameters: * None * @retval * None */ void main() { /* Initialize I/Os in Output Mode */ GPIO_Init(LEDS_PORT, (LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN), GPIO_MODE_OUT_PP_LOW_FAST); /* Optional: put here as example only */ I2C_DeInit(); /* Initialize the I2C */ I2C_EEInit(); /* All LEDs are ON per default */ GPIO_WriteLow(LEDS_PORT, (LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN)); /*** WRITE SEQUENCE ***/ I2C_EE_PageWrite(Buffer, Base_Add, Buffer_size); Delay(1000); /* To let eeprom the time to finish the write operation */ /*** READ SEQUENCE ***/ I2C_EE_BufferRead(Data, Base_Add, Buffer_size); /*** Check transmitted and received buffers ***/ for (i =Buffer_size; i > 0; i--) { if (Buffer[i-1] != Data[i-1]) { /* Toggle only LED1 if error is detected */ while (1) { GPIO_WriteReverse(LEDS_PORT, LED1_PIN); Delay((u16)60000); } } } /* Toggle all LEDs when I2C communication is terminated and buffers are ok */ while (1) { GPIO_WriteReverse(LEDS_PORT, (LED2_PIN | LED3_PIN | LED4_PIN)); Delay((u16)60000); } }
/** * @brief 将缓冲区中的数据写到I2C EEPROM中 * @param * @arg pBuffer:缓冲区指针 * @arg WriteAddr:写地址 * @arg NumByteToWrite:写的字节数 * @retval 无 */ void I2C_EE_BufferWrite(uint8_t* pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite) { uint8_t NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0; Addr = WriteAddr % I2C_PageSize; count = I2C_PageSize - Addr; NumOfPage = NumByteToWrite / I2C_PageSize; NumOfSingle = NumByteToWrite % I2C_PageSize; /* If WriteAddr is I2C_PageSize aligned */ if(Addr == 0) { /* If NumByteToWrite < I2C_PageSize */ if(NumOfPage == 0) { I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle); } /* If NumByteToWrite > I2C_PageSize */ else { while(NumOfPage--) { I2C_EE_PageWrite(pBuffer, WriteAddr, I2C_PageSize); WriteAddr += I2C_PageSize; pBuffer += I2C_PageSize; HAL_Delay(8); } if(NumOfSingle!=0) { I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle); } } } /* If WriteAddr is not I2C_PageSize aligned */ else { /* If NumByteToWrite < I2C_PageSize */ if(NumOfPage== 0) { I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle); } /* If NumByteToWrite > I2C_PageSize */ else { NumByteToWrite -= count; NumOfPage = NumByteToWrite / I2C_PageSize; NumOfSingle = NumByteToWrite % I2C_PageSize; if(count != 0) { I2C_EE_PageWrite(pBuffer, WriteAddr, count); WriteAddr += count; pBuffer += count; HAL_Delay(8); } while(NumOfPage--) { I2C_EE_PageWrite(pBuffer, WriteAddr, I2C_PageSize); WriteAddr += I2C_PageSize; pBuffer += I2C_PageSize; HAL_Delay(8); } if(NumOfSingle != 0) { I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle); } } } }
/******************************************************************************* * Function Name : I2C_EE_BufferWrite * Description : Writes buffer of data to the I2C EEPROM. * Input : - pBuffer : pointer to the buffer containing the data to be * written to the EEPROM. * - WriteAddr : EEPROM's internal address to write to. * - NumByteToWrite : number of bytes to write to the EEPROM. * Output : None * Return : None *******************************************************************************/ void I2C_EE_BufferWrite(u8* pBuffer, u8 WriteAddr, u16 NumByteToWrite) { u8 NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0; Addr = WriteAddr % I2C_PageSize; count = I2C_PageSize - Addr; NumOfPage = NumByteToWrite / I2C_PageSize; NumOfSingle = NumByteToWrite % I2C_PageSize; /* If WriteAddr is I2C_PageSize aligned */ if(Addr == 0) { /* If NumByteToWrite < I2C_PageSize */ if(NumOfPage == 0) { I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle); I2C_EE_WaitEepromStandbyState(); } /* If NumByteToWrite > I2C_PageSize */ else { while(NumOfPage--) { I2C_EE_PageWrite(pBuffer, WriteAddr, I2C_PageSize); I2C_EE_WaitEepromStandbyState(); WriteAddr += I2C_PageSize; pBuffer += I2C_PageSize; } if(NumOfSingle!=0) { I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle); I2C_EE_WaitEepromStandbyState(); } } } /* If WriteAddr is not I2C_PageSize aligned */ else { /* If NumByteToWrite < I2C_PageSize */ if(NumOfPage== 0) { I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle); I2C_EE_WaitEepromStandbyState(); } /* If NumByteToWrite > I2C_PageSize */ else { NumByteToWrite -= count; NumOfPage = NumByteToWrite / I2C_PageSize; NumOfSingle = NumByteToWrite % I2C_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_PageSize); I2C_EE_WaitEepromStandbyState(); WriteAddr += I2C_PageSize; pBuffer += I2C_PageSize; } if(NumOfSingle != 0) { I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle); I2C_EE_WaitEepromStandbyState(); } } } }
/** * @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 */ static 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(); } } } }