AnsweredAssumed Answered

Reading EEPROM using I2C with HAL library

Question asked by Jafari.Alex on Jul 4, 2016
Latest reply on Jul 4, 2016 by FTITI.Walid
Hi, 
I am trying to read the content of an EEPROM, 24AA02E48T, using HAL I2C library. 
There is an example for this task which has been done in SPL instead of HAL and can be found at https://github.com/Catethysis/stm32_i2c. Since HAL library does not have all the SPL functions, its not trivial to port the code. 
In SPL, the code that reads the content of EEPROM is: 
void I2C_burst_read(I2C_TypeDef* I2Cx, uint8_t HW_address, uint8_t addr, uint8_t n_data, uint8_t *data)
{
        while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));
    I2C_GenerateSTART(I2Cx, ENABLE);
        while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
    I2C_Send7bitAddress(I2Cx, HW_address, I2C_Direction_Transmitter);
        while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
    I2C_SendData(I2Cx, addr);
        while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
    I2C_GenerateSTOP(I2Cx, ENABLE);
    I2C_GenerateSTART(I2Cx, ENABLE);
        while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
    I2C_Send7bitAddress(I2Cx, HW_address, I2C_Direction_Receiver);
        while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
    I2C_AcknowledgeConfig(I2Cx, ENABLE);
    while(n_data--) {
        if(!n_data) I2C_AcknowledgeConfig(I2Cx, DISABLE);
            while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED));
        *data++ = I2C_ReceiveData(I2Cx);
    }
    I2C_AcknowledgeConfig(I2Cx, DISABLE);
    I2C_GenerateSTOP(I2Cx, ENABLE);
        while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));
}
Now, I am trying to read the EEPROM so I am trying to address it but HAL_I2C_Mem_Write() always returns HAL_ERROR:
    MX_I2C1_Init();
while((__HAL_I2C_GET_FLAG(&hi2c1, I2C_FLAG_BUSY) == SET));
while(HAL_I2C_Mem_Write(&hi2c1, (uint16_t)I2C_DEV_ADDR_WR, 0xFA, 2,(uint8_t *)aRxBuffer, 1, 2000)!= HAL_OK)
  {
    /* Error_Handler() function is called when Timout error occurs.
       When Acknowledge failure ocucurs (Slave don't acknowledge it's address)
       Master restarts communication */
    if (HAL_I2C_GetError(&hi2c1) != HAL_I2C_ERROR_AF)
        {
        LED_Toggle(LED1);
        HAL_Delay(100);
        }
  }
Is there any example that I can follow which uses HAL library to read from I2C EEPROM? How should I proceed? I really appreciate any help with this. 

Best regards

Outcomes