-
Notifications
You must be signed in to change notification settings - Fork 19
/
configuration_store.cpp
1920 lines (1695 loc) · 60.2 KB
/
configuration_store.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Marlin 3D Printer Firmware
* Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* configuration_store.cpp
*
* Settings and EEPROM storage
*
* IMPORTANT: Whenever there are changes made to the variables stored in EEPROM
* in the functions below, also increment the version number. This makes sure that
* the default values are used whenever there is a change to the data, to prevent
* wrong data being written to the variables.
*
* ALSO: Variables in the Store and Retrieve sections must be in the same order.
* If a feature is disabled, some data must still be written that, when read,
* either sets a Sane Default, or results in No Change to the existing value.
*
*/
#define EEPROM_VERSION "V40"
// Change EEPROM version if these are changed:
#define EEPROM_OFFSET 100
/**
* V39 EEPROM Layout:
*
* 100 Version (char x4)
* 104 EEPROM CRC16 (uint16_t)
*
* 106 E_STEPPERS (uint8_t)
* 107 M92 XYZE planner.axis_steps_per_mm (float x4 ... x8)
* 123 M203 XYZE planner.max_feedrate_mm_s (float x4 ... x8)
* 139 M201 XYZE planner.max_acceleration_mm_per_s2 (uint32_t x4 ... x8)
* 155 M204 P planner.acceleration (float)
* 159 M204 R planner.retract_acceleration (float)
* 163 M204 T planner.travel_acceleration (float)
* 167 M205 S planner.min_feedrate_mm_s (float)
* 171 M205 T planner.min_travel_feedrate_mm_s (float)
* 175 M205 B planner.min_segment_time (ulong)
* 179 M205 X planner.max_jerk[X_AXIS] (float)
* 183 M205 Y planner.max_jerk[Y_AXIS] (float)
* 187 M205 Z planner.max_jerk[Z_AXIS] (float)
* 191 M205 E planner.max_jerk[E_AXIS] (float)
* 195 M206 XYZ home_offset (float x3)
* 207 M218 XYZ hotend_offset (float x3 per additional hotend)
*
* Global Leveling:
* 219 z_fade_height (float)
*
* MESH_BED_LEVELING: 43 bytes
* 223 M420 S from mbl.status (bool)
* 224 mbl.z_offset (float)
* 228 GRID_MAX_POINTS_X (uint8_t)
* 229 GRID_MAX_POINTS_Y (uint8_t)
* 230 G29 S3 XYZ z_values[][] (float x9, up to float x81) +288
*
* HAS_BED_PROBE: 4 bytes
* 266 M851 zprobe_zoffset (float)
*
* ABL_PLANAR: 36 bytes
* 270 planner.bed_level_matrix (matrix_3x3 = float x9)
*
* AUTO_BED_LEVELING_BILINEAR: 47 bytes
* 306 GRID_MAX_POINTS_X (uint8_t)
* 307 GRID_MAX_POINTS_Y (uint8_t)
* 308 bilinear_grid_spacing (int x2)
* 312 G29 L F bilinear_start (int x2)
* 316 z_values[][] (float x9, up to float x256) +988
*
* AUTO_BED_LEVELING_UBL: 6 bytes
* 324 G29 A ubl.state.active (bool)
* 325 G29 Z ubl.state.z_offset (float)
* 329 G29 S ubl.state.storage_slot (int8_t)
*
* DELTA: 48 bytes
* 348 M666 XYZ endstop_adj (float x3)
* 360 M665 R delta_radius (float)
* 364 M665 L delta_diagonal_rod (float)
* 368 M665 S delta_segments_per_second (float)
* 372 M665 B delta_calibration_radius (float)
* 376 M665 X delta_tower_angle_trim[A] (float)
* 380 M665 Y delta_tower_angle_trim[B] (float)
* --- M665 Z delta_tower_angle_trim[C] (float) is always 0.0
*
* Z_DUAL_ENDSTOPS: 48 bytes
* 348 M666 Z z_endstop_adj (float)
* --- dummy data (float x11)
*
* ULTIPANEL: 6 bytes
* 396 M145 S0 H lcd_preheat_hotend_temp (int x2)
* 400 M145 S0 B lcd_preheat_bed_temp (int x2)
* 404 M145 S0 F lcd_preheat_fan_speed (int x2)
*
* PIDTEMP: 66 bytes
* 408 M301 E0 PIDC Kp[0], Ki[0], Kd[0], Kc[0] (float x4)
* 424 M301 E1 PIDC Kp[1], Ki[1], Kd[1], Kc[1] (float x4)
* 440 M301 E2 PIDC Kp[2], Ki[2], Kd[2], Kc[2] (float x4)
* 456 M301 E3 PIDC Kp[3], Ki[3], Kd[3], Kc[3] (float x4)
* 472 M301 E4 PIDC Kp[3], Ki[3], Kd[3], Kc[3] (float x4)
* 488 M301 L lpq_len (int)
*
* PIDTEMPBED: 12 bytes
* 490 M304 PID thermalManager.bedKp, .bedKi, .bedKd (float x3)
*
* DOGLCD: 2 bytes
* 502 M250 C lcd_contrast (uint16_t)
*
* FWRETRACT: 33 bytes
* 504 M209 S autoretract_enabled (bool)
* 505 M207 S retract_length (float)
* 509 M207 F retract_feedrate_mm_s (float)
* 513 M207 Z retract_zlift (float)
* 517 M208 S retract_recover_length (float)
* 521 M208 F retract_recover_feedrate_mm_s (float)
* 525 M207 W swap_retract_length (float)
* 529 M208 W swap_retract_recover_length (float)
* 533 M208 R swap_retract_recover_feedrate_mm_s (float)
*
* Volumetric Extrusion: 21 bytes
* 537 M200 D volumetric_enabled (bool)
* 538 M200 T D filament_size (float x5) (T0..3)
*
* HAVE_TMC2130: 20 bytes
* 558 M906 X Stepper X current (uint16_t)
* 560 M906 Y Stepper Y current (uint16_t)
* 562 M906 Z Stepper Z current (uint16_t)
* 564 M906 X2 Stepper X2 current (uint16_t)
* 566 M906 Y2 Stepper Y2 current (uint16_t)
* 568 M906 Z2 Stepper Z2 current (uint16_t)
* 570 M906 E0 Stepper E0 current (uint16_t)
* 572 M906 E1 Stepper E1 current (uint16_t)
* 574 M906 E2 Stepper E2 current (uint16_t)
* 576 M906 E3 Stepper E3 current (uint16_t)
* 580 M906 E4 Stepper E4 current (uint16_t)
*
* LIN_ADVANCE: 8 bytes
* 584 M900 K extruder_advance_k (float)
* 588 M900 WHD advance_ed_ratio (float)
*
* HAS_MOTOR_CURRENT_PWM:
* 592 M907 X Stepper XY current (uint32_t)
* 596 M907 Z Stepper Z current (uint32_t)
* 600 M907 E Stepper E current (uint32_t)
*
* 604 Minimum end-point
* 1925 (604 + 36 + 9 + 288 + 988) Maximum end-point
*
* ========================================================================
* meshes_begin (between max and min end-point, directly above)
* -- MESHES --
* meshes_end
* -- MAT (Mesh Allocation Table) -- 128 bytes (placeholder size)
* mat_end = E2END (0xFFF)
*
*/
#include "configuration_store.h"
MarlinSettings settings;
#include "Marlin.h"
#include "language.h"
#include "endstops.h"
#include "planner.h"
#include "temperature.h"
#include "ultralcd.h"
#include "stepper.h"
#include "cardreader.h"
#if ENABLED(INCH_MODE_SUPPORT) || (ENABLED(ULTIPANEL) && ENABLED(TEMPERATURE_UNITS_SUPPORT))
#include "gcode.h"
#endif
#if ENABLED(MESH_BED_LEVELING)
#include "mesh_bed_leveling.h"
#endif
#if ENABLED(HAVE_TMC2130)
#include "stepper_indirection.h"
#endif
#if ENABLED(AUTO_BED_LEVELING_UBL)
#include "ubl.h"
#endif
#if ENABLED(AUTO_BED_LEVELING_BILINEAR)
extern void refresh_bed_level();
#endif
/**
* Post-process after Retrieve or Reset
*/
void MarlinSettings::postprocess() {
// steps per s2 needs to be updated to agree with units per s2
planner.reset_acceleration_rates();
// Make sure delta kinematics are updated before refreshing the
// planner position so the stepper counts will be set correctly.
#if ENABLED(DELTA)
recalc_delta_settings(delta_radius, delta_diagonal_rod);
#endif
// Refresh steps_to_mm with the reciprocal of axis_steps_per_mm
// and init stepper.count[], planner.position[] with current_position
planner.refresh_positioning();
#if ENABLED(PIDTEMP)
thermalManager.updatePID();
#endif
calculate_volumetric_multipliers();
#if HAS_HOME_OFFSET || ENABLED(DUAL_X_CARRIAGE)
// Software endstops depend on home_offset
LOOP_XYZ(i) update_software_endstops((AxisEnum)i);
#endif
#if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
set_z_fade_height(planner.z_fade_height);
#endif
#if HAS_BED_PROBE
refresh_zprobe_zoffset();
#endif
#if ENABLED(AUTO_BED_LEVELING_BILINEAR)
refresh_bed_level();
//set_bed_leveling_enabled(leveling_is_on);
#endif
#if HAS_MOTOR_CURRENT_PWM
stepper.refresh_motor_power();
#endif
}
#if ENABLED(EEPROM_SETTINGS)
uint16_t eeprom_checksum;
void _EEPROM_writeData(int &pos, uint8_t* value, uint8_t size) {
uint8_t c;
while (size--) {
eeprom_write_byte((unsigned char*)pos, *value);
c = eeprom_read_byte((unsigned char*)pos);
if (c != *value) {
SERIAL_ECHO_START();
SERIAL_ECHOLNPGM(MSG_ERR_EEPROM_WRITE);
}
eeprom_checksum += c;
pos++;
value++;
};
}
void _EEPROM_readData(int &pos, uint8_t* value, uint8_t size) {
do {
uint8_t c = eeprom_read_byte((unsigned char*)pos);
*value = c;
eeprom_checksum += c;
pos++;
value++;
} while (--size);
}
#define DUMMY_PID_VALUE 3000.0f
#define EEPROM_START() int eeprom_index = EEPROM_OFFSET
#define EEPROM_SKIP(VAR) eeprom_index += sizeof(VAR)
#define EEPROM_WRITE(VAR) write_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc)
#define EEPROM_READ(VAR) read_data(eeprom_index, (uint8_t*)&VAR, sizeof(VAR), &working_crc)
#define EEPROM_ASSERT(TST,ERR) if (!(TST)) do{ SERIAL_ERROR_START(); SERIAL_ERRORLNPGM(ERR); eeprom_read_error = true; }while(0)
#define EEPROM_WRITE_VAR(pos, value) _EEPROM_writeData(pos, (uint8_t*)&value, sizeof(value))
#define EEPROM_READ_VAR(pos, value) _EEPROM_readData(pos, (uint8_t*)&value, sizeof(value))
const char version[4] = EEPROM_VERSION;
float last_position[4]={0.0,0.0,0.0,0.0};
long last_sd_position[1]={0};
void OutageSave()
{
char ver[4]="000";
int j=20;
EEPROM_WRITE_VAR(j,ver);
last_sd_position[0]=card.GetLastSDpos();
last_position[0]=current_position[E_AXIS];
last_position[1]=current_position[Z_AXIS];
last_position[2]=current_position[Y_AXIS];
last_position[3]=current_position[X_AXIS];
EEPROM_WRITE_VAR(j,last_sd_position[0]);
EEPROM_WRITE_VAR(j,last_position[0]); //E
EEPROM_WRITE_VAR(j,last_position[1]); //Z
EEPROM_WRITE_VAR(j,last_position[2]); //Y
EEPROM_WRITE_VAR(j,last_position[3]); //X
}
void OutageRead()
{
int i=20;
char stored_ver[4];
char ver[4]=EEPROM_VERSION;
EEPROM_READ_VAR(i,stored_ver);
EEPROM_READ_VAR(i,last_sd_position[0]);
EEPROM_READ_VAR(i,last_position[0]); //E
EEPROM_READ_VAR(i,last_position[1]); //Z
EEPROM_READ_VAR(i,last_position[2]); //Y
EEPROM_READ_VAR(i,last_position[3]); //X
}
bool MarlinSettings::eeprom_error;
#if ENABLED(AUTO_BED_LEVELING_UBL)
int MarlinSettings::meshes_begin;
#endif
void MarlinSettings::write_data(int &pos, const uint8_t *value, uint16_t size, uint16_t *crc) {
if (eeprom_error) return;
while (size--) {
uint8_t * const p = (uint8_t * const)pos;
uint8_t v = *value;
// EEPROM has only ~100,000 write cycles,
// so only write bytes that have changed!
if (v != eeprom_read_byte(p)) {
eeprom_write_byte(p, v);
if (eeprom_read_byte(p) != v) {
SERIAL_ECHO_START();
SERIAL_ECHOLNPGM(MSG_ERR_EEPROM_WRITE);
eeprom_error = true;
return;
}
}
crc16(crc, &v, 1);
pos++;
value++;
};
}
void MarlinSettings::read_data(int &pos, uint8_t* value, uint16_t size, uint16_t *crc) {
if (eeprom_error) return;
do {
uint8_t c = eeprom_read_byte((unsigned char*)pos);
*value = c;
crc16(crc, &c, 1);
pos++;
value++;
} while (--size);
}
/**
* M500 - Store Configuration
*/
bool MarlinSettings::save() {
float dummy = 0.0f;
char ver[4] = "000";
uint16_t working_crc = 0;
EEPROM_START();
eeprom_error = false;
EEPROM_WRITE(ver); // invalidate data first
EEPROM_SKIP(working_crc); // Skip the checksum slot
working_crc = 0; // clear before first "real data"
const uint8_t esteppers = COUNT(planner.axis_steps_per_mm) - XYZ;
EEPROM_WRITE(esteppers);
EEPROM_WRITE(planner.axis_steps_per_mm);
EEPROM_WRITE(planner.max_feedrate_mm_s);
EEPROM_WRITE(planner.max_acceleration_mm_per_s2);
EEPROM_WRITE(planner.acceleration);
EEPROM_WRITE(planner.retract_acceleration);
EEPROM_WRITE(planner.travel_acceleration);
EEPROM_WRITE(planner.min_feedrate_mm_s);
EEPROM_WRITE(planner.min_travel_feedrate_mm_s);
EEPROM_WRITE(planner.min_segment_time);
EEPROM_WRITE(planner.max_jerk);
#if !HAS_HOME_OFFSET
const float home_offset[XYZ] = { 0 };
#endif
#if ENABLED(DELTA)
dummy = 0.0;
EEPROM_WRITE(dummy);
EEPROM_WRITE(dummy);
dummy = DELTA_HEIGHT + home_offset[Z_AXIS];
EEPROM_WRITE(dummy);
#else
EEPROM_WRITE(home_offset);
#endif
#if HOTENDS > 1
// Skip hotend 0 which must be 0
for (uint8_t e = 1; e < HOTENDS; e++)
LOOP_XYZ(i) EEPROM_WRITE(hotend_offset[i][e]);
#endif
//
// Global Leveling
//
#if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
const float zfh = planner.z_fade_height;
#else
const float zfh = 10.0;
#endif
EEPROM_WRITE(zfh);
//
// Mesh Bed Leveling
//
#if ENABLED(MESH_BED_LEVELING)
// Compile time test that sizeof(mbl.z_values) is as expected
static_assert(
sizeof(mbl.z_values) == GRID_MAX_POINTS * sizeof(mbl.z_values[0][0]),
"MBL Z array is the wrong size."
);
const bool leveling_is_on = TEST(mbl.status, MBL_STATUS_HAS_MESH_BIT);
const uint8_t mesh_num_x = GRID_MAX_POINTS_X, mesh_num_y = GRID_MAX_POINTS_Y;
EEPROM_WRITE(leveling_is_on);
EEPROM_WRITE(mbl.z_offset);
EEPROM_WRITE(mesh_num_x);
EEPROM_WRITE(mesh_num_y);
EEPROM_WRITE(mbl.z_values);
#else // For disabled MBL write a default mesh
const bool leveling_is_on = false;
dummy = 0.0f;
const uint8_t mesh_num_x = 3, mesh_num_y = 3;
EEPROM_WRITE(leveling_is_on);
EEPROM_WRITE(dummy); // z_offset
EEPROM_WRITE(mesh_num_x);
EEPROM_WRITE(mesh_num_y);
for (uint8_t q = mesh_num_x * mesh_num_y; q--;) EEPROM_WRITE(dummy);
#endif // MESH_BED_LEVELING
#if !HAS_BED_PROBE
const float zprobe_zoffset = 0;
#endif
EEPROM_WRITE(zprobe_zoffset);
//
// Planar Bed Leveling matrix
//
#if ABL_PLANAR
EEPROM_WRITE(planner.bed_level_matrix);
#else
dummy = 0.0;
for (uint8_t q = 9; q--;) EEPROM_WRITE(dummy);
#endif
//
// Bilinear Auto Bed Leveling
//
#if ENABLED(AUTO_BED_LEVELING_BILINEAR)
// Compile time test that sizeof(z_values) is as expected
static_assert(
sizeof(z_values) == GRID_MAX_POINTS * sizeof(z_values[0][0]),
"Bilinear Z array is the wrong size."
);
const uint8_t grid_max_x = GRID_MAX_POINTS_X, grid_max_y = GRID_MAX_POINTS_Y;
EEPROM_WRITE(grid_max_x); // 1 byte
EEPROM_WRITE(grid_max_y); // 1 byte
EEPROM_WRITE(bilinear_grid_spacing); // 2 ints
EEPROM_WRITE(bilinear_start); // 2 ints
EEPROM_WRITE(z_values); // 9-256 floats
#else
// For disabled Bilinear Grid write an empty 3x3 grid
const uint8_t grid_max_x = 3, grid_max_y = 3;
const int bilinear_start[2] = { 0 }, bilinear_grid_spacing[2] = { 0 };
dummy = 0.0f;
EEPROM_WRITE(grid_max_x);
EEPROM_WRITE(grid_max_y);
EEPROM_WRITE(bilinear_grid_spacing);
EEPROM_WRITE(bilinear_start);
for (uint16_t q = grid_max_x * grid_max_y; q--;) EEPROM_WRITE(dummy);
#endif // AUTO_BED_LEVELING_BILINEAR
#if ENABLED(AUTO_BED_LEVELING_UBL)
EEPROM_WRITE(ubl.state.active);
EEPROM_WRITE(ubl.state.z_offset);
EEPROM_WRITE(ubl.state.storage_slot);
#else
const bool ubl_active = false;
dummy = 0.0f;
const int8_t storage_slot = -1;
EEPROM_WRITE(ubl_active);
EEPROM_WRITE(dummy);
EEPROM_WRITE(storage_slot);
#endif // AUTO_BED_LEVELING_UBL
// 9 floats for DELTA / Z_DUAL_ENDSTOPS
#if ENABLED(DELTA)
EEPROM_WRITE(endstop_adj); // 3 floats
EEPROM_WRITE(delta_radius); // 1 float
EEPROM_WRITE(delta_diagonal_rod); // 1 float
EEPROM_WRITE(delta_segments_per_second); // 1 float
EEPROM_WRITE(delta_calibration_radius); // 1 float
EEPROM_WRITE(delta_tower_angle_trim); // 2 floats
dummy = 0.0f;
for (uint8_t q = 3; q--;) EEPROM_WRITE(dummy);
#elif ENABLED(Z_DUAL_ENDSTOPS)
EEPROM_WRITE(z_endstop_adj); // 1 float
dummy = 0.0f;
for (uint8_t q = 11; q--;) EEPROM_WRITE(dummy);
#else
dummy = 0.0f;
for (uint8_t q = 12; q--;) EEPROM_WRITE(dummy);
#endif
#if DISABLED(ULTIPANEL)
constexpr int lcd_preheat_hotend_temp[2] = { PREHEAT_1_TEMP_HOTEND, PREHEAT_2_TEMP_HOTEND },
lcd_preheat_bed_temp[2] = { PREHEAT_1_TEMP_BED, PREHEAT_2_TEMP_BED },
lcd_preheat_fan_speed[2] = { PREHEAT_1_FAN_SPEED, PREHEAT_2_FAN_SPEED };
#endif
EEPROM_WRITE(lcd_preheat_hotend_temp);
EEPROM_WRITE(lcd_preheat_bed_temp);
EEPROM_WRITE(lcd_preheat_fan_speed);
for (uint8_t e = 0; e < MAX_EXTRUDERS; e++) {
#if ENABLED(PIDTEMP)
if (e < HOTENDS) {
EEPROM_WRITE(PID_PARAM(Kp, e));
EEPROM_WRITE(PID_PARAM(Ki, e));
EEPROM_WRITE(PID_PARAM(Kd, e));
#if ENABLED(PID_EXTRUSION_SCALING)
EEPROM_WRITE(PID_PARAM(Kc, e));
#else
dummy = 1.0f; // 1.0 = default kc
EEPROM_WRITE(dummy);
#endif
}
else
#endif // !PIDTEMP
{
dummy = DUMMY_PID_VALUE; // When read, will not change the existing value
EEPROM_WRITE(dummy); // Kp
dummy = 0.0f;
for (uint8_t q = 3; q--;) EEPROM_WRITE(dummy); // Ki, Kd, Kc
}
} // Hotends Loop
#if DISABLED(PID_EXTRUSION_SCALING)
int lpq_len = 20;
#endif
EEPROM_WRITE(lpq_len);
#if DISABLED(PIDTEMPBED)
dummy = DUMMY_PID_VALUE;
for (uint8_t q = 3; q--;) EEPROM_WRITE(dummy);
#else
EEPROM_WRITE(thermalManager.bedKp);
EEPROM_WRITE(thermalManager.bedKi);
EEPROM_WRITE(thermalManager.bedKd);
#endif
#if !HAS_LCD_CONTRAST
const uint16_t lcd_contrast = 32;
#endif
EEPROM_WRITE(lcd_contrast);
#if DISABLED(FWRETRACT)
const bool autoretract_enabled = false;
const float retract_length = 3,
retract_feedrate_mm_s = 45,
retract_zlift = 0,
retract_recover_length = 0,
retract_recover_feedrate_mm_s = 0,
swap_retract_length = 13,
swap_retract_recover_length = 0,
swap_retract_recover_feedrate_mm_s = 8;
#endif
EEPROM_WRITE(autoretract_enabled);
EEPROM_WRITE(retract_length);
EEPROM_WRITE(retract_feedrate_mm_s);
EEPROM_WRITE(retract_zlift);
EEPROM_WRITE(retract_recover_length);
EEPROM_WRITE(retract_recover_feedrate_mm_s);
EEPROM_WRITE(swap_retract_length);
EEPROM_WRITE(swap_retract_recover_length);
EEPROM_WRITE(swap_retract_recover_feedrate_mm_s);
EEPROM_WRITE(volumetric_enabled);
// Save filament sizes
for (uint8_t q = 0; q < MAX_EXTRUDERS; q++) {
if (q < COUNT(filament_size)) dummy = filament_size[q];
EEPROM_WRITE(dummy);
}
// Save TMC2130 Configuration, and placeholder values
uint16_t val;
#if ENABLED(HAVE_TMC2130)
#if ENABLED(X_IS_TMC2130)
val = stepperX.getCurrent();
#else
val = 0;
#endif
EEPROM_WRITE(val);
#if ENABLED(Y_IS_TMC2130)
val = stepperY.getCurrent();
#else
val = 0;
#endif
EEPROM_WRITE(val);
#if ENABLED(Z_IS_TMC2130)
val = stepperZ.getCurrent();
#else
val = 0;
#endif
EEPROM_WRITE(val);
#if ENABLED(X2_IS_TMC2130)
val = stepperX2.getCurrent();
#else
val = 0;
#endif
EEPROM_WRITE(val);
#if ENABLED(Y2_IS_TMC2130)
val = stepperY2.getCurrent();
#else
val = 0;
#endif
EEPROM_WRITE(val);
#if ENABLED(Z2_IS_TMC2130)
val = stepperZ2.getCurrent();
#else
val = 0;
#endif
EEPROM_WRITE(val);
#if ENABLED(E0_IS_TMC2130)
val = stepperE0.getCurrent();
#else
val = 0;
#endif
EEPROM_WRITE(val);
#if ENABLED(E1_IS_TMC2130)
val = stepperE1.getCurrent();
#else
val = 0;
#endif
EEPROM_WRITE(val);
#if ENABLED(E2_IS_TMC2130)
val = stepperE2.getCurrent();
#else
val = 0;
#endif
EEPROM_WRITE(val);
#if ENABLED(E3_IS_TMC2130)
val = stepperE3.getCurrent();
#else
val = 0;
#endif
EEPROM_WRITE(val);
#if ENABLED(E4_IS_TMC2130)
val = stepperE4.getCurrent();
#else
val = 0;
#endif
EEPROM_WRITE(val);
#else
val = 0;
for (uint8_t q = 11; q--;) EEPROM_WRITE(val);
#endif
//
// Linear Advance
//
#if ENABLED(LIN_ADVANCE)
EEPROM_WRITE(planner.extruder_advance_k);
EEPROM_WRITE(planner.advance_ed_ratio);
#else
dummy = 0.0f;
EEPROM_WRITE(dummy);
EEPROM_WRITE(dummy);
#endif
#if HAS_MOTOR_CURRENT_PWM
for (uint8_t q = 3; q--;) EEPROM_WRITE(stepper.motor_current_setting[q]);
#else
const uint32_t dummyui32 = 0;
for (uint8_t q = 3; q--;) EEPROM_WRITE(dummyui32);
#endif
if (!eeprom_error) {
const int eeprom_size = eeprom_index;
const uint16_t final_crc = working_crc;
// Write the EEPROM header
eeprom_index = EEPROM_OFFSET;
EEPROM_WRITE(version);
EEPROM_WRITE(final_crc);
// Report storage size
#if ENABLED(EEPROM_CHITCHAT)
SERIAL_ECHO_START();
SERIAL_ECHOPAIR("Settings Stored (", eeprom_size - (EEPROM_OFFSET));
SERIAL_ECHOPAIR(" bytes; crc ", (uint32_t)final_crc);
SERIAL_ECHOLNPGM(")");
#endif
}
#if ENABLED(UBL_SAVE_ACTIVE_ON_M500)
if (ubl.state.storage_slot >= 0)
store_mesh(ubl.state.storage_slot);
#endif
return !eeprom_error;
}
/**
* M501 - Retrieve Configuration
*/
bool MarlinSettings::load() {
uint16_t working_crc = 0;
EEPROM_START();
char stored_ver[4];
EEPROM_READ(stored_ver);
uint16_t stored_crc;
EEPROM_READ(stored_crc);
// Version has to match or defaults are used
if (strncmp(version, stored_ver, 3) != 0) {
if (stored_ver[0] != 'V') {
stored_ver[0] = '?';
stored_ver[1] = '\0';
}
#if ENABLED(EEPROM_CHITCHAT)
SERIAL_ECHO_START();
SERIAL_ECHOPGM("EEPROM version mismatch ");
SERIAL_ECHOPAIR("(EEPROM=", stored_ver);
SERIAL_ECHOLNPGM(" Marlin=" EEPROM_VERSION ")");
#endif
reset();
}
else {
float dummy = 0;
bool dummyb;
working_crc = 0; //clear before reading first "real data"
// Number of esteppers may change
uint8_t esteppers;
EEPROM_READ(esteppers);
// Get only the number of E stepper parameters previously stored
// Any steppers added later are set to their defaults
const float def1[] = DEFAULT_AXIS_STEPS_PER_UNIT, def2[] = DEFAULT_MAX_FEEDRATE;
const uint32_t def3[] = DEFAULT_MAX_ACCELERATION;
float tmp1[XYZ + esteppers], tmp2[XYZ + esteppers];
uint32_t tmp3[XYZ + esteppers];
EEPROM_READ(tmp1);
EEPROM_READ(tmp2);
EEPROM_READ(tmp3);
LOOP_XYZE_N(i) {
planner.axis_steps_per_mm[i] = i < XYZ + esteppers ? tmp1[i] : def1[i < COUNT(def1) ? i : COUNT(def1) - 1];
planner.max_feedrate_mm_s[i] = i < XYZ + esteppers ? tmp2[i] : def2[i < COUNT(def2) ? i : COUNT(def2) - 1];
planner.max_acceleration_mm_per_s2[i] = i < XYZ + esteppers ? tmp3[i] : def3[i < COUNT(def3) ? i : COUNT(def3) - 1];
}
EEPROM_READ(planner.acceleration);
EEPROM_READ(planner.retract_acceleration);
EEPROM_READ(planner.travel_acceleration);
EEPROM_READ(planner.min_feedrate_mm_s);
EEPROM_READ(planner.min_travel_feedrate_mm_s);
EEPROM_READ(planner.min_segment_time);
EEPROM_READ(planner.max_jerk);
#if !HAS_HOME_OFFSET
float home_offset[XYZ];
#endif
EEPROM_READ(home_offset);
#if ENABLED(DELTA)
home_offset[X_AXIS] = 0.0;
home_offset[Y_AXIS] = 0.0;
home_offset[Z_AXIS] -= DELTA_HEIGHT;
#endif
#if HOTENDS > 1
// Skip hotend 0 which must be 0
for (uint8_t e = 1; e < HOTENDS; e++)
LOOP_XYZ(i) EEPROM_READ(hotend_offset[i][e]);
#endif
//
// Global Leveling
//
#if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
EEPROM_READ(planner.z_fade_height);
#else
EEPROM_READ(dummy);
#endif
//
// Mesh (Manual) Bed Leveling
//
bool leveling_is_on;
uint8_t mesh_num_x, mesh_num_y;
EEPROM_READ(leveling_is_on);
EEPROM_READ(dummy);
EEPROM_READ(mesh_num_x);
EEPROM_READ(mesh_num_y);
#if ENABLED(MESH_BED_LEVELING)
mbl.status = leveling_is_on ? _BV(MBL_STATUS_HAS_MESH_BIT) : 0;
mbl.z_offset = dummy;
if (mesh_num_x == GRID_MAX_POINTS_X && mesh_num_y == GRID_MAX_POINTS_Y) {
// EEPROM data fits the current mesh
EEPROM_READ(mbl.z_values);
}
else {
// EEPROM data is stale
mbl.reset();
for (uint16_t q = mesh_num_x * mesh_num_y; q--;) EEPROM_READ(dummy);
}
#else
// MBL is disabled - skip the stored data
for (uint16_t q = mesh_num_x * mesh_num_y; q--;) EEPROM_READ(dummy);
#endif // MESH_BED_LEVELING
#if !HAS_BED_PROBE
float zprobe_zoffset;
#endif
EEPROM_READ(zprobe_zoffset);
//
// Planar Bed Leveling matrix
//
#if ABL_PLANAR
EEPROM_READ(planner.bed_level_matrix);
#else
for (uint8_t q = 9; q--;) EEPROM_READ(dummy);
#endif
//
// Bilinear Auto Bed Leveling
//
uint8_t grid_max_x, grid_max_y;
EEPROM_READ(grid_max_x); // 1 byte
EEPROM_READ(grid_max_y); // 1 byte
#if ENABLED(AUTO_BED_LEVELING_BILINEAR)
if (grid_max_x == GRID_MAX_POINTS_X && grid_max_y == GRID_MAX_POINTS_Y) {
set_bed_leveling_enabled(false);
EEPROM_READ(bilinear_grid_spacing); // 2 ints
EEPROM_READ(bilinear_start); // 2 ints
EEPROM_READ(z_values); // 9 to 256 floats
}
else // EEPROM data is stale
#endif // AUTO_BED_LEVELING_BILINEAR
{
// Skip past disabled (or stale) Bilinear Grid data
int bgs[2], bs[2];
EEPROM_READ(bgs);
EEPROM_READ(bs);
for (uint16_t q = grid_max_x * grid_max_y; q--;) EEPROM_READ(dummy);
}
#if ENABLED(AUTO_BED_LEVELING_UBL)
EEPROM_READ(ubl.state.active);
EEPROM_READ(ubl.state.z_offset);
EEPROM_READ(ubl.state.storage_slot);
#else
uint8_t dummyui8;
EEPROM_READ(dummyb);
EEPROM_READ(dummy);
EEPROM_READ(dummyui8);
#endif // AUTO_BED_LEVELING_UBL
#if ENABLED(DELTA)
EEPROM_READ(endstop_adj); // 3 floats
EEPROM_READ(delta_radius); // 1 float
EEPROM_READ(delta_diagonal_rod); // 1 float
EEPROM_READ(delta_segments_per_second); // 1 float
EEPROM_READ(delta_calibration_radius); // 1 float
EEPROM_READ(delta_tower_angle_trim); // 2 floats
dummy = 0.0f;
for (uint8_t q=3; q--;) EEPROM_READ(dummy);
#elif ENABLED(Z_DUAL_ENDSTOPS)
EEPROM_READ(z_endstop_adj);
dummy = 0.0f;
for (uint8_t q=11; q--;) EEPROM_READ(dummy);
#else
dummy = 0.0f;
for (uint8_t q=12; q--;) EEPROM_READ(dummy);
#endif
#if DISABLED(ULTIPANEL)
int lcd_preheat_hotend_temp[2], lcd_preheat_bed_temp[2], lcd_preheat_fan_speed[2];
#endif
EEPROM_READ(lcd_preheat_hotend_temp);
EEPROM_READ(lcd_preheat_bed_temp);
EEPROM_READ(lcd_preheat_fan_speed);
//EEPROM_ASSERT(
// WITHIN(lcd_preheat_fan_speed, 0, 255),
// "lcd_preheat_fan_speed out of range"
//);
#if ENABLED(PIDTEMP)
for (uint8_t e = 0; e < MAX_EXTRUDERS; e++) {
EEPROM_READ(dummy); // Kp
if (e < HOTENDS && dummy != DUMMY_PID_VALUE) {
// do not need to scale PID values as the values in EEPROM are already scaled
PID_PARAM(Kp, e) = dummy;
EEPROM_READ(PID_PARAM(Ki, e));
EEPROM_READ(PID_PARAM(Kd, e));
#if ENABLED(PID_EXTRUSION_SCALING)
EEPROM_READ(PID_PARAM(Kc, e));
#else
EEPROM_READ(dummy);
#endif
}
else {
for (uint8_t q=3; q--;) EEPROM_READ(dummy); // Ki, Kd, Kc
}
}
#else // !PIDTEMP
// 4 x 4 = 16 slots for PID parameters
for (uint8_t q = MAX_EXTRUDERS * 4; q--;) EEPROM_READ(dummy); // Kp, Ki, Kd, Kc
#endif // !PIDTEMP
#if DISABLED(PID_EXTRUSION_SCALING)
int lpq_len;
#endif
EEPROM_READ(lpq_len);
#if ENABLED(PIDTEMPBED)
EEPROM_READ(dummy); // bedKp
if (dummy != DUMMY_PID_VALUE) {
thermalManager.bedKp = dummy;
EEPROM_READ(thermalManager.bedKi);
EEPROM_READ(thermalManager.bedKd);
}
#else
for (uint8_t q=3; q--;) EEPROM_READ(dummy); // bedKp, bedKi, bedKd
#endif
#if !HAS_LCD_CONTRAST
uint16_t lcd_contrast;
#endif
EEPROM_READ(lcd_contrast);
#if ENABLED(FWRETRACT)
EEPROM_READ(autoretract_enabled);
EEPROM_READ(retract_length);
EEPROM_READ(retract_feedrate_mm_s);
EEPROM_READ(retract_zlift);
EEPROM_READ(retract_recover_length);
EEPROM_READ(retract_recover_feedrate_mm_s);
EEPROM_READ(swap_retract_length);
EEPROM_READ(swap_retract_recover_length);
EEPROM_READ(swap_retract_recover_feedrate_mm_s);
#else
EEPROM_READ(dummyb);
for (uint8_t q=8; q--;) EEPROM_READ(dummy);
#endif
EEPROM_READ(volumetric_enabled);
for (uint8_t q = 0; q < MAX_EXTRUDERS; q++) {