Refactor SD detect handler (#17380)

Co-Authored-By: Eric Ptak <trouch@users.noreply.github.com>
This commit is contained in:
Scott Lahteine 2020-04-04 00:08:25 -05:00 committed by GitHub
parent bc01d8d023
commit 65f6a373b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 156 additions and 119 deletions

View file

@ -443,7 +443,6 @@ void startOrResumeJob() {
/** /**
* Minimal management of Marlin's core activities: * Minimal management of Marlin's core activities:
* - Check for Filament Runout
* - Keep the command buffer full * - Keep the command buffer full
* - Check for maximum inactive time between commands * - Check for maximum inactive time between commands
* - Check for maximum inactive time between stepper commands * - Check for maximum inactive time between stepper commands
@ -454,13 +453,8 @@ void startOrResumeJob() {
* - Check if an idle but hot extruder needs filament extruded (EXTRUDER_RUNOUT_PREVENT) * - Check if an idle but hot extruder needs filament extruded (EXTRUDER_RUNOUT_PREVENT)
* - Pulse FET_SAFETY_PIN if it exists * - Pulse FET_SAFETY_PIN if it exists
*/ */
inline void manage_inactivity(const bool ignore_stepper_queue=false) { inline void manage_inactivity(const bool ignore_stepper_queue=false) {
#if HAS_FILAMENT_SENSOR
runout.run();
#endif
if (queue.length < BUFSIZE) queue.get_available_commands(); if (queue.length < BUFSIZE) queue.get_available_commands();
const millis_t ms = millis(); const millis_t ms = millis();
@ -644,9 +638,53 @@ inline void manage_inactivity(const bool ignore_stepper_queue=false) {
} }
/** /**
* Standard idle routine keeps the machine alive * Standard idle routine keeps the machine alive:
* - Core Marlin activities
* - Manage heaters (and Watchdog)
* - Max7219 heartbeat, animation, etc.
*
* Only after setup() is complete:
* - Handle filament runout sensors
* - Run HAL idle tasks
* - Handle Power-Loss Recovery
* - Run StallGuard endstop checks
* - Handle SD Card insert / remove
* - Handle USB Flash Drive insert / remove
* - Announce Host Keepalive state (if any)
* - Update the Print Job Timer state
* - Update the Beeper queue
* - Read Buttons and Update the LCD
* - Run i2c Position Encoders
* - Auto-report Temperatures / SD Status
* - Update the Prusa MMU2
* - Handle Joystick jogging
*/ */
void idle(TERN_(ADVANCED_PAUSE_FEATURE, bool no_stepper_sleep/*=false*/)) { void idle(TERN_(ADVANCED_PAUSE_FEATURE, bool no_stepper_sleep/*=false*/)) {
// Core Marlin activities
manage_inactivity(TERN_(ADVANCED_PAUSE_FEATURE, no_stepper_sleep));
// Manage Heaters (and Watchdog)
thermalManager.manage_heater();
// Max7219 heartbeat, animation, etc
#if ENABLED(MAX7219_DEBUG)
max7219.idle_tasks();
#endif
// Return if setup() isn't completed
if (marlin_state == MF_INITIALIZING) return;
// Handle filament runout sensors
#if HAS_FILAMENT_SENSOR
runout.run();
#endif
// Run HAL idle tasks
#ifdef HAL_IDLETASK
HAL_idletask();
#endif
// Handle Power-Loss Recovery // Handle Power-Loss Recovery
#if ENABLED(POWER_LOSS_RECOVERY) && PIN_EXISTS(POWER_LOSS) #if ENABLED(POWER_LOSS_RECOVERY) && PIN_EXISTS(POWER_LOSS)
recovery.outage(); recovery.outage();
@ -660,29 +698,21 @@ void idle(TERN_(ADVANCED_PAUSE_FEATURE, bool no_stepper_sleep/*=false*/)) {
if (endstops.tmc_spi_homing_check()) break; if (endstops.tmc_spi_homing_check()) break;
#endif #endif
// Max7219 heartbeat, animation, etc. // Handle SD Card insert / remove
#if ENABLED(MAX7219_DEBUG) #if ENABLED(SDSUPPORT)
max7219.idle_tasks(); card.manage_media();
#endif #endif
// Read Buttons and Update the LCD // Handle USB Flash Drive insert / remove
ui.update(); #if ENABLED(USB_FLASH_DRIVE_SUPPORT)
Sd2Card::idle();
#endif
// Announce Host Keepalive state (if any) // Announce Host Keepalive state (if any)
#if ENABLED(HOST_KEEPALIVE_FEATURE) #if ENABLED(HOST_KEEPALIVE_FEATURE)
gcode.host_keepalive(); gcode.host_keepalive();
#endif #endif
// Core Marlin activities
manage_inactivity(
#if ENABLED(ADVANCED_PAUSE_FEATURE)
no_stepper_sleep
#endif
);
// Manage heaters (and Watchdog)
thermalManager.manage_heater();
// Update the Print Job Timer state // Update the Print Job Timer state
#if ENABLED(PRINTCOUNTER) #if ENABLED(PRINTCOUNTER)
print_job_timer.tick(); print_job_timer.tick();
@ -693,6 +723,9 @@ void idle(TERN_(ADVANCED_PAUSE_FEATURE, bool no_stepper_sleep/*=false*/)) {
buzzer.tick(); buzzer.tick();
#endif #endif
// Read Buttons and Update the LCD
ui.update();
// Run i2c Position Encoders // Run i2c Position Encoders
#if ENABLED(I2C_POSITION_ENCODERS) #if ENABLED(I2C_POSITION_ENCODERS)
static millis_t i2cpem_next_update_ms; static millis_t i2cpem_next_update_ms;
@ -705,11 +738,6 @@ void idle(TERN_(ADVANCED_PAUSE_FEATURE, bool no_stepper_sleep/*=false*/)) {
} }
#endif #endif
// Run HAL idle tasks
#ifdef HAL_IDLETASK
HAL_idletask();
#endif
// Auto-report Temperatures / SD Status // Auto-report Temperatures / SD Status
#if HAS_AUTO_REPORTING #if HAS_AUTO_REPORTING
if (!gcode.autoreport_paused) { if (!gcode.autoreport_paused) {
@ -722,11 +750,6 @@ void idle(TERN_(ADVANCED_PAUSE_FEATURE, bool no_stepper_sleep/*=false*/)) {
} }
#endif #endif
// Handle USB Flash Drive insert / remove
#if ENABLED(USB_FLASH_DRIVE_SUPPORT)
Sd2Card::idle();
#endif
// Update the Prusa MMU2 // Update the Prusa MMU2
#if ENABLED(PRUSA_MMU2) #if ENABLED(PRUSA_MMU2)
mmu2.mmu_loop(); mmu2.mmu_loop();
@ -983,8 +1006,8 @@ void setup() {
SETUP_RUN(ui.show_bootscreen()); SETUP_RUN(ui.show_bootscreen());
#endif #endif
#if ENABLED(SDSUPPORT) && defined(SDCARD_CONNECTION) && !SD_CONNECTION_IS(LCD) #if BOTH(SDSUPPORT, SDCARD_EEPROM_EMULATION)
SETUP_RUN(card.mount()); // Mount onboard / custom SD card before settings.first_load SETUP_RUN(card.mount()); // Mount media with settings before first_load
#endif #endif
SETUP_RUN(settings.first_load()); // Load data from EEPROM if available (or use defaults) SETUP_RUN(settings.first_load()); // Load data from EEPROM if available (or use defaults)
@ -1151,10 +1174,6 @@ void setup() {
queue.inject_P(PSTR(STARTUP_COMMANDS)); queue.inject_P(PSTR(STARTUP_COMMANDS));
#endif #endif
#if ENABLED(INIT_SDCARD_ON_BOOT) && !HAS_SPI_LCD
SETUP_RUN(card.beginautostart());
#endif
#if ENABLED(HOST_PROMPT_SUPPORT) #if ENABLED(HOST_PROMPT_SUPPORT)
SETUP_RUN(host_action_prompt_end()); SETUP_RUN(host_action_prompt_end());
#endif #endif

View file

@ -1149,28 +1149,7 @@ void MarlinUI::init() {
ExtUI::onStartup(); ExtUI::onStartup();
} }
void MarlinUI::update() { void MarlinUI::update() { ExtUI::onIdle(); }
#if ENABLED(SDSUPPORT)
static bool last_sd_status;
const bool sd_status = IS_SD_INSERTED();
if (sd_status != last_sd_status) {
last_sd_status = sd_status;
if (sd_status) {
card.mount();
if (card.isMounted())
ExtUI::onMediaInserted();
else
ExtUI::onMediaError();
}
else {
const bool ok = card.isMounted();
card.release();
if (ok) ExtUI::onMediaRemoved();
}
}
#endif // SDSUPPORT
ExtUI::onIdle();
}
void MarlinUI::kill_screen(PGM_P const error, PGM_P const component) { void MarlinUI::kill_screen(PGM_P const error, PGM_P const component) {
using namespace ExtUI; using namespace ExtUI;

View file

@ -121,10 +121,6 @@ MarlinUI ui;
#endif #endif
#endif #endif
#if ENABLED(INIT_SDCARD_ON_BOOT)
uint8_t lcd_sd_status;
#endif
#if HAS_LCD_MENU && LCD_TIMEOUT_TO_STATUS #if HAS_LCD_MENU && LCD_TIMEOUT_TO_STATUS
bool MarlinUI::defer_return_to_status; bool MarlinUI::defer_return_to_status;
#endif #endif
@ -342,13 +338,8 @@ void MarlinUI::init() {
#endif // HAS_SHIFT_ENCODER #endif // HAS_SHIFT_ENCODER
#if ENABLED(SDSUPPORT) #if ENABLED(SDSUPPORT) && PIN_EXISTS(SD_DETECT)
#if PIN_EXISTS(SD_DETECT) SET_INPUT_PULLUP(SD_DETECT_PIN);
SET_INPUT_PULLUP(SD_DETECT_PIN);
#endif
#if ENABLED(INIT_SDCARD_ON_BOOT)
lcd_sd_status = 2; // UNKNOWN
#endif
#endif #endif
#if HAS_ENCODER_ACTION && HAS_SLOW_BUTTONS #if HAS_ENCODER_ACTION && HAS_SLOW_BUTTONS
@ -744,11 +735,11 @@ void MarlinUI::quick_feedback(const bool clear_buttons/*=true*/) {
*/ */
LCDViewAction MarlinUI::lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW; LCDViewAction MarlinUI::lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW;
millis_t next_lcd_update_ms;
void MarlinUI::update() { void MarlinUI::update() {
static uint16_t max_display_update_time = 0; static uint16_t max_display_update_time = 0;
static millis_t next_lcd_update_ms;
millis_t ms = millis(); millis_t ms = millis();
#if HAS_LCD_MENU && LCD_TIMEOUT_TO_STATUS #if HAS_LCD_MENU && LCD_TIMEOUT_TO_STATUS
@ -824,53 +815,6 @@ void MarlinUI::update() {
#endif // HAS_LCD_MENU #endif // HAS_LCD_MENU
#if ENABLED(INIT_SDCARD_ON_BOOT)
//
// SPI SD Card detection (and first card init when the LCD is present)
//
const uint8_t sd_status = (uint8_t)IS_SD_INSERTED();
if (sd_status != lcd_sd_status && detected()) {
uint8_t old_sd_status = lcd_sd_status; // prevent re-entry to this block!
lcd_sd_status = sd_status;
if (sd_status) {
safe_delay(500); // Some boards need a delay to get settled
card.mount();
if (old_sd_status == 2)
card.beginautostart(); // Initial boot
else
set_status_P(GET_TEXT(MSG_MEDIA_INSERTED));
}
#if PIN_EXISTS(SD_DETECT)
else {
card.release();
if (old_sd_status != 2) {
set_status_P(GET_TEXT(MSG_MEDIA_REMOVED));
#if HAS_LCD_MENU
return_to_status();
#endif
}
}
#if DISABLED(NO_LCD_REINIT)
init_lcd(); // May revive the LCD if static electricity killed it
#endif
#endif
refresh();
ms = millis();
next_lcd_update_ms = ms + LCD_UPDATE_INTERVAL; // delay LCD update until after SD activity completes
#ifdef LED_BACKLIGHT_TIMEOUT
leds.reset_timeout(ms);
#endif
}
#endif // INIT_SDCARD_ON_BOOT
if (ELAPSED(ms, next_lcd_update_ms) if (ELAPSED(ms, next_lcd_update_ms)
#if HAS_GRAPHICAL_LCD #if HAS_GRAPHICAL_LCD
|| drawing_screen || drawing_screen
@ -1595,3 +1539,55 @@ void MarlinUI::update() {
} }
#endif // !HAS_DISPLAY #endif // !HAS_DISPLAY
#if ENABLED(SDSUPPORT)
void MarlinUI::media_changed(const uint8_t old_status, const uint8_t status) {
if (old_status == status) {
#if ENABLED(EXTENSIBLE_UI)
ExtUI::onMediaError(); // Failed to mount/unmount
#endif
return;
}
if (status) {
#if ENABLED(EXTENSIBLE_UI)
ExtUI::onMediaInserted(); // ExtUI response
#endif
if (old_status < 2)
set_status_P(GET_TEXT(MSG_MEDIA_INSERTED));
}
else {
#if ENABLED(EXTENSIBLE_UI)
ExtUI::onMediaRemoved(); // ExtUI response
#endif
if (old_status < 2) {
#if PIN_EXISTS(SD_DETECT)
set_status_P(GET_TEXT(MSG_MEDIA_REMOVED));
#if HAS_LCD_MENU
return_to_status();
#endif
#endif
}
}
#if PIN_EXISTS(SD_DETECT) && DISABLED(NO_LCD_REINIT)
init_lcd(); // Revive a noisy shared SPI LCD
#endif
refresh();
#if HAS_SPI_LCD || defined(LED_BACKLIGHT_TIMEOUT)
const millis_t ms = millis();
#endif
#if HAS_SPI_LCD
next_lcd_update_ms = ms + LCD_UPDATE_INTERVAL; // Delay LCD update for SD activity
#endif
#ifdef LED_BACKLIGHT_TIMEOUT
leds.reset_timeout(ms);
#endif
}
#endif // SDSUPPORT

View file

@ -274,6 +274,10 @@ public:
// LCD implementations // LCD implementations
static void clear_lcd(); static void clear_lcd();
#if ENABLED(SDSUPPORT)
static void media_changed(const uint8_t old_stat, const uint8_t stat);
#endif
#if HAS_SPI_LCD #if HAS_SPI_LCD
static bool detected(); static bool detected();
static void init_lcd(); static void init_lcd();

View file

@ -378,6 +378,42 @@ void CardReader::mount() {
ui.refresh(); ui.refresh();
} }
/**
* Handle SD card events
*/
#if MB(FYSETC_CHEETAH)
#include "../module/stepper.h"
#endif
void CardReader::manage_media() {
static uint8_t prev_stat = TERN(INIT_SDCARD_ON_BOOT, 2, 0);
uint8_t stat = uint8_t(IS_SD_INSERTED());
if (stat != prev_stat && ui.detected()) {
uint8_t old_stat = prev_stat;
prev_stat = stat; // Change now to prevent re-entry
if (stat) { // Media Inserted
safe_delay(500); // Some boards need a delay to get settled
mount(); // Try to mount the media
#if MB(FYSETC_CHEETAH)
reset_stepper_drivers(); // Workaround for Cheetah bug
#endif
if (!isMounted()) stat = 0; // Not mounted?
}
else {
#if PIN_EXISTS(SD_DETECT)
release(); // Card is released
#endif
}
ui.media_changed(old_stat, stat); // Update the UI
if (stat && old_stat == 2) // First mount?
beginautostart(); // Look for autostart files soon
}
}
void CardReader::release() { void CardReader::release() {
endFilePrint(); endFilePrint();
flag.mounted = false; flag.mounted = false;

View file

@ -73,6 +73,9 @@ public:
static inline bool isMounted() { return flag.mounted; } static inline bool isMounted() { return flag.mounted; }
static void ls(); static void ls();
// Handle media insert/remove
static void manage_media();
// SD Card Logging // SD Card Logging
static void openLogFile(char * const path); static void openLogFile(char * const path);
static void write_command(char * const buf); static void write_command(char * const buf);