Handle word-padded flash-based eeprom (STM32F1)

Fix #13445
This commit is contained in:
Scott Lahteine 2019-03-24 19:10:33 -05:00
parent 32332bcd03
commit 380c771988
3 changed files with 31 additions and 12 deletions

View file

@ -74,7 +74,8 @@ bool PersistentStore::access_start() {
// sector is blank so nothing stored yet // 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 < EEPROM_SIZE; i++) ram_eeprom[i] = EEPROM_ERASE;
current_slot = EEPROM_SLOTS; current_slot = EEPROM_SLOTS;
} else { }
else {
// current slot is the first non blank one // current slot is the first non blank one
current_slot = first_nblank_loc / EEPROM_SIZE; current_slot = first_nblank_loc / EEPROM_SIZE;
uint8_t *eeprom_data = SLOT_ADDRESS(EEPROM_SECTOR, current_slot); uint8_t *eeprom_data = SLOT_ADDRESS(EEPROM_SECTOR, current_slot);

View file

@ -79,14 +79,15 @@ bool PersistentStore::write_data(int &pos, const uint8_t *value, const size_t si
} }
// Now, write any remaining single byte // Now, write any remaining single byte
if (size & 1) { const uint16_t odd = size & 1;
if (odd) {
uint16_t temp = value[size - 1]; uint16_t temp = value[size - 1];
status = FLASH_ProgramHalfWord(pageBase + pos + i, temp); status = FLASH_ProgramHalfWord(pageBase + pos + i, temp);
if (status != FLASH_COMPLETE) return true; if (status != FLASH_COMPLETE) return true;
} }
crc16(crc, value, size); crc16(crc, value, size);
pos += ((size + 1) & ~1); pos += size + odd;
return false; 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; if (writing) value[i] = c;
crc16(crc, &c, 1); crc16(crc, &c, 1);
} }
pos += ((size + 1) & ~1); pos += ((size + 1) & ~1); // i.e., size+(size&1), round up odd values
return false; return false;
} }

View file

@ -384,18 +384,35 @@ void MarlinSettings::postprocess() {
#if ENABLED(EEPROM_SETTINGS) #if ENABLED(EEPROM_SETTINGS)
#define EEPROM_START() int eeprom_index = EEPROM_OFFSET; persistentStore.access_start() #define WORD_PADDED_EEPROM ENABLED(__STM32F1__, FLASH_EEPROM_EMULATION)
#define EEPROM_FINISH() persistentStore.access_finish()
#define EEPROM_SKIP(VAR) eeprom_index += sizeof(VAR) #if WORD_PADDED_EEPROM && ENABLED(DEBUG_EEPROM_READWRITE)
#define EEPROM_WRITE(VAR) persistentStore.write_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc) #define UPDATE_TEST_INDEX(VAR) (text_index += sizeof(VAR))
#define EEPROM_READ(VAR) persistentStore.read_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc, !validating) #else
#define EEPROM_READ_ALWAYS(VAR) persistentStore.read_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc) #define UPDATE_TEST_INDEX(VAR) NOOP
#define EEPROM_ASSERT(TST,ERR) do{ if (!(TST)) { SERIAL_ERROR_MSG(ERR); eeprom_error = true; } }while(0) #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 ENABLED(DEBUG_EEPROM_READWRITE)
#if WORD_PADDED_EEPROM
int test_index;
#else
int &test_index = eeprom_index;
#endif
#define _FIELD_TEST(FIELD) \ #define _FIELD_TEST(FIELD) \
EEPROM_ASSERT( \ EEPROM_ASSERT( \
eeprom_error || eeprom_index == offsetof(SettingsData, FIELD) + EEPROM_OFFSET, \ eeprom_error || test_index == offsetof(SettingsData, FIELD) + EEPROM_OFFSET, \
"Field " STRINGIFY(FIELD) " mismatch." \ "Field " STRINGIFY(FIELD) " mismatch." \
) )
#else #else