From 6740f60441144c6c3e2db75063a277f47d457740 Mon Sep 17 00:00:00 2001 From: Victor Oliveira Date: Wed, 18 Nov 2020 02:48:14 -0300 Subject: [PATCH] Improve USB Media Host conditions (#20176) Co-authored-by: Scott Lahteine --- Marlin/src/HAL/LPC1768/inc/Conditionals_adv.h | 4 ++++ Marlin/src/HAL/LPC1768/main.cpp | 4 ++-- .../src/HAL/STM32/Sd2Card_sdio_stm32duino.cpp | 2 +- Marlin/src/HAL/STM32/inc/Conditionals_adv.h | 4 ++++ Marlin/src/HAL/STM32F1/HAL.cpp | 24 +++++++++---------- Marlin/src/HAL/STM32F1/HAL.h | 4 ++-- Marlin/src/HAL/STM32F1/inc/Conditionals_LCD.h | 5 ---- Marlin/src/HAL/STM32F1/inc/Conditionals_adv.h | 8 +++++++ Marlin/src/HAL/STM32F1/msc_sd.cpp | 4 ++-- Marlin/src/HAL/STM32F1/onboard_sd.cpp | 9 +++---- Marlin/src/inc/Conditionals_post.h | 4 +--- 11 files changed, 40 insertions(+), 32 deletions(-) diff --git a/Marlin/src/HAL/LPC1768/inc/Conditionals_adv.h b/Marlin/src/HAL/LPC1768/inc/Conditionals_adv.h index 5f1c4b16019d2..8e7cab185f2a8 100644 --- a/Marlin/src/HAL/LPC1768/inc/Conditionals_adv.h +++ b/Marlin/src/HAL/LPC1768/inc/Conditionals_adv.h @@ -20,3 +20,7 @@ * */ #pragma once + +#if DISABLED(NO_SD_HOST_DRIVE) + #define HAS_SD_HOST_DRIVE 1 +#endif diff --git a/Marlin/src/HAL/LPC1768/main.cpp b/Marlin/src/HAL/LPC1768/main.cpp index 0b4045cb99273..645d6942ebb3c 100644 --- a/Marlin/src/HAL/LPC1768/main.cpp +++ b/Marlin/src/HAL/LPC1768/main.cpp @@ -122,7 +122,7 @@ void HAL_init() { delay(1000); // Give OS time to notice USB_Connect(TRUE); - #if DISABLED(NO_SD_HOST_DRIVE) + #if HAS_SD_HOST_DRIVE MSC_SD_Init(0); // Enable USB SD card access #endif @@ -140,7 +140,7 @@ void HAL_init() { // HAL idle task void HAL_idletask() { - #if HAS_SHARED_MEDIA + #if HAS_SD_HOST_DRIVE // If Marlin is using the SD card we need to lock it to prevent access from // a PC via USB. // Other HALs use IS_SD_PRINTING() and IS_SD_FILE_OPEN() to check for access but diff --git a/Marlin/src/HAL/STM32/Sd2Card_sdio_stm32duino.cpp b/Marlin/src/HAL/STM32/Sd2Card_sdio_stm32duino.cpp index 6e73e87c21239..9c2666ed26f44 100644 --- a/Marlin/src/HAL/STM32/Sd2Card_sdio_stm32duino.cpp +++ b/Marlin/src/HAL/STM32/Sd2Card_sdio_stm32duino.cpp @@ -31,7 +31,7 @@ #error "ERROR - Only STM32F103xE, STM32F103xG, STM32F4xx or STM32F7xx CPUs supported" #endif -#ifdef USBD_USE_CDC_COMPOSITE +#if HAS_SD_HOST_DRIVE // use USB drivers diff --git a/Marlin/src/HAL/STM32/inc/Conditionals_adv.h b/Marlin/src/HAL/STM32/inc/Conditionals_adv.h index 5f1c4b16019d2..e07c0d9cda5e1 100644 --- a/Marlin/src/HAL/STM32/inc/Conditionals_adv.h +++ b/Marlin/src/HAL/STM32/inc/Conditionals_adv.h @@ -20,3 +20,7 @@ * */ #pragma once + +#if defined(USBD_USE_CDC_COMPOSITE) && DISABLED(NO_SD_HOST_DRIVE) + #define HAS_SD_HOST_DRIVE 1 +#endif diff --git a/Marlin/src/HAL/STM32F1/HAL.cpp b/Marlin/src/HAL/STM32F1/HAL.cpp index 6733aead6d7ce..e6ac87b0eb1fb 100644 --- a/Marlin/src/HAL/STM32F1/HAL.cpp +++ b/Marlin/src/HAL/STM32F1/HAL.cpp @@ -82,7 +82,7 @@ // Public Variables // ------------------------ -#if (defined(SERIAL_USB) && !defined(USE_USB_COMPOSITE)) +#if defined(SERIAL_USB) && !HAS_SD_HOST_DRIVE USBSerial SerialUSB; #endif @@ -251,7 +251,7 @@ void HAL_init() { #if PIN_EXISTS(LED) OUT_WRITE(LED_PIN, LOW); #endif - #ifdef USE_USB_COMPOSITE + #if HAS_SD_HOST_DRIVE MSC_SD_init(); #endif #if PIN_EXISTS(USB_CONNECT) @@ -263,17 +263,15 @@ void HAL_init() { // HAL idle task void HAL_idletask() { - #ifdef USE_USB_COMPOSITE - #if HAS_SHARED_MEDIA - // If Marlin is using the SD card we need to lock it to prevent access from - // a PC via USB. - // Other HALs use IS_SD_PRINTING() and IS_SD_FILE_OPEN() to check for access but - // this will not reliably detect delete operations. To be safe we will lock - // the disk if Marlin has it mounted. Unfortunately there is currently no way - // to unmount the disk from the LCD menu. - // if (IS_SD_PRINTING() || IS_SD_FILE_OPEN()) - /* copy from lpc1768 framework, should be fixed later for process HAS_SHARED_MEDIA*/ - #endif + #if HAS_SD_HOST_DRIVE + // If Marlin is using the SD card we need to lock it to prevent access from + // a PC via USB. + // Other HALs use IS_SD_PRINTING() and IS_SD_FILE_OPEN() to check for access but + // this will not reliably detect delete operations. To be safe we will lock + // the disk if Marlin has it mounted. Unfortunately there is currently no way + // to unmount the disk from the LCD menu. + // if (IS_SD_PRINTING() || IS_SD_FILE_OPEN()) + /* copy from lpc1768 framework, should be fixed later for process HAS_SD_HOST_DRIVE*/ // process USB mass storage device class loop MarlinMSC.loop(); #endif diff --git a/Marlin/src/HAL/STM32F1/HAL.h b/Marlin/src/HAL/STM32F1/HAL.h index 58a82c3b01f3b..a765f052e753d 100644 --- a/Marlin/src/HAL/STM32F1/HAL.h +++ b/Marlin/src/HAL/STM32F1/HAL.h @@ -42,7 +42,7 @@ #include "../../inc/MarlinConfigPre.h" -#ifdef USE_USB_COMPOSITE +#if HAS_SD_HOST_DRIVE #include "msc_sd.h" #endif @@ -61,7 +61,7 @@ #endif #ifdef SERIAL_USB - #ifndef USE_USB_COMPOSITE + #if !HAS_SD_HOST_DRIVE #define UsbSerial Serial #else #define UsbSerial MarlinCompositeSerial diff --git a/Marlin/src/HAL/STM32F1/inc/Conditionals_LCD.h b/Marlin/src/HAL/STM32F1/inc/Conditionals_LCD.h index f52e6fec2b1e8..5f1c4b16019d2 100644 --- a/Marlin/src/HAL/STM32F1/inc/Conditionals_LCD.h +++ b/Marlin/src/HAL/STM32F1/inc/Conditionals_LCD.h @@ -20,8 +20,3 @@ * */ #pragma once - -#if ENABLED(USE_USB_COMPOSITE) - //#warning "SD_CHECK_AND_RETRY isn't needed with USE_USB_COMPOSITE." - #undef SD_CHECK_AND_RETRY -#endif diff --git a/Marlin/src/HAL/STM32F1/inc/Conditionals_adv.h b/Marlin/src/HAL/STM32F1/inc/Conditionals_adv.h index 5f1c4b16019d2..0fe79247658c6 100644 --- a/Marlin/src/HAL/STM32F1/inc/Conditionals_adv.h +++ b/Marlin/src/HAL/STM32F1/inc/Conditionals_adv.h @@ -20,3 +20,11 @@ * */ #pragma once + +#ifdef USE_USB_COMPOSITE + //#warning "SD_CHECK_AND_RETRY isn't needed with USE_USB_COMPOSITE." + #undef SD_CHECK_AND_RETRY + #if DISABLED(NO_SD_HOST_DRIVE) + #define HAS_SD_HOST_DRIVE 1 + #endif +#endif diff --git a/Marlin/src/HAL/STM32F1/msc_sd.cpp b/Marlin/src/HAL/STM32F1/msc_sd.cpp index 4f44f2ee905f2..44242358eeb8d 100644 --- a/Marlin/src/HAL/STM32F1/msc_sd.cpp +++ b/Marlin/src/HAL/STM32F1/msc_sd.cpp @@ -13,7 +13,7 @@ * along with this program. If not, see . * */ -#if defined(__STM32F1__) && defined(USE_USB_COMPOSITE) +#if defined(__STM32F1__) && HAS_SD_HOST_DRIVE #include "msc_sd.h" #include "SPI.h" @@ -77,4 +77,4 @@ void MSC_SD_init() { #endif } -#endif // __STM32F1__ && USE_USB_COMPOSITE +#endif // __STM32F1__ && HAS_SD_HOST_DRIVE diff --git a/Marlin/src/HAL/STM32F1/onboard_sd.cpp b/Marlin/src/HAL/STM32F1/onboard_sd.cpp index 9c2b128ddc9a1..6092aea320544 100644 --- a/Marlin/src/HAL/STM32F1/onboard_sd.cpp +++ b/Marlin/src/HAL/STM32F1/onboard_sd.cpp @@ -21,10 +21,11 @@ #include "SPI.h" #include "fastio.h" -#if HAS_SHARED_MEDIA - #ifndef ONBOARD_SPI_DEVICE - #define ONBOARD_SPI_DEVICE SPI_DEVICE - #endif +#ifndef ONBOARD_SPI_DEVICE + #define ONBOARD_SPI_DEVICE SPI_DEVICE +#endif + +#if HAS_SD_HOST_DRIVE #define ONBOARD_SD_SPI SPI #else SPIClass OnboardSPI(ONBOARD_SPI_DEVICE); diff --git a/Marlin/src/inc/Conditionals_post.h b/Marlin/src/inc/Conditionals_post.h index 155344f3685b1..eddc9ff903901 100644 --- a/Marlin/src/inc/Conditionals_post.h +++ b/Marlin/src/inc/Conditionals_post.h @@ -359,14 +359,13 @@ */ #if ENABLED(SDSUPPORT) - #if SD_CONNECTION_IS(ONBOARD) && DISABLED(NO_SD_HOST_DRIVE) && !defined(ARDUINO_GRAND_CENTRAL_M4) + #if HAS_SD_HOST_DRIVE && SD_CONNECTION_IS(ONBOARD) // // The external SD card is not used. Hardware SPI is used to access the card. // When sharing the SD card with a PC we want the menu options to // mount/unmount the card and refresh it. So we disable card detect. // #undef SD_DETECT_PIN - #define HAS_SHARED_MEDIA 1 #endif #if PIN_EXISTS(SD_DETECT) @@ -380,7 +379,6 @@ #define SD_DETECT_STATE LOW #endif #endif - #endif #if ANY(HAS_GRAPHICAL_TFT, LCD_USE_DMA_FSMC, HAS_FSMC_GRAPHICAL_TFT, HAS_SPI_GRAPHICAL_TFT) || !PIN_EXISTS(SD_DETECT)