I am struggling to Write or read to AT24C256 I2C EEPROM. I am using STM32F0 discovery board to read/write to EEPROM.
I am using HAL library and CUBEMX for basic structure. I have written small code to test the read and write function. On debugging the the values of Test is always '2' whereas it should be '1' if it's successful in writing into memory. Here it is :-
#define ADDR_24LCxx_Write 0x50
#define ADDR_24LCxx_Read 0x50
#define BufferSize 5
uint8_t WriteBuffer[BufferSize],ReadBuffer[BufferSize],Test;
uint16_t i;
I2C_HandleTypeDef hi2c1;
int main(void)
{
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C1_Init();
for(i=0; i<5; i++)
{
WriteBuffer[i]=i;
}
if(HAL_I2C_Mem_Write(&hi2c1, ADDR_24LCxx_Write, 0, I2C_MEMADD_SIZE_8BIT,WriteBuffer,BufferSize, 0x10) == HAL_OK)
{
Test = 1;
}
else
{
Test = 2;
}
HAL_I2C_Mem_Read(&hi2c1, ADDR_24LCxx_Read, 0, I2C_MEMADD_SIZE_8BIT,ReadBuffer,BufferSize, 0x10);
if(memcmp(WriteBuffer,ReadBuffer,BufferSize) == 0 ) /* check date */
{
Test = 3;
}
else
{
Test = 4;
}
}