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
     // sector is blank so nothing stored yet
74
     // sector is blank so nothing stored yet
75
     for (int i = 0; i < EEPROM_SIZE; i++) ram_eeprom[i] = EEPROM_ERASE;
75
     for (int i = 0; i < EEPROM_SIZE; i++) ram_eeprom[i] = EEPROM_ERASE;
76
     current_slot = EEPROM_SLOTS;
76
     current_slot = EEPROM_SLOTS;
77
-  } else {
77
+  }
78
+  else {
78
     // current slot is the first non blank one
79
     // current slot is the first non blank one
79
     current_slot = first_nblank_loc / EEPROM_SIZE;
80
     current_slot = first_nblank_loc / EEPROM_SIZE;
80
     uint8_t *eeprom_data = SLOT_ADDRESS(EEPROM_SECTOR, current_slot);
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
   }
79
   }
80
 
80
 
81
   // Now, write any remaining single byte
81
   // Now, write any remaining single byte
82
-  if (size & 1) {
82
+  const uint16_t odd = size & 1;
83
+  if (odd) {
83
     uint16_t temp = value[size - 1];
84
     uint16_t temp = value[size - 1];
84
     status = FLASH_ProgramHalfWord(pageBase + pos + i, temp);
85
     status = FLASH_ProgramHalfWord(pageBase + pos + i, temp);
85
     if (status != FLASH_COMPLETE) return true;
86
     if (status != FLASH_COMPLETE) return true;
86
   }
87
   }
87
 
88
 
88
   crc16(crc, value, size);
89
   crc16(crc, value, size);
89
-  pos += ((size + 1) & ~1);
90
+  pos += size + odd;
90
   return false;
91
   return false;
91
 }
92
 }
92
 
93
 
97
     if (writing) value[i] = c;
98
     if (writing) value[i] = c;
98
     crc16(crc, &c, 1);
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
   return false;
102
   return false;
102
 }
103
 }
103
 
104
 

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

384
 
384
 
385
 #if ENABLED(EEPROM_SETTINGS)
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
   #if ENABLED(DEBUG_EEPROM_READWRITE)
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
     #define _FIELD_TEST(FIELD) \
413
     #define _FIELD_TEST(FIELD) \
397
       EEPROM_ASSERT( \
414
       EEPROM_ASSERT( \
398
-        eeprom_error || eeprom_index == offsetof(SettingsData, FIELD) + EEPROM_OFFSET, \
415
+        eeprom_error || test_index == offsetof(SettingsData, FIELD) + EEPROM_OFFSET, \
399
         "Field " STRINGIFY(FIELD) " mismatch." \
416
         "Field " STRINGIFY(FIELD) " mismatch." \
400
       )
417
       )
401
   #else
418
   #else

Loading…
Cancel
Save