Browse Source

✨ Option to reset EEPROM on first run (#23276)

Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
ellensp 3 years ago
parent
commit
c1dba3d028
No account linked to committer's email address

+ 1
- 0
Marlin/Configuration.h View File

1837
 #define EEPROM_BOOT_SILENT    // Keep M503 quiet and only give errors during first load
1837
 #define EEPROM_BOOT_SILENT    // Keep M503 quiet and only give errors during first load
1838
 #if ENABLED(EEPROM_SETTINGS)
1838
 #if ENABLED(EEPROM_SETTINGS)
1839
   //#define EEPROM_AUTO_INIT  // Init EEPROM automatically on any errors.
1839
   //#define EEPROM_AUTO_INIT  // Init EEPROM automatically on any errors.
1840
+  //#define EEPROM_INIT_NOW   // Init EEPROM on first boot after a new build.
1840
 #endif
1841
 #endif
1841
 
1842
 
1842
 //
1843
 //

+ 57
- 19
Marlin/src/module/settings.cpp View File

36
  */
36
  */
37
 
37
 
38
 // Change EEPROM version if the structure changes
38
 // Change EEPROM version if the structure changes
39
-#define EEPROM_VERSION "V85"
39
+#define EEPROM_VERSION "V86"
40
 #define EEPROM_OFFSET 100
40
 #define EEPROM_OFFSET 100
41
 
41
 
42
 // Check the integrity of data offsets.
42
 // Check the integrity of data offsets.
198
  */
198
  */
199
 typedef struct SettingsDataStruct {
199
 typedef struct SettingsDataStruct {
200
   char      version[4];                                 // Vnn\0
200
   char      version[4];                                 // Vnn\0
201
+  #if ENABLED(EEPROM_INIT_NOW)
202
+    uint32_t build_hash;                                // Unique build hash
203
+  #endif
201
   uint16_t  crc;                                        // Data Checksum
204
   uint16_t  crc;                                        // Data Checksum
202
 
205
 
203
   //
206
   //
204
   // DISTINCT_E_FACTORS
207
   // DISTINCT_E_FACTORS
205
   //
208
   //
206
-  uint8_t   esteppers;                                  // DISTINCT_AXES - LINEAR_AXES
209
+  uint8_t e_factors;                                    // DISTINCT_AXES - LINEAR_AXES
207
 
210
 
211
+  //
212
+  // Planner settings
213
+  //
208
   planner_settings_t planner_settings;
214
   planner_settings_t planner_settings;
209
 
215
 
210
   xyze_float_t planner_max_jerk;                        // M205 XYZE  planner.max_jerk
216
   xyze_float_t planner_max_jerk;                        // M205 XYZE  planner.max_jerk
211
   float planner_junction_deviation_mm;                  // M205 J     planner.junction_deviation_mm
217
   float planner_junction_deviation_mm;                  // M205 J     planner.junction_deviation_mm
212
 
218
 
219
+  //
220
+  // Home Offset
221
+  //
213
   xyz_pos_t home_offset;                                // M206 XYZ / M665 TPZ
222
   xyz_pos_t home_offset;                                // M206 XYZ / M665 TPZ
214
 
223
 
224
+  //
225
+  // Hotend Offset
226
+  //
215
   #if HAS_HOTEND_OFFSET
227
   #if HAS_HOTEND_OFFSET
216
     xyz_pos_t hotend_offset[HOTENDS - 1];               // M218 XYZ
228
     xyz_pos_t hotend_offset[HOTENDS - 1];               // M218 XYZ
217
   #endif
229
   #endif
649
 
661
 
650
   const char version[4] = EEPROM_VERSION;
662
   const char version[4] = EEPROM_VERSION;
651
 
663
 
664
+  #if ENABLED(EEPROM_INIT_NOW)
665
+    constexpr uint32_t strhash32(const char *s, const uint32_t h=0) {
666
+      return *s ? strhash32(s + 1, ((h + *s) << (*s & 3)) ^ *s) : h;
667
+    }
668
+    constexpr uint32_t build_hash = strhash32(__DATE__ __TIME__);
669
+  #endif
670
+
652
   bool MarlinSettings::eeprom_error, MarlinSettings::validating;
671
   bool MarlinSettings::eeprom_error, MarlinSettings::validating;
653
   int MarlinSettings::eeprom_index;
672
   int MarlinSettings::eeprom_index;
654
   uint16_t MarlinSettings::working_crc;
673
   uint16_t MarlinSettings::working_crc;
655
 
674
 
656
   bool MarlinSettings::size_error(const uint16_t size) {
675
   bool MarlinSettings::size_error(const uint16_t size) {
657
     if (size != datasize()) {
676
     if (size != datasize()) {
658
-      DEBUG_ERROR_MSG("EEPROM datasize error.");
677
+      DEBUG_ERROR_MSG("EEPROM datasize error."
678
+        #if ENABLED(MARLIN_DEV_MODE)
679
+          " (Actual:", size, " Expected:", datasize(), ")"
680
+        #endif
681
+      );
659
       return true;
682
       return true;
660
     }
683
     }
661
     return false;
684
     return false;
675
     // Write or Skip version. (Flash doesn't allow rewrite without erase.)
698
     // Write or Skip version. (Flash doesn't allow rewrite without erase.)
676
     TERN(FLASH_EEPROM_EMULATION, EEPROM_SKIP, EEPROM_WRITE)(ver);
699
     TERN(FLASH_EEPROM_EMULATION, EEPROM_SKIP, EEPROM_WRITE)(ver);
677
 
700
 
678
-    EEPROM_SKIP(working_crc); // Skip the checksum slot
701
+    #if ENABLED(EEPROM_INIT_NOW)
702
+      EEPROM_SKIP(build_hash);  // Skip the hash slot
703
+    #endif
704
+
705
+    EEPROM_SKIP(working_crc);   // Skip the checksum slot
679
 
706
 
680
     working_crc = 0; // clear before first "real data"
707
     working_crc = 0; // clear before first "real data"
681
 
708
 
682
-    const uint8_t esteppers = COUNT(planner.settings.axis_steps_per_mm) - LINEAR_AXES;
683
-    _FIELD_TEST(esteppers);
684
-    EEPROM_WRITE(esteppers);
709
+    const uint8_t e_factors = DISTINCT_AXES - (LINEAR_AXES);
710
+    _FIELD_TEST(e_factors);
711
+    EEPROM_WRITE(e_factors);
685
 
712
 
686
     //
713
     //
687
     // Planner Motion
714
     // Planner Motion
1494
       eeprom_index = EEPROM_OFFSET;
1521
       eeprom_index = EEPROM_OFFSET;
1495
 
1522
 
1496
       EEPROM_WRITE(version);
1523
       EEPROM_WRITE(version);
1524
+      #if ENABLED(EEPROM_INIT_NOW)
1525
+        EEPROM_WRITE(build_hash);
1526
+      #endif
1497
       EEPROM_WRITE(final_crc);
1527
       EEPROM_WRITE(final_crc);
1498
 
1528
 
1499
       // Report storage size
1529
       // Report storage size
1527
     char stored_ver[4];
1557
     char stored_ver[4];
1528
     EEPROM_READ_ALWAYS(stored_ver);
1558
     EEPROM_READ_ALWAYS(stored_ver);
1529
 
1559
 
1530
-    uint16_t stored_crc;
1531
-    EEPROM_READ_ALWAYS(stored_crc);
1532
-
1533
     // Version has to match or defaults are used
1560
     // Version has to match or defaults are used
1534
     if (strncmp(version, stored_ver, 3) != 0) {
1561
     if (strncmp(version, stored_ver, 3) != 0) {
1535
       if (stored_ver[3] != '\0') {
1562
       if (stored_ver[3] != '\0') {
1543
       eeprom_error = true;
1570
       eeprom_error = true;
1544
     }
1571
     }
1545
     else {
1572
     else {
1573
+
1574
+      // Optionally reset on the first boot after flashing
1575
+      #if ENABLED(EEPROM_INIT_NOW)
1576
+        uint32_t stored_hash;
1577
+        EEPROM_READ_ALWAYS(stored_hash);
1578
+        if (stored_hash != build_hash) { EEPROM_FINISH(); return true; }
1579
+      #endif
1580
+
1581
+      uint16_t stored_crc;
1582
+      EEPROM_READ_ALWAYS(stored_crc);
1583
+
1546
       float dummyf = 0;
1584
       float dummyf = 0;
1547
       working_crc = 0;  // Init to 0. Accumulated by EEPROM_READ
1585
       working_crc = 0;  // Init to 0. Accumulated by EEPROM_READ
1548
 
1586
 
1549
-      _FIELD_TEST(esteppers);
1587
+      _FIELD_TEST(e_factors);
1550
 
1588
 
1551
-      // Number of esteppers may change
1552
-      uint8_t esteppers;
1553
-      EEPROM_READ_ALWAYS(esteppers);
1589
+      // Number of e_factors may change
1590
+      uint8_t e_factors;
1591
+      EEPROM_READ_ALWAYS(e_factors);
1554
 
1592
 
1555
       //
1593
       //
1556
       // Planner Motion
1594
       // Planner Motion
1558
       {
1596
       {
1559
         // Get only the number of E stepper parameters previously stored
1597
         // Get only the number of E stepper parameters previously stored
1560
         // Any steppers added later are set to their defaults
1598
         // Any steppers added later are set to their defaults
1561
-        uint32_t tmp1[LINEAR_AXES + esteppers];
1562
-        float tmp2[LINEAR_AXES + esteppers];
1563
-        feedRate_t tmp3[LINEAR_AXES + esteppers];
1599
+        uint32_t tmp1[LINEAR_AXES + e_factors];
1600
+        float tmp2[LINEAR_AXES + e_factors];
1601
+        feedRate_t tmp3[LINEAR_AXES + e_factors];
1564
         EEPROM_READ((uint8_t *)tmp1, sizeof(tmp1)); // max_acceleration_mm_per_s2
1602
         EEPROM_READ((uint8_t *)tmp1, sizeof(tmp1)); // max_acceleration_mm_per_s2
1565
         EEPROM_READ(planner.settings.min_segment_time_us);
1603
         EEPROM_READ(planner.settings.min_segment_time_us);
1566
         EEPROM_READ((uint8_t *)tmp2, sizeof(tmp2)); // axis_steps_per_mm
1604
         EEPROM_READ((uint8_t *)tmp2, sizeof(tmp2)); // axis_steps_per_mm
1567
         EEPROM_READ((uint8_t *)tmp3, sizeof(tmp3)); // max_feedrate_mm_s
1605
         EEPROM_READ((uint8_t *)tmp3, sizeof(tmp3)); // max_feedrate_mm_s
1568
 
1606
 
1569
         if (!validating) LOOP_DISTINCT_AXES(i) {
1607
         if (!validating) LOOP_DISTINCT_AXES(i) {
1570
-          const bool in = (i < esteppers + LINEAR_AXES);
1608
+          const bool in = (i < e_factors + LINEAR_AXES);
1571
           planner.settings.max_acceleration_mm_per_s2[i] = in ? tmp1[i] : pgm_read_dword(&_DMA[ALIM(i, _DMA)]);
1609
           planner.settings.max_acceleration_mm_per_s2[i] = in ? tmp1[i] : pgm_read_dword(&_DMA[ALIM(i, _DMA)]);
1572
           planner.settings.axis_steps_per_mm[i]          = in ? tmp2[i] : pgm_read_float(&_DASU[ALIM(i, _DASU)]);
1610
           planner.settings.axis_steps_per_mm[i]          = in ? tmp2[i] : pgm_read_float(&_DASU[ALIM(i, _DASU)]);
1573
           planner.settings.max_feedrate_mm_s[i]          = in ? tmp3[i] : pgm_read_float(&_DMF[ALIM(i, _DMF)]);
1611
           planner.settings.max_feedrate_mm_s[i]          = in ? tmp3[i] : pgm_read_float(&_DMF[ALIM(i, _DMF)]);
2496
       return success;
2534
       return success;
2497
     }
2535
     }
2498
     reset();
2536
     reset();
2499
-    #if ENABLED(EEPROM_AUTO_INIT)
2537
+    #if EITHER(EEPROM_AUTO_INIT, EEPROM_INIT_NOW)
2500
       (void)save();
2538
       (void)save();
2501
       SERIAL_ECHO_MSG("EEPROM Initialized");
2539
       SERIAL_ECHO_MSG("EEPROM Initialized");
2502
     #endif
2540
     #endif

+ 10
- 1
buildroot/share/PlatformIO/scripts/preflight-checks.py View File

81
 		#
81
 		#
82
 		# Give warnings on every build
82
 		# Give warnings on every build
83
 		#
83
 		#
84
-		warnfile = os.path.join(env['PROJECT_BUILD_DIR'], build_env, "src", "src", "inc", "Warnings.cpp.o")
84
+		srcpath = os.path.join(env['PROJECT_BUILD_DIR'], build_env, "src", "src")
85
+		warnfile = os.path.join(srcpath, "inc", "Warnings.cpp.o")
85
 		if os.path.exists(warnfile):
86
 		if os.path.exists(warnfile):
86
 			os.remove(warnfile)
87
 			os.remove(warnfile)
87
 
88
 
88
 		#
89
 		#
90
+		# Rebuild 'settings.cpp' for EEPROM_INIT_NOW
91
+		#
92
+		if 'EEPROM_INIT_NOW' in env['MARLIN_FEATURES']:
93
+			setfile = os.path.join(srcpath, "module", "settings.cpp.o")
94
+			if os.path.exists(setfile):
95
+				os.remove(setfile)
96
+
97
+		#
89
 		# Check for old files indicating an entangled Marlin (mixing old and new code)
98
 		# Check for old files indicating an entangled Marlin (mixing old and new code)
90
 		#
99
 		#
91
 		mixedin = []
100
 		mixedin = []

Loading…
Cancel
Save