Skip to content

Commit b98a094

Browse files
Define correct size for EEPROM
This was previously hardcoded to 4k, which is intended for devices with 6k of EEPROM reserving 2k for the LoRaWAN library. For devices like the L053, that only has 2k of EEPROM, this would result in an invalid EEPROM size. Now, this reserved value is more explicit and is configured on a per-board basis in variant.h using the new `STM32L0_CONFIG_EEPROM_RESERVED` macro. This is set to 2048 for all boards that have an embedded LoRaWAN transceiver and left undefined for the more generic boards. Then the full EEPROM size is autodetected using the STM32 CMSIS header files and exposed as `REAL_E2END`, and the reserved area is subtracted from that and exposed as `E2END` (and also through `EEPROM.length()`). This also changes `Arduino.h` to move the `avr/*.h` includes down to below the `variant.h` include (just moving `avr/io.h` would have been enough, but this is more consistent), so `variant.h` can be used in io.h. This also includes `Arduino.h` from `io.h`, in case `io.h` is included directly by a sketch. Note that the autodetection uses macros that include a typecast, so the resulting `E2END` macro cannot be examined by the preprocessor (e.g. an `#if` statement), which is why this uses a `static_assert` instead to check that `STM32L0_CONFIG_EEPROM_RESERVED` is not too big. The resulting expression is a valid constant expression in both C and C++ contexts, though.
1 parent 65b6883 commit b98a094

File tree

10 files changed

+47
-9
lines changed

10 files changed

+47
-9
lines changed

cores/arduino/Arduino.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,6 @@ typedef bool boolean;
3030
typedef uint8_t byte;
3131
typedef uint16_t word;
3232

33-
// some libraries and sketches depend on this AVR stuff,
34-
// assuming Arduino.h or WProgram.h automatically includes it...
35-
//
36-
#include "avr/pgmspace.h"
37-
#include "avr/interrupt.h"
38-
#include "avr/io.h"
39-
4033
#include "binary.h"
4134
#include "itoa.h"
4235

@@ -89,6 +82,12 @@ void loop( void ) ;
8982
// Include board variant
9083
#include "variant.h"
9184

85+
// some libraries and sketches depend on this AVR stuff,
86+
// assuming Arduino.h or WProgram.h automatically includes it...
87+
#include "avr/pgmspace.h"
88+
#include "avr/interrupt.h"
89+
#include "avr/io.h"
90+
9291
#include "wiring.h"
9392
#include "wiring_digital.h"
9493
#include "wiring_analog.h"

cores/arduino/avr/io.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,27 @@
2525
#ifndef _IO_H_
2626
#define _IO_H_
2727

28-
#include <stm32l0xx.h>
28+
#include <Arduino.h>
29+
#include <assert.h>
2930

3031
#define RAMSTART (SRAM_BASE)
3132
#define RAMSIZE (SRAM_SIZE_MAX)
3233
#define RAMEND (RAMSTART + RAMSIZE - 1)
3334

34-
#define E2END 0xfff
35+
#if defined(DATA_EEPROM_BASE) && defined(DATA_EEPROM_END)
36+
#define REAL_E2END (DATA_EEPROM_END - DATA_EEPROM_BASE)
37+
#elif defined(DATA_EEPROM_BASE) && defined(DATA_EEPROM_BANK2_BASE) && defined(DATA_EEPROM_BANK1_END) && defined(DATA_EEPROM_BANK2_END)
38+
#define REAL_E2END (DATA_EEPROM_BANK1_END - DATA_EEPROM_BASE + 1 + DATA_EEPROM_BANK2_END - DATA_EEPROM_BANK2_BASE)
39+
#else
40+
#error "Cannot determine EEPROM size"
41+
#endif
42+
43+
#if defined(STM32L0_CONFIG_EEPROM_RESERVED)
44+
static_assert(STM32L0_CONFIG_EEPROM_RESERVED <= REAL_E2END + 1, "STM32L0_CONFIG_EEPROM_RESERVED bigger than EEPROM");
45+
#define E2END (REAL_E2END - STM32L0_CONFIG_EEPROM_RESERVED)
46+
#else
47+
#define E2END REAL_E2END
48+
#endif
49+
3550

3651
#endif

variants/B-L072Z-LRWAN1/variant.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
#define STM32L0_CONFIG_HSECLK 0
3838
#define STM32L0_CONFIG_SYSOPT 0
3939

40+
// Reserve some room at the end of EEPROM for LoRaWAN library
41+
#define STM32L0_CONFIG_EEPROM_RESERVED 2048
42+
4043
/** Master clock frequency */
4144
#define VARIANT_MCK F_CPU
4245

variants/Cicada-L082CZ/variant.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646

4747
#define STM32L0_CONFIG_SFLASH_DATA_START (256 * 1024)
4848

49+
// Reserve some room at the end of EEPROM for LoRaWAN library
50+
#define STM32L0_CONFIG_EEPROM_RESERVED 2048
51+
4952
#define USBCON
5053

5154
/** Master clock frequency */

variants/Cricket-L082CZ/variant.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252

5353
#define STM32L0_CONFIG_SFLASH_DATA_START (256 * 1024)
5454

55+
// Reserve some room at the end of EEPROM for LoRaWAN library
56+
#define STM32L0_CONFIG_EEPROM_RESERVED 2048
57+
5558
#define USBCON
5659

5760
/** Master clock frequency */

variants/Gnat-L082CZ/variant.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@
5050
#define STM32L0_CONFIG_PIN_GNSS_RX STM32L0_GPIO_PIN_PA10_USART1_RX
5151
#define STM32L0_CONFIG_PIN_GNSS_TX STM32L0_GPIO_PIN_PA9_USART1_TX
5252

53+
// Reserve some room at the end of EEPROM for LoRaWAN library
54+
#define STM32L0_CONFIG_EEPROM_RESERVED 2048
55+
5356
#define USBCON
5457

5558
/** Master clock frequency */

variants/Grasshopper-L082CZ/variant.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242

4343
#define STM32L0_CONFIG_SFLASH_DATA_START (256 * 1024)
4444

45+
// Reserve some room at the end of EEPROM for LoRaWAN library
46+
#define STM32L0_CONFIG_EEPROM_RESERVED 2048
47+
4548
#define USBCON
4649

4750
/** Master clock frequency */

variants/I-NUCLEO-LRWAN1/variant.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
#define STM32L0_CONFIG_HSECLK 0
3838
#define STM32L0_CONFIG_SYSOPT 0
3939

40+
// Reserve some room at the end of EEPROM for LoRaWAN library
41+
#define STM32L0_CONFIG_EEPROM_RESERVED 2048
42+
4043
/** Master clock frequency */
4144
#define VARIANT_MCK F_CPU
4245

variants/NUCLEO-L073RZ/variant.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
#define STM32L0_CONFIG_HSECLK 0
3838
#define STM32L0_CONFIG_SYSOPT 0
3939

40+
// Reserve some room at the end of EEPROM for LoRaWAN library
41+
#define STM32L0_CONFIG_EEPROM_RESERVED 2048
42+
4043
/** Master clock frequency */
4144
#define VARIANT_MCK F_CPU
4245

variants/P-NUCLEO-LRWAN1/variant.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
#define STM32L0_CONFIG_HSECLK 0
3838
#define STM32L0_CONFIG_SYSOPT 0
3939

40+
// Reserve some room at the end of EEPROM for LoRaWAN library
41+
#define STM32L0_CONFIG_EEPROM_RESERVED 2048
42+
4043
/** Master clock frequency */
4144
#define VARIANT_MCK F_CPU
4245

0 commit comments

Comments
 (0)