Browse Source

Optimize EEPROM usage/functions

D.R.racer 2 years ago
parent
commit
455c29e78b

+ 2 - 2
Firmware/Filament_sensor.cpp

@@ -161,8 +161,8 @@ void Filament_sensor::filRunout() {
     autoLoadEnabled = false;
     stop_and_save_print_to_ram(0, 0);
     restore_print_from_ram_and_continue(0);
-    eeprom_update_byte((uint8_t *)EEPROM_FERROR_COUNT, eeprom_read_byte((uint8_t *)EEPROM_FERROR_COUNT) + 1);
-    eeprom_update_word((uint16_t *)EEPROM_FERROR_COUNT_TOT, eeprom_read_word((uint16_t *)EEPROM_FERROR_COUNT_TOT) + 1);
+    eeprom_increment_byte((uint8_t *)EEPROM_FERROR_COUNT, 1);
+    eeprom_increment_word((uint16_t *)EEPROM_FERROR_COUNT_TOT, 1);
     enquecommand_front_P((PSTR("M600")));
 }
 

+ 10 - 15
Firmware/Marlin_main.cpp

@@ -609,13 +609,13 @@ void crashdet_detected(uint8_t mask)
 
 	if (mask & X_AXIS_MASK)
 	{
-		eeprom_update_byte((uint8_t*)EEPROM_CRASH_COUNT_X, eeprom_read_byte((uint8_t*)EEPROM_CRASH_COUNT_X) + 1);
-		eeprom_update_word((uint16_t*)EEPROM_CRASH_COUNT_X_TOT, eeprom_read_word((uint16_t*)EEPROM_CRASH_COUNT_X_TOT) + 1);
+		eeprom_increment_byte((uint8_t*)EEPROM_CRASH_COUNT_X, 1);
+		eeprom_increment_word((uint16_t*)EEPROM_CRASH_COUNT_X_TOT, 1);
 	}
 	if (mask & Y_AXIS_MASK)
 	{
-		eeprom_update_byte((uint8_t*)EEPROM_CRASH_COUNT_Y, eeprom_read_byte((uint8_t*)EEPROM_CRASH_COUNT_Y) + 1);
-		eeprom_update_word((uint16_t*)EEPROM_CRASH_COUNT_Y_TOT, eeprom_read_word((uint16_t*)EEPROM_CRASH_COUNT_Y_TOT) + 1);
+		eeprom_increment_byte((uint8_t*)EEPROM_CRASH_COUNT_Y, 1);
+		eeprom_increment_word((uint16_t*)EEPROM_CRASH_COUNT_Y_TOT, 1);
 	}
 
 	lcd_update_enable(true);
@@ -1444,9 +1444,7 @@ void setup()
 
 #endif //(LANG_MODE != 0)
 
-	if (eeprom_read_byte((uint8_t*)EEPROM_TEMP_CAL_ACTIVE) == 255) {
-		eeprom_write_byte((uint8_t*)EEPROM_TEMP_CAL_ACTIVE, 0);
-	}
+	eeprom_init_default_byte((uint8_t*)EEPROM_TEMP_CAL_ACTIVE, 0);
 
 	if (eeprom_read_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA) == 255) {
 		//eeprom_write_byte((uint8_t*)EEPROM_CALIBRATION_STATUS_PINDA, 0);
@@ -1457,12 +1455,9 @@ void setup()
 		}
 		eeprom_write_byte((uint8_t*)EEPROM_TEMP_CAL_ACTIVE, 0);
 	}
-	if (eeprom_read_byte((uint8_t*)EEPROM_UVLO) == 255) {
-		eeprom_write_byte((uint8_t*)EEPROM_UVLO, 0);
-	}
-	if (eeprom_read_byte((uint8_t*)EEPROM_SD_SORT) == 255) {
-		eeprom_write_byte((uint8_t*)EEPROM_SD_SORT, 0);
-	}
+	eeprom_init_default_byte((uint8_t*)EEPROM_UVLO, 0);
+	eeprom_init_default_byte((uint8_t*)EEPROM_SD_SORT, 0);
+
 	//mbl_mode_init();
 	mbl_settings_init();
 	SilentModeMenu_MMU = eeprom_read_byte((uint8_t*)EEPROM_MMU_STEALTH);
@@ -10686,8 +10681,8 @@ void uvlo_()
 	if(sd_print) eeprom_update_byte((uint8_t*)EEPROM_UVLO, 1);
 
     // Increment power failure counter
-	eeprom_update_byte((uint8_t*)EEPROM_POWER_COUNT, eeprom_read_byte((uint8_t*)EEPROM_POWER_COUNT) + 1);
-	eeprom_update_word((uint16_t*)EEPROM_POWER_COUNT_TOT, eeprom_read_word((uint16_t*)EEPROM_POWER_COUNT_TOT) + 1);
+	eeprom_increment_byte((uint8_t*)EEPROM_POWER_COUNT, 1);
+	eeprom_increment_word((uint16_t*)EEPROM_POWER_COUNT_TOT, 1);
 
     printf_P(_N("UVLO - end %d\n"), _millis() - time_start);
     WRITE(BEEPER,HIGH);

+ 33 - 15
Firmware/eeprom.cpp

@@ -49,19 +49,19 @@ bool eeprom_is_sheet_initialized(uint8_t sheet_num)
 
 void eeprom_init()
 {
-    if (eeprom_read_byte((uint8_t*)EEPROM_POWER_COUNT) == 0xff) eeprom_write_byte((uint8_t*)EEPROM_POWER_COUNT, 0);
-    if (eeprom_read_byte((uint8_t*)EEPROM_CRASH_COUNT_X) == 0xff) eeprom_write_byte((uint8_t*)EEPROM_CRASH_COUNT_X, 0);
-    if (eeprom_read_byte((uint8_t*)EEPROM_CRASH_COUNT_Y) == 0xff) eeprom_write_byte((uint8_t*)EEPROM_CRASH_COUNT_Y, 0);
-    if (eeprom_read_byte((uint8_t*)EEPROM_FERROR_COUNT) == 0xff) eeprom_write_byte((uint8_t*)EEPROM_FERROR_COUNT, 0);
-    if (eeprom_read_word((uint16_t*)EEPROM_POWER_COUNT_TOT) == 0xffff) eeprom_write_word((uint16_t*)EEPROM_POWER_COUNT_TOT, 0);
-    if (eeprom_read_word((uint16_t*)EEPROM_CRASH_COUNT_X_TOT) == 0xffff) eeprom_write_word((uint16_t*)EEPROM_CRASH_COUNT_X_TOT, 0);
-    if (eeprom_read_word((uint16_t*)EEPROM_CRASH_COUNT_Y_TOT) == 0xffff) eeprom_write_word((uint16_t*)EEPROM_CRASH_COUNT_Y_TOT, 0);
-    if (eeprom_read_word((uint16_t*)EEPROM_FERROR_COUNT_TOT) == 0xffff) eeprom_write_word((uint16_t*)EEPROM_FERROR_COUNT_TOT, 0);
-
-    if (eeprom_read_word((uint16_t*)EEPROM_MMU_FAIL_TOT) == 0xffff) eeprom_update_word((uint16_t *)EEPROM_MMU_FAIL_TOT, 0);
-    if (eeprom_read_word((uint16_t*)EEPROM_MMU_LOAD_FAIL_TOT) == 0xffff) eeprom_update_word((uint16_t *)EEPROM_MMU_LOAD_FAIL_TOT, 0);
-    if (eeprom_read_byte((uint8_t*)EEPROM_MMU_FAIL) == 0xff) eeprom_update_byte((uint8_t *)EEPROM_MMU_FAIL, 0);
-    if (eeprom_read_byte((uint8_t*)EEPROM_MMU_LOAD_FAIL) == 0xff) eeprom_update_byte((uint8_t *)EEPROM_MMU_LOAD_FAIL, 0);
+    eeprom_init_default_byte((uint8_t*)EEPROM_POWER_COUNT, 0);
+    eeprom_init_default_byte((uint8_t*)EEPROM_CRASH_COUNT_X, 0);
+    eeprom_init_default_byte((uint8_t*)EEPROM_CRASH_COUNT_Y, 0);
+    eeprom_init_default_byte((uint8_t*)EEPROM_FERROR_COUNT, 0);
+    eeprom_init_default_word((uint16_t*)EEPROM_POWER_COUNT_TOT, 0);
+    eeprom_init_default_word((uint16_t*)EEPROM_CRASH_COUNT_X_TOT, 0);
+    eeprom_init_default_word((uint16_t*)EEPROM_CRASH_COUNT_Y_TOT, 0);
+    eeprom_init_default_word((uint16_t*)EEPROM_FERROR_COUNT_TOT, 0);
+
+    eeprom_init_default_word((uint16_t*)EEPROM_MMU_FAIL_TOT, 0);
+    eeprom_init_default_word((uint16_t*)EEPROM_MMU_LOAD_FAIL_TOT, 0);
+    eeprom_init_default_byte((uint8_t*)EEPROM_MMU_FAIL, 0);
+    eeprom_init_default_byte((uint8_t*)EEPROM_MMU_LOAD_FAIL, 0);
     if (eeprom_read_dword((uint32_t*)EEPROM_TOTAL_TOOLCHANGE_COUNT) == 0xffffffff) eeprom_update_dword((uint32_t *)EEPROM_TOTAL_TOOLCHANGE_COUNT, 0);
     if (eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet)) == EEPROM_EMPTY_VALUE)
     {
@@ -102,12 +102,12 @@ if (eeprom_read_byte((uint8_t*)EEPROM_PINDA_TEMP_COMPENSATION) == 0xff) eeprom_u
 	if (eeprom_read_dword((uint32_t*)EEPROM_JOB_ID) == EEPROM_EMPTY_VALUE32)
 		eeprom_update_dword((uint32_t*)EEPROM_JOB_ID, 0);
 
-    if (eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME) == 255 && eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME + 1) == 255 && eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME + 2) == 255 && eeprom_read_byte((uint8_t *)EEPROM_TOTALTIME + 3) == 255) {
+    if (eeprom_read_dword((uint32_t *)EEPROM_TOTALTIME) == 0xffffffff) {
         eeprom_update_dword((uint32_t *)EEPROM_TOTALTIME, 0);
         eeprom_update_dword((uint32_t *)EEPROM_FILAMENTUSED, 0);
     }
 //Set Cutter OFF if 0xff
-    if (eeprom_read_byte((uint8_t*)EEPROM_MMU_CUTTER_ENABLED) == 0xff) eeprom_update_byte((uint8_t *)EEPROM_MMU_CUTTER_ENABLED, 0);
+    eeprom_init_default_byte((uint8_t*)EEPROM_MMU_CUTTER_ENABLED, 0);
 }
 
 //! @brief Get default sheet name for index
@@ -181,3 +181,21 @@ void eeprom_switch_to_next_sheet()
     sheet = eeprom_next_initialized_sheet(sheet);
     if (sheet >= 0) eeprom_update_byte(&(EEPROM_Sheets_base->active_sheet), sheet);
 }
+
+void __attribute__((noinline)) eeprom_increment_byte(uint8_t *__p, uint8_t inc){
+    eeprom_update_byte(__p, eeprom_read_byte(__p) + inc);
+}
+
+void __attribute__((noinline)) eeprom_increment_word(uint16_t *__p, uint8_t inc){
+    eeprom_update_word(__p, eeprom_read_word(__p) + inc);
+}
+
+void __attribute__((noinline)) eeprom_init_default_byte(uint8_t *__p, uint8_t def){
+    if (eeprom_read_byte(__p) == 0xff)
+        eeprom_write_byte(__p, def);
+}
+
+void __attribute__((noinline)) eeprom_init_default_word(uint16_t *__p, uint16_t def){
+    if (eeprom_read_word(__p) == 0xffff)
+        eeprom_write_word(__p, def);
+}

+ 6 - 0
Firmware/eeprom.h

@@ -612,6 +612,12 @@ struct SheetName
 void eeprom_default_sheet_name(uint8_t index, SheetName &sheetName);
 int8_t eeprom_next_initialized_sheet(int8_t sheet);
 void eeprom_switch_to_next_sheet();
+
+void eeprom_increment_byte(uint8_t *__p, uint8_t inc);
+void eeprom_increment_word(uint16_t *__p, uint8_t inc);
+
+void eeprom_init_default_byte(uint8_t *__p, uint8_t def);
+void eeprom_init_default_word(uint16_t *__p, uint16_t def);
 #endif
 
 #endif // EEPROM_H

+ 2 - 6
Firmware/mesh_bed_calibration.cpp

@@ -3123,12 +3123,8 @@ void mbl_mode_init() {
 void mbl_settings_init() {
 //3x3 mesh; 3 Z-probes on each point, magnet elimination on
 //magnet elimination: use aaproximate Z-coordinate instead of measured values for points which are near magnets
-	if (eeprom_read_byte((uint8_t*)EEPROM_MBL_MAGNET_ELIMINATION) == 0xFF) {
-		eeprom_update_byte((uint8_t*)EEPROM_MBL_MAGNET_ELIMINATION, 1);
-	}
-	if (eeprom_read_byte((uint8_t*)EEPROM_MBL_POINTS_NR) == 0xFF) {
-		eeprom_update_byte((uint8_t*)EEPROM_MBL_POINTS_NR, 3);
-	}
+	eeprom_init_default_byte((uint8_t*)EEPROM_MBL_MAGNET_ELIMINATION, 1);
+	eeprom_init_default_byte((uint8_t*)EEPROM_MBL_POINTS_NR, 3);
 	mbl_z_probe_nr = eeprom_read_byte((uint8_t*)EEPROM_MBL_PROBE_NR);
 	if (mbl_z_probe_nr == 0xFF) {
 		mbl_z_probe_nr = 3;

+ 1 - 2
Firmware/util.cpp

@@ -243,8 +243,7 @@ void fCheckModeInit() {
     }
     if (farm_mode) {
         oCheckMode = ClCheckMode::_Strict;
-        if (eeprom_read_word((uint16_t *)EEPROM_NOZZLE_DIAMETER_uM) == EEPROM_EMPTY_VALUE16)
-            eeprom_update_word((uint16_t *)EEPROM_NOZZLE_DIAMETER_uM, EEPROM_NOZZLE_DIAMETER_uM_DEFAULT);
+        eeprom_init_default_word((uint16_t *)EEPROM_NOZZLE_DIAMETER_uM, EEPROM_NOZZLE_DIAMETER_uM_DEFAULT);
     }
     oNozzleDiameter = (ClNozzleDiameter)eeprom_read_byte((uint8_t *)EEPROM_NOZZLE_DIAMETER);
     if ((oNozzleDiameter == ClNozzleDiameter::_Diameter_Undef) && !farm_mode) {