Skip to content

Commit

Permalink
[STM32F1] Simpler Flash EEPROM (#14829)
Browse files Browse the repository at this point in the history
  • Loading branch information
trouch authored and thinkyhead committed Aug 11, 2019
1 parent 0745d48 commit 9c5086e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 48 deletions.
36 changes: 10 additions & 26 deletions Marlin/src/HAL/HAL_STM32F1/persistent_store_flash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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__
27 changes: 5 additions & 22 deletions Marlin/src/module/configuration_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions Marlin/src/pins/stm32/pins_FYSETC_CHEETAH.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down

0 comments on commit 9c5086e

Please sign in to comment.