From e1e0b0afa128f86e236140baf44d6497c4c62094 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Sun, 13 Aug 2023 18:24:20 +0000 Subject: [PATCH] Refactor code which resets crash detection setting Add function crashdet_use_eeprom_setting Change in memory: Flash: -52 bytes SRAM: 0 bytes --- Firmware/Marlin_main.cpp | 21 +++++++++++---------- Firmware/tmc2130.cpp | 3 +++ Firmware/tmc2130.h | 3 +++ Firmware/ultralcd.cpp | 34 +++++----------------------------- Firmware/ultralcd.h | 6 ------ 5 files changed, 22 insertions(+), 45 deletions(-) diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp index f7590f976..01c423711 100644 --- a/Firmware/Marlin_main.cpp +++ b/Firmware/Marlin_main.cpp @@ -631,12 +631,16 @@ void crashdet_detected(uint8_t mask) void crashdet_recover() { - if (!print_job_timer.isPaused()) crashdet_restore_print_and_continue(); - if (lcd_crash_detect_enabled()) tmc2130_sg_stop_on_crash = true; + if (!print_job_timer.isPaused()) crashdet_restore_print_and_continue(); + crashdet_use_eeprom_setting(); } /// Crash detection cancels the print void crashdet_cancel() { + // Restore crash detection + crashdet_use_eeprom_setting(); + + // Abort the print print_stop(); } @@ -1269,14 +1273,11 @@ void setup() if (silentMode == 0xff) silentMode = 0; tmc2130_mode = TMC2130_MODE_NORMAL; - if (lcd_crash_detect_enabled() && !farm_mode) - { - lcd_crash_detect_enable(); - puts_P(_N("CrashDetect ENABLED!")); - } - else - { - lcd_crash_detect_disable(); + tmc2130_sg_stop_on_crash = eeprom_init_default_byte((uint8_t*)EEPROM_CRASH_DET, farm_mode ? false : true); + + if (tmc2130_sg_stop_on_crash) { + puts_P(_N("CrashDetect ENABLED!")); + } else { puts_P(_N("CrashDetect DISABLED")); } diff --git a/Firmware/tmc2130.cpp b/Firmware/tmc2130.cpp index 1e4cce56d..d95f6dff3 100755 --- a/Firmware/tmc2130.cpp +++ b/Firmware/tmc2130.cpp @@ -390,6 +390,9 @@ void tmc2130_st_isr() } } +void crashdet_use_eeprom_setting() { + tmc2130_sg_stop_on_crash = eeprom_read_byte((uint8_t*)EEPROM_CRASH_DET); +} bool tmc2130_update_sg() { diff --git a/Firmware/tmc2130.h b/Firmware/tmc2130.h index 7ebc875e6..8f4b7761a 100644 --- a/Firmware/tmc2130.h +++ b/Firmware/tmc2130.h @@ -162,6 +162,9 @@ extern void tmc2130_sg_measure_start(uint8_t axis); //stop current stallguard measuring and report result extern uint16_t tmc2130_sg_measure_stop(); +// Enable or Disable crash detection according to EEPROM +void crashdet_use_eeprom_setting(); + extern void tmc2130_setup_chopper(uint8_t axis, uint8_t mres); //set holding current for any axis (M911) diff --git a/Firmware/ultralcd.cpp b/Firmware/ultralcd.cpp index 85f76c6a7..2fe0d2fbf 100644 --- a/Firmware/ultralcd.cpp +++ b/Firmware/ultralcd.cpp @@ -3412,7 +3412,7 @@ static void lcd_silent_mode_set() { #endif //TMC2130 #ifdef TMC2130 - if (lcd_crash_detect_enabled() && (SilentModeMenu != SILENT_MODE_NORMAL)) + if (eeprom_read_byte((uint8_t*)EEPROM_CRASH_DET) && (SilentModeMenu != SILENT_MODE_NORMAL)) menu_submenu(lcd_crash_mode_info2); #endif //TMC2130 } @@ -3420,8 +3420,8 @@ static void lcd_silent_mode_set() { #ifdef TMC2130 static void crash_mode_switch() { - if (lcd_crash_detect_enabled()) lcd_crash_detect_disable(); - else lcd_crash_detect_enable(); + eeprom_toggle((uint8_t*)EEPROM_CRASH_DET); + crashdet_use_eeprom_setting(); } #endif //TMC2130 @@ -4114,7 +4114,7 @@ static void SETTINGS_SILENT_MODE() { MENU_ITEM_TOGGLE_P(_T(MSG_MODE), _T(MSG_NORMAL), lcd_silent_mode_set); } - MENU_ITEM_TOGGLE_P(_T(MSG_CRASHDETECT), lcd_crash_detect_enabled() ? _T(MSG_ON) : _T(MSG_OFF), crash_mode_switch); + MENU_ITEM_TOGGLE_P(_T(MSG_CRASHDETECT), eeprom_read_byte((uint8_t*)EEPROM_CRASH_DET) ? _T(MSG_ON) : _T(MSG_OFF), crash_mode_switch); } else { @@ -6204,7 +6204,7 @@ static void reset_crash_det(uint8_t axis) { current_position[axis] += 10; plan_buffer_line_curposXYZE(manual_feedrate[0] / 60); st_synchronize(); - if (eeprom_read_byte((uint8_t*)EEPROM_CRASH_DET)) tmc2130_sg_stop_on_crash = true; + crashdet_use_eeprom_setting(); } static bool lcd_selfcheck_axis_sg(uint8_t axis) { @@ -7348,30 +7348,6 @@ void menu_lcd_lcdupdate_func(void) if (lcd_commands_type == LcdCommands::Layer1Cal) lcd_commands(); } -#ifdef TMC2130 -//! @brief Is crash detection enabled? -//! -//! @retval true crash detection enabled -//! @retval false crash detection disabled -bool lcd_crash_detect_enabled() -{ - return eeprom_read_byte((uint8_t*)EEPROM_CRASH_DET); -} - -void lcd_crash_detect_enable() -{ - tmc2130_sg_stop_on_crash = true; - eeprom_update_byte((uint8_t*)EEPROM_CRASH_DET, 0xFF); -} - -void lcd_crash_detect_disable() -{ - tmc2130_sg_stop_on_crash = false; - tmc2130_sg_crash = 0; - eeprom_update_byte((uint8_t*)EEPROM_CRASH_DET, 0x00); -} -#endif - #ifdef TMC2130 void UserECool_toggle(){ // this is only called when the experimental menu is visible, thus the first condition for enabling of the ECool mode is met in this place diff --git a/Firmware/ultralcd.h b/Firmware/ultralcd.h index e8d3bb8ab..a396af470 100755 --- a/Firmware/ultralcd.h +++ b/Firmware/ultralcd.h @@ -71,12 +71,6 @@ void lcd_status_screen(); // NOT static due to using ins void lcd_menu_extruder_info(); // NOT static due to using inside "Marlin_main" module ("manage_inactivity()") void lcd_menu_show_sensors_state(); // NOT static due to using inside "Marlin_main" module ("manage_inactivity()") -#ifdef TMC2130 -bool lcd_crash_detect_enabled(); -void lcd_crash_detect_enable(); -void lcd_crash_detect_disable(); -#endif - enum LCDButtonChoice : uint_fast8_t { LCD_LEFT_BUTTON_CHOICE = 0, LCD_MIDDLE_BUTTON_CHOICE = 1,