From 604c09e6787afec4aad84674896d248a5541336a Mon Sep 17 00:00:00 2001 From: ellensp <530024+ellensp@users.noreply.github.com> Date: Tue, 10 Feb 2026 18:41:48 +1300 Subject: [PATCH 1/2] add sd detect and SD_MAX_READ_ERRORS --- Marlin/Configuration_adv.h | 5 +++++ Marlin/src/core/language.h | 2 ++ Marlin/src/gcode/queue.cpp | 21 ++++++++++++++++++++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h index 84fbd6f8ed..15b5684a86 100644 --- a/Marlin/Configuration_adv.h +++ b/Marlin/Configuration_adv.h @@ -1886,6 +1886,11 @@ #if ENABLED(POWER_LOSS_RECOVER_ZHOME) //#define POWER_LOSS_ZHOME_POS { 0, 0 } // Safe XY position to home Z while avoiding objects on the bed #endif + + // Maximum number of consecutive SD card read errors before aborting the print + // Prevents infinite loops when reading from SD card with hardware/communication faults + #define SD_MAX_READ_ERRORS 5 + #endif /** diff --git a/Marlin/src/core/language.h b/Marlin/src/core/language.h index 837f3885b9..b9ab7c850e 100644 --- a/Marlin/src/core/language.h +++ b/Marlin/src/core/language.h @@ -184,6 +184,8 @@ #define STR_SD_NOT_PRINTING "Not SD printing" #define STR_SD_ERR_WRITE_TO_FILE "error writing to file" #define STR_SD_ERR_READ "SD read error" +#define STR_SD_ERR_TOO_MANY_READ_ERRORS "Too many SD read errors, print aborted" +#define STR_SD_ERR_CARD_REMOVED "SD card removed, print aborted" #define STR_SD_CANT_ENTER_SUBDIR "Cannot enter subdir: " #define STR_ENDSTOPS_HIT "endstops hit: " diff --git a/Marlin/src/gcode/queue.cpp b/Marlin/src/gcode/queue.cpp index 0014ca44a5..011c23afff 100644 --- a/Marlin/src/gcode/queue.cpp +++ b/Marlin/src/gcode/queue.cpp @@ -570,11 +570,30 @@ void GCodeQueue::get_serial_commands() { // Get commands if there are more in the file if (!card.isStillFetching()) return; + #if HAS_SD_DETECT + // Abort print if SD card was removed mid-print + if (!card.isInserted()) { + SERIAL_ERROR_MSG(STR_SD_ERR_CARD_REMOVED); + card.abortFilePrintNow(); + return; + } + #endif + int sd_count = 0; + uint16_t sd_read_errors = 0; while (!ring_buffer.full() && !card.eof()) { const int16_t n = card.get(); const bool card_eof = card.eof(); - if (n < 0 && !card_eof) { SERIAL_ERROR_MSG(STR_SD_ERR_READ); continue; } + if (n < 0 && !card_eof) { + SERIAL_ERROR_MSG(STR_SD_ERR_READ); + if (++sd_read_errors >= SD_MAX_READ_ERRORS) { + SERIAL_ERROR_MSG(STR_SD_ERR_TOO_MANY_READ_ERRORS); + card.abortFilePrintNow(); + break; + } + continue; + } + sd_read_errors = 0; // Reset on successful read CommandLine &command = ring_buffer.commands[ring_buffer.index_w]; const char sd_char = (char)n; From 204ba64266e5de26e4a1e3fc1a0fb76f396f002e Mon Sep 17 00:00:00 2001 From: ellensp <530024+ellensp@users.noreply.github.com> Date: Tue, 10 Feb 2026 19:17:58 +1300 Subject: [PATCH 2/2] move SD_MAX_READ_ERRORS --- Marlin/Configuration_adv.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h index 15b5684a86..2dae863006 100644 --- a/Marlin/Configuration_adv.h +++ b/Marlin/Configuration_adv.h @@ -1886,11 +1886,6 @@ #if ENABLED(POWER_LOSS_RECOVER_ZHOME) //#define POWER_LOSS_ZHOME_POS { 0, 0 } // Safe XY position to home Z while avoiding objects on the bed #endif - - // Maximum number of consecutive SD card read errors before aborting the print - // Prevents infinite loops when reading from SD card with hardware/communication faults - #define SD_MAX_READ_ERRORS 5 - #endif /** @@ -2066,6 +2061,10 @@ #define DEFAULT_SHARED_VOLUME USB_FLASH_DRIVE // :[ 'SD_ONBOARD', 'USB_FLASH_DRIVE' ] #endif + // Maximum number of consecutive SD card read errors before aborting the print + // Prevents infinite loops when reading from SD card with hardware/communication faults + #define SD_MAX_READ_ERRORS 5 + #endif // HAS_MEDIA /**