From 8f4ac82273489ebd014b573561d0785abc78e69a Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Thu, 17 Nov 2022 13:19:08 +0100 Subject: [PATCH] eeprom_init_default also returns the read/default value --- Firmware/Filament_sensor.cpp | 4 +-- Firmware/Marlin_main.cpp | 54 ++++++++++-------------------------- Firmware/eeprom.cpp | 39 +++++++++++++++----------- Firmware/eeprom.h | 6 ++-- 4 files changed, 42 insertions(+), 61 deletions(-) diff --git a/Firmware/Filament_sensor.cpp b/Firmware/Filament_sensor.cpp index 504931071..dbccf2760 100644 --- a/Firmware/Filament_sensor.cpp +++ b/Firmware/Filament_sensor.cpp @@ -481,8 +481,8 @@ void PAT9125_sensor::filJam() { jamDetection = 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); + eeprom_increment_word((uint16_t *)EEPROM_FERROR_COUNT_TOT); enquecommand_front_P((PSTR("M600"))); } diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp index 104a532e6..91bb05852 100644 --- a/Firmware/Marlin_main.cpp +++ b/Firmware/Marlin_main.cpp @@ -1045,9 +1045,7 @@ void setup() lcd_splash(); Sound_Init(); // also guarantee "SET_OUTPUT(BEEPER)" - selectedSerialPort = eeprom_read_byte((uint8_t *)EEPROM_SECOND_SERIAL_ACTIVE); - if (selectedSerialPort == 0xFF) selectedSerialPort = 0; - eeprom_update_byte((uint8_t *)EEPROM_SECOND_SERIAL_ACTIVE, selectedSerialPort); + selectedSerialPort = eeprom_init_default_byte((uint8_t *)EEPROM_SECOND_SERIAL_ACTIVE, 0); MYSERIAL.begin(BAUDRATE); fdev_setup_stream(uartout, uart_putchar, NULL, _FDEV_SETUP_WRITE); //setup uart out stream stdout = uartout; @@ -1253,22 +1251,11 @@ void setup() plan_init(); // Initialize planner; factory_reset(); - if (eeprom_read_dword((uint32_t*)(EEPROM_TOP - 4)) == 0x0ffffffff && - eeprom_read_dword((uint32_t*)(EEPROM_TOP - 8)) == 0x0ffffffff) - { - // Maiden startup. The firmware has been loaded and first started on a virgin RAMBo board, - // where all the EEPROM entries are set to 0x0ff. - // Once a firmware boots up, it forces at least a language selection, which changes - // EEPROM_LANG to number lower than 0x0ff. - // 1) Set a high power mode. - eeprom_update_byte((uint8_t*)EEPROM_SILENT, SILENT_MODE_OFF); -#ifdef TMC2130 - tmc2130_mode = TMC2130_MODE_NORMAL; -#endif //TMC2130 - eeprom_write_byte((uint8_t*)EEPROM_WIZARD_ACTIVE, 1); //run wizard - } - lcd_encoder_diff=0; + eeprom_init_default_byte((uint8_t*)EEPROM_SILENT, SILENT_MODE_OFF); + eeprom_init_default_byte((uint8_t*)EEPROM_WIZARD_ACTIVE, 1); //run wizard if uninitialized + + lcd_encoder_diff=0; #ifdef TMC2130 uint8_t silentMode = eeprom_read_byte((uint8_t*)EEPROM_SILENT); @@ -1550,22 +1537,15 @@ void setup() update_current_firmware_version_to_eeprom(); #ifdef TMC2130 - tmc2130_home_origin[X_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_HOME_X_ORIGIN); - tmc2130_home_bsteps[X_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_HOME_X_BSTEPS); - tmc2130_home_fsteps[X_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_HOME_X_FSTEPS); - if (tmc2130_home_origin[X_AXIS] == 0xff) tmc2130_home_origin[X_AXIS] = 0; - if (tmc2130_home_bsteps[X_AXIS] == 0xff) tmc2130_home_bsteps[X_AXIS] = 48; - if (tmc2130_home_fsteps[X_AXIS] == 0xff) tmc2130_home_fsteps[X_AXIS] = 48; + tmc2130_home_origin[X_AXIS] = eeprom_init_default_byte((uint8_t*)EEPROM_TMC2130_HOME_X_ORIGIN, 0); + tmc2130_home_bsteps[X_AXIS] = eeprom_init_default_byte((uint8_t*)EEPROM_TMC2130_HOME_X_BSTEPS, 48); + tmc2130_home_fsteps[X_AXIS] = eeprom_init_default_byte((uint8_t*)EEPROM_TMC2130_HOME_X_FSTEPS, 48); - tmc2130_home_origin[Y_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_HOME_Y_ORIGIN); - tmc2130_home_bsteps[Y_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_HOME_Y_BSTEPS); - tmc2130_home_fsteps[Y_AXIS] = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_HOME_Y_FSTEPS); - if (tmc2130_home_origin[Y_AXIS] == 0xff) tmc2130_home_origin[Y_AXIS] = 0; - if (tmc2130_home_bsteps[Y_AXIS] == 0xff) tmc2130_home_bsteps[Y_AXIS] = 48; - if (tmc2130_home_fsteps[Y_AXIS] == 0xff) tmc2130_home_fsteps[Y_AXIS] = 48; + tmc2130_home_origin[Y_AXIS] = eeprom_init_default_byte((uint8_t*)EEPROM_TMC2130_HOME_Y_ORIGIN, 0); + tmc2130_home_bsteps[Y_AXIS] = eeprom_init_default_byte((uint8_t*)EEPROM_TMC2130_HOME_Y_BSTEPS, 48); + tmc2130_home_fsteps[Y_AXIS] = eeprom_init_default_byte((uint8_t*)EEPROM_TMC2130_HOME_Y_FSTEPS, 48); - tmc2130_home_enabled = eeprom_read_byte((uint8_t*)EEPROM_TMC2130_HOME_ENABLED); - if (tmc2130_home_enabled == 0xff) tmc2130_home_enabled = 0; + tmc2130_home_enabled = eeprom_init_default_byte((uint8_t*)EEPROM_TMC2130_HOME_ENABLED, 0); #endif //TMC2130 // report crash failures @@ -9776,14 +9756,8 @@ bool setTargetedHotend(int code, uint8_t &extruder) void save_statistics(unsigned long _total_filament_used, unsigned long _total_print_time) //_total_filament_used unit: mm/100; print time in s { - 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) - { - eeprom_update_dword((uint32_t *)EEPROM_TOTALTIME, 0); - eeprom_update_dword((uint32_t *)EEPROM_FILAMENTUSED, 0); - } - - unsigned long _previous_filament = eeprom_read_dword((uint32_t *)EEPROM_FILAMENTUSED); //_previous_filament unit: cm - unsigned long _previous_time = eeprom_read_dword((uint32_t *)EEPROM_TOTALTIME); //_previous_time unit: min + unsigned long _previous_filament = eeprom_init_default_dword((uint32_t *)EEPROM_FILAMENTUSED, 0); //_previous_filament unit: cm + unsigned long _previous_time = eeprom_init_default_dword((uint32_t *)EEPROM_TOTALTIME, 0); //_previous_time unit: min eeprom_update_dword((uint32_t *)EEPROM_TOTALTIME, _previous_time + (_total_print_time/60)); //EEPROM_TOTALTIME unit: min eeprom_update_dword((uint32_t *)EEPROM_FILAMENTUSED, _previous_filament + (_total_filament_used / 1000)); diff --git a/Firmware/eeprom.cpp b/Firmware/eeprom.cpp index 2d256af12..df0b89694 100644 --- a/Firmware/eeprom.cpp +++ b/Firmware/eeprom.cpp @@ -26,7 +26,7 @@ void eeprom_init() 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); + eeprom_init_default_dword((uint32_t*)EEPROM_TOTAL_TOOLCHANGE_COUNT, 0); if (eeprom_read_byte(&(EEPROM_Sheets_base->active_sheet)) == EEPROM_EMPTY_VALUE) { eeprom_update_byte(&(EEPROM_Sheets_base->active_sheet), 0); @@ -50,17 +50,12 @@ void eeprom_init() check_babystep(); #ifdef PINDA_TEMP_COMP -if (eeprom_read_byte((uint8_t*)EEPROM_PINDA_TEMP_COMPENSATION) == 0xff) eeprom_update_byte((uint8_t *)EEPROM_PINDA_TEMP_COMPENSATION, 0); + eeprom_init_default_byte((uint8_t*)EEPROM_PINDA_TEMP_COMPENSATION, 0); #endif //PINDA_TEMP_COMP - if (eeprom_read_dword((uint32_t*)EEPROM_JOB_ID) == EEPROM_EMPTY_VALUE32) - eeprom_update_dword((uint32_t*)EEPROM_JOB_ID, 0); - - 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 + eeprom_init_default_dword((uint32_t*)EEPROM_JOB_ID, 0); + eeprom_init_default_dword((uint32_t*)EEPROM_TOTALTIME, 0); + eeprom_init_default_dword((uint32_t*)EEPROM_FILAMENTUSED, 0); eeprom_init_default_byte((uint8_t*)EEPROM_MMU_CUTTER_ENABLED, 0); } @@ -184,19 +179,31 @@ void __attribute__((noinline)) eeprom_add_dword(uint32_t *__p, uint32_t add) { } -void __attribute__((noinline)) eeprom_init_default_byte(uint8_t *__p, uint8_t def) { - if (eeprom_read_byte(__p) == EEPROM_EMPTY_VALUE) +uint8_t __attribute__((noinline)) eeprom_init_default_byte(uint8_t *__p, uint8_t def) { + uint8_t val = eeprom_read_byte(__p); + if (val == EEPROM_EMPTY_VALUE) { eeprom_write_byte(__p, def); + return def; + } + return val; } -void __attribute__((noinline)) eeprom_init_default_word(uint16_t *__p, uint16_t def) { - if (eeprom_read_word(__p) == EEPROM_EMPTY_VALUE16) +uint16_t __attribute__((noinline)) eeprom_init_default_word(uint16_t *__p, uint16_t def) { + uint16_t val = eeprom_read_word(__p); + if (val == EEPROM_EMPTY_VALUE16) { eeprom_write_word(__p, def); + return def; + } + return val; } -void __attribute__((noinline)) eeprom_init_default_dword(uint32_t *__p, uint32_t def) { - if (eeprom_read_dword(__p) == EEPROM_EMPTY_VALUE32) +uint32_t __attribute__((noinline)) eeprom_init_default_dword(uint32_t *__p, uint32_t def) { + uint32_t val = eeprom_read_dword(__p); + if (val == EEPROM_EMPTY_VALUE32) { eeprom_write_dword(__p, def); + return def; + } + return val; } void __attribute__((noinline)) eeprom_init_default_float(float *__p, float def) { diff --git a/Firmware/eeprom.h b/Firmware/eeprom.h index 465bf8d25..42d3a242c 100644 --- a/Firmware/eeprom.h +++ b/Firmware/eeprom.h @@ -624,9 +624,9 @@ void eeprom_add_byte(uint8_t *__p, uint8_t add); void eeprom_add_word(uint16_t *__p, uint16_t add); void eeprom_add_dword(uint32_t *__p, uint32_t add); -void eeprom_init_default_byte(uint8_t *__p, uint8_t def); -void eeprom_init_default_word(uint16_t *__p, uint16_t def); -void eeprom_init_default_dword(uint32_t *__p, uint32_t def); +uint8_t eeprom_init_default_byte(uint8_t *__p, uint8_t def); +uint16_t eeprom_init_default_word(uint16_t *__p, uint16_t def); +uint32_t eeprom_init_default_dword(uint32_t *__p, uint32_t def); void eeprom_init_default_float(float *__p, float def); void eeprom_init_default_block(void *__p, size_t __n, const void *def); void eeprom_init_default_block_P(void *__p, size_t __n, const void *def);