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

This commit is contained in:
Victor Oliveira
2020-12-29 02:16:38 -03:00
committed by GitHub
parent d6a56b882f
commit 84ab088b40
13 changed files with 300 additions and 33 deletions

View File

@ -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

View File

@ -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;

View File

@ -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.

View File

@ -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

View File

@ -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

View File

@ -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
//

View File

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

View File

@ -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

View File

@ -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

View File

@ -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
@ -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"
@ -250,7 +262,7 @@ bool Sd2Card::isInserted() {
return state == MEDIA_READY;
}
bool Sd2Card::ready() {
bool Sd2Card::isReady() {
return state > DO_STARTUP;
}

View File

@ -23,26 +23,27 @@
/**
* \file
* \brief Sd2Card class for V2 SD/SDHC cards
* \brief Sd2Card class for USB Flash Drive
*/
#include "../SdFatConfig.h"
#include "../SdInfo.h"
/**
* Define SOFTWARE_SPI to use bit-bang SPI
*/
#if EITHER(MEGA_SOFT_SPI, USE_SOFTWARE_SPI)
#define SOFTWARE_SPI
#endif
#if DISABLED(USE_OTG_USB_HOST)
/**
* Define SOFTWARE_SPI to use bit-bang SPI
*/
#if EITHER(MEGA_SOFT_SPI, USE_SOFTWARE_SPI)
#define SOFTWARE_SPI
#endif
// SPI pin definitions - do not edit here - change in SdFatConfig.h
#if ENABLED(SOFTWARE_SPI)
#warning "Auto-assigning '10' as the SD_CHIP_SELECT_PIN."
#define SD_CHIP_SELECT_PIN 10 // Software SPI chip select pin for the SD
#else
// hardware pin defs
#define SD_CHIP_SELECT_PIN SS_PIN // The default chip select pin for the SD card is SS.
// SPI pin definitions - do not edit here - change in SdFatConfig.h
#if ENABLED(SOFTWARE_SPI)
#warning "Auto-assigning '10' as the SD_CHIP_SELECT_PIN."
#define SD_CHIP_SELECT_PIN 10 // Software SPI chip select pin for the SD
#else
// hardware pin defs
#define SD_CHIP_SELECT_PIN SS_PIN // The default chip select pin for the SD card is SS.
#endif
#endif
class Sd2Card {
@ -54,22 +55,24 @@ class Sd2Card {
public:
static bool usbStartup();
bool init(const uint8_t sckRateID=0, const pin_t chipSelectPin=SD_CHIP_SELECT_PIN);
bool init(const uint8_t sckRateID=0, const pin_t chipSelectPin=TERN(USE_OTG_USB_HOST, 0, SD_CHIP_SELECT_PIN));
static void idle();
inline bool readStart(const uint32_t block) { pos = block; return ready(); }
inline bool readStart(const uint32_t block) { pos = block; return isReady(); }
inline bool readData(uint8_t* dst) { return readBlock(pos++, dst); }
inline bool readStop() const { return true; }
inline bool writeStart(const uint32_t block, const uint32_t) { pos = block; return ready(); }
inline bool writeStart(const uint32_t block, const uint32_t) { pos = block; return isReady(); }
inline bool writeData(uint8_t* src) { return writeBlock(pos++, src); }
inline bool writeStop() const { return true; }
bool readBlock(uint32_t block, uint8_t* dst);
bool writeBlock(uint32_t blockNumber, const uint8_t* src);
bool readCSD(csd_t* csd) { return true; };
uint32_t cardSize();
static bool isInserted();
static bool ready();
bool isReady();
};