2024-10-15 02:00 AM
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
2024-10-15 02:31 AM
Please see the Posting Tips for how to properly post source code:
2024-10-15 04:32 AM
+1 for the hint of @Andrew Neil
@curiae Did you try the suggestion of @dmeehan in this thread?
Regards
/Peter
2024-10-15 09:09 AM
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)
{}
}
2024-10-15 09:22 AM - edited 2024-10-15 09:22 AM
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!
2024-10-15 11:33 AM
So what should I change my code to read out?
Or tell me the proper EEPROM read function.
2024-10-15 08:01 PM
Hello
there is an AN for your reference:
How to best use STMicroelectronics serial EEPROMs - Application note
2024-10-15 11:53 PM
Hello,
I think it's a general explanation document about EEPROM.
Could you show me some sample sources for EEPROM read/write?
Thanks
2024-10-31 05:18 PM
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();
2024-10-31 05:25 PM
you need also pay attention if the eeprom is write protected or not