Only write to EEPROM when a byte changes

master
Scott Lahteine 8 years ago
parent 8383f35b40
commit 53c9fca0f6

@ -135,16 +135,25 @@
uint16_t eeprom_checksum; uint16_t eeprom_checksum;
const char version[4] = EEPROM_VERSION; const char version[4] = EEPROM_VERSION;
bool eeprom_write_error;
void _EEPROM_writeData(int &pos, uint8_t* value, uint8_t size) { void _EEPROM_writeData(int &pos, uint8_t* value, uint8_t size) {
uint8_t c; if (eeprom_write_error) return;
while (size--) { while (size--) {
eeprom_write_byte((unsigned char*)pos, *value); uint8_t * const p = (uint8_t * const)pos;
c = eeprom_read_byte((unsigned char*)pos); const uint8_t v = *value;
if (c != *value) { // EEPROM has only ~100,000 write cycles,
// so only write bytes that have changed!
if (v != eeprom_read_byte(p)) {
eeprom_write_byte(p, v);
if (eeprom_read_byte(p) != v) {
SERIAL_ECHO_START; SERIAL_ECHO_START;
SERIAL_ECHOLNPGM(MSG_ERR_EEPROM_WRITE); SERIAL_ECHOLNPGM(MSG_ERR_EEPROM_WRITE);
eeprom_write_error = true;
return;
} }
eeprom_checksum += c; }
eeprom_checksum += v;
pos++; pos++;
value++; value++;
}; };
@ -203,6 +212,8 @@ void Config_Postprocess() {
EEPROM_START(); EEPROM_START();
eeprom_write_error = false;
EEPROM_WRITE(ver); // invalidate data first EEPROM_WRITE(ver); // invalidate data first
EEPROM_SKIP(eeprom_checksum); // Skip the checksum slot EEPROM_SKIP(eeprom_checksum); // Skip the checksum slot

Loading…
Cancel
Save