diff options
| author | Selene ToyKeeper | 2017-08-26 17:12:31 -0600 |
|---|---|---|
| committer | Selene ToyKeeper | 2017-08-26 17:12:31 -0600 |
| commit | 2370e0a3ce0a3ab928ac19af074a3ef67d533ca6 (patch) | |
| tree | 50cafac4f25b9abab55eedd3cd305381302b347e /spaghetti-monster | |
| parent | Added eeprom load/save API (no wear levelling yet), verified it works in Dark... (diff) | |
| download | anduril-2370e0a3ce0a3ab928ac19af074a3ef67d533ca6.tar.gz anduril-2370e0a3ce0a3ab928ac19af074a3ef67d533ca6.tar.bz2 anduril-2370e0a3ce0a3ab928ac19af074a3ef67d533ca6.zip | |
Made wear-levelling work. Takes a bunch of extra ROM though.
Also, I've only tested it a tiny amount.
Diffstat (limited to 'spaghetti-monster')
| -rw-r--r-- | spaghetti-monster/darkhorse.c | 30 | ||||
| -rw-r--r-- | spaghetti-monster/fsm-eeprom.c | 48 | ||||
| -rw-r--r-- | spaghetti-monster/fsm-eeprom.h | 6 |
3 files changed, 63 insertions, 21 deletions
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<EEPROM_WL_BYTES; i++) { + eeprom_wl[i] = eeprom_read_byte((uint8_t *)(offset+1+i)); + } + } + sei(); + return found; } void save_wl_eeprom() { + cli(); + // erase old state + EEP_OFFSET_T offset = eep_wl_prev_offset; + for (uint8_t i = 0; i < EEPROM_WL_BYTES+1; i ++) { + eeprom_update_byte((uint8_t *)offset+i, 0xFF); + } + + // save new state + offset += EEPROM_WL_BYTES+1; + if (offset > 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<EEPROM_WL_BYTES; i++, offset++) { + eeprom_update_byte((uint8_t *)(offset), eeprom_wl[i]); + } + sei(); } #endif diff --git a/spaghetti-monster/fsm-eeprom.h b/spaghetti-monster/fsm-eeprom.h index 3d34f23..c35c822 100644 --- a/spaghetti-monster/fsm-eeprom.h +++ b/spaghetti-monster/fsm-eeprom.h @@ -33,6 +33,9 @@ #endif #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(); // 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(); |
