From 65b6883c47abbeca504e4ce9ca0b34fa43f2259f Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Mon, 2 Nov 2020 17:55:38 +0100 Subject: [PATCH 1/2] Define correct size for RAM This was previously hardcoded to a single value, but is now derived from the STM32 chip-specific header files. --- cores/arduino/avr/io.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cores/arduino/avr/io.h b/cores/arduino/avr/io.h index 01982e0b..e0bdf5c3 100644 --- a/cores/arduino/avr/io.h +++ b/cores/arduino/avr/io.h @@ -25,8 +25,10 @@ #ifndef _IO_H_ #define _IO_H_ -#define RAMSTART 0x20000000 -#define RAMSIZE (20 * 1024) +#include + +#define RAMSTART (SRAM_BASE) +#define RAMSIZE (SRAM_SIZE_MAX) #define RAMEND (RAMSTART + RAMSIZE - 1) #define E2END 0xfff From b98a094c2d88cf5e04fedff9a0c00d0a338a7c7a Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Mon, 2 Nov 2020 17:55:38 +0100 Subject: [PATCH 2/2] 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. --- cores/arduino/Arduino.h | 13 ++++++------- cores/arduino/avr/io.h | 19 +++++++++++++++++-- variants/B-L072Z-LRWAN1/variant.h | 3 +++ variants/Cicada-L082CZ/variant.h | 3 +++ variants/Cricket-L082CZ/variant.h | 3 +++ variants/Gnat-L082CZ/variant.h | 3 +++ variants/Grasshopper-L082CZ/variant.h | 3 +++ variants/I-NUCLEO-LRWAN1/variant.h | 3 +++ variants/NUCLEO-L073RZ/variant.h | 3 +++ variants/P-NUCLEO-LRWAN1/variant.h | 3 +++ 10 files changed, 47 insertions(+), 9 deletions(-) diff --git a/cores/arduino/Arduino.h b/cores/arduino/Arduino.h index 013e5a61..54e31813 100644 --- a/cores/arduino/Arduino.h +++ b/cores/arduino/Arduino.h @@ -30,13 +30,6 @@ typedef bool boolean; typedef uint8_t byte; typedef uint16_t word; -// some libraries and sketches depend on this AVR stuff, -// assuming Arduino.h or WProgram.h automatically includes it... -// -#include "avr/pgmspace.h" -#include "avr/interrupt.h" -#include "avr/io.h" - #include "binary.h" #include "itoa.h" @@ -89,6 +82,12 @@ void loop( void ) ; // Include board variant #include "variant.h" +// some libraries and sketches depend on this AVR stuff, +// assuming Arduino.h or WProgram.h automatically includes it... +#include "avr/pgmspace.h" +#include "avr/interrupt.h" +#include "avr/io.h" + #include "wiring.h" #include "wiring_digital.h" #include "wiring_analog.h" diff --git a/cores/arduino/avr/io.h b/cores/arduino/avr/io.h index e0bdf5c3..5793ede4 100644 --- a/cores/arduino/avr/io.h +++ b/cores/arduino/avr/io.h @@ -25,12 +25,27 @@ #ifndef _IO_H_ #define _IO_H_ -#include +#include +#include #define RAMSTART (SRAM_BASE) #define RAMSIZE (SRAM_SIZE_MAX) #define RAMEND (RAMSTART + RAMSIZE - 1) -#define E2END 0xfff +#if defined(DATA_EEPROM_BASE) && defined(DATA_EEPROM_END) + #define REAL_E2END (DATA_EEPROM_END - DATA_EEPROM_BASE) +#elif defined(DATA_EEPROM_BASE) && defined(DATA_EEPROM_BANK2_BASE) && defined(DATA_EEPROM_BANK1_END) && defined(DATA_EEPROM_BANK2_END) + #define REAL_E2END (DATA_EEPROM_BANK1_END - DATA_EEPROM_BASE + 1 + DATA_EEPROM_BANK2_END - DATA_EEPROM_BANK2_BASE) +#else + #error "Cannot determine EEPROM size" +#endif + +#if defined(STM32L0_CONFIG_EEPROM_RESERVED) + static_assert(STM32L0_CONFIG_EEPROM_RESERVED <= REAL_E2END + 1, "STM32L0_CONFIG_EEPROM_RESERVED bigger than EEPROM"); + #define E2END (REAL_E2END - STM32L0_CONFIG_EEPROM_RESERVED) +#else + #define E2END REAL_E2END +#endif + #endif diff --git a/variants/B-L072Z-LRWAN1/variant.h b/variants/B-L072Z-LRWAN1/variant.h index e1249656..52f11fcc 100644 --- a/variants/B-L072Z-LRWAN1/variant.h +++ b/variants/B-L072Z-LRWAN1/variant.h @@ -37,6 +37,9 @@ #define STM32L0_CONFIG_HSECLK 0 #define STM32L0_CONFIG_SYSOPT 0 +// Reserve some room at the end of EEPROM for LoRaWAN library +#define STM32L0_CONFIG_EEPROM_RESERVED 2048 + /** Master clock frequency */ #define VARIANT_MCK F_CPU diff --git a/variants/Cicada-L082CZ/variant.h b/variants/Cicada-L082CZ/variant.h index ac6b4304..1994bae4 100644 --- a/variants/Cicada-L082CZ/variant.h +++ b/variants/Cicada-L082CZ/variant.h @@ -46,6 +46,9 @@ #define STM32L0_CONFIG_SFLASH_DATA_START (256 * 1024) +// Reserve some room at the end of EEPROM for LoRaWAN library +#define STM32L0_CONFIG_EEPROM_RESERVED 2048 + #define USBCON /** Master clock frequency */ diff --git a/variants/Cricket-L082CZ/variant.h b/variants/Cricket-L082CZ/variant.h index fbea4971..63ada1b3 100644 --- a/variants/Cricket-L082CZ/variant.h +++ b/variants/Cricket-L082CZ/variant.h @@ -52,6 +52,9 @@ #define STM32L0_CONFIG_SFLASH_DATA_START (256 * 1024) +// Reserve some room at the end of EEPROM for LoRaWAN library +#define STM32L0_CONFIG_EEPROM_RESERVED 2048 + #define USBCON /** Master clock frequency */ diff --git a/variants/Gnat-L082CZ/variant.h b/variants/Gnat-L082CZ/variant.h index 5ec123e2..e46c1263 100644 --- a/variants/Gnat-L082CZ/variant.h +++ b/variants/Gnat-L082CZ/variant.h @@ -50,6 +50,9 @@ #define STM32L0_CONFIG_PIN_GNSS_RX STM32L0_GPIO_PIN_PA10_USART1_RX #define STM32L0_CONFIG_PIN_GNSS_TX STM32L0_GPIO_PIN_PA9_USART1_TX +// Reserve some room at the end of EEPROM for LoRaWAN library +#define STM32L0_CONFIG_EEPROM_RESERVED 2048 + #define USBCON /** Master clock frequency */ diff --git a/variants/Grasshopper-L082CZ/variant.h b/variants/Grasshopper-L082CZ/variant.h index 6048dac7..5fdd4e9d 100644 --- a/variants/Grasshopper-L082CZ/variant.h +++ b/variants/Grasshopper-L082CZ/variant.h @@ -42,6 +42,9 @@ #define STM32L0_CONFIG_SFLASH_DATA_START (256 * 1024) +// Reserve some room at the end of EEPROM for LoRaWAN library +#define STM32L0_CONFIG_EEPROM_RESERVED 2048 + #define USBCON /** Master clock frequency */ diff --git a/variants/I-NUCLEO-LRWAN1/variant.h b/variants/I-NUCLEO-LRWAN1/variant.h index 2c092dde..552293b6 100644 --- a/variants/I-NUCLEO-LRWAN1/variant.h +++ b/variants/I-NUCLEO-LRWAN1/variant.h @@ -37,6 +37,9 @@ #define STM32L0_CONFIG_HSECLK 0 #define STM32L0_CONFIG_SYSOPT 0 +// Reserve some room at the end of EEPROM for LoRaWAN library +#define STM32L0_CONFIG_EEPROM_RESERVED 2048 + /** Master clock frequency */ #define VARIANT_MCK F_CPU diff --git a/variants/NUCLEO-L073RZ/variant.h b/variants/NUCLEO-L073RZ/variant.h index 5036804f..157c1720 100644 --- a/variants/NUCLEO-L073RZ/variant.h +++ b/variants/NUCLEO-L073RZ/variant.h @@ -37,6 +37,9 @@ #define STM32L0_CONFIG_HSECLK 0 #define STM32L0_CONFIG_SYSOPT 0 +// Reserve some room at the end of EEPROM for LoRaWAN library +#define STM32L0_CONFIG_EEPROM_RESERVED 2048 + /** Master clock frequency */ #define VARIANT_MCK F_CPU diff --git a/variants/P-NUCLEO-LRWAN1/variant.h b/variants/P-NUCLEO-LRWAN1/variant.h index 1ee4f78d..b717fa2f 100644 --- a/variants/P-NUCLEO-LRWAN1/variant.h +++ b/variants/P-NUCLEO-LRWAN1/variant.h @@ -37,6 +37,9 @@ #define STM32L0_CONFIG_HSECLK 0 #define STM32L0_CONFIG_SYSOPT 0 +// Reserve some room at the end of EEPROM for LoRaWAN library +#define STM32L0_CONFIG_EEPROM_RESERVED 2048 + /** Master clock frequency */ #define VARIANT_MCK F_CPU