Browse Source

🩹 Adjust manage_media for slow/late media init (USB FD) (#24015)

Robby Candra 2 years ago
parent
commit
15b6159f6a
1 changed files with 25 additions and 13 deletions
  1. 25
    13
      Marlin/src/sd/cardreader.cpp

+ 25
- 13
Marlin/src/sd/cardreader.cpp View File

@@ -472,29 +472,32 @@ void CardReader::mount() {
472 472
 #endif
473 473
 
474 474
 void CardReader::manage_media() {
475
-  static uint8_t prev_stat = 2;     // First call, no prior state
475
+  static uint8_t prev_stat = 2;     // At boot we don't know if media is present or not
476 476
   uint8_t stat = uint8_t(IS_SD_INSERTED());
477
-  if (stat == prev_stat) return;
477
+  if (stat == prev_stat) return;    // Already checked and still no change?
478 478
 
479
-  DEBUG_SECTION(mm, "CardReader::manage_media", true);
480
-  DEBUG_ECHOLNPGM("SD Status ", prev_stat, " -> ", stat);
481
-
482
-  flag.workDirIsRoot = true;        // Return to root on mount/release
479
+  DEBUG_SECTION(cmm, "CardReader::manage_media()", true);
480
+  DEBUG_ECHOLNPGM("Media present: ", prev_stat, " -> ", stat);
483 481
 
484 482
   if (!ui.detected()) {
485 483
     DEBUG_ECHOLNPGM("SD: No UI Detected.");
486 484
     return;
487 485
   }
488 486
 
489
-  uint8_t old_stat = prev_stat;
490
-  prev_stat = stat;                 // Change now to prevent re-entry
487
+  flag.workDirIsRoot = true;        // Return to root on mount/release/init
488
+
489
+  const uint8_t old_stat = prev_stat;
490
+  prev_stat = stat;                 // Change now to prevent re-entry in safe_delay
491 491
 
492 492
   if (stat) {                       // Media Inserted
493 493
     safe_delay(500);                // Some boards need a delay to get settled
494 494
 
495 495
     // Try to mount the media (only later with SD_IGNORE_AT_STARTUP)
496 496
     if (TERN1(SD_IGNORE_AT_STARTUP, old_stat != 2)) mount();
497
-    if (!isMounted()) stat = 0;     // Not mounted?
497
+    if (!isMounted()) {             // Not mounted?
498
+      stat = 0;
499
+      IF_DISABLED(SD_IGNORE_AT_STARTUP, prev_stat = 0);
500
+    }
498 501
 
499 502
     TERN_(RESET_STEPPERS_ON_MEDIA_INSERT, reset_stepper_drivers()); // Workaround for Cheetah bug
500 503
   }
@@ -504,16 +507,25 @@ void CardReader::manage_media() {
504 507
     #endif
505 508
   }
506 509
 
507
-  ui.media_changed(old_stat, stat); // Update the UI
510
+  ui.media_changed(old_stat, stat); // Update the UI or flag an error
508 511
 
509 512
   if (!stat) return;                // Exit if no media is present
510 513
 
511
-  TERN_(SDCARD_EEPROM_EMULATION, settings.first_load());
512
-
513
-  if (old_stat != 2) return;        // First mount?
514
+  static bool did_first_insert = false;
515
+  if (did_first_insert) return;     // Did a media insert already happen?
516
+  did_first_insert = true;          // Definitely handling this media insert...
514 517
 
515 518
   DEBUG_ECHOLNPGM("First mount.");
516 519
 
520
+  // Load settings the first time media is inserted (not just during init)
521
+  TERN_(SDCARD_EEPROM_EMULATION, settings.first_load());
522
+
523
+  #if HAS_USB_FLASH_DRIVE
524
+    const millis_t ms = millis();
525
+    DEBUG_ECHOLNPGM("USB mount waiting time = ", ms);
526
+    if (ms > 5000) return;          // Too late to be considered "already inserted"?
527
+  #endif
528
+
517 529
   bool do_auto = true; UNUSED(do_auto);
518 530
 
519 531
   // Check for PLR file.

Loading…
Cancel
Save