Skip to content

Commit

Permalink
Use MARLIN_EEPROM_SIZE with E2END as fallback (#18059)
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead authored May 22, 2020
1 parent 31eb487 commit 461647f
Show file tree
Hide file tree
Showing 49 changed files with 201 additions and 197 deletions.
5 changes: 4 additions & 1 deletion Marlin/src/HAL/AVR/eeprom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@

#include "../shared/eeprom_api.h"

size_t PersistentStore::capacity() { return E2END + 1; }
#ifndef MARLIN_EEPROM_SIZE
#define MARLIN_EEPROM_SIZE size_t(E2END + 1)
#endif
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }
bool PersistentStore::access_start() { return true; }
bool PersistentStore::access_finish() { return true; }

Expand Down
9 changes: 4 additions & 5 deletions Marlin/src/HAL/DUE/eeprom_flash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@

#if ENABLED(FLASH_EEPROM_EMULATION)

#ifndef E2END
#define E2END 0xFFF // Default to Flash emulated EEPROM size (eeprom_emul.cpp)
#endif

/* EEPROM emulation over flash with reduced wear
*
* We will use 2 contiguous groups of pages as main and alternate.
Expand Down Expand Up @@ -973,7 +969,10 @@ static void ee_Init() {

#include "../shared/eeprom_api.h"

size_t PersistentStore::capacity() { return E2END + 1; }
#ifndef MARLIN_EEPROM_SIZE
#define MARLIN_EEPROM_SIZE 0x1000 // 4KB
#endif
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }
bool PersistentStore::access_start() { ee_Init(); return true; }
bool PersistentStore::access_finish() { ee_Flush(); return true; }

Expand Down
5 changes: 4 additions & 1 deletion Marlin/src/HAL/DUE/eeprom_wired.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
#include "../shared/eeprom_if.h"
#include "../shared/eeprom_api.h"

size_t PersistentStore::capacity() { return E2END + 1; }
#ifndef MARLIN_EEPROM_SIZE
#error "MARLIN_EEPROM_SIZE is required for I2C / SPI EEPROM."
#endif
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }
bool PersistentStore::access_start() { return true; }
bool PersistentStore::access_finish() { return true; }

Expand Down
17 changes: 6 additions & 11 deletions Marlin/src/HAL/ESP32/eeprom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,13 @@
#include "../shared/eeprom_api.h"
#include <EEPROM.h>

#define EEPROM_SIZE 4096
#ifndef MARLIN_EEPROM_SIZE
#define MARLIN_EEPROM_SIZE 0x1000 // 4KB
#endif
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }

bool PersistentStore::access_start() {
return EEPROM.begin(EEPROM_SIZE);
}

bool PersistentStore::access_finish() {
EEPROM.end();
return true;
}
bool PersistentStore::access_start() { return EEPROM.begin(MARLIN_EEPROM_SIZE); }
bool PersistentStore::access_finish() { EEPROM.end(); return true; }

bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
for (size_t i = 0; i < size; i++) {
Expand All @@ -56,7 +53,5 @@ bool PersistentStore::read_data(int &pos, uint8_t* value, size_t size, uint16_t
return false;
}

size_t PersistentStore::capacity() { return EEPROM_SIZE; }

#endif // EEPROM_SETTINGS
#endif // ARDUINO_ARCH_ESP32
15 changes: 9 additions & 6 deletions Marlin/src/HAL/LINUX/eeprom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@
#include "../shared/eeprom_api.h"
#include <stdio.h>

#define LINUX_EEPROM_SIZE (E2END + 1)
uint8_t buffer[LINUX_EEPROM_SIZE];
#ifndef MARLIN_EEPROM_SIZE
#define MARLIN_EEPROM_SIZE 0x1000 // 4KB of Emulated EEPROM
#endif

uint8_t buffer[MARLIN_EEPROM_SIZE];
char filename[] = "eeprom.dat";

size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }

bool PersistentStore::access_start() {
const char eeprom_erase_value = 0xFF;
FILE * eeprom_file = fopen(filename, "rb");
Expand All @@ -40,8 +45,8 @@ bool PersistentStore::access_start() {
fseek(eeprom_file, 0L, SEEK_END);
std::size_t file_size = ftell(eeprom_file);

if (file_size < LINUX_EEPROM_SIZE) {
memset(buffer + file_size, eeprom_erase_value, LINUX_EEPROM_SIZE - file_size);
if (file_size < MARLIN_EEPROM_SIZE) {
memset(buffer + file_size, eeprom_erase_value, MARLIN_EEPROM_SIZE - file_size);
}
else {
fseek(eeprom_file, 0L, SEEK_SET);
Expand Down Expand Up @@ -95,7 +100,5 @@ bool PersistentStore::read_data(int &pos, uint8_t* value, const size_t size, uin
return bytes_read != size; // return true for any error
}

size_t PersistentStore::capacity() { return 4096; } // 4KiB of Emulated EEPROM

#endif // EEPROM_SETTINGS
#endif // __PLAT_LINUX__
25 changes: 14 additions & 11 deletions Marlin/src/HAL/LPC1768/eeprom_flash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,22 @@ extern "C" {
#include <lpc17xx_iap.h>
}

#define SECTOR_START(sector) ((sector < 16) ? (sector * 0x1000) : ((sector - 14) * 0x8000))
#ifndef MARLIN_EEPROM_SIZE
#define MARLIN_EEPROM_SIZE 0x1000 // 4KB
#endif

#define SECTOR_START(sector) ((sector < 16) ? (sector << 12) : ((sector - 14) << 15))
#define EEPROM_SECTOR 29
#define EEPROM_SIZE (4096)
#define SECTOR_SIZE (32768)
#define EEPROM_SLOTS (SECTOR_SIZE/EEPROM_SIZE)
#define EEPROM_ERASE (0xFF)
#define SLOT_ADDRESS(sector, slot) (((uint8_t *)SECTOR_START(sector)) + slot * EEPROM_SIZE)
#define SECTOR_SIZE 32768
#define EEPROM_SLOTS ((SECTOR_SIZE)/(MARLIN_EEPROM_SIZE))
#define EEPROM_ERASE 0xFF
#define SLOT_ADDRESS(sector, slot) (((uint8_t *)SECTOR_START(sector)) + slot * (MARLIN_EEPROM_SIZE))

static uint8_t ram_eeprom[EEPROM_SIZE] __attribute__((aligned(4))) = {0};
static uint8_t ram_eeprom[MARLIN_EEPROM_SIZE] __attribute__((aligned(4))) = {0};
static bool eeprom_dirty = false;
static int current_slot = 0;

size_t PersistentStore::capacity() { return EEPROM_SIZE; }
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }

bool PersistentStore::access_start() {
uint32_t first_nblank_loc, first_nblank_val;
Expand All @@ -71,15 +74,15 @@ bool PersistentStore::access_start() {

if (status == CMD_SUCCESS) {
// sector is blank so nothing stored yet
for (int i = 0; i < EEPROM_SIZE; i++) ram_eeprom[i] = EEPROM_ERASE;
for (int i = 0; i < MARLIN_EEPROM_SIZE; i++) ram_eeprom[i] = EEPROM_ERASE;
current_slot = EEPROM_SLOTS;
}
else {
// current slot is the first non blank one
current_slot = first_nblank_loc / EEPROM_SIZE;
current_slot = first_nblank_loc / (MARLIN_EEPROM_SIZE);
uint8_t *eeprom_data = SLOT_ADDRESS(EEPROM_SECTOR, current_slot);
// load current settings
for (int i = 0; i < EEPROM_SIZE; i++) ram_eeprom[i] = eeprom_data[i];
for (int i = 0; i < MARLIN_EEPROM_SIZE; i++) ram_eeprom[i] = eeprom_data[i];
}
eeprom_dirty = false;

Expand Down
5 changes: 4 additions & 1 deletion Marlin/src/HAL/LPC1768/eeprom_sdcard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ FATFS fat_fs;
FIL eeprom_file;
bool eeprom_file_open = false;

size_t PersistentStore::capacity() { return 4096; } // 4KiB of Emulated EEPROM
#ifndef MARLIN_EEPROM_SIZE
#define MARLIN_EEPROM_SIZE size_t(0x1000) // 4KiB of Emulated EEPROM
#endif
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }

bool PersistentStore::access_start() {
const char eeprom_erase_value = 0xFF;
Expand Down
12 changes: 4 additions & 8 deletions Marlin/src/HAL/LPC1768/eeprom_wired.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,14 @@
#include "../shared/eeprom_if.h"
#include "../shared/eeprom_api.h"

#ifndef EEPROM_SIZE
#define EEPROM_SIZE 0x8000 // 32kB
#ifndef MARLIN_EEPROM_SIZE
#define MARLIN_EEPROM_SIZE 0x8000 // 32KB
#endif
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }

size_t PersistentStore::capacity() { return EEPROM_SIZE; }
bool PersistentStore::access_start() { eeprom_init(); return true; }
bool PersistentStore::access_finish() { return true; }

bool PersistentStore::access_start() {
eeprom_init();
return true;
}

bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
while (size--) {
uint8_t v = *value;
Expand Down
12 changes: 6 additions & 6 deletions Marlin/src/HAL/SAMD51/eeprom_wired.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
#include "../shared/eeprom_if.h"
#include "../shared/eeprom_api.h"

size_t PersistentStore::capacity() { return E2END + 1; }
bool PersistentStore::access_finish() { return true; }
#ifndef MARLIN_EEPROM_SIZE
#error "MARLIN_EEPROM_SIZE is required for I2C / SPI EEPROM."
#endif
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }

bool PersistentStore::access_start() {
eeprom_init();
return true;
}
bool PersistentStore::access_start() { eeprom_init(); return true; }
bool PersistentStore::access_finish() { return true; }

bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
while (size--) {
Expand Down
39 changes: 20 additions & 19 deletions Marlin/src/HAL/STM32/eeprom_flash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include "Servo.h"
#define PAUSE_SERVO_OUTPUT() libServo::pause_all_servos()
#define RESUME_SERVO_OUTPUT() libServo::resume_all_servos()
#else
#else
#define PAUSE_SERVO_OUTPUT()
#define RESUME_SERVO_OUTPUT()
#endif
Expand All @@ -59,8 +59,8 @@
#define DEBUG_OUT ENABLED(EEPROM_CHITCHAT)
#include "src/core/debug_out.h"

#ifndef EEPROM_SIZE
#define EEPROM_SIZE 0x1000 // 4kB
#ifndef MARLIN_EEPROM_SIZE
#define MARLIN_EEPROM_SIZE 0x1000 // 4KB
#endif

#ifndef FLASH_SECTOR
Expand All @@ -70,11 +70,11 @@
#define FLASH_UNIT_SIZE 0x20000 // 128kB
#endif

#define FLASH_ADDRESS_START (FLASH_END - ((FLASH_SECTOR_TOTAL - FLASH_SECTOR) * FLASH_UNIT_SIZE) + 1)
#define FLASH_ADDRESS_START (FLASH_END - ((FLASH_SECTOR_TOTAL - (FLASH_SECTOR)) * (FLASH_UNIT_SIZE)) + 1)
#define FLASH_ADDRESS_END (FLASH_ADDRESS_START + FLASH_UNIT_SIZE - 1)

#define EEPROM_SLOTS (FLASH_UNIT_SIZE/EEPROM_SIZE)
#define SLOT_ADDRESS(slot) (FLASH_ADDRESS_START + (slot * EEPROM_SIZE))
#define EEPROM_SLOTS ((FLASH_UNIT_SIZE) / (MARLIN_EEPROM_SIZE))
#define SLOT_ADDRESS(slot) (FLASH_ADDRESS_START + (slot * (MARLIN_EEPROM_SIZE)))

#define UNLOCK_FLASH() if (!flash_unlocked) { \
HAL_FLASH_Unlock(); \
Expand All @@ -87,19 +87,24 @@
#define EMPTY_UINT32 ((uint32_t)-1)
#define EMPTY_UINT8 ((uint8_t)-1)

static uint8_t ram_eeprom[EEPROM_SIZE] __attribute__((aligned(4))) = {0};
static uint8_t ram_eeprom[MARLIN_EEPROM_SIZE] __attribute__((aligned(4))) = {0};
static int current_slot = -1;

static_assert(0 == EEPROM_SIZE % 4, "EEPROM_SIZE must be a multiple of 4"); // Ensure copying as uint32_t is safe
static_assert(0 == FLASH_UNIT_SIZE % EEPROM_SIZE, "EEPROM_SIZE must divide evenly into your FLASH_UNIT_SIZE");
static_assert(FLASH_UNIT_SIZE >= EEPROM_SIZE, "FLASH_UNIT_SIZE must be greater than or equal to your EEPROM_SIZE");
static_assert(0 == MARLIN_EEPROM_SIZE % 4, "MARLIN_EEPROM_SIZE must be a multiple of 4"); // Ensure copying as uint32_t is safe
static_assert(0 == FLASH_UNIT_SIZE % MARLIN_EEPROM_SIZE, "MARLIN_EEPROM_SIZE must divide evenly into your FLASH_UNIT_SIZE");
static_assert(FLASH_UNIT_SIZE >= MARLIN_EEPROM_SIZE, "FLASH_UNIT_SIZE must be greater than or equal to your MARLIN_EEPROM_SIZE");
static_assert(IS_FLASH_SECTOR(FLASH_SECTOR), "FLASH_SECTOR is invalid");
static_assert(IS_POWER_OF_2(FLASH_UNIT_SIZE), "FLASH_UNIT_SIZE should be a power of 2, please check your chip's spec sheet");

#endif

static bool eeprom_data_written = false;

#ifndef MARLIN_EEPROM_SIZE
#define MARLIN_EEPROM_SIZE size_t(E2END + 1)
#endif
size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }

bool PersistentStore::access_start() {

#if ENABLED(FLASH_EEPROM_LEVELING)
Expand All @@ -113,20 +118,20 @@ bool PersistentStore::access_start() {
while (address <= FLASH_ADDRESS_END) {
uint32_t address_value = (*(__IO uint32_t*)address);
if (address_value != EMPTY_UINT32) {
current_slot = (address - FLASH_ADDRESS_START) / EEPROM_SIZE;
current_slot = (address - (FLASH_ADDRESS_START)) / (MARLIN_EEPROM_SIZE);
break;
}
address += sizeof(uint32_t);
}
if (current_slot == -1) {
// We didn't find anything, so we'll just intialize to empty
for (int i = 0; i < EEPROM_SIZE; i++) ram_eeprom[i] = EMPTY_UINT8;
for (int i = 0; i < MARLIN_EEPROM_SIZE; i++) ram_eeprom[i] = EMPTY_UINT8;
current_slot = EEPROM_SLOTS;
}
else {
// load current settings
uint8_t *eeprom_data = (uint8_t *)SLOT_ADDRESS(current_slot);
for (int i = 0; i < EEPROM_SIZE; i++) ram_eeprom[i] = eeprom_data[i];
for (int i = 0; i < MARLIN_EEPROM_SIZE; i++) ram_eeprom[i] = eeprom_data[i];
DEBUG_ECHOLNPAIR("EEPROM loaded from slot ", current_slot, ".");
}
eeprom_data_written = false;
Expand All @@ -146,7 +151,7 @@ bool PersistentStore::access_finish() {
// MCU may come up with flash error bits which prevent some flash operations.
// Clear flags prior to flash operations to prevent errors.
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
#endif
#endif

#if ENABLED(FLASH_EEPROM_LEVELING)

Expand Down Expand Up @@ -185,7 +190,7 @@ bool PersistentStore::access_finish() {

uint32_t offset = 0;
uint32_t address = SLOT_ADDRESS(current_slot);
uint32_t address_end = address + EEPROM_SIZE;
uint32_t address_end = address + MARLIN_EEPROM_SIZE;
uint32_t data = 0;

bool success = true;
Expand Down Expand Up @@ -267,9 +272,5 @@ bool PersistentStore::read_data(int &pos, uint8_t* value, size_t size, uint16_t
return false;
}

size_t PersistentStore::capacity() {
return TERN(FLASH_EEPROM_LEVELING, EEPROM_SIZE, E2END + 1);
}

#endif // FLASH_EEPROM_EMULATION
#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC
Loading

0 comments on commit 461647f

Please sign in to comment.