Skip to main content
Explorer II
October 15, 2024
Question

Couldn't write and read EEPROM on STM32L0538-Discovery

  • October 15, 2024
  • 3 replies
  • 1806 views

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

    This topic has been closed for replies.

    3 replies

    Super User
    October 15, 2024
    Technical Moderator
    October 15, 2024

    +1 for the hint of @Andrew Neil 

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

    Regards
    /Peter

    curiaeAuthor
    Explorer II
    October 15, 2024

    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)
     {}
    }

     

     

    Super User
    October 15, 2024

    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!

     

    ST Employee
    October 16, 2024
    curiaeAuthor
    Explorer II
    October 16, 2024

    Hello,

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

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

    Thanks

     

    ST Employee
    November 1, 2024

    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();