cancel
Showing results for 
Search instead for 
Did you mean: 

Couldn't write and read EEPROM on STM32L0538-Discovery

curiae
Associate II

Hello,

I  tried to EEPROM write/read test but it seems to not have written into EEPROM normally.

My defined functions corresponding to EEPROM are like following.

#define FLASH_USER_START_ADDR ((uint32_t)0x8008000)
#define FLASH_USER_END_ADDR ((uint32_t)0x800A000)

void eeprom_write_Word(uint32_t address, uint32_t value)
{
if(!IS_FLASH_DATA_ADDRESS(address))
{
return;
}
HAL_FLASHEx_DATAEEPROM_Unlock();
HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_WORD, address, value);
HAL_FLASHEx_DATAEEPROM_Lock();
}

void eeprom_read_Word(uint32_t address, uint32_t* value)
{
if(!IS_FLASH_DATA_ADDRESS(address))
{
return;
}
*value = *(__I uint32_t*)address;
}

And write/read lines in main are

eeprom_write_Word(FLASH_USER_START_ADDR, 1);
HAL_Delay(100);
eeprom_read_Word(FLASH_USER_START_ADDR, eeprom_buf);

As result buffer was empty.

How could I write and read normally?

Thanks

9 REPLIES 9
Andrew Neil
Evangelist III
Peter BENSCH
ST Employee

+1 for the hint of @Andrew Neil 

@curiae Did you try the suggestion of @dmeehan in this thread?

Regards
/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Hello,

I think you mean about Edit/code so I'll ask again.

I tried to EEPROM write/read test but it seemed to not have written into EEPROM normally.

My test code  are like following.

As result buffer was empty.

How could I write and read normally?

Thanks

#define FLASH_USER_START_ADDR            ((uint32_t)0x8008000)
#define FLASH_USER_END_ADDR              ((uint32_t)0x800A000)

void eeprom_write_Word(uint32_t address, uint32_t value)
{
    if(!IS_FLASH_DATA_ADDRESS(address))
    {
        return;
    }
    HAL_FLASHEx_DATAEEPROM_Unlock();
    HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_WORD, address, value);
    HAL_FLASHEx_DATAEEPROM_Lock();
}

void eeprom_read_Word(uint32_t address, uint32_t* value)
{
    if(!IS_FLASH_DATA_ADDRESS(address))
    {
        return;
    }
    *value = *(__I uint32_t*)address;
}

int main(void)
{
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_RTC_Init();
  eeprom_write_Word(FLASH_USER_START_ADDR, 1);
  HAL_Delay(100);
  eeprom_read_Word(FLASH_USER_START_ADDR, (uint32_t*)test_buf);
  uint32_t test_dat = test_buf[0];
  while(1)
  {}
}

 

 

You are not checking any of the return codes from any of the HAL functions - that would be the first thing to do.

Also, your code can return silently and do nothing:

void eeprom_read_Word(uint32_t address, uint32_t* value)
{
    if(!IS_FLASH_DATA_ADDRESS(address))
    {
        return;
    }
    *value = *(__I uint32_t*)address;
}

So if the IS_FLASH_DATA_ADDRESS test fails, that function gives you no indication at all of the error!

 

So what should I change my code to read out?

Or tell me the proper EEPROM read function.

Sco
ST Employee
curiae
Associate II

Hello,

I think it's a general explanation document about EEPROM.

Could you show me some sample sources for EEPROM read/write?

Thanks

 

Hello,

 

you need to check if the function HAL_FLASHEx_DATAEEPROM_Program executed successfully.

 

for example,

 

// Unlock the EEPROM memory for writing

HAL_FLASHEx_DATAEEPROM_Unlock(); // Write data to EEPROM

if (HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_WORD, address, writeData) == HAL_OK)

{

// Read data from EEPROM
readData = *(__IO uint32_t*)address;

// Check if the read data matches the written data

if (readData == writeData)

{

// Data matches

} else {

// Data does not match
}

}

// Lock the EEPROM memory to prevent further writing

HAL_FLASHEx_DATAEEPROM_Lock();

 

 

you need also pay attention if the eeprom is write protected or not