EEPROM problem with Atmega2560

Hello!

I use Atmega2560, with built in EEPROM libraray, and have an EEPROM problem. My first question is, can the data stored in EEPROM be corrupted if the Atmega2560 stops reading it before finish due power shutdown?

This is a target device I have created, I won't link the source code now because it is huge. The idea is that it controls a process, but by pressing a button you enter the setup menu where you can change values that affect the operation of the machine. These settings are stored in EEPROM. I store integer, float and double values in EEPROM, and the addresses they are stored with the address offset by 5


int value1 = 1000
float value2 = 1.1111111
double value3 = 12.22

EEPROM.put(0, value1)
EEPROM.put(5, value2)
EEPROM.put(10, value3)

So the offset between the EEPROM addresses is 5. As I understand that in the case of atmeg2560 both float and double are 4 bytes, integers are 2 byte, so I can fit into the offset of 5.

Reproduction of the problem is difficult and relatively rare.
While changing the settings, the rest of the program line does not run, it only starts again after you exit the settings. On exit, the changed values are written to EEPROM using the EEPROM.put() function. This works 99% of the time. The machine remembers the changed values after the next power off, reads them out nicely and everything works perfectly.

However, it rarely happens that one of the read values that should be between 1000-5000 will be ~27.000, which should not be the case, because when I write the variables to EEPROM I check the maximum value and if it is above 5000 I set it to 5000 and only then write it to EEPROM.

But then how does the value of 27.000 get in?

As an added info, the device does not always power on the first time. It has a banana plug power connector, and sometimes the operator makes a mistake and turns on the power, the device turns on for a moment but the banana plug slips and the power goes out immediately, then it plugs it back in. So the Atmega2560 starts up for a very brief moment but stops almost immediately. And since the EEPROM reads are the very first step in the SETUP part of the program, I suspected at first that the power supply would be cut off during the read and possibly corrupt the data.

Translated with DeepL Translate: The world's most accurate translator (free version)

I'd use a checksum to verify data validity.
Perhaps store a second record to increase the chance for reading valid data. A sequence number will tell the newest data if both records are valid.

Corruption while reading is unlikely.

See if you can replicate the problem in a minimal example - or… we are pretty good at reading huge blocks of code.

so you mean that the damage is definitely caused by writing?

Is that a good idea?:

As soon as I change a value and save it, I write it to EEPROM. then when I exit the setup, all the variables are written to EEPROM at once to a different area. That way even if it makes a mistake when writing, there is little chance of it making the same mistake twice. then I compare the duplicated contiguous values. If they all match then no problem.

Alternatively, I can write them all out on exit and then read them back. If the read value and the value of the corresponding variable do not match then corrupt data has been saved, rewrite it to EEPROM. And we do this until all data is super.

Power loss or brownout while writing will result in corrupt data.

I think power loss or brownout is unlikely, because the control has not restarted, the program has not stopped running.

By the way 2,7V bronout detection enoguh for Mega2560? It's the default value

Not for 16 MHz.