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/darkhorse.c | 30 +++++++++++++------------- spaghetti-monster/fsm-eeprom.c | 48 ++++++++++++++++++++++++++++++++++++------ spaghetti-monster/fsm-eeprom.h | 6 ++++++ 3 files changed, 63 insertions(+), 21 deletions(-) (limited to 'spaghetti-monster') diff --git a/spaghetti-monster/darkhorse.c b/spaghetti-monster/darkhorse.c index 2aa1b7b..0d02481 100644 --- a/spaghetti-monster/darkhorse.c +++ b/spaghetti-monster/darkhorse.c @@ -28,7 +28,7 @@ #define BATTCHECK_4bars #define DONT_DELAY_AFTER_BATTCHECK #define USE_EEPROM -#define EEPROM_BYTES 5 +#define EEPROM_WL_BYTES 5 #include "spaghetti-monster.h" // FSM states @@ -305,25 +305,25 @@ void strobe(uint8_t level, uint16_t ontime, uint16_t offtime) { } void load_config() { - if (load_eeprom()) { - H1 = !(!(eeprom[0] & 0b00000100)); - M1 = !(!(eeprom[0] & 0b00000010)); - L1 = !(!(eeprom[0] & 0b00000001)); - H2 = eeprom[1]; - M2 = eeprom[2]; - L2 = eeprom[3]; - strobe_beacon_mode = eeprom[4]; + if (load_wl_eeprom()) { + H1 = !(!(eeprom_wl[0] & 0b00000100)); + M1 = !(!(eeprom_wl[0] & 0b00000010)); + L1 = !(!(eeprom_wl[0] & 0b00000001)); + H2 = eeprom_wl[1]; + M2 = eeprom_wl[2]; + L2 = eeprom_wl[3]; + strobe_beacon_mode = eeprom_wl[4]; } } void save_config() { - eeprom[0] = (H1<<2) | (M1<<1) | (L1); - eeprom[1] = H2; - eeprom[2] = M2; - eeprom[3] = L2; - eeprom[4] = strobe_beacon_mode; + eeprom_wl[0] = (H1<<2) | (M1<<1) | (L1); + eeprom_wl[1] = H2; + eeprom_wl[2] = M2; + eeprom_wl[3] = L2; + eeprom_wl[4] = strobe_beacon_mode; - save_eeprom(); + save_wl_eeprom(); } void setup() { 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 0 +#if EEPROM_BYTES >= (EEPSIZE/2) +#error Requested EEPROM_BYTES too big. +#endif uint8_t eeprom[EEPROM_BYTES]; uint8_t load_eeprom(); // returns 1 for success, 0 for no data found void save_eeprom(); @@ -40,6 +43,9 @@ 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]; uint8_t load_wl_eeprom(); // returns 1 for success, 0 for no data found void save_wl_eeprom(); -- cgit v1.2.3