From fcd61a378d2f58514b87f88f60ad50733f2b9e7a Mon Sep 17 00:00:00 2001 From: gudnimg Date: Sun, 1 Oct 2023 12:44:49 +0000 Subject: [PATCH 1/3] power panic: save print type in EEPROM The printer should know whether it is recovering a SD print or a USB print PFW-1543 --- Firmware/eeprom.h | 8 +++++--- Firmware/power_panic.cpp | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Firmware/eeprom.h b/Firmware/eeprom.h index cc6546b6f..e73c2e80e 100644 --- a/Firmware/eeprom.h +++ b/Firmware/eeprom.h @@ -134,7 +134,9 @@ static_assert(sizeof(Sheets) == EEPROM_SHEETS_SIZEOF, "Sizeof(Sheets) is not EEP | 0x0F95 3989 | char | EEPROM_FILENAME | ??? | ffh 255 | Power Panic Filename | ??? | D3 Ax0f95 C8 | 0x0F91 3985 | unint32 | EEPROM_FILE_POSITION | ??? | ff ff ff ffh | Power Panic File Position | ??? | D3 Ax0f91 C4 | 0x0F8D 3981 | float | EEPROM_UVLO_CURRENT_POSITION_Z | ??? | ff ff ff ffh | Power Panic Z Position | ^ | D3 Ax0f8d C4 -| 0x0F8C 3980 | ??? | EEPROM_UVLO_UNUSED_001 | ??? | ffh 255 | Power Panic _unused_ | ^ | D3 Ax0f8c C1 +| 0x0F8C 3980 | uint8 | EEPROM_UVLO_PRINT_TYPE | 00h 0 | ffh 255 | Power Panic print type: SD | ??? | D3 Ax0f8c C1 +| ^ | ^ | ^ | 01h 1 | ffh 255 | Power Panic print type: USB | ^ | ^ +| ^ | ^ | ^ | 02h 2 | ffh 255 | Power Panic print type: None | ^ | ^ | 0x0F8B 3979 | uint8 | EEPROM_UVLO_TARGET_BED | ??? | ffh 255 | Power Panic Bed temperature | ^ | D3 Ax0f8b C1 | 0x0F89 3977 | uint16 | EEPROM_UVLO_FEEDRATE | ??? | ff ffh 65535 | Power Panic Feedrate | ^ | D3 Ax0f89 C2 | 0x0F88 3976 | uint8 | EEPROM_UVLO_FAN_SPEED | ??? | ffh 255 | Power Panic Fan speed | ^ | D3 Ax0f88 C1 @@ -424,8 +426,8 @@ static_assert(sizeof(Sheets) == EEPROM_SHEETS_SIZEOF, "Sizeof(Sheets) is not EEP #define EEPROM_FILENAME (EEPROM_UVLO_CURRENT_POSITION - 8) //8chars to store filename without extension #define EEPROM_FILE_POSITION (EEPROM_FILENAME - 4) //32 bit for uint32_t file position #define EEPROM_UVLO_CURRENT_POSITION_Z (EEPROM_FILE_POSITION - 4) //float for current position in Z -#define EEPROM_UVLO_UNUSED_001 (EEPROM_UVLO_CURRENT_POSITION_Z - 1) // uint8_t (unused) -#define EEPROM_UVLO_TARGET_BED (EEPROM_UVLO_UNUSED_001 - 1) +#define EEPROM_UVLO_PRINT_TYPE (EEPROM_UVLO_CURRENT_POSITION_Z - 1) // uint8_t +#define EEPROM_UVLO_TARGET_BED (EEPROM_UVLO_PRINT_TYPE - 1) #define EEPROM_UVLO_FEEDRATE (EEPROM_UVLO_TARGET_BED - 2) //uint16_t #define EEPROM_UVLO_FAN_SPEED (EEPROM_UVLO_FEEDRATE - 1) #define EEPROM_FAN_CHECK_ENABLED (EEPROM_UVLO_FAN_SPEED - 1) diff --git a/Firmware/power_panic.cpp b/Firmware/power_panic.cpp index adf3a033d..1c7a7f928 100644 --- a/Firmware/power_panic.cpp +++ b/Firmware/power_panic.cpp @@ -189,6 +189,7 @@ void uvlo_() { eeprom_update_block(saved_start_position, (float *)EEPROM_UVLO_SAVED_START_POSITION, sizeof(saved_start_position)); eeprom_update_word((uint16_t*)EEPROM_UVLO_SAVED_SEGMENT_IDX, saved_segment_idx); + eeprom_update_byte((uint8_t*)EEPROM_UVLO_PRINT_TYPE, saved_printing_type); #ifdef LIN_ADVANCE eeprom_update_float((float*)(EEPROM_UVLO_LA_K), extruder_advance_K); From 3eaca29b0ba6297b1312b78e87cf1668033a259d Mon Sep 17 00:00:00 2001 From: gudnimg Date: Sun, 1 Oct 2023 12:57:25 +0000 Subject: [PATCH 2/3] PFW-1543 Move printing types into enum Also add power panic namespace --- Firmware/Marlin.h | 3 --- Firmware/Marlin_main.cpp | 32 ++++++++++++++++---------------- Firmware/cardreader.cpp | 3 ++- Firmware/power_panic.cpp | 12 ++++++------ Firmware/power_panic.h | 9 +++++++++ 5 files changed, 33 insertions(+), 26 deletions(-) diff --git a/Firmware/Marlin.h b/Firmware/Marlin.h index b0c024c0e..091541310 100755 --- a/Firmware/Marlin.h +++ b/Firmware/Marlin.h @@ -298,9 +298,6 @@ extern bool mesh_bed_leveling_flag; extern bool saved_printing; extern uint32_t saved_sdpos; extern uint8_t saved_printing_type; -#define PRINTING_TYPE_SD 0 -#define PRINTING_TYPE_USB 1 -#define PRINTING_TYPE_NONE 2 extern uint16_t saved_extruder_temperature; //!< Active extruder temperature extern uint8_t saved_bed_temperature; //!< Bed temperature diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp index 4730f7f00..e1fc9e7be 100644 --- a/Firmware/Marlin_main.cpp +++ b/Firmware/Marlin_main.cpp @@ -310,7 +310,7 @@ static bool chdkActive = false; //! @{ bool saved_printing = false; //!< Print is paused and saved in RAM uint32_t saved_sdpos = 0; //!< SD card position, or line number in case of USB printing -uint8_t saved_printing_type = PRINTING_TYPE_SD; +uint8_t saved_printing_type = PowerPanic::PRINT_TYPE_SD; float saved_pos[NUM_AXIS] = { X_COORD_INVALID, 0, 0, 0 }; uint16_t saved_feedrate2 = 0; //!< Default feedrate (truncated from float) static int saved_feedmultiply2 = 0; @@ -638,9 +638,9 @@ void crashdet_cancel() { saved_printing = false; tmc2130_sg_stop_on_crash = true; - if (saved_printing_type == PRINTING_TYPE_SD) { + if (saved_printing_type == PowerPanic::PRINT_TYPE_SD) { print_stop(); - }else if(saved_printing_type == PRINTING_TYPE_USB){ + }else if(saved_printing_type == PowerPanic::PRINT_TYPE_USB){ SERIAL_ECHOLNRPGM(MSG_OCTOPRINT_CANCEL); //for Octoprint: works the same as clicking "Abort" button in Octoprint GUI cmdqueue_reset(); } @@ -1458,7 +1458,7 @@ void setup() } eeprom_write_byte((uint8_t*)EEPROM_TEMP_CAL_ACTIVE, 0); } - eeprom_init_default_byte((uint8_t*)EEPROM_UVLO, NO_PENDING_RECOVERY); + eeprom_init_default_byte((uint8_t*)EEPROM_UVLO, PowerPanic::NO_PENDING_RECOVERY); eeprom_init_default_byte((uint8_t*)EEPROM_SD_SORT, 0); //mbl_mode_init(); @@ -1579,7 +1579,7 @@ void setup() fw_crash_init(); #ifdef UVLO_SUPPORT - if (eeprom_read_byte((uint8_t*)EEPROM_UVLO) != NO_PENDING_RECOVERY) { //previous print was terminated by UVLO + if (eeprom_read_byte((uint8_t*)EEPROM_UVLO) != PowerPanic::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)); @@ -1597,7 +1597,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, NO_PENDING_RECOVERY); + eeprom_update_byte((uint8_t*)EEPROM_UVLO, PowerPanic::NO_PENDING_RECOVERY); lcd_update_enable(true); lcd_update(2); lcd_setstatuspgm(MSG_WELCOME); @@ -1728,7 +1728,7 @@ void loop() KEEPALIVE_STATE(NOT_BUSY); } - if (isPrintPaused && saved_printing_type == PRINTING_TYPE_USB) { //keep believing that usb is being printed. Prevents accessing dangerous menus while pausing. + if (isPrintPaused && saved_printing_type == PowerPanic::PRINT_TYPE_USB) { //keep believing that usb is being printed. Prevents accessing dangerous menus while pausing. usb_timer.start(); } else if (usb_timer.expired(10000)) { //just need to check if it expired. Nothing else is needed to be done. @@ -4084,7 +4084,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, NO_PENDING_RECOVERY); + eeprom_update_byte((uint8_t*)EEPROM_UVLO, PowerPanic::NO_PENDING_RECOVERY); enquecommand_P(MSG_M24); } else if (code_seen_P(PSTR("MMURES"))) // PRUSA MMURES @@ -10341,7 +10341,7 @@ void save_print_file_state() { saved_sdpos -= sdlen_planner; sdlen_cmdqueue = cmdqueue_calc_sd_length(); //length of sd commands in cmdqueue saved_sdpos -= sdlen_cmdqueue; - saved_printing_type = PRINTING_TYPE_SD; + saved_printing_type = PowerPanic::PRINT_TYPE_SD; } else if (usb_timer.running()) { //reuse saved_sdpos for storing line number saved_sdpos = gcode_LastN; //start with line number of command added recently to cmd queue @@ -10349,10 +10349,10 @@ void save_print_file_state() { nlines = planner_calc_sd_length(); //number of lines of commands in planner saved_sdpos -= nlines; saved_sdpos -= buflen; //number of blocks in cmd buffer - saved_printing_type = PRINTING_TYPE_USB; + saved_printing_type = PowerPanic::PRINT_TYPE_USB; } else { - saved_printing_type = PRINTING_TYPE_NONE; + saved_printing_type = PowerPanic::PRINT_TYPE_NONE; //not sd printing nor usb printing } @@ -10446,11 +10446,11 @@ void save_print_file_state() { } void restore_print_file_state() { - if (saved_printing_type == PRINTING_TYPE_SD) { //was sd printing + if (saved_printing_type == PowerPanic::PRINT_TYPE_SD) { //was sd printing card.setIndex(saved_sdpos); sdpos_atomic = saved_sdpos; card.sdprinting = true; - } else if (saved_printing_type == PRINTING_TYPE_USB) { //was usb printing + } else if (saved_printing_type == PowerPanic::PRINT_TYPE_USB) { //was usb printing gcode_LastN = saved_sdpos; //saved_sdpos was reused for storing line number when usb printing serial_count = 0; FlushSerialRequestResend(); @@ -10642,7 +10642,7 @@ void restore_print_from_ram_and_continue(float e_move) restore_print_file_state(); lcd_setstatuspgm(MSG_WELCOME); - saved_printing_type = PRINTING_TYPE_NONE; + saved_printing_type = PowerPanic::PRINT_TYPE_NONE; saved_printing = false; planner_aborted = true; // unroll the stack } @@ -10650,9 +10650,9 @@ 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, NO_PENDING_RECOVERY); + eeprom_update_byte((uint8_t*)EEPROM_UVLO, PowerPanic::NO_PENDING_RECOVERY); saved_start_position[0] = SAVED_START_POSITION_UNSET; - saved_printing_type = PRINTING_TYPE_NONE; + saved_printing_type = PowerPanic::PRINT_TYPE_NONE; saved_printing = false; } diff --git a/Firmware/cardreader.cpp b/Firmware/cardreader.cpp index 3f437af1f..1d6c50ec6 100644 --- a/Firmware/cardreader.cpp +++ b/Firmware/cardreader.cpp @@ -7,6 +7,7 @@ #include "temperature.h" #include "language.h" #include "Prusa_farm.h" +#include "power_panic.h" #ifdef SDSUPPORT @@ -557,7 +558,7 @@ void CardReader::getStatus(bool arg_P) { if (isPrintPaused) { - if (saved_printing && (saved_printing_type == PRINTING_TYPE_SD)) + if (saved_printing && (saved_printing_type == PowerPanic::PRINT_TYPE_SD)) SERIAL_PROTOCOLLNPGM("SD print paused"); else SERIAL_PROTOCOLLNPGM("Print saved"); diff --git a/Firmware/power_panic.cpp b/Firmware/power_panic.cpp index 1c7a7f928..f439c9c40 100644 --- a/Firmware/power_panic.cpp +++ b/Firmware/power_panic.cpp @@ -41,7 +41,7 @@ void uvlo_() { unsigned long time_start = _millis(); // True if a print is already saved to RAM - bool sd_print_saved_in_ram = saved_printing && (saved_printing_type == PRINTING_TYPE_SD); + bool sd_print_saved_in_ram = saved_printing && (saved_printing_type == PowerPanic::PRINT_TYPE_SD); // Flag to decide whether or not to set EEPROM_UVLO bit bool sd_print = card.sdprinting || sd_print_saved_in_ram; @@ -199,7 +199,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, PENDING_RECOVERY); + if(sd_print) eeprom_update_byte((uint8_t*)EEPROM_UVLO, PowerPanic::PENDING_RECOVERY); // Increment power failure counter eeprom_increment_byte((uint8_t*)EEPROM_POWER_COUNT); @@ -272,7 +272,7 @@ static void uvlo_tiny() { } // Update the the "power outage" flag. - eeprom_update_byte((uint8_t*)EEPROM_UVLO, PENDING_RECOVERY_RETRY); + eeprom_update_byte((uint8_t*)EEPROM_UVLO, PowerPanic::PENDING_RECOVERY_RETRY); // Increment power failure counter eeprom_increment_byte((uint8_t*)EEPROM_POWER_COUNT); @@ -294,7 +294,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) != NO_PENDING_RECOVERY) + if(!(PINE & (1 << 4)) && eeprom_read_byte((uint8_t*)EEPROM_UVLO) != PowerPanic::NO_PENDING_RECOVERY) { SERIAL_ECHOLNRPGM(MSG_INT4); uvlo_drain_reset(); @@ -304,7 +304,7 @@ 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); - if (eeprom_read_byte((uint8_t*)EEPROM_UVLO) == NO_PENDING_RECOVERY) + if (eeprom_read_byte((uint8_t*)EEPROM_UVLO) == PowerPanic::NO_PENDING_RECOVERY) { if(printer_active()) { uvlo_(); @@ -327,7 +327,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) == PENDING_RECOVERY) + if(eeprom_read_byte((uint8_t*)EEPROM_UVLO) == PowerPanic::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 6a70a2103..7dcb627dc 100644 --- a/Firmware/power_panic.h +++ b/Firmware/power_panic.h @@ -1,11 +1,20 @@ #pragma once +namespace PowerPanic { 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 }; +// Types of printjobs possible when power panic is triggered +enum PrintType : uint8_t { + PRINT_TYPE_SD = 0, + PRINT_TYPE_USB = 1, + PRINT_TYPE_NONE = 2, +}; +} // namespace PowerPanic + void uvlo_(); void recover_print(uint8_t automatic); void setup_uvlo_interrupt(); From b89c62bd5099a2dabd4015ac826d35817311d377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Wed, 4 Oct 2023 20:57:05 +0000 Subject: [PATCH 3/3] PFW-1543 Replace ffh 255 with ^ --- Firmware/eeprom.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Firmware/eeprom.h b/Firmware/eeprom.h index e73c2e80e..2a0c1d2cf 100644 --- a/Firmware/eeprom.h +++ b/Firmware/eeprom.h @@ -135,8 +135,8 @@ static_assert(sizeof(Sheets) == EEPROM_SHEETS_SIZEOF, "Sizeof(Sheets) is not EEP | 0x0F91 3985 | unint32 | EEPROM_FILE_POSITION | ??? | ff ff ff ffh | Power Panic File Position | ??? | D3 Ax0f91 C4 | 0x0F8D 3981 | float | EEPROM_UVLO_CURRENT_POSITION_Z | ??? | ff ff ff ffh | Power Panic Z Position | ^ | D3 Ax0f8d C4 | 0x0F8C 3980 | uint8 | EEPROM_UVLO_PRINT_TYPE | 00h 0 | ffh 255 | Power Panic print type: SD | ??? | D3 Ax0f8c C1 -| ^ | ^ | ^ | 01h 1 | ffh 255 | Power Panic print type: USB | ^ | ^ -| ^ | ^ | ^ | 02h 2 | ffh 255 | Power Panic print type: None | ^ | ^ +| ^ | ^ | ^ | 01h 1 | ^ | Power Panic print type: USB | ^ | ^ +| ^ | ^ | ^ | 02h 2 | ^ | Power Panic print type: None | ^ | ^ | 0x0F8B 3979 | uint8 | EEPROM_UVLO_TARGET_BED | ??? | ffh 255 | Power Panic Bed temperature | ^ | D3 Ax0f8b C1 | 0x0F89 3977 | uint16 | EEPROM_UVLO_FEEDRATE | ??? | ff ffh 65535 | Power Panic Feedrate | ^ | D3 Ax0f89 C2 | 0x0F88 3976 | uint8 | EEPROM_UVLO_FAN_SPEED | ??? | ffh 255 | Power Panic Fan speed | ^ | D3 Ax0f88 C1