aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSelene ToyKeeper2023-11-07 08:09:10 -0700
committerSelene ToyKeeper2023-11-07 08:09:10 -0700
commite00326a6db6671414b43ee15e092c57ea976102f (patch)
tree314608a1c7ba0d0177cab6ead8186638aa8a7e64
parentmemes (diff)
downloadanduril-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
-rw-r--r--arch/mcu.h24
-rw-r--r--fsm/eeprom.h49
-rw-r--r--fsm/tk.h14
3 files changed, 49 insertions, 38 deletions
diff --git a/arch/mcu.h b/arch/mcu.h
index 5de6ea9..3b2b974 100644
--- a/arch/mcu.h
+++ b/arch/mcu.h
@@ -5,14 +5,18 @@
// This helps abstract away the differences between various attiny MCUs.
-// Choose your MCU here, or in the main .c file, or in the build script
-//#define ATTINY 13
-//#define ATTINY 25
+// auto-detect eeprom size from avr-libc headers
+#ifndef EEPROM_SIZE
+ #ifdef E2SIZE
+ #define EEPROM_SIZE E2SIZE
+ #elif defined(E2END)
+ #define EEPROM_SIZE (E2END+1)
+ #endif
+#endif
/******************** hardware-specific values **************************/
#if (ATTINY == 13)
#define F_CPU 4800000UL
- //#define EEPSIZE 64
#define V_REF REFS0
#define BOGOMIPS 950
#define ADMUX_VCC 0b00001100
@@ -22,7 +26,6 @@
#elif (ATTINY == 25)
// TODO: Use 6.4 MHz instead of 8 MHz?
#define F_CPU 8000000UL
- //#define EEPSIZE 128
#define V_REF REFS1
#define BOGOMIPS (F_CPU/4000)
#define ADMUX_VCC 0b00001100
@@ -33,7 +36,6 @@
#elif (ATTINY == 85)
// TODO: Use 6.4 MHz instead of 8 MHz?
#define F_CPU 8000000UL
- //#define EEPSIZE 512
#define V_REF REFS1
#define BOGOMIPS (F_CPU/4000)
// (1 << V_REF) | (0 << ADLAR) | (VCC_CHANNEL)
@@ -59,21 +61,11 @@
#define AVRXMEGA3
#define F_CPU 10000000UL
#define BOGOMIPS (F_CPU/4700)
- #define EEPSIZE 128
#define DELAY_ZERO_TIME 1020
#else
#error Hey, you need to define ATTINY.
#endif
-// auto-detect eeprom size from avr-libc headers
-#ifndef EEPSIZE
-#ifdef E2SIZE
-#define EEPSIZE E2SIZE
-#else
-#define EEPSIZE (E2END+1)
-#endif
-#endif
-
#include <avr/interrupt.h>
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
diff --git a/fsm/tk.h b/fsm/tk.h
index 785808d..f2e7efe 100644
--- a/fsm/tk.h
+++ b/fsm/tk.h
@@ -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)])); }
+