From 2370e0a3ce0a3ab928ac19af074a3ef67d533ca6 Mon Sep 17 00:00:00 2001 From: Selene ToyKeeper Date: Sat, 26 Aug 2017 17:12:31 -0600 Subject: Made wear-levelling work. Takes a bunch of extra ROM though. Also, I've only tested it a tiny amount. --- spaghetti-monster/fsm-eeprom.c | 48 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) (limited to 'spaghetti-monster/fsm-eeprom.c') diff --git a/spaghetti-monster/fsm-eeprom.c b/spaghetti-monster/fsm-eeprom.c index 5451baf..e464785 100644 --- a/spaghetti-monster/fsm-eeprom.c +++ b/spaghetti-monster/fsm-eeprom.c @@ -23,9 +23,6 @@ #include "fsm-eeprom.h" #if EEPROM_BYTES > 0 -#if EEPROM_BYTES >= (EEPSIZE/2) -#error Requested EEPROM_BYTES too big. -#endif uint8_t eeprom[EEPROM_BYTES]; uint8_t load_eeprom() { @@ -55,15 +52,54 @@ void save_eeprom() { #endif #if EEPROM_WL_BYTES > 0 -#if EEPROM_WL_BYTES >= (EEPSIZE/4) -#error Requested EEPROM_WL_BYTES too big. -#endif uint8_t eeprom_wl[EEPROM_WL_BYTES]; +EEP_OFFSET_T eep_wl_prev_offset; uint8_t load_wl_eeprom() { + cli(); + // check if eeprom has been initialized; abort if it hasn't + uint8_t found = 0; + EEP_OFFSET_T offset; + for(offset = 0; + offset < EEP_WL_SIZE - EEPROM_WL_BYTES - 1; + offset += (EEPROM_WL_BYTES + 1)) { + if (eeprom_read_byte((uint8_t *)offset) == EEP_MARKER) { + found = 1; + eep_wl_prev_offset = offset; + break; + } + } + + if (found) { + // load the actual data + for(uint8_t i=0; i EEP_WL_SIZE-EEPROM_WL_BYTES-1) offset = 0; + eep_wl_prev_offset = offset; + // marker byte + eeprom_update_byte((uint8_t *)offset, EEP_MARKER); + offset ++; + // user data + for(uint8_t i=0; i