Browse Source

USB FD via native USB Host + MSC (#20571)

Victor Oliveira 4 years ago
parent
commit
84ab088b40

+ 11 - 3
Marlin/Configuration_adv.h

@@ -1326,9 +1326,6 @@
    */
   //#define USB_FLASH_DRIVE_SUPPORT
   #if ENABLED(USB_FLASH_DRIVE_SUPPORT)
-    #define USB_CS_PIN    SDSS
-    #define USB_INTR_PIN  SD_DETECT_PIN
-
     /**
      * USB Host Shield Library
      *
@@ -1339,7 +1336,18 @@
      *   is less tested and is known to interfere with Servos.
      *   [1] This requires USB_INTR_PIN to be interrupt-capable.
      */
+    //#define USE_UHS2_USB
     //#define USE_UHS3_USB
+
+    /**
+     * Native USB Host supported by some boards (USB OTG)
+     */
+    //#define USE_OTG_USB_HOST
+
+    #if DISABLED(USE_OTG_USB_HOST)
+      #define USB_CS_PIN    SDSS
+      #define USB_INTR_PIN  SD_DETECT_PIN
+    #endif
   #endif
 
   /**

+ 117 - 0
Marlin/src/HAL/STM32/usb_host.cpp

@@ -0,0 +1,117 @@
+/**
+ * Marlin 3D Printer Firmware
+ * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
+ *
+ * Based on Sprinter and grbl.
+ * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC)
+
+#include "../../inc/MarlinConfig.h"
+
+#if BOTH(USE_OTG_USB_HOST, USBHOST)
+
+#include "usb_host.h"
+#include "../shared/Marduino.h"
+#include "usbh_core.h"
+#include "usbh_msc.h"
+
+USBH_HandleTypeDef hUsbHost;
+USBHost usb;
+BulkStorage bulk(&usb);
+
+static void USBH_UserProcess(USBH_HandleTypeDef *phost, uint8_t id) {
+  switch(id) {
+    case HOST_USER_SELECT_CONFIGURATION:
+      //SERIAL_ECHOLNPGM("APPLICATION_SELECT_CONFIGURATION");
+      break;
+    case HOST_USER_DISCONNECTION:
+      //SERIAL_ECHOLNPGM("APPLICATION_DISCONNECT");
+      //usb.setUsbTaskState(USB_STATE_RUNNING);
+      break;
+    case HOST_USER_CLASS_ACTIVE:
+      //SERIAL_ECHOLNPGM("APPLICATION_READY");
+      usb.setUsbTaskState(USB_STATE_RUNNING);
+      break;
+    case HOST_USER_CONNECTION:
+      break;
+    default:
+      break;
+  }
+}
+
+bool USBHost::start() {
+  if (USBH_Init(&hUsbHost, USBH_UserProcess, TERN(USE_USB_HS_IN_FS, HOST_HS, HOST_FS)) != USBH_OK) {
+    SERIAL_ECHOLNPGM("Error: USBH_Init");
+    return false;
+  }
+  if (USBH_RegisterClass(&hUsbHost, USBH_MSC_CLASS) != USBH_OK) {
+    SERIAL_ECHOLNPGM("Error: USBH_RegisterClass");
+    return false;
+  }
+  if (USBH_Start(&hUsbHost) != USBH_OK) {
+    SERIAL_ECHOLNPGM("Error: USBH_Start");
+    return false;
+  }
+  return true;
+}
+
+void USBHost::Task() {
+  USBH_Process(&hUsbHost);
+}
+
+uint8_t USBHost::getUsbTaskState() {
+  return usb_task_state;
+}
+
+void USBHost::setUsbTaskState(uint8_t state) {
+  usb_task_state = state;
+  if (usb_task_state == USB_STATE_RUNNING) {
+    MSC_LUNTypeDef info;
+    USBH_MSC_GetLUNInfo(&hUsbHost, usb.lun, &info);
+    capacity = info.capacity.block_nbr / 2000;
+    block_size = info.capacity.block_size;
+    block_count = info.capacity.block_nbr;
+    // SERIAL_ECHOLNPAIR("info.capacity.block_nbr : %ld\n", info.capacity.block_nbr);
+    // SERIAL_ECHOLNPAIR("info.capacity.block_size: %d\n", info.capacity.block_size);
+    // SERIAL_ECHOLNPAIR("capacity                : %d MB\n", capacity);
+  }
+};
+
+bool BulkStorage::LUNIsGood(uint8_t t) {
+  return USBH_MSC_IsReady(&hUsbHost) && USBH_MSC_UnitIsReady(&hUsbHost, t);
+}
+
+uint32_t BulkStorage::GetCapacity(uint8_t lun) {
+  return usb->block_count;
+}
+
+uint16_t BulkStorage::GetSectorSize(uint8_t lun) {
+  return usb->block_size;
+}
+
+uint8_t BulkStorage::Read(uint8_t lun, uint32_t addr, uint16_t bsize, uint8_t blocks, uint8_t *buf) {
+  return USBH_MSC_Read(&hUsbHost, lun, addr, buf, blocks) != USBH_OK;
+}
+
+uint8_t BulkStorage::Write(uint8_t lun, uint32_t addr, uint16_t bsize, uint8_t blocks, const uint8_t * buf) {
+  return USBH_MSC_Write(&hUsbHost, lun, addr, const_cast <uint8_t*>(buf), blocks) != USBH_OK;
+}
+
+#endif // USE_OTG_USB_HOST && USBHOST
+#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC

+ 60 - 0
Marlin/src/HAL/STM32/usb_host.h

@@ -0,0 +1,60 @@
+/**
+ * Marlin 3D Printer Firmware
+ * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
+ *
+ * Based on Sprinter and grbl.
+ * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+#pragma once
+
+#include <stdint.h>
+
+typedef enum {
+  USB_STATE_INIT,
+  USB_STATE_ERROR,
+  USB_STATE_RUNNING,
+} usb_state_t;
+
+class USBHost {
+public:
+  bool start();
+  void Task();
+  uint8_t getUsbTaskState();
+  void setUsbTaskState(uint8_t state);
+  uint8_t regRd(uint8_t reg) { return 0x0; };
+  uint8_t usb_task_state = USB_STATE_INIT;
+  uint8_t lun = 0;
+  uint32_t capacity = 0;
+  uint16_t block_size = 0;
+  uint32_t block_count = 0;
+};
+
+class BulkStorage {
+public:
+  BulkStorage(USBHost *usb) : usb(usb) {};
+
+  bool LUNIsGood(uint8_t t);
+  uint32_t GetCapacity(uint8_t lun);
+  uint16_t GetSectorSize(uint8_t lun);
+  uint8_t Read(uint8_t lun, uint32_t addr, uint16_t bsize, uint8_t blocks, uint8_t *buf);
+  uint8_t Write(uint8_t lun, uint32_t addr, uint16_t bsize, uint8_t blocks, const uint8_t * buf);
+
+  USBHost *usb;
+};
+
+extern USBHost usb;
+extern BulkStorage bulk;

+ 4 - 0
Marlin/src/inc/Conditionals_adv.h

@@ -386,6 +386,10 @@
   #define HOMING_BUMP_MM { 0, 0, 0 }
 #endif
 
+#if ENABLED(USB_FLASH_DRIVE_SUPPORT) && NONE(USE_OTG_USB_HOST, USE_UHS3_USB)
+  #define USE_UHS2_USB
+#endif
+
 /**
  * Driver Timings (in nanoseconds)
  * NOTE: Driver timing order is longest-to-shortest duration.

+ 5 - 1
Marlin/src/inc/SanityCheck.h

@@ -2912,10 +2912,14 @@ static_assert(   _ARR_TEST(3,0) && _ARR_TEST(3,1) && _ARR_TEST(3,2)
   #error "PRINTCOUNTER requires EEPROM_SETTINGS."
 #endif
 
-#if ENABLED(USB_FLASH_DRIVE_SUPPORT) && !PINS_EXIST(USB_CS, USB_INTR)
+#if ENABLED(USB_FLASH_DRIVE_SUPPORT) && !PINS_EXIST(USB_CS, USB_INTR) && DISABLED(USE_OTG_USB_HOST)
   #error "USB_CS_PIN and USB_INTR_PIN are required for USB_FLASH_DRIVE_SUPPORT."
 #endif
 
+#if ENABLED(USE_OTG_USB_HOST) && !defined(HAS_OTG_USB_HOST_SUPPORT)
+  #error "The current board does not support USE_OTG_USB_HOST."
+#endif
+
 #if ENABLED(SD_FIRMWARE_UPDATE) && !defined(__AVR_ATmega2560__)
   #error "SD_FIRMWARE_UPDATE requires an ATmega2560-based (Arduino Mega) board."
 #endif

+ 8 - 4
Marlin/src/pins/stm32f1/pins_MKS_ROBIN_MINI.h

@@ -52,6 +52,13 @@
 
 #define SPI_DEVICE                             2
 
+//
+// Servos
+//
+#ifndef SERVO0_PIN
+  #define SERVO0_PIN                        PA8   // Enable BLTOUCH support on IO0 (WIFI connector)
+#endif
+
 //
 // Limit Switches
 //
@@ -91,6 +98,7 @@
 #ifndef DEFAULT_PWM_MOTOR_CURRENT
   #define DEFAULT_PWM_MOTOR_CURRENT { 800, 800, 800 }
 #endif
+
 //
 // Temperature Sensors
 //
@@ -111,10 +119,6 @@
 #define POWER_LOSS_PIN                      PA2   // PW_DET
 #define PS_ON_PIN                           PA3   // PW_OFF
 
-#ifndef SERVO0_PIN
-  #define SERVO0_PIN                        PA8   // Enable BLTOUCH support on IO0 (WIFI connector)
-#endif
-
 #define MT_DET_1_PIN                        PA4
 #define MT_DET_PIN_INVERTING               false
 

+ 3 - 0
Marlin/src/pins/stm32f4/pins_BTT_GTR_V1_0.h

@@ -35,6 +35,9 @@
 #define I2C_EEPROM
 #define MARLIN_EEPROM_SIZE                0x2000  // 8KB (24C64 ... 64Kb = 8KB)
 
+// USB Flash Drive support
+#define HAS_OTG_USB_HOST_SUPPORT
+
 #define TP                                        // Enable to define servo and probe pins
 
 //

+ 3 - 0
Marlin/src/pins/stm32f4/pins_BTT_SKR_PRO_common.h

@@ -44,6 +44,9 @@
   #define FLASH_EEPROM_LEVELING
 #endif
 
+// USB Flash Drive support
+#define HAS_OTG_USB_HOST_SUPPORT
+
 //
 // Servos
 //

+ 1 - 0
Marlin/src/pins/stm32f4/pins_MKS_ROBIN_NANO_V3.h

@@ -41,6 +41,7 @@
 //#define SRAM_EEPROM_EMULATION                   // Use BackSRAM-based EEPROM emulation
 //#define FLASH_EEPROM_EMULATION                  // Use Flash-based EEPROM emulation
 #define I2C_EEPROM
+#define MARLIN_EEPROM_SIZE                0x1000  // 4KB
 
 //
 // Release PB4 (Z_DIR_PIN) from JTAG NRST role

+ 4 - 0
Marlin/src/pins/stm32f4/pins_MKS_ROBIN_PRO_V2.h

@@ -36,6 +36,10 @@
 //#define SRAM_EEPROM_EMULATION                   // Use BackSRAM-based EEPROM emulation
 //#define FLASH_EEPROM_EMULATION                  // Use Flash-based EEPROM emulation
 #define I2C_EEPROM
+#define MARLIN_EEPROM_SIZE                0x1000  // 4KB
+
+// USB Flash Drive support
+#define HAS_OTG_USB_HOST_SUPPORT
 
 //
 // Release PB4 (Y_ENABLE_PIN) from JTAG NRST role

Some files were not shown because too many files changed in this diff