Better EEPROM read/write error handling

2.0.x
Scott Lahteine 8 years ago
parent 55722c09da
commit 492b68f8e3

@ -188,10 +188,11 @@ void Config_Postprocess() {
value++; value++;
}; };
} }
bool eeprom_read_error;
void _EEPROM_readData(int &pos, uint8_t* value, uint8_t size) { void _EEPROM_readData(int &pos, uint8_t* value, uint8_t size) {
do { do {
uint8_t c = eeprom_read_byte((unsigned char*)pos); uint8_t c = eeprom_read_byte((unsigned char*)pos);
*value = c; if (!eeprom_read_error) *value = c;
eeprom_checksum += c; eeprom_checksum += c;
pos++; pos++;
value++; value++;
@ -203,6 +204,7 @@ void Config_Postprocess() {
#define EEPROM_SKIP(VAR) eeprom_index += sizeof(VAR) #define EEPROM_SKIP(VAR) eeprom_index += sizeof(VAR)
#define EEPROM_WRITE(VAR) _EEPROM_writeData(eeprom_index, (uint8_t*)&VAR, sizeof(VAR)) #define EEPROM_WRITE(VAR) _EEPROM_writeData(eeprom_index, (uint8_t*)&VAR, sizeof(VAR))
#define EEPROM_READ(VAR) _EEPROM_readData(eeprom_index, (uint8_t*)&VAR, sizeof(VAR)) #define EEPROM_READ(VAR) _EEPROM_readData(eeprom_index, (uint8_t*)&VAR, sizeof(VAR))
#define EEPROM_ASSERT(TST,ERR) if () do{ SERIAL_ERROR_START; SERIAL_ERRORLNPGM(ERR); eeprom_read_error |= true; }while(0)
/** /**
* M500 - Store Configuration * M500 - Store Configuration
@ -371,9 +373,12 @@ void Config_Postprocess() {
EEPROM_WRITE(dummy); EEPROM_WRITE(dummy);
} }
if (!eeprom_write_error) {
uint16_t final_checksum = eeprom_checksum, uint16_t final_checksum = eeprom_checksum,
eeprom_size = eeprom_index; eeprom_size = eeprom_index;
// Write the EEPROM header
eeprom_index = EEPROM_OFFSET; eeprom_index = EEPROM_OFFSET;
EEPROM_WRITE(version); EEPROM_WRITE(version);
EEPROM_WRITE(final_checksum); EEPROM_WRITE(final_checksum);
@ -383,6 +388,7 @@ void Config_Postprocess() {
SERIAL_ECHOPAIR("Settings Stored (", eeprom_size); SERIAL_ECHOPAIR("Settings Stored (", eeprom_size);
SERIAL_ECHOLNPGM(" bytes)"); SERIAL_ECHOLNPGM(" bytes)");
} }
}
/** /**
* M501 - Retrieve Configuration * M501 - Retrieve Configuration
@ -390,6 +396,7 @@ void Config_Postprocess() {
void Config_RetrieveSettings() { void Config_RetrieveSettings() {
EEPROM_START(); EEPROM_START();
eeprom_read_error = false; // If set EEPROM_READ won't write into RAM
char stored_ver[4]; char stored_ver[4];
EEPROM_READ(stored_ver); EEPROM_READ(stored_ver);
@ -568,12 +575,16 @@ void Config_Postprocess() {
} }
if (eeprom_checksum == stored_checksum) { if (eeprom_checksum == stored_checksum) {
if (eeprom_read_error)
Config_ResetDefault();
else {
Config_Postprocess(); Config_Postprocess();
SERIAL_ECHO_START; SERIAL_ECHO_START;
SERIAL_ECHO(version); SERIAL_ECHO(version);
SERIAL_ECHOPAIR(" stored settings retrieved (", eeprom_index); SERIAL_ECHOPAIR(" stored settings retrieved (", eeprom_index);
SERIAL_ECHOLNPGM(" bytes)"); SERIAL_ECHOLNPGM(" bytes)");
} }
}
else { else {
SERIAL_ERROR_START; SERIAL_ERROR_START;
SERIAL_ERRORLNPGM("EEPROM checksum mismatch"); SERIAL_ERRORLNPGM("EEPROM checksum mismatch");

Loading…
Cancel
Save