Skip to content

Commit 9c5086e

Browse files
trouchthinkyhead
authored andcommitted
[STM32F1] Simpler Flash EEPROM (MarlinFirmware#14829)
1 parent 0745d48 commit 9c5086e

File tree

3 files changed

+21
-48
lines changed

3 files changed

+21
-48
lines changed

Marlin/src/HAL/HAL_STM32F1/persistent_store_flash.cpp

+10-26
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
// Store settings in the last two pages
4343
// Flash pages must be erased before writing, so keep track.
4444
bool firstWrite = false;
45-
uint32_t pageBase = EEPROM_START_ADDRESS;
4645

4746
bool PersistentStore::access_start() {
4847
firstWrite = true;
@@ -67,42 +66,27 @@ bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, ui
6766
firstWrite = false;
6867
}
6968

70-
// First write full words
71-
int i = 0;
72-
int wordsToWrite = size / sizeof(uint16_t);
73-
uint16_t* wordBuffer = (uint16_t *)value;
74-
while (wordsToWrite) {
75-
status = FLASH_ProgramHalfWord(pageBase + pos + (i * 2), wordBuffer[i]);
76-
if (status != FLASH_COMPLETE) return true;
77-
wordsToWrite--;
78-
i++;
79-
}
80-
81-
// Now, write any remaining single byte
82-
const uint16_t odd = size & 1;
83-
if (odd) {
84-
uint16_t temp = value[size - 1];
85-
status = FLASH_ProgramHalfWord(pageBase + pos + i, temp);
86-
if (status != FLASH_COMPLETE) return true;
69+
for (size_t i = 0; i < size; i++) {
70+
if (FLASH_ProgramHalfWord(EEPROM_PAGE0_BASE + (pos + i) * 2, value[i]) != FLASH_COMPLETE)
71+
return true;
8772
}
8873

8974
crc16(crc, value, size);
90-
pos += size + odd;
75+
pos += size;
9176
return false;
9277
}
9378

9479
bool PersistentStore::read_data(int &pos, uint8_t* value, const size_t size, uint16_t *crc, const bool writing/*=true*/) {
95-
for (uint16_t i = 0; i < size; i++) {
96-
byte* accessPoint = (byte*)(pageBase + pos + i);
97-
uint8_t c = *accessPoint;
98-
if (writing) value[i] = c;
99-
crc16(crc, &c, 1);
80+
for (size_t i = 0; i < size; i++) {
81+
uint8_t v = *(uint16_t *)(EEPROM_PAGE0_BASE + (pos + i) * 2);
82+
if (writing) value[i] = v;
83+
crc16(crc, &v, 1);
10084
}
101-
pos += ((size + 1) & ~1); // i.e., size+(size&1), round up odd values
85+
pos += size;
10286
return false;
10387
}
10488

105-
size_t PersistentStore::capacity() { return E2END + 1; }
89+
size_t PersistentStore::capacity() { return size_t(E2END + 1); }
10690

10791
#endif // EEPROM_SETTINGS && EEPROM FLASH
10892
#endif // __STM32F1__

Marlin/src/module/configuration_store.cpp

+5-22
Original file line numberDiff line numberDiff line change
@@ -447,36 +447,19 @@ void MarlinSettings::postprocess() {
447447

448448
#if ENABLED(EEPROM_SETTINGS)
449449

450-
#define WORD_PADDED_EEPROM ENABLED(__STM32F1__, FLASH_EEPROM_EMULATION)
451-
452-
#if WORD_PADDED_EEPROM && ENABLED(DEBUG_EEPROM_READWRITE)
453-
#define UPDATE_TEST_INDEX(VAR) (test_index += sizeof(VAR))
454-
#else
455-
#define UPDATE_TEST_INDEX(VAR) NOOP
456-
#endif
457-
#if WORD_PADDED_EEPROM
458-
#define EEPROM_SKIP(VAR) do{ eeprom_index += sizeof(VAR) + (sizeof(VAR) & 1); UPDATE_TEST_INDEX(sizeof(VAR)); }while(0)
459-
#else
460-
#define EEPROM_SKIP(VAR) (eeprom_index += sizeof(VAR))
461-
#endif
462-
463450
#define EEPROM_START() if (!persistentStore.access_start()) { SERIAL_ECHO_MSG("No EEPROM."); return false; } \
464451
int eeprom_index = EEPROM_OFFSET
465452
#define EEPROM_FINISH() persistentStore.access_finish()
466-
#define EEPROM_WRITE(VAR) do{ persistentStore.write_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc); UPDATE_TEST_INDEX(VAR); }while(0)
467-
#define EEPROM_READ(VAR) do{ persistentStore.read_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc, !validating); UPDATE_TEST_INDEX(VAR); }while(0)
468-
#define EEPROM_READ_ALWAYS(VAR) do{ persistentStore.read_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc); UPDATE_TEST_INDEX(VAR); }while(0)
453+
#define EEPROM_SKIP(VAR) (eeprom_index += sizeof(VAR))
454+
#define EEPROM_WRITE(VAR) do{ persistentStore.write_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc); }while(0)
455+
#define EEPROM_READ(VAR) do{ persistentStore.read_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc, !validating); }while(0)
456+
#define EEPROM_READ_ALWAYS(VAR) do{ persistentStore.read_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc); }while(0)
469457
#define EEPROM_ASSERT(TST,ERR) do{ if (!(TST)) { SERIAL_ERROR_MSG(ERR); eeprom_error = true; } }while(0)
470458

471459
#if ENABLED(DEBUG_EEPROM_READWRITE)
472-
#if WORD_PADDED_EEPROM
473-
int test_index;
474-
#else
475-
#define test_index eeprom_index
476-
#endif
477460
#define _FIELD_TEST(FIELD) \
478461
EEPROM_ASSERT( \
479-
eeprom_error || test_index == offsetof(SettingsData, FIELD) + EEPROM_OFFSET, \
462+
eeprom_error || eeprom_index == offsetof(SettingsData, FIELD) + EEPROM_OFFSET, \
480463
"Field " STRINGIFY(FIELD) " mismatch." \
481464
)
482465
#else

Marlin/src/pins/stm32/pins_FYSETC_CHEETAH.h

+6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@
3535

3636
#define DISABLE_JTAG
3737

38+
#define FLASH_EEPROM_EMULATION
39+
#define EEPROM_PAGE_SIZE uint16(0x800) // 2KB
40+
#define EEPROM_START_ADDRESS uint32(0x8000000 + 256 * 1024 - 2 * EEPROM_PAGE_SIZE)
41+
#undef E2END
42+
#define E2END (EEPROM_PAGE_SIZE - 1) // 2KB
43+
3844
//
3945
// Servos
4046
//

0 commit comments

Comments
 (0)