From 2cd5ab2349dc9e89987e6c8fe68a84a2fa16c2ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Sun, 16 Jul 2023 14:43:08 +0000 Subject: [PATCH] power panic: add readable flags and optimise ISR(INT4_vect) Add more readable names for the EEPROM_UVLO values Change in memory: Flash: -14 bytes SRAM: 0 bytes --- Firmware/Marlin_main.cpp | 10 +++++----- Firmware/power_panic.cpp | 22 +++++++++++++++------- Firmware/power_panic.h | 6 ++++++ 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp index 8763b45dc..abb7b2002 100644 --- a/Firmware/Marlin_main.cpp +++ b/Firmware/Marlin_main.cpp @@ -1457,7 +1457,7 @@ void setup() } eeprom_write_byte((uint8_t*)EEPROM_TEMP_CAL_ACTIVE, 0); } - eeprom_init_default_byte((uint8_t*)EEPROM_UVLO, 0); + eeprom_init_default_byte((uint8_t*)EEPROM_UVLO, NO_PENDING_RECOVERY); eeprom_init_default_byte((uint8_t*)EEPROM_SD_SORT, 0); //mbl_mode_init(); @@ -1578,7 +1578,7 @@ void setup() fw_crash_init(); #ifdef UVLO_SUPPORT - if (eeprom_read_byte((uint8_t*)EEPROM_UVLO) != 0) { //previous print was terminated by UVLO + if (eeprom_read_byte((uint8_t*)EEPROM_UVLO) != NO_PENDING_RECOVERY) { //previous print was terminated by UVLO manage_heater(); // Update temperatures #ifdef DEBUG_UVLO_AUTOMATIC_RECOVER printf_P(_N("Power panic detected!\nCurrent bed temp:%d\nSaved bed temp:%d\n"), (int)degBed(), eeprom_read_byte((uint8_t*)EEPROM_UVLO_TARGET_BED)); @@ -1596,7 +1596,7 @@ void setup() if ( lcd_show_fullscreen_message_yes_no_and_wait_P(_T(MSG_RECOVER_PRINT), false) == LCD_LEFT_BUTTON_CHOICE) { recover_print(0); } else { - eeprom_update_byte((uint8_t*)EEPROM_UVLO, 0); + eeprom_update_byte((uint8_t*)EEPROM_UVLO, NO_PENDING_RECOVERY); lcd_update_enable(true); lcd_update(2); lcd_setstatuspgm(MSG_WELCOME); @@ -4149,7 +4149,7 @@ void process_commands() printf_P(_N("E0:%d RPM\nPRN0:%d RPM\n"), 60*fan_speed[0], 60*fan_speed[1]); } else if (code_seen_P(PSTR("uvlo"))) { // PRUSA uvlo - eeprom_update_byte((uint8_t*)EEPROM_UVLO,0); + eeprom_update_byte((uint8_t*)EEPROM_UVLO, NO_PENDING_RECOVERY); enquecommand_P(MSG_M24); } else if (code_seen_P(PSTR("MMURES"))) // PRUSA MMURES @@ -10660,7 +10660,7 @@ void restore_print_from_ram_and_continue(float e_move) // Cancel the state related to a currently saved print void cancel_saved_printing() { - eeprom_update_byte((uint8_t*)EEPROM_UVLO, 0); + eeprom_update_byte((uint8_t*)EEPROM_UVLO, NO_PENDING_RECOVERY); saved_start_position[0] = SAVED_START_POSITION_UNSET; saved_printing_type = PRINTING_TYPE_NONE; saved_printing = false; diff --git a/Firmware/power_panic.cpp b/Firmware/power_panic.cpp index 4e0704103..0c0837c6a 100644 --- a/Firmware/power_panic.cpp +++ b/Firmware/power_panic.cpp @@ -196,7 +196,7 @@ void uvlo_() { // Note: Recovering a print from EEPROM currently assumes the user // is printing from an SD card, this is why this EEPROM byte is only set // when SD card print is detected - if(sd_print) eeprom_update_byte((uint8_t*)EEPROM_UVLO, 1); + if(sd_print) eeprom_update_byte((uint8_t*)EEPROM_UVLO, PENDING_RECOVERY); // Increment power failure counter eeprom_increment_byte((uint8_t*)EEPROM_POWER_COUNT); @@ -268,7 +268,7 @@ static void uvlo_tiny() { } // Update the the "power outage" flag. - eeprom_update_byte((uint8_t*)EEPROM_UVLO,2); + eeprom_update_byte((uint8_t*)EEPROM_UVLO, PENDING_RECOVERY_RETRY); // Increment power failure counter eeprom_update_byte((uint8_t*)EEPROM_POWER_COUNT, eeprom_read_byte((uint8_t*)EEPROM_POWER_COUNT) + 1); @@ -290,7 +290,7 @@ void setup_uvlo_interrupt() { EIMSK |= (1 << 4); // check if power was lost before we armed the interrupt - if(!(PINE & (1 << 4)) && eeprom_read_byte((uint8_t*)EEPROM_UVLO)) + if(!(PINE & (1 << 4)) && eeprom_read_byte((uint8_t*)EEPROM_UVLO) != NO_PENDING_RECOVERY) { SERIAL_ECHOLNRPGM(MSG_INT4); uvlo_drain_reset(); @@ -300,9 +300,17 @@ void setup_uvlo_interrupt() { ISR(INT4_vect) { EIMSK &= ~(1 << 4); //disable INT4 interrupt to make sure that this code will be executed just once SERIAL_ECHOLNRPGM(MSG_INT4); - //fire normal uvlo only in case where EEPROM_UVLO is 0 or if IS_SD_PRINTING is 1. - if(printer_active() && (!(eeprom_read_byte((uint8_t*)EEPROM_UVLO)))) uvlo_(); - if(eeprom_read_byte((uint8_t*)EEPROM_UVLO)) uvlo_tiny(); + if (eeprom_read_byte((uint8_t*)EEPROM_UVLO) == NO_PENDING_RECOVERY) + { + if(printer_active()) { + uvlo_(); + } + } else { + // There is already a pending recovery waiting. Power outage in this scenario + // arrives before we can fully recover the print. In that case call a 'tiny' + // version of uvlo_() which doesn't overwrite the print state already waiting in EEPROM + uvlo_tiny(); + } } void recover_print(uint8_t automatic) { @@ -315,7 +323,7 @@ void recover_print(uint8_t automatic) { // Lift the print head 25mm, first to avoid collisions with oozed material with the print, // and second also so one may remove the excess priming material. - if(eeprom_read_byte((uint8_t*)EEPROM_UVLO) == 1) + if(eeprom_read_byte((uint8_t*)EEPROM_UVLO) == PENDING_RECOVERY) { enquecommandf_P(PSTR("G1 Z%.3f F800"), current_position[Z_AXIS] + 25); } diff --git a/Firmware/power_panic.h b/Firmware/power_panic.h index cc9926fe6..6a70a2103 100644 --- a/Firmware/power_panic.h +++ b/Firmware/power_panic.h @@ -1,5 +1,11 @@ #pragma once +enum PowerPanicFlag : uint8_t { + NO_PENDING_RECOVERY = 0, + PENDING_RECOVERY = 1, // First power panic, print state is saved in EEPROM + PENDING_RECOVERY_RETRY = 2, // Power outage occured during recovery, print is still saved in EEPROM +}; + void uvlo_(); void recover_print(uint8_t automatic); void setup_uvlo_interrupt();