From cf0e0f3718095c6b91563493b95101e0dd810ba4 Mon Sep 17 00:00:00 2001 From: gudnimg Date: Sun, 24 Sep 2023 12:30:35 +0000 Subject: [PATCH] M600: use partial backup in RAM To handle power panic in M600 we started saving relevant data at the start of M600 gcode. We are currently also saving the same data within gcode_M600 which is saved on the stack. I propose we just use the data already saved in SRAM to reduce stack usage. Change in memory: Flash: -64 bytes SRAM: 0 bytes --- Firmware/Marlin.h | 4 ++-- Firmware/Marlin_main.cpp | 43 +++++++++++++++------------------------- 2 files changed, 18 insertions(+), 29 deletions(-) diff --git a/Firmware/Marlin.h b/Firmware/Marlin.h index 3526bae66..924ef30ab 100755 --- a/Firmware/Marlin.h +++ b/Firmware/Marlin.h @@ -449,8 +449,8 @@ void gcode_M701(float fastLoadLength, uint8_t mmuSlotIndex); void M600_load_filament(); void M600_load_filament_movements(); -void M600_wait_for_user(float HotendTempBckp); -void M600_check_state(float nozzle_temp); +void M600_wait_for_user(); +void M600_check_state(); void load_filament_final_feed(); void marlin_wait_for_click(); float raise_z(float delta); diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp index f1007ec3f..404e79f66 100644 --- a/Firmware/Marlin_main.cpp +++ b/Firmware/Marlin_main.cpp @@ -3385,7 +3385,7 @@ static void mmu_M600_unload_filament() { /// @brief load filament for mmu v2 /// @par nozzle_temp nozzle temperature to load filament -static void mmu_M600_load_filament(bool automatic, float nozzle_temp) { +static void mmu_M600_load_filament(bool automatic) { uint8_t slot; if (automatic) { slot = SpoolJoin::spooljoin.nextSlot(); @@ -3394,7 +3394,7 @@ static void mmu_M600_load_filament(bool automatic, float nozzle_temp) { slot = choose_menu_P(_T(MSG_SELECT_FILAMENT), _T(MSG_FILAMENT)); } - setTargetHotend(nozzle_temp); + setTargetHotend(saved_extruder_temperature); MMU2::mmu2.load_filament_to_nozzle(slot); @@ -3404,7 +3404,6 @@ static void mmu_M600_load_filament(bool automatic, float nozzle_temp) { static void gcode_M600(const bool automatic, const float x_position, const float y_position, const float z_shift, const float e_shift, const float e_shift_late) { st_synchronize(); - float lastpos[4]; // When using an MMU, save the currently use slot number // so the firmware can know which slot to eject after the filament @@ -3413,13 +3412,6 @@ static void gcode_M600(const bool automatic, const float x_position, const float prusa_statistics(22); - //First backup current position and settings - int feedmultiplyBckp = feedmultiply; - float HotendTempBckp = saved_extruder_temperature; - uint8_t fanSpeedBckp = fanSpeed; - - memcpy(lastpos, current_position, sizeof(lastpos)); - // Turn off the fan fanSpeed = 0; @@ -3450,7 +3442,7 @@ static void gcode_M600(const bool automatic, const float x_position, const float mmu_M600_unload_filament(); } else { // Beep, manage nozzle heater and wait for user to start unload filament - M600_wait_for_user(HotendTempBckp); + M600_wait_for_user(); unload_filament(e_shift_late); } st_synchronize(); // finish moves @@ -3475,15 +3467,15 @@ static void gcode_M600(const bool automatic, const float x_position, const float else // MMU is enabled { if (!automatic) mmu_M600_filament_change_screen(eject_slot); - mmu_M600_load_filament(automatic, HotendTempBckp); + mmu_M600_load_filament(automatic); } if (!automatic) - M600_check_state(HotendTempBckp); + M600_check_state(); lcd_update_enable(true); // Not let's go back to print - fanSpeed = fanSpeedBckp; + fanSpeed = saved_fan_speed; // Feed a little of filament to stabilize pressure if (!automatic) { @@ -3506,22 +3498,22 @@ static void gcode_M600(const bool automatic, const float x_position, const float } // Move XY back - plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], FILAMENTCHANGE_XYFEED); + plan_buffer_line(saved_pos[X_AXIS], saved_pos[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], FILAMENTCHANGE_XYFEED); st_synchronize(); // Move Z back - plan_buffer_line(lastpos[X_AXIS], lastpos[Y_AXIS], lastpos[Z_AXIS], current_position[E_AXIS], FILAMENTCHANGE_ZFEED); + plan_buffer_line(saved_pos[X_AXIS], saved_pos[Y_AXIS], saved_pos[Z_AXIS], current_position[E_AXIS], FILAMENTCHANGE_ZFEED); st_synchronize(); // Set E position to original - plan_set_e_position(lastpos[E_AXIS]); + plan_set_e_position(saved_pos[E_AXIS]); - memcpy(current_position, lastpos, sizeof(lastpos)); + memcpy(current_position, saved_pos, sizeof(saved_pos)); set_destination_to_current(); // Recover feed rate - feedmultiply = feedmultiplyBckp; - enquecommandf_P(MSG_M220, feedmultiplyBckp); + feedmultiply = saved_feedmultiply2; + enquecommandf_P(MSG_M220, saved_feedmultiply2); } if (isPrintPaused) lcd_setstatuspgm(_T(MSG_PRINT_PAUSED)); else lcd_setstatuspgm(MSG_WELCOME); @@ -10832,8 +10824,7 @@ void load_filament_final_feed() } //! @brief Wait for user to check the state -//! @par nozzle_temp nozzle temperature to load filament -void M600_check_state(float nozzle_temp) +void M600_check_state() { uint8_t lcd_change_filament_state = 0; while (lcd_change_filament_state != 1) @@ -10855,7 +10846,7 @@ void M600_check_state(float nozzle_temp) mmu_M600_filament_change_screen(eject_slot); // After user clicks knob, MMU will load the filament - mmu_M600_load_filament(false, nozzle_temp); + mmu_M600_load_filament(false); } else { M600_load_filament_movements(); } @@ -10881,9 +10872,7 @@ void M600_check_state(float nozzle_temp) //! //! Beep, manage nozzle heater and wait for user to start unload filament //! If times out, active extruder temperature is set to 0. -//! -//! @param HotendTempBckp Temperature to be restored for active extruder, after user resolves MMU problem. -void M600_wait_for_user(float HotendTempBckp) { +void M600_wait_for_user() { KEEPALIVE_STATE(PAUSED_FOR_USER); @@ -10912,7 +10901,7 @@ void M600_wait_for_user(float HotendTempBckp) { delay_keep_alive(4); if (lcd_clicked()) { - setTargetHotend(HotendTempBckp); + setTargetHotend(saved_extruder_temperature); lcd_wait_for_heater(); wait_for_user_state = 2; }