Browse Source

Merge pull request #4167 from thinkyhead/rc_eeprom_checksum

EEPROM Checksum
Scott Lahteine 9 years ago
parent
commit
ac4f235912
1 changed files with 132 additions and 104 deletions
  1. 132
    104
      Marlin/configuration_store.cpp

+ 132
- 104
Marlin/configuration_store.cpp View File

@@ -36,88 +36,93 @@
36 36
  *
37 37
  */
38 38
 
39
-#define EEPROM_VERSION "V23"
39
+#define EEPROM_VERSION "V24"
40
+
41
+// Change EEPROM version if these are changed:
42
+#define EEPROM_OFFSET 100
43
+#define MAX_EXTRUDERS 4
40 44
 
41 45
 /**
42
- * V23 EEPROM Layout:
46
+ * V24 EEPROM Layout:
43 47
  *
44 48
  *  100  Version (char x4)
49
+ *  104  EEPROM Checksum (uint16_t)
45 50
  *
46
- *  104  M92 XYZE  planner.axis_steps_per_mm (float x4)
47
- *  120  M203 XYZE planner.max_feedrate (float x4)
48
- *  136  M201 XYZE planner.max_acceleration_mm_per_s2 (uint32_t x4)
49
- *  152  M204 P    planner.acceleration (float)
50
- *  156  M204 R    planner.retract_acceleration (float)
51
- *  160  M204 T    planner.travel_acceleration (float)
52
- *  164  M205 S    planner.min_feedrate (float)
53
- *  168  M205 T    planner.min_travel_feedrate (float)
54
- *  172  M205 B    planner.min_segment_time (ulong)
55
- *  176  M205 X    planner.max_xy_jerk (float)
56
- *  180  M205 Z    planner.max_z_jerk (float)
57
- *  184  M205 E    planner.max_e_jerk (float)
58
- *  188  M206 XYZ  home_offset (float x3)
51
+ *  106  M92 XYZE  planner.axis_steps_per_mm (float x4)
52
+ *  122  M203 XYZE planner.max_feedrate (float x4)
53
+ *  138  M201 XYZE planner.max_acceleration_mm_per_s2 (uint32_t x4)
54
+ *  154  M204 P    planner.acceleration (float)
55
+ *  158  M204 R    planner.retract_acceleration (float)
56
+ *  162  M204 T    planner.travel_acceleration (float)
57
+ *  166  M205 S    planner.min_feedrate (float)
58
+ *  170  M205 T    planner.min_travel_feedrate (float)
59
+ *  174  M205 B    planner.min_segment_time (ulong)
60
+ *  178  M205 X    planner.max_xy_jerk (float)
61
+ *  182  M205 Z    planner.max_z_jerk (float)
62
+ *  186  M205 E    planner.max_e_jerk (float)
63
+ *  190  M206 XYZ  home_offset (float x3)
59 64
  *
60 65
  * Mesh bed leveling:
61
- *  200  M420 S    status (uint8)
62
- *  201            z_offset (float)
63
- *  205            mesh_num_x (uint8 as set in firmware)
64
- *  206            mesh_num_y (uint8 as set in firmware)
65
- *  207 G29 S3 XYZ z_values[][] (float x9, by default)
66
+ *  202  M420 S    status (uint8)
67
+ *  203            z_offset (float)
68
+ *  207            mesh_num_x (uint8 as set in firmware)
69
+ *  208            mesh_num_y (uint8 as set in firmware)
70
+ *  209 G29 S3 XYZ z_values[][] (float x9, by default)
66 71
  *
67 72
  * AUTO BED LEVELING
68
- *  243  M851      zprobe_zoffset (float)
73
+ *  245  M851      zprobe_zoffset (float)
69 74
  *
70 75
  * DELTA:
71
- *  247  M666 XYZ  endstop_adj (float x3)
72
- *  259  M665 R    delta_radius (float)
73
- *  263  M665 L    delta_diagonal_rod (float)
74
- *  267  M665 S    delta_segments_per_second (float)
75
- *  271  M665 A    delta_diagonal_rod_trim_tower_1 (float)
76
- *  275  M665 B    delta_diagonal_rod_trim_tower_2 (float)
77
- *  279  M665 C    delta_diagonal_rod_trim_tower_3 (float)
76
+ *  249  M666 XYZ  endstop_adj (float x3)
77
+ *  261  M665 R    delta_radius (float)
78
+ *  265  M665 L    delta_diagonal_rod (float)
79
+ *  269  M665 S    delta_segments_per_second (float)
80
+ *  273  M665 A    delta_diagonal_rod_trim_tower_1 (float)
81
+ *  277  M665 B    delta_diagonal_rod_trim_tower_2 (float)
82
+ *  281  M665 C    delta_diagonal_rod_trim_tower_3 (float)
78 83
  *
79 84
  * Z_DUAL_ENDSTOPS:
80
- *  283  M666 Z    z_endstop_adj (float)
85
+ *  285  M666 Z    z_endstop_adj (float)
81 86
  *
82 87
  * ULTIPANEL:
83
- *  287  M145 S0 H plaPreheatHotendTemp (int)
84
- *  289  M145 S0 B plaPreheatHPBTemp (int)
85
- *  291  M145 S0 F plaPreheatFanSpeed (int)
86
- *  293  M145 S1 H absPreheatHotendTemp (int)
87
- *  295  M145 S1 B absPreheatHPBTemp (int)
88
- *  297  M145 S1 F absPreheatFanSpeed (int)
88
+ *  289  M145 S0 H plaPreheatHotendTemp (int)
89
+ *  291  M145 S0 B plaPreheatHPBTemp (int)
90
+ *  293  M145 S0 F plaPreheatFanSpeed (int)
91
+ *  295  M145 S1 H absPreheatHotendTemp (int)
92
+ *  297  M145 S1 B absPreheatHPBTemp (int)
93
+ *  299  M145 S1 F absPreheatFanSpeed (int)
89 94
  *
90 95
  * PIDTEMP:
91
- *  299  M301 E0 PIDC  Kp[0], Ki[0], Kd[0], Kc[0] (float x4)
92
- *  315  M301 E1 PIDC  Kp[1], Ki[1], Kd[1], Kc[1] (float x4)
93
- *  331  M301 E2 PIDC  Kp[2], Ki[2], Kd[2], Kc[2] (float x4)
94
- *  347  M301 E3 PIDC  Kp[3], Ki[3], Kd[3], Kc[3] (float x4)
95
- *  363  M301 L        lpq_len (int)
96
+ *  301  M301 E0 PIDC  Kp[0], Ki[0], Kd[0], Kc[0] (float x4)
97
+ *  317  M301 E1 PIDC  Kp[1], Ki[1], Kd[1], Kc[1] (float x4)
98
+ *  333  M301 E2 PIDC  Kp[2], Ki[2], Kd[2], Kc[2] (float x4)
99
+ *  349  M301 E3 PIDC  Kp[3], Ki[3], Kd[3], Kc[3] (float x4)
100
+ *  365  M301 L        lpq_len (int)
96 101
  *
97 102
  * PIDTEMPBED:
98
- *  365  M304 PID  thermalManager.bedKp, thermalManager.bedKi, thermalManager.bedKd (float x3)
103
+ *  367  M304 PID  thermalManager.bedKp, thermalManager.bedKi, thermalManager.bedKd (float x3)
99 104
  *
100 105
  * DOGLCD:
101
- *  377  M250 C    lcd_contrast (int)
106
+ *  379  M250 C    lcd_contrast (int)
102 107
  *
103 108
  * SCARA:
104
- *  379  M365 XYZ  axis_scaling (float x3)
109
+ *  381  M365 XYZ  axis_scaling (float x3)
105 110
  *
106 111
  * FWRETRACT:
107
- *  391  M209 S    autoretract_enabled (bool)
108
- *  392  M207 S    retract_length (float)
109
- *  396  M207 W    retract_length_swap (float)
110
- *  400  M207 F    retract_feedrate_mm_s (float)
111
- *  404  M207 Z    retract_zlift (float)
112
- *  408  M208 S    retract_recover_length (float)
113
- *  412  M208 W    retract_recover_length_swap (float)
114
- *  416  M208 F    retract_recover_feedrate (float)
112
+ *  393  M209 S    autoretract_enabled (bool)
113
+ *  394  M207 S    retract_length (float)
114
+ *  398  M207 W    retract_length_swap (float)
115
+ *  402  M207 F    retract_feedrate_mm_s (float)
116
+ *  406  M207 Z    retract_zlift (float)
117
+ *  410  M208 S    retract_recover_length (float)
118
+ *  414  M208 W    retract_recover_length_swap (float)
119
+ *  418  M208 F    retract_recover_feedrate (float)
115 120
  *
116 121
  * Volumetric Extrusion:
117
- *  420  M200 D    volumetric_enabled (bool)
118
- *  421  M200 T D  filament_size (float x4) (T0..3)
122
+ *  422  M200 D    volumetric_enabled (bool)
123
+ *  423  M200 T D  filament_size (float x4) (T0..3)
119 124
  *
120
- *  437  This Slot is Available!
125
+ *  439  This Slot is Available!
121 126
  *
122 127
  */
123 128
 #include "Marlin.h"
@@ -131,6 +136,9 @@
131 136
   #include "mesh_bed_leveling.h"
132 137
 #endif
133 138
 
139
+uint16_t eeprom_checksum;
140
+const char version[4] = EEPROM_VERSION;
141
+
134 142
 void _EEPROM_writeData(int &pos, uint8_t* value, uint8_t size) {
135 143
   uint8_t c;
136 144
   while (size--) {
@@ -140,39 +148,58 @@ void _EEPROM_writeData(int &pos, uint8_t* value, uint8_t size) {
140 148
       SERIAL_ECHO_START;
141 149
       SERIAL_ECHOLNPGM(MSG_ERR_EEPROM_WRITE);
142 150
     }
151
+    eeprom_checksum += c;
143 152
     pos++;
144 153
     value++;
145 154
   };
146 155
 }
147 156
 void _EEPROM_readData(int &pos, uint8_t* value, uint8_t size) {
148 157
   do {
149
-    *value = eeprom_read_byte((unsigned char*)pos);
158
+    uint8_t c = eeprom_read_byte((unsigned char*)pos);
159
+    *value = c;
160
+    eeprom_checksum += c;
150 161
     pos++;
151 162
     value++;
152 163
   } while (--size);
153 164
 }
154
-#define EEPROM_WRITE_VAR(pos, value) _EEPROM_writeData(pos, (uint8_t*)&value, sizeof(value))
155
-#define EEPROM_READ_VAR(pos, value) _EEPROM_readData(pos, (uint8_t*)&value, sizeof(value))
156 165
 
157 166
 /**
158
- * Store Configuration Settings - M500
167
+ * Post-process after Retrieve or Reset
159 168
  */
169
+void Config_Postprocess() {
170
+  // steps per s2 needs to be updated to agree with units per s2
171
+  planner.reset_acceleration_rates();
160 172
 
161
-#define DUMMY_PID_VALUE 3000.0f
173
+  #if ENABLED(DELTA)
174
+    recalc_delta_settings(delta_radius, delta_diagonal_rod);
175
+  #endif
162 176
 
163
-#define EEPROM_OFFSET 100
177
+  #if ENABLED(PIDTEMP)
178
+    thermalManager.updatePID();
179
+  #endif
180
+
181
+  calculate_volumetric_multipliers();
182
+}
164 183
 
165 184
 #if ENABLED(EEPROM_SETTINGS)
166 185
 
186
+  #define DUMMY_PID_VALUE 3000.0f
187
+  #define EEPROM_WRITE_VAR(pos, value) _EEPROM_writeData(pos, (uint8_t*)&value, sizeof(value))
188
+  #define EEPROM_READ_VAR(pos, value) _EEPROM_readData(pos, (uint8_t*)&value, sizeof(value))
189
+
167 190
 /**
168
- * Store Configuration Settings - M500
191
+ * M500 - Store Configuration
169 192
  */
170
-
171 193
 void Config_StoreSettings()  {
172 194
   float dummy = 0.0f;
173 195
   char ver[4] = "000";
174 196
   int i = EEPROM_OFFSET;
175
-  EEPROM_WRITE_VAR(i, ver); // invalidate data first
197
+
198
+  EEPROM_WRITE_VAR(i, ver);     // invalidate data first
199
+  i += sizeof(eeprom_checksum); // Skip the checksum slot
200
+
201
+  eeprom_checksum = 0; // clear before first "real data"
202
+
176 203
   EEPROM_WRITE_VAR(i, planner.axis_steps_per_mm);
177 204
   EEPROM_WRITE_VAR(i, planner.max_feedrate);
178 205
   EEPROM_WRITE_VAR(i, planner.max_acceleration_mm_per_s2);
@@ -199,6 +226,7 @@ void Config_StoreSettings()  {
199 226
     EEPROM_WRITE_VAR(i, mesh_num_y);
200 227
     EEPROM_WRITE_VAR(i, mbl.z_values);
201 228
   #else
229
+    // For disabled MBL write a default mesh
202 230
     uint8_t mesh_num_x = 3,
203 231
             mesh_num_y = 3,
204 232
             dummy_uint8 = 0;
@@ -215,6 +243,7 @@ void Config_StoreSettings()  {
215 243
   #endif
216 244
   EEPROM_WRITE_VAR(i, zprobe_zoffset);
217 245
 
246
+  // 9 floats for DELTA / Z_DUAL_ENDSTOPS
218 247
   #if ENABLED(DELTA)
219 248
     EEPROM_WRITE_VAR(i, endstop_adj);               // 3 floats
220 249
     EEPROM_WRITE_VAR(i, delta_radius);              // 1 float
@@ -244,7 +273,7 @@ void Config_StoreSettings()  {
244 273
   EEPROM_WRITE_VAR(i, absPreheatHPBTemp);
245 274
   EEPROM_WRITE_VAR(i, absPreheatFanSpeed);
246 275
 
247
-  for (uint8_t e = 0; e < 4; e++) {
276
+  for (uint8_t e = 0; e < MAX_EXTRUDERS; e++) {
248 277
 
249 278
     #if ENABLED(PIDTEMP)
250 279
       if (e < HOTENDS) {
@@ -319,14 +348,16 @@ void Config_StoreSettings()  {
319 348
   EEPROM_WRITE_VAR(i, volumetric_enabled);
320 349
 
321 350
   // Save filament sizes
322
-  for (uint8_t q = 0; q < 4; q++) {
351
+  for (uint8_t q = 0; q < MAX_EXTRUDERS; q++) {
323 352
     if (q < EXTRUDERS) dummy = filament_size[q];
324 353
     EEPROM_WRITE_VAR(i, dummy);
325 354
   }
326 355
 
327
-  char ver2[4] = EEPROM_VERSION;
356
+  uint16_t final_checksum = eeprom_checksum;
357
+
328 358
   int j = EEPROM_OFFSET;
329
-  EEPROM_WRITE_VAR(j, ver2); // validate data
359
+  EEPROM_WRITE_VAR(j, version);
360
+  EEPROM_WRITE_VAR(j, final_checksum);
330 361
 
331 362
   // Report storage size
332 363
   SERIAL_ECHO_START;
@@ -335,33 +366,31 @@ void Config_StoreSettings()  {
335 366
 }
336 367
 
337 368
 /**
338
- * Retrieve Configuration Settings - M501
369
+ * M501 - Retrieve Configuration
339 370
  */
340
-
341 371
 void Config_RetrieveSettings() {
342
-
343 372
   int i = EEPROM_OFFSET;
344 373
   char stored_ver[4];
345
-  char ver[4] = EEPROM_VERSION;
346
-  EEPROM_READ_VAR(i, stored_ver); //read stored version
374
+  uint16_t stored_checksum;
375
+  EEPROM_READ_VAR(i, stored_ver);
376
+  EEPROM_READ_VAR(i, stored_checksum);
347 377
   //  SERIAL_ECHOPAIR("Version: [", ver);
348 378
   //  SERIAL_ECHOPAIR("] Stored version: [", stored_ver);
349 379
   //  SERIAL_ECHOLNPGM("]");
350 380
 
351
-  if (strncmp(ver, stored_ver, 3) != 0) {
381
+  if (strncmp(version, stored_ver, 3) != 0) {
352 382
     Config_ResetDefault();
353 383
   }
354 384
   else {
355 385
     float dummy = 0;
356 386
 
387
+    eeprom_checksum = 0; // clear before reading first "real data"
388
+
357 389
     // version number match
358 390
     EEPROM_READ_VAR(i, planner.axis_steps_per_mm);
359 391
     EEPROM_READ_VAR(i, planner.max_feedrate);
360 392
     EEPROM_READ_VAR(i, planner.max_acceleration_mm_per_s2);
361 393
 
362
-    // steps per sq second need to be updated to agree with the units per sq second (as they are what is used in the planner)
363
-    planner.reset_acceleration_rates();
364
-
365 394
     EEPROM_READ_VAR(i, planner.acceleration);
366 395
     EEPROM_READ_VAR(i, planner.retract_acceleration);
367 396
     EEPROM_READ_VAR(i, planner.travel_acceleration);
@@ -382,12 +411,16 @@ void Config_RetrieveSettings() {
382 411
       mbl.status = dummy_uint8;
383 412
       mbl.z_offset = dummy;
384 413
       if (mesh_num_x == MESH_NUM_X_POINTS && mesh_num_y == MESH_NUM_Y_POINTS) {
414
+        // EEPROM data fits the current mesh
385 415
         EEPROM_READ_VAR(i, mbl.z_values);
386
-      } else {
416
+      }
417
+      else {
418
+        // EEPROM data is stale
387 419
         mbl.reset();
388 420
         for (uint8_t q = 0; q < mesh_num_x * mesh_num_y; q++) EEPROM_READ_VAR(i, dummy);
389 421
       }
390 422
     #else
423
+      // MBL is disabled - skip the stored data
391 424
       for (uint8_t q = 0; q < mesh_num_x * mesh_num_y; q++) EEPROM_READ_VAR(i, dummy);
392 425
     #endif // MESH_BED_LEVELING
393 426
 
@@ -404,7 +437,6 @@ void Config_RetrieveSettings() {
404 437
       EEPROM_READ_VAR(i, delta_diagonal_rod_trim_tower_1);  // 1 float
405 438
       EEPROM_READ_VAR(i, delta_diagonal_rod_trim_tower_2);  // 1 float
406 439
       EEPROM_READ_VAR(i, delta_diagonal_rod_trim_tower_3);  // 1 float
407
-      recalc_delta_settings(delta_radius, delta_diagonal_rod);
408 440
     #elif ENABLED(Z_DUAL_ENDSTOPS)
409 441
       EEPROM_READ_VAR(i, z_endstop_adj);
410 442
       dummy = 0.0f;
@@ -427,7 +459,7 @@ void Config_RetrieveSettings() {
427 459
     EEPROM_READ_VAR(i, absPreheatFanSpeed);
428 460
 
429 461
     #if ENABLED(PIDTEMP)
430
-      for (uint8_t e = 0; e < 4; e++) { // 4 = max extruders currently supported by Marlin
462
+      for (uint8_t e = 0; e < MAX_EXTRUDERS; e++) {
431 463
         EEPROM_READ_VAR(i, dummy); // Kp
432 464
         if (e < HOTENDS && dummy != DUMMY_PID_VALUE) {
433 465
           // do not need to scale PID values as the values in EEPROM are already scaled
@@ -446,7 +478,7 @@ void Config_RetrieveSettings() {
446 478
       }
447 479
     #else // !PIDTEMP
448 480
       // 4 x 4 = 16 slots for PID parameters
449
-      for (uint8_t q=16; q--;) EEPROM_READ_VAR(i, dummy);  // 4x Kp, Ki, Kd, Kc
481
+      for (uint8_t q = MAX_EXTRUDERS * 4; q--;) EEPROM_READ_VAR(i, dummy);  // Kp, Ki, Kd, Kc
450 482
     #endif // !PIDTEMP
451 483
 
452 484
     #if DISABLED(PID_ADD_EXTRUSION_RATE)
@@ -497,21 +529,24 @@ void Config_RetrieveSettings() {
497 529
 
498 530
     EEPROM_READ_VAR(i, volumetric_enabled);
499 531
 
500
-    for (uint8_t q = 0; q < 4; q++) {
532
+    for (uint8_t q = 0; q < MAX_EXTRUDERS; q++) {
501 533
       EEPROM_READ_VAR(i, dummy);
502 534
       if (q < EXTRUDERS) filament_size[q] = dummy;
503 535
     }
504 536
 
505
-    calculate_volumetric_multipliers();
506
-    // Call thermalManager.updatePID (similar to when we have processed M301)
507
-    thermalManager.updatePID();
508
-
509
-    // Report settings retrieved and length
510
-    SERIAL_ECHO_START;
511
-    SERIAL_ECHO(ver);
512
-    SERIAL_ECHOPAIR(" stored settings retrieved (", i);
513
-    SERIAL_ECHOLNPGM(" bytes)");
514
-  }
537
+    if (eeprom_checksum == stored_checksum) {
538
+      Config_Postprocess();
539
+      SERIAL_ECHO_START;
540
+      SERIAL_ECHO(version);
541
+      SERIAL_ECHOPAIR(" stored settings retrieved (", i);
542
+      SERIAL_ECHOLNPGM(" bytes)");
543
+    }
544
+    else {
545
+      SERIAL_ERROR_START;
546
+      SERIAL_ERRORLNPGM("EEPROM checksum mismatch");
547
+      Config_ResetDefault();
548
+    }
549
+ }
515 550
 
516 551
   #if ENABLED(EEPROM_CHITCHAT)
517 552
     Config_PrintSettings();
@@ -521,9 +556,8 @@ void Config_RetrieveSettings() {
521 556
 #endif // EEPROM_SETTINGS
522 557
 
523 558
 /**
524
- * Reset Configuration Settings - M502
559
+ * M502 - Reset Configuration
525 560
  */
526
-
527 561
 void Config_ResetDefault() {
528 562
   float tmp1[] = DEFAULT_AXIS_STEPS_PER_UNIT;
529 563
   float tmp2[] = DEFAULT_MAX_FEEDRATE;
@@ -538,9 +572,6 @@ void Config_ResetDefault() {
538 572
     #endif
539 573
   }
540 574
 
541
-  // steps per sq second need to be updated to agree with the units per sq second
542
-  planner.reset_acceleration_rates();
543
-
544 575
   planner.acceleration = DEFAULT_ACCELERATION;
545 576
   planner.retract_acceleration = DEFAULT_RETRACT_ACCELERATION;
546 577
   planner.travel_acceleration = DEFAULT_TRAVEL_ACCELERATION;
@@ -568,7 +599,6 @@ void Config_ResetDefault() {
568 599
     delta_diagonal_rod_trim_tower_1 = DELTA_DIAGONAL_ROD_TRIM_TOWER_1;
569 600
     delta_diagonal_rod_trim_tower_2 = DELTA_DIAGONAL_ROD_TRIM_TOWER_2;
570 601
     delta_diagonal_rod_trim_tower_3 = DELTA_DIAGONAL_ROD_TRIM_TOWER_3;
571
-    recalc_delta_settings(delta_radius, delta_diagonal_rod);
572 602
   #elif ENABLED(Z_DUAL_ENDSTOPS)
573 603
     z_endstop_adj = 0;
574 604
   #endif
@@ -603,8 +633,6 @@ void Config_ResetDefault() {
603 633
     #if ENABLED(PID_ADD_EXTRUSION_RATE)
604 634
       lpq_len = 20; // default last-position-queue size
605 635
     #endif
606
-    // call thermalManager.updatePID (similar to when we have processed M301)
607
-    thermalManager.updatePID();
608 636
   #endif // PIDTEMP
609 637
 
610 638
   #if ENABLED(PIDTEMPBED)
@@ -631,7 +659,8 @@ void Config_ResetDefault() {
631 659
   volumetric_enabled = false;
632 660
   for (uint8_t q = 0; q < COUNT(filament_size); q++)
633 661
     filament_size[q] = DEFAULT_NOMINAL_FILAMENT_DIA;
634
-  calculate_volumetric_multipliers();
662
+
663
+  Config_Postprocess();
635 664
 
636 665
   SERIAL_ECHO_START;
637 666
   SERIAL_ECHOLNPGM("Hardcoded Default Settings Loaded");
@@ -639,12 +668,11 @@ void Config_ResetDefault() {
639 668
 
640 669
 #if DISABLED(DISABLE_M503)
641 670
 
642
-/**
643
- * Print Configuration Settings - M503
644
- */
645
-
646 671
 #define CONFIG_ECHO_START do{ if (!forReplay) SERIAL_ECHO_START; }while(0)
647 672
 
673
+/**
674
+ * M503 - Print Configuration
675
+ */
648 676
 void Config_PrintSettings(bool forReplay) {
649 677
   // Always have this function, even with EEPROM_SETTINGS disabled, the current values will be shown
650 678
 

Loading…
Cancel
Save