0

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;
    }

   }
RS System
  • 21
  • 2
  • 7

1 Answers1

0

You should step in the function HAL_I2C_Mem_Write to understand why it does not return HAL_OK. More particularly, you should check what it exactly returns, it would help you. Looking at your code, I am confident that the issue is with I2C address. In the AT24C256 datasheet, they say that the I2C address is:

1 0 1 0 0 A1 A2 R/W

Assuming that you connected the pins A1 and A2 to GND, the I2C address is:

1 0 1 0 0 0 0 R/W

In hex, the I2C address is 0xA0. So, change your adress definition as follows:

 #define ADDR_24LCxx 0xA0

And in the HAL functions:

HAL_I2C_Mem_Write(&hi2c1, ADDR_24LCxx, 0, I2C_MEMADD_SIZE_8BIT,WriteBuffer,BufferSize, 100)
HAL_I2C_Mem_Read(&hi2c1, ADDR_24LCxx, 0, I2C_MEMADD_SIZE_8BIT,ReadBuffer,BufferSize, 100)

Please note that I have also increased the timeout to 100ms. For testing, you don't really want to have timeout issues....

Guillaume Michel
  • 1,189
  • 8
  • 14