Skip to content

Commit 2c94197

Browse files
rhapsodyvthinkyhead
authored andcommitted
USB FD via native USB Host + MSC (MarlinFirmware#20571)
1 parent 998cb6f commit 2c94197

File tree

13 files changed

+300
-33
lines changed

13 files changed

+300
-33
lines changed

Marlin/Configuration_adv.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,9 +1326,6 @@
13261326
*/
13271327
//#define USB_FLASH_DRIVE_SUPPORT
13281328
#if ENABLED(USB_FLASH_DRIVE_SUPPORT)
1329-
#define USB_CS_PIN SDSS
1330-
#define USB_INTR_PIN SD_DETECT_PIN
1331-
13321329
/**
13331330
* USB Host Shield Library
13341331
*
@@ -1339,7 +1336,18 @@
13391336
* is less tested and is known to interfere with Servos.
13401337
* [1] This requires USB_INTR_PIN to be interrupt-capable.
13411338
*/
1339+
//#define USE_UHS2_USB
13421340
//#define USE_UHS3_USB
1341+
1342+
/**
1343+
* Native USB Host supported by some boards (USB OTG)
1344+
*/
1345+
//#define USE_OTG_USB_HOST
1346+
1347+
#if DISABLED(USE_OTG_USB_HOST)
1348+
#define USB_CS_PIN SDSS
1349+
#define USB_INTR_PIN SD_DETECT_PIN
1350+
#endif
13431351
#endif
13441352

13451353
/**

Marlin/src/HAL/STM32/usb_host.cpp

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
* Marlin 3D Printer Firmware
3+
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4+
*
5+
* Based on Sprinter and grbl.
6+
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
#if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC)
24+
25+
#include "../../inc/MarlinConfig.h"
26+
27+
#if BOTH(USE_OTG_USB_HOST, USBHOST)
28+
29+
#include "usb_host.h"
30+
#include "../shared/Marduino.h"
31+
#include "usbh_core.h"
32+
#include "usbh_msc.h"
33+
34+
USBH_HandleTypeDef hUsbHost;
35+
USBHost usb;
36+
BulkStorage bulk(&usb);
37+
38+
static void USBH_UserProcess(USBH_HandleTypeDef *phost, uint8_t id) {
39+
switch(id) {
40+
case HOST_USER_SELECT_CONFIGURATION:
41+
//SERIAL_ECHOLNPGM("APPLICATION_SELECT_CONFIGURATION");
42+
break;
43+
case HOST_USER_DISCONNECTION:
44+
//SERIAL_ECHOLNPGM("APPLICATION_DISCONNECT");
45+
//usb.setUsbTaskState(USB_STATE_RUNNING);
46+
break;
47+
case HOST_USER_CLASS_ACTIVE:
48+
//SERIAL_ECHOLNPGM("APPLICATION_READY");
49+
usb.setUsbTaskState(USB_STATE_RUNNING);
50+
break;
51+
case HOST_USER_CONNECTION:
52+
break;
53+
default:
54+
break;
55+
}
56+
}
57+
58+
bool USBHost::start() {
59+
if (USBH_Init(&hUsbHost, USBH_UserProcess, TERN(USE_USB_HS_IN_FS, HOST_HS, HOST_FS)) != USBH_OK) {
60+
SERIAL_ECHOLNPGM("Error: USBH_Init");
61+
return false;
62+
}
63+
if (USBH_RegisterClass(&hUsbHost, USBH_MSC_CLASS) != USBH_OK) {
64+
SERIAL_ECHOLNPGM("Error: USBH_RegisterClass");
65+
return false;
66+
}
67+
if (USBH_Start(&hUsbHost) != USBH_OK) {
68+
SERIAL_ECHOLNPGM("Error: USBH_Start");
69+
return false;
70+
}
71+
return true;
72+
}
73+
74+
void USBHost::Task() {
75+
USBH_Process(&hUsbHost);
76+
}
77+
78+
uint8_t USBHost::getUsbTaskState() {
79+
return usb_task_state;
80+
}
81+
82+
void USBHost::setUsbTaskState(uint8_t state) {
83+
usb_task_state = state;
84+
if (usb_task_state == USB_STATE_RUNNING) {
85+
MSC_LUNTypeDef info;
86+
USBH_MSC_GetLUNInfo(&hUsbHost, usb.lun, &info);
87+
capacity = info.capacity.block_nbr / 2000;
88+
block_size = info.capacity.block_size;
89+
block_count = info.capacity.block_nbr;
90+
// SERIAL_ECHOLNPAIR("info.capacity.block_nbr : %ld\n", info.capacity.block_nbr);
91+
// SERIAL_ECHOLNPAIR("info.capacity.block_size: %d\n", info.capacity.block_size);
92+
// SERIAL_ECHOLNPAIR("capacity : %d MB\n", capacity);
93+
}
94+
};
95+
96+
bool BulkStorage::LUNIsGood(uint8_t t) {
97+
return USBH_MSC_IsReady(&hUsbHost) && USBH_MSC_UnitIsReady(&hUsbHost, t);
98+
}
99+
100+
uint32_t BulkStorage::GetCapacity(uint8_t lun) {
101+
return usb->block_count;
102+
}
103+
104+
uint16_t BulkStorage::GetSectorSize(uint8_t lun) {
105+
return usb->block_size;
106+
}
107+
108+
uint8_t BulkStorage::Read(uint8_t lun, uint32_t addr, uint16_t bsize, uint8_t blocks, uint8_t *buf) {
109+
return USBH_MSC_Read(&hUsbHost, lun, addr, buf, blocks) != USBH_OK;
110+
}
111+
112+
uint8_t BulkStorage::Write(uint8_t lun, uint32_t addr, uint16_t bsize, uint8_t blocks, const uint8_t * buf) {
113+
return USBH_MSC_Write(&hUsbHost, lun, addr, const_cast <uint8_t*>(buf), blocks) != USBH_OK;
114+
}
115+
116+
#endif // USE_OTG_USB_HOST && USBHOST
117+
#endif // ARDUINO_ARCH_STM32 && !STM32GENERIC

Marlin/src/HAL/STM32/usb_host.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Marlin 3D Printer Firmware
3+
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4+
*
5+
* Based on Sprinter and grbl.
6+
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
#pragma once
23+
24+
#include <stdint.h>
25+
26+
typedef enum {
27+
USB_STATE_INIT,
28+
USB_STATE_ERROR,
29+
USB_STATE_RUNNING,
30+
} usb_state_t;
31+
32+
class USBHost {
33+
public:
34+
bool start();
35+
void Task();
36+
uint8_t getUsbTaskState();
37+
void setUsbTaskState(uint8_t state);
38+
uint8_t regRd(uint8_t reg) { return 0x0; };
39+
uint8_t usb_task_state = USB_STATE_INIT;
40+
uint8_t lun = 0;
41+
uint32_t capacity = 0;
42+
uint16_t block_size = 0;
43+
uint32_t block_count = 0;
44+
};
45+
46+
class BulkStorage {
47+
public:
48+
BulkStorage(USBHost *usb) : usb(usb) {};
49+
50+
bool LUNIsGood(uint8_t t);
51+
uint32_t GetCapacity(uint8_t lun);
52+
uint16_t GetSectorSize(uint8_t lun);
53+
uint8_t Read(uint8_t lun, uint32_t addr, uint16_t bsize, uint8_t blocks, uint8_t *buf);
54+
uint8_t Write(uint8_t lun, uint32_t addr, uint16_t bsize, uint8_t blocks, const uint8_t * buf);
55+
56+
USBHost *usb;
57+
};
58+
59+
extern USBHost usb;
60+
extern BulkStorage bulk;

Marlin/src/inc/Conditionals_adv.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,10 @@
386386
#define HOMING_BUMP_MM { 0, 0, 0 }
387387
#endif
388388

389+
#if ENABLED(USB_FLASH_DRIVE_SUPPORT) && NONE(USE_OTG_USB_HOST, USE_UHS3_USB)
390+
#define USE_UHS2_USB
391+
#endif
392+
389393
/**
390394
* Driver Timings (in nanoseconds)
391395
* NOTE: Driver timing order is longest-to-shortest duration.

Marlin/src/inc/SanityCheck.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2917,10 +2917,14 @@ static_assert( _ARR_TEST(3,0) && _ARR_TEST(3,1) && _ARR_TEST(3,2)
29172917
#error "PRINTCOUNTER requires EEPROM_SETTINGS."
29182918
#endif
29192919

2920-
#if ENABLED(USB_FLASH_DRIVE_SUPPORT) && !PINS_EXIST(USB_CS, USB_INTR)
2920+
#if ENABLED(USB_FLASH_DRIVE_SUPPORT) && !PINS_EXIST(USB_CS, USB_INTR) && DISABLED(USE_OTG_USB_HOST)
29212921
#error "USB_CS_PIN and USB_INTR_PIN are required for USB_FLASH_DRIVE_SUPPORT."
29222922
#endif
29232923

2924+
#if ENABLED(USE_OTG_USB_HOST) && !defined(HAS_OTG_USB_HOST_SUPPORT)
2925+
#error "The current board does not support USE_OTG_USB_HOST."
2926+
#endif
2927+
29242928
#if ENABLED(SD_FIRMWARE_UPDATE) && !defined(__AVR_ATmega2560__)
29252929
#error "SD_FIRMWARE_UPDATE requires an ATmega2560-based (Arduino Mega) board."
29262930
#endif

Marlin/src/pins/stm32f1/pins_MKS_ROBIN_MINI.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@
5252

5353
#define SPI_DEVICE 2
5454

55+
//
56+
// Servos
57+
//
58+
#ifndef SERVO0_PIN
59+
#define SERVO0_PIN PA8 // Enable BLTOUCH support on IO0 (WIFI connector)
60+
#endif
61+
5562
//
5663
// Limit Switches
5764
//
@@ -91,6 +98,7 @@
9198
#ifndef DEFAULT_PWM_MOTOR_CURRENT
9299
#define DEFAULT_PWM_MOTOR_CURRENT { 800, 800, 800 }
93100
#endif
101+
94102
//
95103
// Temperature Sensors
96104
//
@@ -111,10 +119,6 @@
111119
#define POWER_LOSS_PIN PA2 // PW_DET
112120
#define PS_ON_PIN PA3 // PW_OFF
113121

114-
#ifndef SERVO0_PIN
115-
#define SERVO0_PIN PA8 // Enable BLTOUCH support on IO0 (WIFI connector)
116-
#endif
117-
118122
#define MT_DET_1_PIN PA4
119123
#define MT_DET_PIN_INVERTING false
120124

Marlin/src/pins/stm32f4/pins_BTT_GTR_V1_0.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
#define I2C_EEPROM
3636
#define MARLIN_EEPROM_SIZE 0x2000 // 8KB (24C64 ... 64Kb = 8KB)
3737

38+
// USB Flash Drive support
39+
#define HAS_OTG_USB_HOST_SUPPORT
40+
3841
#define TP // Enable to define servo and probe pins
3942

4043
//

Marlin/src/pins/stm32f4/pins_BTT_SKR_PRO_common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
#define FLASH_EEPROM_LEVELING
4545
#endif
4646

47+
// USB Flash Drive support
48+
#define HAS_OTG_USB_HOST_SUPPORT
49+
4750
//
4851
// Servos
4952
//

Marlin/src/pins/stm32f4/pins_MKS_ROBIN_NANO_V3.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
//#define SRAM_EEPROM_EMULATION // Use BackSRAM-based EEPROM emulation
4242
//#define FLASH_EEPROM_EMULATION // Use Flash-based EEPROM emulation
4343
#define I2C_EEPROM
44+
#define MARLIN_EEPROM_SIZE 0x1000 // 4KB
4445

4546
//
4647
// Release PB4 (Z_DIR_PIN) from JTAG NRST role

Marlin/src/pins/stm32f4/pins_MKS_ROBIN_PRO_V2.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
//#define SRAM_EEPROM_EMULATION // Use BackSRAM-based EEPROM emulation
3737
//#define FLASH_EEPROM_EMULATION // Use Flash-based EEPROM emulation
3838
#define I2C_EEPROM
39+
#define MARLIN_EEPROM_SIZE 0x1000 // 4KB
40+
41+
// USB Flash Drive support
42+
#define HAS_OTG_USB_HOST_SUPPORT
3943

4044
//
4145
// Release PB4 (Y_ENABLE_PIN) from JTAG NRST role

0 commit comments

Comments
 (0)