diff --git a/Marlin/src/HAL/DUE/usb/sd_mmc_spi_mem.cpp b/Marlin/src/HAL/DUE/usb/sd_mmc_spi_mem.cpp index e64c6755bf32d..a7789c5a6455b 100644 --- a/Marlin/src/HAL/DUE/usb/sd_mmc_spi_mem.cpp +++ b/Marlin/src/HAL/DUE/usb/sd_mmc_spi_mem.cpp @@ -19,7 +19,9 @@ void sd_mmc_spi_mem_init() { } inline bool media_ready() { - return IS_SD_INSERTED() && !IS_SD_PRINTING() && !IS_SD_FILE_OPEN() && card.isMounted(); + return DISABLED(DISABLE_DUE_SD_MMC) + && IS_SD_MOUNTED() && IS_SD_INSERTED() + && !IS_SD_FILE_OPEN() && !IS_SD_PRINTING(); } bool sd_mmc_spi_unload(bool) { return true; } @@ -29,17 +31,14 @@ bool sd_mmc_spi_wr_protect() { return false; } bool sd_mmc_spi_removal() { return !media_ready(); } Ctrl_status sd_mmc_spi_test_unit_ready() { - #ifdef DISABLE_DUE_SD_MMC - return CTRL_NO_PRESENT; - #endif - if (!media_ready()) return CTRL_NO_PRESENT; + if (sd_mmc_spi_removal()) return CTRL_NO_PRESENT; return CTRL_GOOD; } // NOTE: This function is defined as returning the address of the last block // in the card, which is cardSize() - 1 Ctrl_status sd_mmc_spi_read_capacity(uint32_t *nb_sector) { - if (!media_ready()) return CTRL_NO_PRESENT; + if (sd_mmc_spi_removal()) return CTRL_NO_PRESENT; *nb_sector = card.diskIODriver()->cardSize() - 1; return CTRL_GOOD; } @@ -58,10 +57,7 @@ uint8_t sector_buf[SD_MMC_BLOCK_SIZE]; // #define DEBUG_MMC Ctrl_status sd_mmc_spi_usb_read_10(uint32_t addr, uint16_t nb_sector) { - #ifdef DISABLE_DUE_SD_MMC - return CTRL_NO_PRESENT; - #endif - if (!media_ready()) return CTRL_NO_PRESENT; + if (sd_mmc_spi_removal()) return CTRL_NO_PRESENT; #ifdef DEBUG_MMC { @@ -97,10 +93,7 @@ Ctrl_status sd_mmc_spi_usb_read_10(uint32_t addr, uint16_t nb_sector) { } Ctrl_status sd_mmc_spi_usb_write_10(uint32_t addr, uint16_t nb_sector) { - #ifdef DISABLE_DUE_SD_MMC - return CTRL_NO_PRESENT; - #endif - if (!media_ready()) return CTRL_NO_PRESENT; + if (sd_mmc_spi_removal()) return CTRL_NO_PRESENT; #ifdef DEBUG_MMC { diff --git a/Marlin/src/feature/pause.cpp b/Marlin/src/feature/pause.cpp index 38ba0d66df9b6..b6cd8c562bda9 100644 --- a/Marlin/src/feature/pause.cpp +++ b/Marlin/src/feature/pause.cpp @@ -421,7 +421,7 @@ bool pause_print(const_float_t retract, const xyz_pos_t &park_point, const bool // Pause the print job and timer #if HAS_MEDIA - const bool was_sd_printing = IS_SD_PRINTING(); + const bool was_sd_printing = card.isStillPrinting(); if (was_sd_printing) { card.pauseSDPrint(); ++did_pause_print; // Indicate SD pause also diff --git a/Marlin/src/feature/powerloss.cpp b/Marlin/src/feature/powerloss.cpp index cd5fc13ddab3b..611f70c0452cb 100644 --- a/Marlin/src/feature/powerloss.cpp +++ b/Marlin/src/feature/powerloss.cpp @@ -113,7 +113,7 @@ void PrintJobRecovery::enable(const bool onoff) { void PrintJobRecovery::changed() { if (!enabled) purge(); - else if (IS_SD_PRINTING()) + else if (card.isStillPrinting()) save(true); TERN_(EXTENSIBLE_UI, ExtUI::onSetPowerLoss(enabled)); } @@ -198,7 +198,7 @@ void PrintJobRecovery::save(const bool force/*=false*/, const float zraise/*=POW // Set Head and Foot to matching non-zero values if (!++info.valid_head) ++info.valid_head; // non-zero in sequence - //if (!IS_SD_PRINTING()) info.valid_head = 0; + //if (!card.isStillPrinting()) info.valid_head = 0; info.valid_foot = info.valid_head; // Machine state @@ -321,7 +321,7 @@ void PrintJobRecovery::save(const bool force/*=false*/, const float zraise/*=POW // Save the current position, distance that Z was (or should be) raised, // and a flag whether the raise was already done here. - if (IS_SD_PRINTING()) save(true, zraise, ENABLED(BACKUP_POWER_SUPPLY)); + if (card.isStillPrinting()) save(true, zraise, ENABLED(BACKUP_POWER_SUPPLY)); // Tell the LCD about the outage, even though it is about to die TERN_(EXTENSIBLE_UI, ExtUI::onPowerLoss()); diff --git a/Marlin/src/gcode/feature/pause/M125.cpp b/Marlin/src/gcode/feature/pause/M125.cpp index b8d9d4811bc77..71fa7c7b4dc9e 100644 --- a/Marlin/src/gcode/feature/pause/M125.cpp +++ b/Marlin/src/gcode/feature/pause/M125.cpp @@ -88,7 +88,7 @@ void GcodeSuite::M125() { park_point += hotend_offset[active_extruder]; #endif - const bool sd_printing = TERN0(HAS_MEDIA, IS_SD_PRINTING()); + const bool sd_printing = IS_SD_PRINTING(); ui.pause_show_message(PAUSE_MESSAGE_PARKING, PAUSE_MODE_PAUSE_PRINT); diff --git a/Marlin/src/gcode/sd/M21_M22.cpp b/Marlin/src/gcode/sd/M21_M22.cpp index 3347168151e73..4c4275f51c6ba 100644 --- a/Marlin/src/gcode/sd/M21_M22.cpp +++ b/Marlin/src/gcode/sd/M21_M22.cpp @@ -49,7 +49,7 @@ void GcodeSuite::M21() { * M22: Release SD Card */ void GcodeSuite::M22() { - if (!IS_SD_PRINTING()) card.release(); + if (!card.isStillPrinting()) card.release(); } #endif // HAS_MEDIA diff --git a/Marlin/src/gcode/sd/M24_M25.cpp b/Marlin/src/gcode/sd/M24_M25.cpp index 7bf1ab74d5bb7..f4ad7c7dabd3c 100644 --- a/Marlin/src/gcode/sd/M24_M25.cpp +++ b/Marlin/src/gcode/sd/M24_M25.cpp @@ -101,7 +101,7 @@ void GcodeSuite::M25() { #else // Set initial pause flag to prevent more commands from landing in the queue while we try to pause - if (IS_SD_PRINTING()) card.pauseSDPrint(); + if (card.isStillPrinting()) card.pauseSDPrint(); #if ENABLED(POWER_LOSS_RECOVERY) && DISABLED(DGUS_LCD_UI_MKS) if (recovery.enabled) recovery.save(true); diff --git a/Marlin/src/gcode/sd/M524.cpp b/Marlin/src/gcode/sd/M524.cpp index 61185b7e0bedc..d72aa0ac3513a 100644 --- a/Marlin/src/gcode/sd/M524.cpp +++ b/Marlin/src/gcode/sd/M524.cpp @@ -42,7 +42,7 @@ void GcodeSuite::M524() { #else - if (IS_SD_PRINTING()) + if (card.isStillPrinting()) card.abortFilePrintSoon(); else if (card.isMounted()) card.closefile(); diff --git a/Marlin/src/lcd/e3v2/jyersui/dwin.cpp b/Marlin/src/lcd/e3v2/jyersui/dwin.cpp index 8e71da82c6de7..ffab19846a5d8 100644 --- a/Marlin/src/lcd/e3v2/jyersui/dwin.cpp +++ b/Marlin/src/lcd/e3v2/jyersui/dwin.cpp @@ -4653,7 +4653,7 @@ void JyersDWIN::popupControl() { #if ENABLED(PARK_HEAD_ON_PAUSE) popupHandler(Popup_Home, true); #if HAS_MEDIA - if (IS_SD_PRINTING()) card.pauseSDPrint(); + if (card.isStillPrinting()) card.pauseSDPrint(); #endif planner.synchronize(); queue.inject(F("M125")); diff --git a/Marlin/src/lcd/extui/ui_api.cpp b/Marlin/src/lcd/extui/ui_api.cpp index 3b30332488a67..a38d45f0738b5 100644 --- a/Marlin/src/lcd/extui/ui_api.cpp +++ b/Marlin/src/lcd/extui/ui_api.cpp @@ -1178,10 +1178,10 @@ namespace ExtUI { } bool isPrintingFromMediaPaused() { - return TERN0(HAS_MEDIA, IS_SD_PAUSED()); + return IS_SD_PAUSED(); } - bool isPrintingFromMedia() { return TERN0(HAS_MEDIA, IS_SD_PRINTING() || IS_SD_PAUSED()); } + bool isPrintingFromMedia() { return IS_SD_PRINTING() || IS_SD_PAUSED(); } bool isPrinting() { return commandsInQueue() || isPrintingFromMedia() || printJobOngoing() || printingIsPaused(); diff --git a/Marlin/src/lcd/marlinui.cpp b/Marlin/src/lcd/marlinui.cpp index 2a78192a70c75..222af64ebf3bf 100644 --- a/Marlin/src/lcd/marlinui.cpp +++ b/Marlin/src/lcd/marlinui.cpp @@ -1480,7 +1480,7 @@ void MarlinUI::host_notify(const char * const cstr) { if (printingIsPaused()) msg = GET_TEXT_F(MSG_PRINT_PAUSED); #if HAS_MEDIA - else if (IS_SD_PRINTING()) + else if (card.isStillPrinting()) return set_status_no_expire(card.longest_filename()); #endif else if (print_job_timer.isRunning()) @@ -1674,7 +1674,7 @@ void MarlinUI::host_notify(const char * const cstr) { void MarlinUI::abort_print() { #if HAS_MEDIA wait_for_heatup = wait_for_user = false; - if (IS_SD_PRINTING()) + if (card.isStillPrinting()) card.abortFilePrintSoon(); else if (card.isMounted()) card.closefile(); diff --git a/Marlin/src/sd/cardreader.cpp b/Marlin/src/sd/cardreader.cpp index c13ba37a96ef7..829c8796434c9 100644 --- a/Marlin/src/sd/cardreader.cpp +++ b/Marlin/src/sd/cardreader.cpp @@ -184,7 +184,7 @@ CardReader::CardReader() { } // -// Get a DOS 8.3 filename in its useful form +// Get a DOS 8.3 filename in its useful form, e.g., "MYFILE.EXT" // char *createFilename(char * const buffer, const dir_t &p) { char *pos = buffer; @@ -193,10 +193,14 @@ char *createFilename(char * const buffer, const dir_t &p) { if (i == 8) *pos++ = '.'; *pos++ = p.name[i]; } - *pos++ = 0; + *pos++ = '\0'; return buffer; } +inline bool extIsBIN(char *ext) { + return ext[0] == 'B' && ext[1] == 'I' && ext[2] == 'N'; +} + // // Return 'true' if the item is a folder, G-code file or Binary file // @@ -215,9 +219,7 @@ bool CardReader::is_visible_entity(const dir_t &p OPTARG(CUSTOM_FIRMWARE_UPLOAD, ) return false; flag.filenameIsDir = DIR_IS_SUBDIR(&p); // We know it's a File or Folder - setBinFlag(p.name[8] == 'B' && // List .bin files (a firmware file for flashing) - p.name[9] == 'I' && - p.name[10]== 'N'); + setBinFlag(extIsBIN((char *)&p.name[8])); // List .bin files (a firmware file for flashing) return ( flag.filenameIsDir // All Directories are ok @@ -576,7 +578,7 @@ void CardReader::manage_media() { */ void CardReader::release() { // Card removed while printing? Abort! - if (IS_SD_PRINTING()) + if (isStillPrinting()) abortFilePrintSoon(); else endFilePrintNow(); @@ -993,7 +995,7 @@ void CardReader::selectFileByIndex(const int16_t nr) { strcpy(filename, sortshort[nr]); strcpy(longFilename, sortnames[nr]); TERN_(HAS_FOLDER_SORTING, flag.filenameIsDir = IS_DIR(nr)); - setBinFlag(strcmp_P(strrchr(filename, '.'), PSTR(".BIN")) == 0); + setBinFlag(extIsBIN(strrchr(filename, '.') + 1)); return; } #endif @@ -1011,7 +1013,7 @@ void CardReader::selectFileByName(const char * const match) { strcpy(filename, sortshort[nr]); strcpy(longFilename, sortnames[nr]); TERN_(HAS_FOLDER_SORTING, flag.filenameIsDir = IS_DIR(nr)); - setBinFlag(strcmp_P(strrchr(filename, '.'), PSTR(".BIN")) == 0); + setBinFlag(extIsBIN(strrchr(filename, '.') + 1)); return; } #endif diff --git a/Marlin/src/sd/cardreader.h b/Marlin/src/sd/cardreader.h index 7dc140b3170ee..86fb6edef67c5 100644 --- a/Marlin/src/sd/cardreader.h +++ b/Marlin/src/sd/cardreader.h @@ -69,19 +69,19 @@ extern const char M23_STR[], M24_STR[]; #endif typedef struct { - bool saving:1, - logging:1, - sdprinting:1, - sdprintdone:1, - mounted:1, - filenameIsDir:1, - workDirIsRoot:1, - abort_sd_printing:1 + bool saving:1, // Receiving a G-code file or logging commands during a print + logging:1, // Log enqueued commands to the open file. See GCodeQueue::advance() + sdprinting:1, // Actively printing from the open file + sdprintdone:1, // The active job has reached the end, 100% + mounted:1, // The card or flash drive is mounted and ready to read/write + filenameIsDir:1, // The working item is a directory + workDirIsRoot:1, // The working directory is / so there's no parent + abort_sd_printing:1 // Abort by calling abortSDPrinting() at the main loop() #if DO_LIST_BIN_FILES - , filenameIsBin:1 + , filenameIsBin:1 // The working item is a BIN file #endif #if ENABLED(BINARY_FILE_TRANSFER) - , binary_mode:1 + , binary_mode:1 // Use the serial line buffer as BinaryStream input #endif ; } card_flags_t; @@ -173,6 +173,7 @@ class CardReader { static void abortFilePrintSoon() { flag.abort_sd_printing = isFileOpen(); } static void pauseSDPrint() { flag.sdprinting = false; } static bool isPrinting() { return flag.sdprinting; } + static bool isStillPrinting() { return flag.sdprinting && !flag.abort_sd_printing; } static bool isPaused() { return isFileOpen() && !isPrinting(); } #if HAS_PRINT_PROGRESS_PERMYRIAD static uint16_t permyriadDone() { @@ -367,8 +368,9 @@ class CardReader { #define IS_SD_INSERTED() true #endif -#define IS_SD_PRINTING() (card.flag.sdprinting && !card.flag.abort_sd_printing) -#define IS_SD_FETCHING() (!card.flag.sdprintdone && IS_SD_PRINTING()) +#define IS_SD_MOUNTED() card.isMounted() +#define IS_SD_PRINTING() card.isStillPrinting() +#define IS_SD_FETCHING() (!card.flag.sdprintdone && card.isStillPrinting()) #define IS_SD_PAUSED() card.isPaused() #define IS_SD_FILE_OPEN() card.isFileOpen() @@ -376,6 +378,7 @@ extern CardReader card; #else // !HAS_MEDIA +#define IS_SD_MOUNTED() false #define IS_SD_PRINTING() false #define IS_SD_FETCHING() false #define IS_SD_PAUSED() false