Skip to content

Commit

Permalink
USB FD via native USB Host + MSC (MarlinFirmware#20571)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhapsodyv authored and kpishere committed Feb 19, 2021
1 parent 61c0309 commit c662e6f
Show file tree
Hide file tree
Showing 13 changed files with 300 additions and 33 deletions.
14 changes: 11 additions & 3 deletions Marlin/Configuration_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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

/**
Expand Down
117 changes: 117 additions & 0 deletions Marlin/src/HAL/STM32/usb_host.cpp
Original file line number Diff line number Diff line change
@@ -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 changes: 60 additions & 0 deletions Marlin/src/HAL/STM32/usb_host.h
Original file line number Diff line number Diff line change
@@ -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 changes: 4 additions & 0 deletions Marlin/src/inc/Conditionals_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion Marlin/src/inc/SanityCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions Marlin/src/pins/stm32f1/pins_MKS_ROBIN_MINI.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down Expand Up @@ -91,6 +98,7 @@
#ifndef DEFAULT_PWM_MOTOR_CURRENT
#define DEFAULT_PWM_MOTOR_CURRENT { 800, 800, 800 }
#endif

//
// Temperature Sensors
//
Expand All @@ -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

Expand Down
3 changes: 3 additions & 0 deletions Marlin/src/pins/stm32f4/pins_BTT_GTR_V1_0.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

//
Expand Down
3 changes: 3 additions & 0 deletions Marlin/src/pins/stm32f4/pins_BTT_SKR_PRO_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
#define FLASH_EEPROM_LEVELING
#endif

// USB Flash Drive support
#define HAS_OTG_USB_HOST_SUPPORT

//
// Servos
//
Expand Down
1 change: 1 addition & 0 deletions Marlin/src/pins/stm32f4/pins_MKS_ROBIN_NANO_V3.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Marlin/src/pins/stm32f4/pins_MKS_ROBIN_PRO_V2.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions Marlin/src/sd/usb_flashdrive/Sd2Card_FlashDrive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@
#include "../../core/serial.h"
#include "../../module/temperature.h"

static_assert(USB_CS_PIN != -1, "USB_CS_PIN must be defined");
static_assert(USB_INTR_PIN != -1, "USB_INTR_PIN must be defined");
#if DISABLED(USE_OTG_USB_HOST) && !PINS_EXIST(USB_CS, USB_INTR)
#error "USB_FLASH_DRIVE_SUPPORT requires USB_CS_PIN and USB_INTR_PIN to be defined."
#endif

#if ENABLED(USE_UHS3_USB)
#define NO_AUTO_SPEED
Expand Down Expand Up @@ -81,6 +82,17 @@ static_assert(USB_INTR_PIN != -1, "USB_INTR_PIN must be defined");

#define UHS_START (usb.Init() == 0)
#define UHS_STATE(state) UHS_USB_HOST_STATE_##state
#elif ENABLED(USE_OTG_USB_HOST)

#if HAS_SD_HOST_DRIVE
#include HAL_PATH(../../HAL, msc_sd.h)
#endif

#include HAL_PATH(../../HAL, usb_host.h)

#define UHS_START usb.start()
#define rREVISION 0
#define UHS_STATE(state) USB_STATE_##state
#else
#include "lib-uhs2/Usb.h"
#include "lib-uhs2/masstorage.h"
Expand Down Expand Up @@ -250,7 +262,7 @@ bool Sd2Card::isInserted() {
return state == MEDIA_READY;
}

bool Sd2Card::ready() {
bool Sd2Card::isReady() {
return state > DO_STARTUP;
}

Expand Down
Loading

0 comments on commit c662e6f

Please sign in to comment.