diff options
| author | Selene ToyKeeper | 2023-11-07 08:09:10 -0700 |
|---|---|---|
| committer | Selene ToyKeeper | 2023-11-07 08:09:10 -0700 |
| commit | e00326a6db6671414b43ee15e092c57ea976102f (patch) | |
| tree | 314608a1c7ba0d0177cab6ead8186638aa8a7e64 /fsm | |
| parent | memes (diff) | |
| download | anduril-e00326a6db6671414b43ee15e092c57ea976102f.tar.gz anduril-e00326a6db6671414b43ee15e092c57ea976102f.tar.bz2 anduril-e00326a6db6671414b43ee15e092c57ea976102f.zip | |
made build fail if eeprom data bigger than MCU's eeprom size
Based on techniques suggested by DurvalMenezes here:
https://github.com/ToyKeeper/anduril/pull/4
Diffstat (limited to '')
| -rw-r--r-- | fsm/eeprom.h | 49 | ||||
| -rw-r--r-- | fsm/tk.h | 14 |
2 files changed, 41 insertions, 22 deletions
diff --git a/fsm/eeprom.h b/fsm/eeprom.h index 440d2b3..1e10fd2 100644 --- a/fsm/eeprom.h +++ b/fsm/eeprom.h @@ -7,44 +7,49 @@ #include <avr/eeprom.h> // set this higher to enable normal eeprom functions +// TODO: rename to EEPROM_BYTES_NEEDED or similar, to make purpose clearer #ifndef EEPROM_BYTES #define EEPROM_BYTES 0 #endif // set this higher to enable wear-levelled eeprom functions +// TODO: rename to EEPROM_WL_BYTES_NEEDED or similar, to make purpose clearer #ifndef EEPROM_WL_BYTES #define EEPROM_WL_BYTES 0 #endif #ifdef USE_EEPROM -// this fails when EEPROM_BYTES is a sizeof() -//#if EEPROM_BYTES >= (EEPSIZE/2) -//#error Requested EEPROM_BYTES too big. -//#endif -#ifdef EEPROM_OVERRIDE -uint8_t *eeprom; -#else -uint8_t eeprom[EEPROM_BYTES]; -#endif -uint8_t load_eeprom(); // returns 1 for success, 0 for no data found -void save_eeprom(); -#define EEP_START (EEPSIZE/2) + #ifdef USE_EEPROM_WL + // split eeprom in half + #define EEP_START (EEPROM_SIZE/2) + BUILD_ASSERT(eep_less_than_half, EEPROM_BYTES <= (EEPROM_SIZE/2)); + #else + // use entire eeprom + #define EEP_START 0 + BUILD_ASSERT(eep_data_fits, EEPROM_BYTES <= EEPROM_SIZE); + #endif + #ifdef EEPROM_OVERRIDE + uint8_t *eeprom; + #else + uint8_t eeprom[EEPROM_BYTES]; + #endif + uint8_t load_eeprom(); // returns 1 for success, 0 for no data found + void save_eeprom(); #endif #ifdef USE_EEPROM_WL -#if EEPROM_WL_BYTES >= (EEPSIZE/4) -#error Requested EEPROM_WL_BYTES too big. -#endif -uint8_t eeprom_wl[EEPROM_WL_BYTES]; -uint8_t load_eeprom_wl(); // returns 1 for success, 0 for no data found -void save_eeprom_wl(); -#define EEP_WL_SIZE (EEPSIZE/2) + #define EEP_WL_SIZE (EEPROM_SIZE/2) + // ensure space for at least 2 slots for wear levelling + BUILD_ASSERT(eepwl_two_slots_minimum, EEPROM_WL_BYTES <= (EEP_WL_SIZE/2)); + uint8_t eeprom_wl[EEPROM_WL_BYTES]; + uint8_t load_eeprom_wl(); // returns 1 for success, 0 for no data found + void save_eeprom_wl(); #endif -#if EEPSIZE > 256 -#define EEP_OFFSET_T uint16_t +#if EEPROM_SIZE > 256 + #define EEP_OFFSET_T uint16_t #else -#define EEP_OFFSET_T uint8_t + #define EEP_OFFSET_T uint8_t #endif // if this marker isn't found, the eeprom is assumed to be blank @@ -24,3 +24,17 @@ // use it like this: //#include incfile(CONFIGFILE) + +// cause a build failure if `condition` is true +// (gcc compile trick taken from an old version of include/linux/kernel.h) +// more info here: +// https://scaryreasoner.wordpress.com/2009/02/28/checking-sizeof-at-compile-time/ +#define BUILD_FAIL_IF(name, condition) \ + __attribute__((unused)) static void name() \ + { ((void)sizeof(char[1 - 2*!!(condition)])); } + +// cause a build failure if `condition` is false +#define BUILD_ASSERT(name, condition) \ + __attribute__((unused)) static void name() \ + { ((void)sizeof(char[1 - 2*!(condition)])); } + |
