From 5c8f7c7ea319d02223d291ead8a03ed815eb02c5 Mon Sep 17 00:00:00 2001 From: Ramiro Polla Date: Wed, 24 Mar 2021 13:32:08 +0100 Subject: [PATCH] Optimize MarlinSettings with template methods (#21426) --- Marlin/src/module/settings.cpp | 23 ++++++------------ Marlin/src/module/settings.h | 44 +++++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 19 deletions(-) diff --git a/Marlin/src/module/settings.cpp b/Marlin/src/module/settings.cpp index bf10902c6..d98ee3c21 100644 --- a/Marlin/src/module/settings.cpp +++ b/Marlin/src/module/settings.cpp @@ -572,13 +572,6 @@ void MarlinSettings::postprocess() { #if ENABLED(EEPROM_SETTINGS) - #define EEPROM_START() if (!persistentStore.access_start()) { SERIAL_ECHO_MSG("No EEPROM."); return false; } \ - int eeprom_index = EEPROM_OFFSET - #define EEPROM_FINISH() persistentStore.access_finish() - #define EEPROM_SKIP(VAR) (eeprom_index += sizeof(VAR)) - #define EEPROM_WRITE(VAR) do{ persistentStore.write_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc); }while(0) - #define EEPROM_READ(VAR) do{ persistentStore.read_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc, !validating); }while(0) - #define EEPROM_READ_ALWAYS(VAR) do{ persistentStore.read_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc); }while(0) #define EEPROM_ASSERT(TST,ERR) do{ if (!(TST)) { SERIAL_ERROR_MSG(ERR); eeprom_error = true; } }while(0) #if ENABLED(DEBUG_EEPROM_READWRITE) @@ -594,6 +587,8 @@ void MarlinSettings::postprocess() { const char version[4] = EEPROM_VERSION; bool MarlinSettings::eeprom_error, MarlinSettings::validating; + int MarlinSettings::eeprom_index; + uint16_t MarlinSettings::working_crc; bool MarlinSettings::size_error(const uint16_t size) { if (size != datasize()) { @@ -610,9 +605,7 @@ void MarlinSettings::postprocess() { float dummyf = 0; char ver[4] = "ERR"; - uint16_t working_crc = 0; - - EEPROM_START(); + if (!EEPROM_START(EEPROM_OFFSET)) return false; eeprom_error = false; @@ -1456,9 +1449,7 @@ void MarlinSettings::postprocess() { * M501 - Retrieve Configuration */ bool MarlinSettings::_load() { - uint16_t working_crc = 0; - - EEPROM_START(); + if (!EEPROM_START(EEPROM_OFFSET)) return false; char stored_ver[4]; EEPROM_READ_ALWAYS(stored_ver); @@ -1496,10 +1487,10 @@ void MarlinSettings::postprocess() { uint32_t tmp1[XYZ + esteppers]; float tmp2[XYZ + esteppers]; feedRate_t tmp3[XYZ + esteppers]; - EEPROM_READ(tmp1); // max_acceleration_mm_per_s2 + EEPROM_READ((uint8_t *)tmp1, sizeof(tmp1)); // max_acceleration_mm_per_s2 EEPROM_READ(planner.settings.min_segment_time_us); - EEPROM_READ(tmp2); // axis_steps_per_mm - EEPROM_READ(tmp3); // max_feedrate_mm_s + EEPROM_READ((uint8_t *)tmp2, sizeof(tmp2)); // axis_steps_per_mm + EEPROM_READ((uint8_t *)tmp3, sizeof(tmp3)); // max_feedrate_mm_s if (!validating) LOOP_XYZE_N(i) { const bool in = (i < esteppers + XYZ); diff --git a/Marlin/src/module/settings.h b/Marlin/src/module/settings.h index b213de93a..967d49c07 100644 --- a/Marlin/src/module/settings.h +++ b/Marlin/src/module/settings.h @@ -76,12 +76,15 @@ class MarlinSettings { //static void delete_mesh(); // necessary if we have a MAT //static void defrag_meshes(); // " #endif - #else + + #else // !EEPROM_SETTINGS + FORCE_INLINE static bool load() { reset(); report(); return true; } FORCE_INLINE static void first_load() { (void)load(); } - #endif + + #endif // !EEPROM_SETTINGS #if DISABLED(DISABLE_M503) static void report(const bool forReplay=false); @@ -105,7 +108,42 @@ class MarlinSettings { static bool _load(); static bool size_error(const uint16_t size); - #endif + + static int eeprom_index; + static uint16_t working_crc; + + static bool EEPROM_START(int eeprom_offset) { + if (!persistentStore.access_start()) { SERIAL_ECHO_MSG("No EEPROM."); return false; } + eeprom_index = eeprom_offset; + working_crc = 0; + return true; + } + + static void EEPROM_FINISH(void) { persistentStore.access_finish(); } + + template + static void EEPROM_SKIP(const T &VAR) { eeprom_index += sizeof(VAR); } + + template + static void EEPROM_WRITE(const T &VAR) { + persistentStore.write_data(eeprom_index, (const uint8_t *) &VAR, sizeof(VAR), &working_crc); + } + + template + static void EEPROM_READ(T &VAR) { + persistentStore.read_data(eeprom_index, (uint8_t *) &VAR, sizeof(VAR), &working_crc, !validating); + } + + static void EEPROM_READ(uint8_t *VAR, size_t sizeof_VAR) { + persistentStore.read_data(eeprom_index, VAR, sizeof_VAR, &working_crc, !validating); + } + + template + static void EEPROM_READ_ALWAYS(T &VAR) { + persistentStore.read_data(eeprom_index, (uint8_t *) &VAR, sizeof(VAR), &working_crc); + } + + #endif // EEPROM_SETTINGS }; extern MarlinSettings settings;