Browse Source

Handle word-padded flash-based eeprom (STM32F1)

Fix #13445
Scott Lahteine 6 years ago
parent
commit
380c771988

+ 2
- 1
Marlin/src/HAL/HAL_LPC1768/persistent_store_flash.cpp View File

@@ -74,7 +74,8 @@ bool PersistentStore::access_start() {
74 74
     // sector is blank so nothing stored yet
75 75
     for (int i = 0; i < EEPROM_SIZE; i++) ram_eeprom[i] = EEPROM_ERASE;
76 76
     current_slot = EEPROM_SLOTS;
77
-  } else {
77
+  }
78
+  else {
78 79
     // current slot is the first non blank one
79 80
     current_slot = first_nblank_loc / EEPROM_SIZE;
80 81
     uint8_t *eeprom_data = SLOT_ADDRESS(EEPROM_SECTOR, current_slot);

+ 4
- 3
Marlin/src/HAL/HAL_STM32F1/persistent_store_flash.cpp View File

@@ -79,14 +79,15 @@ bool PersistentStore::write_data(int &pos, const uint8_t *value, const size_t si
79 79
   }
80 80
 
81 81
   // Now, write any remaining single byte
82
-  if (size & 1) {
82
+  const uint16_t odd = size & 1;
83
+  if (odd) {
83 84
     uint16_t temp = value[size - 1];
84 85
     status = FLASH_ProgramHalfWord(pageBase + pos + i, temp);
85 86
     if (status != FLASH_COMPLETE) return true;
86 87
   }
87 88
 
88 89
   crc16(crc, value, size);
89
-  pos += ((size + 1) & ~1);
90
+  pos += size + odd;
90 91
   return false;
91 92
 }
92 93
 
@@ -97,7 +98,7 @@ bool PersistentStore::read_data(int &pos, uint8_t* value, const size_t size, uin
97 98
     if (writing) value[i] = c;
98 99
     crc16(crc, &c, 1);
99 100
   }
100
-  pos += ((size + 1) & ~1);
101
+  pos += ((size + 1) & ~1); // i.e., size+(size&1), round up odd values
101 102
   return false;
102 103
 }
103 104
 

+ 25
- 8
Marlin/src/module/configuration_store.cpp View File

@@ -384,18 +384,35 @@ void MarlinSettings::postprocess() {
384 384
 
385 385
 #if ENABLED(EEPROM_SETTINGS)
386 386
 
387
-  #define EEPROM_START() int eeprom_index = EEPROM_OFFSET; persistentStore.access_start()
388
-  #define EEPROM_FINISH() persistentStore.access_finish()
389
-  #define EEPROM_SKIP(VAR) eeprom_index += sizeof(VAR)
390
-  #define EEPROM_WRITE(VAR) persistentStore.write_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc)
391
-  #define EEPROM_READ(VAR) persistentStore.read_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc, !validating)
392
-  #define EEPROM_READ_ALWAYS(VAR) persistentStore.read_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc)
393
-  #define EEPROM_ASSERT(TST,ERR) do{ if (!(TST)) { SERIAL_ERROR_MSG(ERR); eeprom_error = true; } }while(0)
387
+  #define WORD_PADDED_EEPROM ENABLED(__STM32F1__, FLASH_EEPROM_EMULATION)
388
+
389
+  #if WORD_PADDED_EEPROM && ENABLED(DEBUG_EEPROM_READWRITE)
390
+    #define UPDATE_TEST_INDEX(VAR) (text_index += sizeof(VAR))
391
+  #else
392
+    #define UPDATE_TEST_INDEX(VAR) NOOP
393
+  #endif
394
+  #if WORD_PADDED_EEPROM
395
+    #define EEPROM_SKIP(VAR) do{ eeprom_index += sizeof(VAR) + (sizeof(VAR) & 1); UPDATE_TEST_INDEX(sizeof(VAR)); }while(0)
396
+  #else
397
+    #define EEPROM_SKIP(VAR) (eeprom_index += sizeof(VAR))
398
+  #endif
399
+
400
+  #define EEPROM_START()          int eeprom_index = EEPROM_OFFSET; persistentStore.access_start()
401
+  #define EEPROM_FINISH()         persistentStore.access_finish()
402
+  #define EEPROM_WRITE(VAR)       do{ persistentStore.write_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc);              UPDATE_TEST_INDEX(VAR); }while(0)
403
+  #define EEPROM_READ(VAR)        do{ persistentStore.read_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc, !validating);  UPDATE_TEST_INDEX(VAR); }while(0)
404
+  #define EEPROM_READ_ALWAYS(VAR) do{ persistentStore.read_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc);               UPDATE_TEST_INDEX(VAR); }while(0)
405
+  #define EEPROM_ASSERT(TST,ERR)  do{ if (!(TST)) { SERIAL_ERROR_MSG(ERR); eeprom_error = true; } }while(0)
394 406
 
395 407
   #if ENABLED(DEBUG_EEPROM_READWRITE)
408
+    #if WORD_PADDED_EEPROM
409
+      int test_index;
410
+    #else
411
+      int &test_index = eeprom_index;
412
+    #endif
396 413
     #define _FIELD_TEST(FIELD) \
397 414
       EEPROM_ASSERT( \
398
-        eeprom_error || eeprom_index == offsetof(SettingsData, FIELD) + EEPROM_OFFSET, \
415
+        eeprom_error || test_index == offsetof(SettingsData, FIELD) + EEPROM_OFFSET, \
399 416
         "Field " STRINGIFY(FIELD) " mismatch." \
400 417
       )
401 418
   #else

Loading…
Cancel
Save