[STM32F1] Simpler Flash EEPROM (#14829)

This commit is contained in:
Eric Ptak 2019-08-11 02:14:31 +02:00 committed by Scott Lahteine
parent 0745d48993
commit 9c5086e6af
3 changed files with 21 additions and 48 deletions

View file

@ -42,7 +42,6 @@
// Store settings in the last two pages // Store settings in the last two pages
// Flash pages must be erased before writing, so keep track. // Flash pages must be erased before writing, so keep track.
bool firstWrite = false; bool firstWrite = false;
uint32_t pageBase = EEPROM_START_ADDRESS;
bool PersistentStore::access_start() { bool PersistentStore::access_start() {
firstWrite = true; firstWrite = true;
@ -67,42 +66,27 @@ bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, ui
firstWrite = false; firstWrite = false;
} }
// First write full words for (size_t i = 0; i < size; i++) {
int i = 0; if (FLASH_ProgramHalfWord(EEPROM_PAGE0_BASE + (pos + i) * 2, value[i]) != FLASH_COMPLETE)
int wordsToWrite = size / sizeof(uint16_t); return true;
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;
} }
crc16(crc, value, size); crc16(crc, value, size);
pos += size + odd; pos += size;
return false; return false;
} }
bool PersistentStore::read_data(int &pos, uint8_t* value, const size_t size, uint16_t *crc, const bool writing/*=true*/) { 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++) { for (size_t i = 0; i < size; i++) {
byte* accessPoint = (byte*)(pageBase + pos + i); uint8_t v = *(uint16_t *)(EEPROM_PAGE0_BASE + (pos + i) * 2);
uint8_t c = *accessPoint; if (writing) value[i] = v;
if (writing) value[i] = c; crc16(crc, &v, 1);
crc16(crc, &c, 1);
} }
pos += ((size + 1) & ~1); // i.e., size+(size&1), round up odd values pos += size;
return false; return false;
} }
size_t PersistentStore::capacity() { return E2END + 1; } size_t PersistentStore::capacity() { return size_t(E2END + 1); }
#endif // EEPROM_SETTINGS && EEPROM FLASH #endif // EEPROM_SETTINGS && EEPROM FLASH
#endif // __STM32F1__ #endif // __STM32F1__

View file

@ -447,36 +447,19 @@ void MarlinSettings::postprocess() {
#if ENABLED(EEPROM_SETTINGS) #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; } \ #define EEPROM_START() if (!persistentStore.access_start()) { SERIAL_ECHO_MSG("No EEPROM."); return false; } \
int eeprom_index = EEPROM_OFFSET int eeprom_index = EEPROM_OFFSET
#define EEPROM_FINISH() persistentStore.access_finish() #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_SKIP(VAR) (eeprom_index += sizeof(VAR))
#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_WRITE(VAR) do{ persistentStore.write_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc); }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_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) #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
#define test_index eeprom_index
#endif
#define _FIELD_TEST(FIELD) \ #define _FIELD_TEST(FIELD) \
EEPROM_ASSERT( \ EEPROM_ASSERT( \
eeprom_error || test_index == offsetof(SettingsData, FIELD) + EEPROM_OFFSET, \ eeprom_error || eeprom_index == offsetof(SettingsData, FIELD) + EEPROM_OFFSET, \
"Field " STRINGIFY(FIELD) " mismatch." \ "Field " STRINGIFY(FIELD) " mismatch." \
) )
#else #else

View file

@ -35,6 +35,12 @@
#define DISABLE_JTAG #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 // Servos
// //