cancel
Showing results for 
Search instead for 
Did you mean: 

Reading EEPROM using I2C with HAL library

alexjafari2005
Associate II
Posted on July 04, 2016 at 09:30

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 athttps://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 #stm32f-i2c-eeprom
2 REPLIES 2
slimen
Senior
Posted on July 04, 2016 at 10:44

Hi,

You can find HAL I2C examples under ST web page within the STM32CubeXX firmware package.

For example, the

http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32cube-embedded-software/stm32cubel4.html

 firmware package provides a full set of running examples showing how to use the I2C EEPROM under this path:

STM32Cube_FW_L4_V1.5.0\Projects\STM32L476G_EVAL\Examples\I2C

Also, you can find I2C EEPROM example within the

http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32cube-embedded-software/stm32cubef4.html

firmware package:  STM32Cube_FW_F4_V1.12.0\Projects\STM32469I_EVAL\Examples\I2C\I2C_EEPROM

Regards

Walid FTITI_O
Senior II
Posted on July 04, 2016 at 10:55

Hi jafari.alex,

You find ''I2C_EEPROM'' example un the

http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32cube-embedded-software/stm32cubef7.html

at this path STM32Cube_FW_F7_V1.4.0\Projects\STM32756G_EVAL\Examples\I2C

Note that the Hal examples are listed in a application note called ''STM32Cube firmware examples'' as 

http://www.st.com/content/ccc/resource/technical/document/application_note/83/72/1e/52/2b/de/4c/7e/DM00210367.pdf/files/DM00210367.pdf/jcr:content/translations/en.DM00210367.pdf

.

-Hannibal-