From 380c771988ff415d48cc06df74b6c86967087016 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sun, 24 Mar 2019 19:10:33 -0500 Subject: [PATCH] Handle word-padded flash-based eeprom (STM32F1) Fix #13445 --- .../HAL_LPC1768/persistent_store_flash.cpp | 3 +- .../HAL_STM32F1/persistent_store_flash.cpp | 7 ++-- Marlin/src/module/configuration_store.cpp | 33 ++++++++++++++----- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/Marlin/src/HAL/HAL_LPC1768/persistent_store_flash.cpp b/Marlin/src/HAL/HAL_LPC1768/persistent_store_flash.cpp index df7cc76b0..4b02544e1 100644 --- a/Marlin/src/HAL/HAL_LPC1768/persistent_store_flash.cpp +++ b/Marlin/src/HAL/HAL_LPC1768/persistent_store_flash.cpp @@ -74,7 +74,8 @@ bool PersistentStore::access_start() { // sector is blank so nothing stored yet for (int i = 0; i < EEPROM_SIZE; i++) ram_eeprom[i] = EEPROM_ERASE; current_slot = EEPROM_SLOTS; - } else { + } + else { // current slot is the first non blank one current_slot = first_nblank_loc / EEPROM_SIZE; uint8_t *eeprom_data = SLOT_ADDRESS(EEPROM_SECTOR, current_slot); diff --git a/Marlin/src/HAL/HAL_STM32F1/persistent_store_flash.cpp b/Marlin/src/HAL/HAL_STM32F1/persistent_store_flash.cpp index 43c3fbca0..aa03474d2 100644 --- a/Marlin/src/HAL/HAL_STM32F1/persistent_store_flash.cpp +++ b/Marlin/src/HAL/HAL_STM32F1/persistent_store_flash.cpp @@ -79,14 +79,15 @@ bool PersistentStore::write_data(int &pos, const uint8_t *value, const size_t si } // Now, write any remaining single byte - if (size & 1) { + const uint16_t odd = size & 1; + if (odd) { uint16_t temp = value[size - 1]; status = FLASH_ProgramHalfWord(pageBase + pos + i, temp); if (status != FLASH_COMPLETE) return true; } crc16(crc, value, size); - pos += ((size + 1) & ~1); + pos += size + odd; return false; } @@ -97,7 +98,7 @@ bool PersistentStore::read_data(int &pos, uint8_t* value, const size_t size, uin if (writing) value[i] = c; crc16(crc, &c, 1); } - pos += ((size + 1) & ~1); + pos += ((size + 1) & ~1); // i.e., size+(size&1), round up odd values return false; } diff --git a/Marlin/src/module/configuration_store.cpp b/Marlin/src/module/configuration_store.cpp index d71c97ae2..833d94444 100644 --- a/Marlin/src/module/configuration_store.cpp +++ b/Marlin/src/module/configuration_store.cpp @@ -384,18 +384,35 @@ void MarlinSettings::postprocess() { #if ENABLED(EEPROM_SETTINGS) - #define EEPROM_START() int eeprom_index = EEPROM_OFFSET; persistentStore.access_start() - #define EEPROM_FINISH() persistentStore.access_finish() - #define EEPROM_SKIP(VAR) eeprom_index += sizeof(VAR) - #define EEPROM_WRITE(VAR) persistentStore.write_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc) - #define EEPROM_READ(VAR) persistentStore.read_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc, !validating) - #define EEPROM_READ_ALWAYS(VAR) persistentStore.read_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc) - #define EEPROM_ASSERT(TST,ERR) do{ if (!(TST)) { SERIAL_ERROR_MSG(ERR); eeprom_error = true; } }while(0) + #define WORD_PADDED_EEPROM ENABLED(__STM32F1__, FLASH_EEPROM_EMULATION) + + #if WORD_PADDED_EEPROM && ENABLED(DEBUG_EEPROM_READWRITE) + #define UPDATE_TEST_INDEX(VAR) (text_index += sizeof(VAR)) + #else + #define UPDATE_TEST_INDEX(VAR) NOOP + #endif + #if WORD_PADDED_EEPROM + #define EEPROM_SKIP(VAR) do{ eeprom_index += sizeof(VAR) + (sizeof(VAR) & 1); UPDATE_TEST_INDEX(sizeof(VAR)); }while(0) + #else + #define EEPROM_SKIP(VAR) (eeprom_index += sizeof(VAR)) + #endif + + #define EEPROM_START() int eeprom_index = EEPROM_OFFSET; persistentStore.access_start() + #define EEPROM_FINISH() persistentStore.access_finish() + #define EEPROM_WRITE(VAR) do{ persistentStore.write_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc); UPDATE_TEST_INDEX(VAR); }while(0) + #define EEPROM_READ(VAR) do{ persistentStore.read_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc, !validating); UPDATE_TEST_INDEX(VAR); }while(0) + #define EEPROM_READ_ALWAYS(VAR) do{ persistentStore.read_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc); UPDATE_TEST_INDEX(VAR); }while(0) + #define EEPROM_ASSERT(TST,ERR) do{ if (!(TST)) { SERIAL_ERROR_MSG(ERR); eeprom_error = true; } }while(0) #if ENABLED(DEBUG_EEPROM_READWRITE) + #if WORD_PADDED_EEPROM + int test_index; + #else + int &test_index = eeprom_index; + #endif #define _FIELD_TEST(FIELD) \ EEPROM_ASSERT( \ - eeprom_error || eeprom_index == offsetof(SettingsData, FIELD) + EEPROM_OFFSET, \ + eeprom_error || test_index == offsetof(SettingsData, FIELD) + EEPROM_OFFSET, \ "Field " STRINGIFY(FIELD) " mismatch." \ ) #else