Skip to content

Commit 0717e9b

Browse files
thinkyheadtrouch
authored and
Andrew McDonald
committed
Refactor SD detect handler (MarlinFirmware#17380)
Co-Authored-By: Eric Ptak <[email protected]>
1 parent 1cbf042 commit 0717e9b

File tree

6 files changed

+156
-119
lines changed

6 files changed

+156
-119
lines changed

Marlin/src/MarlinCore.cpp

+57-38
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,6 @@ void startOrResumeJob() {
443443

444444
/**
445445
* Minimal management of Marlin's core activities:
446-
* - Check for Filament Runout
447446
* - Keep the command buffer full
448447
* - Check for maximum inactive time between commands
449448
* - Check for maximum inactive time between stepper commands
@@ -454,13 +453,8 @@ void startOrResumeJob() {
454453
* - Check if an idle but hot extruder needs filament extruded (EXTRUDER_RUNOUT_PREVENT)
455454
* - Pulse FET_SAFETY_PIN if it exists
456455
*/
457-
458456
inline void manage_inactivity(const bool ignore_stepper_queue=false) {
459457

460-
#if HAS_FILAMENT_SENSOR
461-
runout.run();
462-
#endif
463-
464458
if (queue.length < BUFSIZE) queue.get_available_commands();
465459

466460
const millis_t ms = millis();
@@ -644,9 +638,53 @@ inline void manage_inactivity(const bool ignore_stepper_queue=false) {
644638
}
645639

646640
/**
647-
* Standard idle routine keeps the machine alive
641+
* Standard idle routine keeps the machine alive:
642+
* - Core Marlin activities
643+
* - Manage heaters (and Watchdog)
644+
* - Max7219 heartbeat, animation, etc.
645+
*
646+
* Only after setup() is complete:
647+
* - Handle filament runout sensors
648+
* - Run HAL idle tasks
649+
* - Handle Power-Loss Recovery
650+
* - Run StallGuard endstop checks
651+
* - Handle SD Card insert / remove
652+
* - Handle USB Flash Drive insert / remove
653+
* - Announce Host Keepalive state (if any)
654+
* - Update the Print Job Timer state
655+
* - Update the Beeper queue
656+
* - Read Buttons and Update the LCD
657+
* - Run i2c Position Encoders
658+
* - Auto-report Temperatures / SD Status
659+
* - Update the Prusa MMU2
660+
* - Handle Joystick jogging
648661
*/
649662
void idle(TERN_(ADVANCED_PAUSE_FEATURE, bool no_stepper_sleep/*=false*/)) {
663+
664+
// Core Marlin activities
665+
manage_inactivity(TERN_(ADVANCED_PAUSE_FEATURE, no_stepper_sleep));
666+
667+
// Manage Heaters (and Watchdog)
668+
thermalManager.manage_heater();
669+
670+
// Max7219 heartbeat, animation, etc
671+
#if ENABLED(MAX7219_DEBUG)
672+
max7219.idle_tasks();
673+
#endif
674+
675+
// Return if setup() isn't completed
676+
if (marlin_state == MF_INITIALIZING) return;
677+
678+
// Handle filament runout sensors
679+
#if HAS_FILAMENT_SENSOR
680+
runout.run();
681+
#endif
682+
683+
// Run HAL idle tasks
684+
#ifdef HAL_IDLETASK
685+
HAL_idletask();
686+
#endif
687+
650688
// Handle Power-Loss Recovery
651689
#if ENABLED(POWER_LOSS_RECOVERY) && PIN_EXISTS(POWER_LOSS)
652690
recovery.outage();
@@ -660,29 +698,21 @@ void idle(TERN_(ADVANCED_PAUSE_FEATURE, bool no_stepper_sleep/*=false*/)) {
660698
if (endstops.tmc_spi_homing_check()) break;
661699
#endif
662700

663-
// Max7219 heartbeat, animation, etc.
664-
#if ENABLED(MAX7219_DEBUG)
665-
max7219.idle_tasks();
701+
// Handle SD Card insert / remove
702+
#if ENABLED(SDSUPPORT)
703+
card.manage_media();
666704
#endif
667705

668-
// Read Buttons and Update the LCD
669-
ui.update();
706+
// Handle USB Flash Drive insert / remove
707+
#if ENABLED(USB_FLASH_DRIVE_SUPPORT)
708+
Sd2Card::idle();
709+
#endif
670710

671711
// Announce Host Keepalive state (if any)
672712
#if ENABLED(HOST_KEEPALIVE_FEATURE)
673713
gcode.host_keepalive();
674714
#endif
675715

676-
// Core Marlin activities
677-
manage_inactivity(
678-
#if ENABLED(ADVANCED_PAUSE_FEATURE)
679-
no_stepper_sleep
680-
#endif
681-
);
682-
683-
// Manage heaters (and Watchdog)
684-
thermalManager.manage_heater();
685-
686716
// Update the Print Job Timer state
687717
#if ENABLED(PRINTCOUNTER)
688718
print_job_timer.tick();
@@ -693,6 +723,9 @@ void idle(TERN_(ADVANCED_PAUSE_FEATURE, bool no_stepper_sleep/*=false*/)) {
693723
buzzer.tick();
694724
#endif
695725

726+
// Read Buttons and Update the LCD
727+
ui.update();
728+
696729
// Run i2c Position Encoders
697730
#if ENABLED(I2C_POSITION_ENCODERS)
698731
static millis_t i2cpem_next_update_ms;
@@ -705,11 +738,6 @@ void idle(TERN_(ADVANCED_PAUSE_FEATURE, bool no_stepper_sleep/*=false*/)) {
705738
}
706739
#endif
707740

708-
// Run HAL idle tasks
709-
#ifdef HAL_IDLETASK
710-
HAL_idletask();
711-
#endif
712-
713741
// Auto-report Temperatures / SD Status
714742
#if HAS_AUTO_REPORTING
715743
if (!gcode.autoreport_paused) {
@@ -722,11 +750,6 @@ void idle(TERN_(ADVANCED_PAUSE_FEATURE, bool no_stepper_sleep/*=false*/)) {
722750
}
723751
#endif
724752

725-
// Handle USB Flash Drive insert / remove
726-
#if ENABLED(USB_FLASH_DRIVE_SUPPORT)
727-
Sd2Card::idle();
728-
#endif
729-
730753
// Update the Prusa MMU2
731754
#if ENABLED(PRUSA_MMU2)
732755
mmu2.mmu_loop();
@@ -983,8 +1006,8 @@ void setup() {
9831006
SETUP_RUN(ui.show_bootscreen());
9841007
#endif
9851008

986-
#if ENABLED(SDSUPPORT) && defined(SDCARD_CONNECTION) && !SD_CONNECTION_IS(LCD)
987-
SETUP_RUN(card.mount()); // Mount onboard / custom SD card before settings.first_load
1009+
#if BOTH(SDSUPPORT, SDCARD_EEPROM_EMULATION)
1010+
SETUP_RUN(card.mount()); // Mount media with settings before first_load
9881011
#endif
9891012

9901013
SETUP_RUN(settings.first_load()); // Load data from EEPROM if available (or use defaults)
@@ -1151,10 +1174,6 @@ void setup() {
11511174
queue.inject_P(PSTR(STARTUP_COMMANDS));
11521175
#endif
11531176

1154-
#if ENABLED(INIT_SDCARD_ON_BOOT) && !HAS_SPI_LCD
1155-
SETUP_RUN(card.beginautostart());
1156-
#endif
1157-
11581177
#if ENABLED(HOST_PROMPT_SUPPORT)
11591178
SETUP_RUN(host_action_prompt_end());
11601179
#endif

Marlin/src/lcd/extui/ui_api.cpp

+1-22
Original file line numberDiff line numberDiff line change
@@ -1149,28 +1149,7 @@ void MarlinUI::init() {
11491149
ExtUI::onStartup();
11501150
}
11511151

1152-
void MarlinUI::update() {
1153-
#if ENABLED(SDSUPPORT)
1154-
static bool last_sd_status;
1155-
const bool sd_status = IS_SD_INSERTED();
1156-
if (sd_status != last_sd_status) {
1157-
last_sd_status = sd_status;
1158-
if (sd_status) {
1159-
card.mount();
1160-
if (card.isMounted())
1161-
ExtUI::onMediaInserted();
1162-
else
1163-
ExtUI::onMediaError();
1164-
}
1165-
else {
1166-
const bool ok = card.isMounted();
1167-
card.release();
1168-
if (ok) ExtUI::onMediaRemoved();
1169-
}
1170-
}
1171-
#endif // SDSUPPORT
1172-
ExtUI::onIdle();
1173-
}
1152+
void MarlinUI::update() { ExtUI::onIdle(); }
11741153

11751154
void MarlinUI::kill_screen(PGM_P const error, PGM_P const component) {
11761155
using namespace ExtUI;

Marlin/src/lcd/ultralcd.cpp

+55-59
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,6 @@ MarlinUI ui;
121121
#endif
122122
#endif
123123

124-
#if ENABLED(INIT_SDCARD_ON_BOOT)
125-
uint8_t lcd_sd_status;
126-
#endif
127-
128124
#if HAS_LCD_MENU && LCD_TIMEOUT_TO_STATUS
129125
bool MarlinUI::defer_return_to_status;
130126
#endif
@@ -342,13 +338,8 @@ void MarlinUI::init() {
342338

343339
#endif // HAS_SHIFT_ENCODER
344340

345-
#if ENABLED(SDSUPPORT)
346-
#if PIN_EXISTS(SD_DETECT)
347-
SET_INPUT_PULLUP(SD_DETECT_PIN);
348-
#endif
349-
#if ENABLED(INIT_SDCARD_ON_BOOT)
350-
lcd_sd_status = 2; // UNKNOWN
351-
#endif
341+
#if ENABLED(SDSUPPORT) && PIN_EXISTS(SD_DETECT)
342+
SET_INPUT_PULLUP(SD_DETECT_PIN);
352343
#endif
353344

354345
#if HAS_ENCODER_ACTION && HAS_SLOW_BUTTONS
@@ -744,11 +735,11 @@ void MarlinUI::quick_feedback(const bool clear_buttons/*=true*/) {
744735
*/
745736

746737
LCDViewAction MarlinUI::lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW;
738+
millis_t next_lcd_update_ms;
747739

748740
void MarlinUI::update() {
749741

750742
static uint16_t max_display_update_time = 0;
751-
static millis_t next_lcd_update_ms;
752743
millis_t ms = millis();
753744

754745
#if HAS_LCD_MENU && LCD_TIMEOUT_TO_STATUS
@@ -824,53 +815,6 @@ void MarlinUI::update() {
824815

825816
#endif // HAS_LCD_MENU
826817

827-
#if ENABLED(INIT_SDCARD_ON_BOOT)
828-
//
829-
// SPI SD Card detection (and first card init when the LCD is present)
830-
//
831-
const uint8_t sd_status = (uint8_t)IS_SD_INSERTED();
832-
if (sd_status != lcd_sd_status && detected()) {
833-
834-
uint8_t old_sd_status = lcd_sd_status; // prevent re-entry to this block!
835-
lcd_sd_status = sd_status;
836-
837-
if (sd_status) {
838-
safe_delay(500); // Some boards need a delay to get settled
839-
card.mount();
840-
if (old_sd_status == 2)
841-
card.beginautostart(); // Initial boot
842-
else
843-
set_status_P(GET_TEXT(MSG_MEDIA_INSERTED));
844-
}
845-
#if PIN_EXISTS(SD_DETECT)
846-
else {
847-
card.release();
848-
if (old_sd_status != 2) {
849-
set_status_P(GET_TEXT(MSG_MEDIA_REMOVED));
850-
#if HAS_LCD_MENU
851-
return_to_status();
852-
#endif
853-
}
854-
}
855-
856-
#if DISABLED(NO_LCD_REINIT)
857-
init_lcd(); // May revive the LCD if static electricity killed it
858-
#endif
859-
860-
#endif
861-
862-
refresh();
863-
864-
ms = millis();
865-
next_lcd_update_ms = ms + LCD_UPDATE_INTERVAL; // delay LCD update until after SD activity completes
866-
867-
#ifdef LED_BACKLIGHT_TIMEOUT
868-
leds.reset_timeout(ms);
869-
#endif
870-
}
871-
872-
#endif // INIT_SDCARD_ON_BOOT
873-
874818
if (ELAPSED(ms, next_lcd_update_ms)
875819
#if HAS_GRAPHICAL_LCD
876820
|| drawing_screen
@@ -1595,3 +1539,55 @@ void MarlinUI::update() {
15951539
}
15961540

15971541
#endif // !HAS_DISPLAY
1542+
1543+
#if ENABLED(SDSUPPORT)
1544+
1545+
void MarlinUI::media_changed(const uint8_t old_status, const uint8_t status) {
1546+
if (old_status == status) {
1547+
#if ENABLED(EXTENSIBLE_UI)
1548+
ExtUI::onMediaError(); // Failed to mount/unmount
1549+
#endif
1550+
return;
1551+
}
1552+
1553+
if (status) {
1554+
#if ENABLED(EXTENSIBLE_UI)
1555+
ExtUI::onMediaInserted(); // ExtUI response
1556+
#endif
1557+
if (old_status < 2)
1558+
set_status_P(GET_TEXT(MSG_MEDIA_INSERTED));
1559+
}
1560+
else {
1561+
#if ENABLED(EXTENSIBLE_UI)
1562+
ExtUI::onMediaRemoved(); // ExtUI response
1563+
#endif
1564+
if (old_status < 2) {
1565+
#if PIN_EXISTS(SD_DETECT)
1566+
set_status_P(GET_TEXT(MSG_MEDIA_REMOVED));
1567+
#if HAS_LCD_MENU
1568+
return_to_status();
1569+
#endif
1570+
#endif
1571+
}
1572+
}
1573+
1574+
#if PIN_EXISTS(SD_DETECT) && DISABLED(NO_LCD_REINIT)
1575+
init_lcd(); // Revive a noisy shared SPI LCD
1576+
#endif
1577+
1578+
refresh();
1579+
1580+
#if HAS_SPI_LCD || defined(LED_BACKLIGHT_TIMEOUT)
1581+
const millis_t ms = millis();
1582+
#endif
1583+
1584+
#if HAS_SPI_LCD
1585+
next_lcd_update_ms = ms + LCD_UPDATE_INTERVAL; // Delay LCD update for SD activity
1586+
#endif
1587+
1588+
#ifdef LED_BACKLIGHT_TIMEOUT
1589+
leds.reset_timeout(ms);
1590+
#endif
1591+
}
1592+
1593+
#endif // SDSUPPORT

Marlin/src/lcd/ultralcd.h

+4
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ class MarlinUI {
274274
// LCD implementations
275275
static void clear_lcd();
276276

277+
#if ENABLED(SDSUPPORT)
278+
static void media_changed(const uint8_t old_stat, const uint8_t stat);
279+
#endif
280+
277281
#if HAS_SPI_LCD
278282
static bool detected();
279283
static void init_lcd();

0 commit comments

Comments
 (0)