Skip to content

Commit

Permalink
Define correct size for EEPROM
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
matthijskooijman committed Nov 4, 2020
1 parent 65b6883 commit b98a094
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 9 deletions.
13 changes: 6 additions & 7 deletions cores/arduino/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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"
Expand Down
19 changes: 17 additions & 2 deletions cores/arduino/avr/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,27 @@
#ifndef _IO_H_
#define _IO_H_

#include <stm32l0xx.h>
#include <Arduino.h>
#include <assert.h>

#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
3 changes: 3 additions & 0 deletions variants/B-L072Z-LRWAN1/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions variants/Cicada-L082CZ/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
3 changes: 3 additions & 0 deletions variants/Cricket-L082CZ/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
3 changes: 3 additions & 0 deletions variants/Gnat-L082CZ/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
3 changes: 3 additions & 0 deletions variants/Grasshopper-L082CZ/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
3 changes: 3 additions & 0 deletions variants/I-NUCLEO-LRWAN1/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions variants/NUCLEO-L073RZ/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions variants/P-NUCLEO-LRWAN1/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit b98a094

Please sign in to comment.