Browse Source

Only write to EEPROM when a byte changes

Scott Lahteine 8 years ago
parent
commit
53c9fca0f6
1 changed files with 18 additions and 7 deletions
  1. 18
    7
      Marlin/configuration_store.cpp

+ 18
- 7
Marlin/configuration_store.cpp View File

@@ -135,16 +135,25 @@
135 135
 uint16_t eeprom_checksum;
136 136
 const char version[4] = EEPROM_VERSION;
137 137
 
138
+bool eeprom_write_error;
139
+
138 140
 void _EEPROM_writeData(int &pos, uint8_t* value, uint8_t size) {
139
-  uint8_t c;
141
+  if (eeprom_write_error) return;
140 142
   while (size--) {
141
-    eeprom_write_byte((unsigned char*)pos, *value);
142
-    c = eeprom_read_byte((unsigned char*)pos);
143
-    if (c != *value) {
144
-      SERIAL_ECHO_START;
145
-      SERIAL_ECHOLNPGM(MSG_ERR_EEPROM_WRITE);
143
+    uint8_t * const p = (uint8_t * const)pos;
144
+    const uint8_t v = *value;
145
+    // EEPROM has only ~100,000 write cycles,
146
+    // so only write bytes that have changed!
147
+    if (v != eeprom_read_byte(p)) {
148
+      eeprom_write_byte(p, v);
149
+      if (eeprom_read_byte(p) != v) {
150
+        SERIAL_ECHO_START;
151
+        SERIAL_ECHOLNPGM(MSG_ERR_EEPROM_WRITE);
152
+        eeprom_write_error = true;
153
+        return;
154
+      }
146 155
     }
147
-    eeprom_checksum += c;
156
+    eeprom_checksum += v;
148 157
     pos++;
149 158
     value++;
150 159
   };
@@ -203,6 +212,8 @@ void Config_Postprocess() {
203 212
 
204 213
     EEPROM_START();
205 214
 
215
+    eeprom_write_error = false;
216
+
206 217
     EEPROM_WRITE(ver);     // invalidate data first
207 218
     EEPROM_SKIP(eeprom_checksum); // Skip the checksum slot
208 219
 

Loading…
Cancel
Save