Bläddra i källkod

Refactor SD detect handler (#17380)

Co-Authored-By: Eric Ptak <trouch@users.noreply.github.com>
Scott Lahteine 4 år sedan
förälder
incheckning
65f6a373b0
Inget konto är kopplat till bidragsgivarens mejladress

+ 57
- 38
Marlin/src/MarlinCore.cpp Visa fil

@@ -443,7 +443,6 @@ void startOrResumeJob() {
443 443
 
444 444
 /**
445 445
  * Minimal management of Marlin's core activities:
446
- *  - Check for Filament Runout
447 446
  *  - Keep the command buffer full
448 447
  *  - Check for maximum inactive time between commands
449 448
  *  - Check for maximum inactive time between stepper commands
@@ -454,13 +453,8 @@ void startOrResumeJob() {
454 453
  *  - Check if an idle but hot extruder needs filament extruded (EXTRUDER_RUNOUT_PREVENT)
455 454
  *  - Pulse FET_SAFETY_PIN if it exists
456 455
  */
457
-
458 456
 inline void manage_inactivity(const bool ignore_stepper_queue=false) {
459 457
 
460
-  #if HAS_FILAMENT_SENSOR
461
-    runout.run();
462
-  #endif
463
-
464 458
   if (queue.length < BUFSIZE) queue.get_available_commands();
465 459
 
466 460
   const millis_t ms = millis();
@@ -644,9 +638,53 @@ inline void manage_inactivity(const bool ignore_stepper_queue=false) {
644 638
 }
645 639
 
646 640
 /**
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
648 661
  */
649 662
 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
+
650 688
   // Handle Power-Loss Recovery
651 689
   #if ENABLED(POWER_LOSS_RECOVERY) && PIN_EXISTS(POWER_LOSS)
652 690
     recovery.outage();
@@ -660,29 +698,21 @@ void idle(TERN_(ADVANCED_PAUSE_FEATURE, bool no_stepper_sleep/*=false*/)) {
660 698
         if (endstops.tmc_spi_homing_check()) break;
661 699
   #endif
662 700
 
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();
666 704
   #endif
667 705
 
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
670 710
 
671 711
   // Announce Host Keepalive state (if any)
672 712
   #if ENABLED(HOST_KEEPALIVE_FEATURE)
673 713
     gcode.host_keepalive();
674 714
   #endif
675 715
 
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
-
686 716
   // Update the Print Job Timer state
687 717
   #if ENABLED(PRINTCOUNTER)
688 718
     print_job_timer.tick();
@@ -693,6 +723,9 @@ void idle(TERN_(ADVANCED_PAUSE_FEATURE, bool no_stepper_sleep/*=false*/)) {
693 723
     buzzer.tick();
694 724
   #endif
695 725
 
726
+  // Read Buttons and Update the LCD
727
+  ui.update();
728
+
696 729
   // Run i2c Position Encoders
697 730
   #if ENABLED(I2C_POSITION_ENCODERS)
698 731
     static millis_t i2cpem_next_update_ms;
@@ -705,11 +738,6 @@ void idle(TERN_(ADVANCED_PAUSE_FEATURE, bool no_stepper_sleep/*=false*/)) {
705 738
     }
706 739
   #endif
707 740
 
708
-  // Run HAL idle tasks
709
-  #ifdef HAL_IDLETASK
710
-    HAL_idletask();
711
-  #endif
712
-
713 741
   // Auto-report Temperatures / SD Status
714 742
   #if HAS_AUTO_REPORTING
715 743
     if (!gcode.autoreport_paused) {
@@ -722,11 +750,6 @@ void idle(TERN_(ADVANCED_PAUSE_FEATURE, bool no_stepper_sleep/*=false*/)) {
722 750
     }
723 751
   #endif
724 752
 
725
-  // Handle USB Flash Drive insert / remove
726
-  #if ENABLED(USB_FLASH_DRIVE_SUPPORT)
727
-    Sd2Card::idle();
728
-  #endif
729
-
730 753
   // Update the Prusa MMU2
731 754
   #if ENABLED(PRUSA_MMU2)
732 755
     mmu2.mmu_loop();
@@ -983,8 +1006,8 @@ void setup() {
983 1006
     SETUP_RUN(ui.show_bootscreen());
984 1007
   #endif
985 1008
 
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
988 1011
   #endif
989 1012
 
990 1013
   SETUP_RUN(settings.first_load());   // Load data from EEPROM if available (or use defaults)
@@ -1151,10 +1174,6 @@ void setup() {
1151 1174
     queue.inject_P(PSTR(STARTUP_COMMANDS));
1152 1175
   #endif
1153 1176
 
1154
-  #if ENABLED(INIT_SDCARD_ON_BOOT) && !HAS_SPI_LCD
1155
-    SETUP_RUN(card.beginautostart());
1156
-  #endif
1157
-
1158 1177
   #if ENABLED(HOST_PROMPT_SUPPORT)
1159 1178
     SETUP_RUN(host_action_prompt_end());
1160 1179
   #endif

+ 1
- 22
Marlin/src/lcd/extui/ui_api.cpp Visa fil

@@ -1149,28 +1149,7 @@ void MarlinUI::init() {
1149 1149
   ExtUI::onStartup();
1150 1150
 }
1151 1151
 
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(); }
1174 1153
 
1175 1154
 void MarlinUI::kill_screen(PGM_P const error, PGM_P const component) {
1176 1155
   using namespace ExtUI;

+ 55
- 59
Marlin/src/lcd/ultralcd.cpp Visa fil

@@ -121,10 +121,6 @@ MarlinUI ui;
121 121
   #endif
122 122
 #endif
123 123
 
124
-#if ENABLED(INIT_SDCARD_ON_BOOT)
125
-  uint8_t lcd_sd_status;
126
-#endif
127
-
128 124
 #if HAS_LCD_MENU && LCD_TIMEOUT_TO_STATUS
129 125
   bool MarlinUI::defer_return_to_status;
130 126
 #endif
@@ -342,13 +338,8 @@ void MarlinUI::init() {
342 338
 
343 339
   #endif // HAS_SHIFT_ENCODER
344 340
 
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);
352 343
   #endif
353 344
 
354 345
   #if HAS_ENCODER_ACTION && HAS_SLOW_BUTTONS
@@ -744,11 +735,11 @@ void MarlinUI::quick_feedback(const bool clear_buttons/*=true*/) {
744 735
  */
745 736
 
746 737
 LCDViewAction MarlinUI::lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW;
738
+millis_t next_lcd_update_ms;
747 739
 
748 740
 void MarlinUI::update() {
749 741
 
750 742
   static uint16_t max_display_update_time = 0;
751
-  static millis_t next_lcd_update_ms;
752 743
   millis_t ms = millis();
753 744
 
754 745
   #if HAS_LCD_MENU && LCD_TIMEOUT_TO_STATUS
@@ -824,53 +815,6 @@ void MarlinUI::update() {
824 815
 
825 816
   #endif // HAS_LCD_MENU
826 817
 
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
-
874 818
   if (ELAPSED(ms, next_lcd_update_ms)
875 819
     #if HAS_GRAPHICAL_LCD
876 820
       || drawing_screen
@@ -1595,3 +1539,55 @@ void MarlinUI::update() {
1595 1539
   }
1596 1540
 
1597 1541
 #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

+ 4
- 0
Marlin/src/lcd/ultralcd.h Visa fil

@@ -274,6 +274,10 @@ public:
274 274
   // LCD implementations
275 275
   static void clear_lcd();
276 276
 
277
+  #if ENABLED(SDSUPPORT)
278
+    static void media_changed(const uint8_t old_stat, const uint8_t stat);
279
+  #endif
280
+
277 281
   #if HAS_SPI_LCD
278 282
     static bool detected();
279 283
     static void init_lcd();

+ 36
- 0
Marlin/src/sd/cardreader.cpp Visa fil

@@ -378,6 +378,42 @@ void CardReader::mount() {
378 378
   ui.refresh();
379 379
 }
380 380
 
381
+/**
382
+ * Handle SD card events
383
+ */
384
+#if MB(FYSETC_CHEETAH)
385
+  #include "../module/stepper.h"
386
+#endif
387
+
388
+void CardReader::manage_media() {
389
+  static uint8_t prev_stat = TERN(INIT_SDCARD_ON_BOOT, 2, 0);
390
+  uint8_t stat = uint8_t(IS_SD_INSERTED());
391
+  if (stat != prev_stat && ui.detected()) {
392
+
393
+    uint8_t old_stat = prev_stat;
394
+    prev_stat = stat;                 // Change now to prevent re-entry
395
+
396
+    if (stat) {                       // Media Inserted
397
+      safe_delay(500);                // Some boards need a delay to get settled
398
+      mount();                        // Try to mount the media
399
+      #if MB(FYSETC_CHEETAH)
400
+        reset_stepper_drivers();      // Workaround for Cheetah bug
401
+      #endif
402
+      if (!isMounted()) stat = 0;     // Not mounted?
403
+    }
404
+    else {
405
+      #if PIN_EXISTS(SD_DETECT)
406
+        release();                    // Card is released
407
+      #endif
408
+    }
409
+
410
+    ui.media_changed(old_stat, stat); // Update the UI
411
+
412
+    if (stat && old_stat == 2)        // First mount?
413
+      beginautostart();               // Look for autostart files soon
414
+  }
415
+}
416
+
381 417
 void CardReader::release() {
382 418
   endFilePrint();
383 419
   flag.mounted = false;

+ 3
- 0
Marlin/src/sd/cardreader.h Visa fil

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

Laddar…
Avbryt
Spara