EEPROM write sometimes takes too much time

Post here all questions related to STM32 core if you can't find a relevant section!
Step73
Posts: 27
Joined: Tue Jan 14, 2020 2:55 pm

EEPROM write sometimes takes too much time

Post by Step73 »

I use this function to write some parameters into the EEPROM (actually, in Flash):

Code: Select all

#define BASE_ADDRESS 64

typedef struct
{
    float a;
    float b;
    float c;
} settings_t;
settings_t _settings[8];

void write_settings(int ch) 
{
    Serial.print("put ");
    Serial.println(millis());
    EEPROM.put(BASE_ADDRESS + ch * sizeof(settings_t), _settings[ch]);
    Serial.print("get ");
    Serial.println(millis());
    EEPROM.get(BASE_ADDRESS + ch * sizeof(settings_t), _settings[ch]);
    Serial.print("end ");
    Serial.println(millis());
}

Here some real outputs:

Code: Select all

put 63884
get 76547
end 76556

put 76765
get 76776
end 76785

put 87356
get 100018
end 100027
Even if sometimes it takes the correct time, other it hangs for about 13 s (quite precisely).
Did I make a mistake in my code?
Step73
Posts: 27
Joined: Tue Jan 14, 2020 2:55 pm

Re: EEPROM write sometimes takes too much time

Post by Step73 »

Trying to save another structure leads to a complete hang of the board:

Code: Select all

#define P_PARAM_NUM 8
typedef union 
{
	int8_t _int8;
	uint8_t _uint8;
	int16_t _int16;
	uint16_t _uint16;
	int32_t _int32;
	uint32_t _uint32;	
	float _float;
} param_u;
param_u params[P_PARAM_NUM];

void storeParameters()
{
    Serial.println(sizeof(params));
    Serial.println("put");
    EEPROM.put(0, params);
    Serial.println("end");
}
Outputs:

Code: Select all

32
put
And never prints `end`, even after few minutes. I need to reset the whole board.
I'm surely doing something wrong!
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: EEPROM write sometimes takes too much time

Post by fpiSTM »

No but EEPROM emulation depends of the flash page size.
Depending of the MCU it is not the same. By default, it uses the last page and sometimes can be huge (128k).

The fact sometimes could takes more time than other depends of flash states,... So do not wait it is the same each time you try.
One member of the community provide a way to buffer the eeprom and write only one time.
See
https://github.com/stm32duino/STM32Exam ... EEPROM.ino
Step73
Posts: 27
Joined: Tue Jan 14, 2020 2:55 pm

Re: EEPROM write sometimes takes too much time

Post by Step73 »

fpiSTM wrote: Fri Jan 24, 2020 10:16 am No but EEPROM emulation depends of the flash page size.
Depending of the MCU it is not the same. By default, it uses the last page and sometimes can be huge (128k).

The fact sometimes could takes more time than other depends of flash states,... So do not wait it is the same each time you try.
One member of the community provide a way to buffer the eeprom and write only one time.
See
https://github.com/stm32duino/STM32Exam ... EEPROM.ino
13 s for a page write is still too much even if the size is 128k, I guess.
Anyway, I may live with such a delay, but not with a complete hang!

Any thought about this?
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: EEPROM write sometimes takes too much time

Post by fpiSTM »

Step73 wrote: Fri Jan 24, 2020 12:05 pm Any thought about this?
No you provide only a part of your sketch, I could not help.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: EEPROM write sometimes takes too much time

Post by ag123 »

if you don't have too much data you may like to explore the backup registers. unfortunately they are a bit tricky at least as well and requires power to keep those memory, at least a coin cell at vbat. the other unfortunate thing is that there are normally very few backup registers available.
but i think they feel a little easier to use as it is pretty much *ram*, no erase and all, just write whatever value you need.

i used a backup register as my little 'columb counter' i.e. the accumulated power on time of the device. it helps me decides when to recharge the batteries and pretty accurately.
mcu8484
Posts: 13
Joined: Tue Oct 20, 2020 1:11 am

Re: EEPROM write sometimes takes too much time

Post by mcu8484 »

Hi,

I am testing EEPROM library (https://github.com/stm32duino/Arduino_C ... ies/EEPROM) on Nucleo H743ZI2. When I run the built-in example (https://github.com/stm32duino/Arduino_C ... _clear.ino) it is very slow (> 1 hour). Is this expected behavior? Any way to speed it up?

Thanks
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: EEPROM write sometimes takes too much time

Post by fpiSTM »

This is already answer above.
The clear.ino write each byte to 0 one by one. So it takes lot of time as the flash page is huge so thi example is really inneficient.
Try the buffered example.
mrburnette
Posts: 633
Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: EEPROM write sometimes takes too much time

Post by mrburnette »

IMO:
EEPROM simulation of microcontroller flash is one of those libraries that should never have never been created: that is, it is far too easy to abuse the proper limited-use scenario.

Rather, use external SPI/I2C storage:

Arduino resource page: https://www.arduino.cc/reference/en/lib ... a-storage/

Examples:
External I2C: https://www.hobbytronics.co.uk/arduino-external-eeprom

External SPI: https://www.arduino.cc/en/Tutorial/SPIEEPROM
mcu8484
Posts: 13
Joined: Tue Oct 20, 2020 1:11 am

Re: EEPROM write sometimes takes too much time

Post by mcu8484 »

As a newcomer to stm32duino looking to port some AVR code to stm32 I started by following the info on https://github.com/stm32duino/Arduino_C ... ies/EEPROM and naturally the examples in https://github.com/stm32duino/Arduino_C ... M/examples. I tried the eeprom_clear.ino example because I have AVR code that does the exact same thing. After seeing the extremely slow performance I decided to dig into the source code and to my shock and horror I found the library performs an entire erase/write cycle for every single byte written! This is not mentioned anywhere in the documentation or in this thread. The library is not just inefficient it's greatly harmful in severely reducing the flash lifespan as flash lifespan is only 10K cycles. By naively running the eeprom_clear.ino I have probably reduced the flash lifespan by 30-50% since the buffer is 16KB long. IMO, the library is unusable in its current form and the documentation (https://github.com/stm32duino/Arduino_C ... ies/EEPROM) should really have explicit warning at the top about the risk of greatly reducing flash lifespan.
Post Reply

Return to “General discussion”