From 9c5086e6af1e31095fc691090969e97e4828f72b Mon Sep 17 00:00:00 2001 From: Eric Ptak Date: Sun, 11 Aug 2019 02:14:31 +0200 Subject: [PATCH] [STM32F1] Simpler Flash EEPROM (#14829) --- .../HAL_STM32F1/persistent_store_flash.cpp | 36 ++++++------------- Marlin/src/module/configuration_store.cpp | 27 +++----------- Marlin/src/pins/stm32/pins_FYSETC_CHEETAH.h | 6 ++++ 3 files changed, 21 insertions(+), 48 deletions(-) diff --git a/Marlin/src/HAL/HAL_STM32F1/persistent_store_flash.cpp b/Marlin/src/HAL/HAL_STM32F1/persistent_store_flash.cpp index 7004d88ffd..8097a28487 100644 --- a/Marlin/src/HAL/HAL_STM32F1/persistent_store_flash.cpp +++ b/Marlin/src/HAL/HAL_STM32F1/persistent_store_flash.cpp @@ -42,7 +42,6 @@ // Store settings in the last two pages // Flash pages must be erased before writing, so keep track. bool firstWrite = false; -uint32_t pageBase = EEPROM_START_ADDRESS; bool PersistentStore::access_start() { firstWrite = true; @@ -67,42 +66,27 @@ bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, ui firstWrite = false; } - // First write full words - int i = 0; - int wordsToWrite = size / sizeof(uint16_t); - uint16_t* wordBuffer = (uint16_t *)value; - while (wordsToWrite) { - status = FLASH_ProgramHalfWord(pageBase + pos + (i * 2), wordBuffer[i]); - if (status != FLASH_COMPLETE) return true; - wordsToWrite--; - i++; - } - - // Now, write any remaining single byte - 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; + for (size_t i = 0; i < size; i++) { + if (FLASH_ProgramHalfWord(EEPROM_PAGE0_BASE + (pos + i) * 2, value[i]) != FLASH_COMPLETE) + return true; } crc16(crc, value, size); - pos += size + odd; + pos += size; return false; } bool PersistentStore::read_data(int &pos, uint8_t* value, const size_t size, uint16_t *crc, const bool writing/*=true*/) { - for (uint16_t i = 0; i < size; i++) { - byte* accessPoint = (byte*)(pageBase + pos + i); - uint8_t c = *accessPoint; - if (writing) value[i] = c; - crc16(crc, &c, 1); + for (size_t i = 0; i < size; i++) { + uint8_t v = *(uint16_t *)(EEPROM_PAGE0_BASE + (pos + i) * 2); + if (writing) value[i] = v; + crc16(crc, &v, 1); } - pos += ((size + 1) & ~1); // i.e., size+(size&1), round up odd values + pos += size; return false; } -size_t PersistentStore::capacity() { return E2END + 1; } +size_t PersistentStore::capacity() { return size_t(E2END + 1); } #endif // EEPROM_SETTINGS && EEPROM FLASH #endif // __STM32F1__ diff --git a/Marlin/src/module/configuration_store.cpp b/Marlin/src/module/configuration_store.cpp index da3a52035d..161e360c21 100644 --- a/Marlin/src/module/configuration_store.cpp +++ b/Marlin/src/module/configuration_store.cpp @@ -447,36 +447,19 @@ void MarlinSettings::postprocess() { #if ENABLED(EEPROM_SETTINGS) - #define WORD_PADDED_EEPROM ENABLED(__STM32F1__, FLASH_EEPROM_EMULATION) - - #if WORD_PADDED_EEPROM && ENABLED(DEBUG_EEPROM_READWRITE) - #define UPDATE_TEST_INDEX(VAR) (test_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() if (!persistentStore.access_start()) { SERIAL_ECHO_MSG("No EEPROM."); return false; } \ int eeprom_index = EEPROM_OFFSET #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_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) - #if WORD_PADDED_EEPROM - int test_index; - #else - #define test_index eeprom_index - #endif #define _FIELD_TEST(FIELD) \ EEPROM_ASSERT( \ - eeprom_error || test_index == offsetof(SettingsData, FIELD) + EEPROM_OFFSET, \ + eeprom_error || eeprom_index == offsetof(SettingsData, FIELD) + EEPROM_OFFSET, \ "Field " STRINGIFY(FIELD) " mismatch." \ ) #else diff --git a/Marlin/src/pins/stm32/pins_FYSETC_CHEETAH.h b/Marlin/src/pins/stm32/pins_FYSETC_CHEETAH.h index e3aac9e84a..b40507f43c 100644 --- a/Marlin/src/pins/stm32/pins_FYSETC_CHEETAH.h +++ b/Marlin/src/pins/stm32/pins_FYSETC_CHEETAH.h @@ -35,6 +35,12 @@ #define DISABLE_JTAG +#define FLASH_EEPROM_EMULATION +#define EEPROM_PAGE_SIZE uint16(0x800) // 2KB +#define EEPROM_START_ADDRESS uint32(0x8000000 + 256 * 1024 - 2 * EEPROM_PAGE_SIZE) +#undef E2END +#define E2END (EEPROM_PAGE_SIZE - 1) // 2KB + // // Servos //