Skip to content

Commit c7236af

Browse files
committed
stm32/usb: Add TinyUSB Mass Storage support.
Implements USB MSC functionality for STM32 port when using TinyUSB stack, supporting both internal Flash and SD card storage mediums. Signed-off-by: Andrew Leech <[email protected]>
1 parent b4a4b40 commit c7236af

File tree

9 files changed

+220
-10
lines changed

9 files changed

+220
-10
lines changed

ports/stm32/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ SRC_C += \
257257
usbd_hid_interface.c \
258258
usbd_msc_interface.c \
259259
mphalport.c \
260+
msc_disk.c \
260261
mpnetworkport.c \
261262
mpthreadport.c \
262263
irq.c \

ports/stm32/main.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@
9090
#if MICROPY_HW_TINYUSB_STACK
9191
#include "usbd_conf.h"
9292
#include "shared/tinyusb/mp_usbd.h"
93-
#else
94-
#include "usb.h"
9593
#endif
9694

95+
#include "usb.h"
96+
9797
#if MICROPY_PY_THREAD
9898
static pyb_thread_t pyb_thread_main;
9999
#endif
@@ -281,7 +281,7 @@ static bool init_sdcard_fs(void) {
281281
}
282282
}
283283

284-
#if MICROPY_HW_ENABLE_USB && !MICROPY_HW_TINYUSB_STACK
284+
#if MICROPY_HW_USB_MSC
285285
if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_NONE) {
286286
// if no USB MSC medium is selected then use the SD card
287287
pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_SDCARD;
@@ -640,7 +640,7 @@ void stm32_main(uint32_t reset_mode) {
640640
}
641641
#endif
642642

643-
#if MICROPY_HW_STM_USB_STACK
643+
#if MICROPY_HW_USB_MSC
644644
// if the SD card isn't used as the USB MSC medium then use the internal flash
645645
if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_NONE) {
646646
pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_FLASH;

ports/stm32/mpconfigboard_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@
277277
#define MICROPY_HW_USB_CDC_NUM (1)
278278
#endif
279279
#ifndef MICROPY_HW_USB_MSC
280-
#define MICROPY_HW_USB_MSC (MICROPY_HW_STM_USB_STACK)
280+
#define MICROPY_HW_USB_MSC (MICROPY_HW_ENABLE_USB)
281281
#endif
282282
#ifndef MICROPY_HW_USB_HID
283283
#define MICROPY_HW_USB_HID (MICROPY_HW_STM_USB_STACK)

ports/stm32/mpconfigport.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,6 @@
176176
#define MICROPY_FATFS_USE_LABEL (1)
177177
#define MICROPY_FATFS_RPATH (2)
178178
#define MICROPY_FATFS_MULTI_PARTITION (1)
179-
#if MICROPY_HW_TINYUSB_STACK && CFG_TUD_MSC
180-
#define MICROPY_FATFS_MAX_SS (4096)
181-
#endif
182179

183180
#if MICROPY_PY_PYB
184181
extern const struct _mp_obj_module_t pyb_module;

ports/stm32/msc_disk.c

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020-2021 Damien P. George
7+
* Copyright (c) 2025 Andrew Leech
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#include "py/mpconfig.h"
29+
30+
#if MICROPY_HW_USB_MSC && MICROPY_HW_TINYUSB_STACK
31+
32+
#include "py/misc.h"
33+
#include "tusb.h"
34+
#include "storage.h"
35+
#include "sdcard.h"
36+
#include "usb.h"
37+
38+
// Storage medium selection (shared with main.c for compatibility)
39+
pyb_usb_storage_medium_t pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_NONE;
40+
41+
// Ejection state
42+
static bool msc_ejected = false;
43+
44+
// Invoked on SCSI_CMD_INQUIRY.
45+
// Fill vendor id, product id and revision with string up to 8, 16, 4 characters respectively.
46+
void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4]) {
47+
(void)lun;
48+
memcpy(vendor_id, MICROPY_HW_USB_MSC_INQUIRY_VENDOR_STRING, MIN(strlen(MICROPY_HW_USB_MSC_INQUIRY_VENDOR_STRING), 8));
49+
memcpy(product_id, MICROPY_HW_USB_MSC_INQUIRY_PRODUCT_STRING, MIN(strlen(MICROPY_HW_USB_MSC_INQUIRY_PRODUCT_STRING), 16));
50+
memcpy(product_rev, MICROPY_HW_USB_MSC_INQUIRY_REVISION_STRING, MIN(strlen(MICROPY_HW_USB_MSC_INQUIRY_REVISION_STRING), 4));
51+
}
52+
53+
// Invoked on Test-Unit-Ready command.
54+
// Return true allowing host to read/write this LUN (e.g., SD card inserted).
55+
bool tud_msc_test_unit_ready_cb(uint8_t lun) {
56+
(void)lun;
57+
58+
if (msc_ejected || pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_NONE) {
59+
tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x3a, 0x00);
60+
return false;
61+
}
62+
63+
#if MICROPY_HW_ENABLE_SDCARD
64+
if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_SDCARD) {
65+
if (!sdcard_is_present()) {
66+
tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x3a, 0x00);
67+
return false;
68+
}
69+
}
70+
#endif
71+
72+
return true;
73+
}
74+
75+
// Invoked on SCSI_CMD_READ_CAPACITY_10 and SCSI_CMD_READ_FORMAT_CAPACITY to determine the disk size.
76+
void tud_msc_capacity_cb(uint8_t lun, uint32_t *block_count, uint16_t *block_size) {
77+
(void)lun;
78+
79+
if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_FLASH) {
80+
*block_size = storage_get_block_size();
81+
*block_count = storage_get_block_count();
82+
#if MICROPY_HW_ENABLE_SDCARD
83+
} else if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_SDCARD) {
84+
*block_size = SDCARD_BLOCK_SIZE;
85+
*block_count = sdcard_get_capacity_in_bytes() / (uint64_t)SDCARD_BLOCK_SIZE;
86+
#endif
87+
} else {
88+
*block_size = 0;
89+
*block_count = 0;
90+
}
91+
}
92+
93+
// Invoked on Start-Stop-Unit command:
94+
// - Start = 0 : stopped power mode, if load_eject = 1 : unload disk storage
95+
// - Start = 1 : active mode, if load_eject = 1 : load disk storage
96+
bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject) {
97+
(void)lun;
98+
(void)power_condition;
99+
100+
if (load_eject) {
101+
if (start) {
102+
// Load disk storage
103+
msc_ejected = false;
104+
} else {
105+
// Unload disk storage
106+
msc_ejected = true;
107+
}
108+
}
109+
return true;
110+
}
111+
112+
// Callback invoked on READ10 command.
113+
// Copy disk's data to buffer (up to bufsize) and return number of read bytes.
114+
int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void *buffer, uint32_t bufsize) {
115+
(void)lun;
116+
(void)offset;
117+
118+
if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_FLASH) {
119+
uint32_t block_count = bufsize / FLASH_BLOCK_SIZE;
120+
int ret = storage_read_blocks(buffer, lba, block_count);
121+
if (ret != 0) {
122+
return -1;
123+
}
124+
return block_count * FLASH_BLOCK_SIZE;
125+
#if MICROPY_HW_ENABLE_SDCARD
126+
} else if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_SDCARD) {
127+
uint32_t block_count = bufsize / SDCARD_BLOCK_SIZE;
128+
if (sdcard_read_blocks(buffer, lba, block_count) != 0) {
129+
return -1;
130+
}
131+
return block_count * SDCARD_BLOCK_SIZE;
132+
#endif
133+
}
134+
135+
return -1;
136+
}
137+
138+
// Callback invoked on WRITE10 command.
139+
// Process data in buffer to disk's storage and return number of written bytes.
140+
int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t *buffer, uint32_t bufsize) {
141+
(void)lun;
142+
(void)offset;
143+
144+
if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_FLASH) {
145+
uint32_t block_count = bufsize / FLASH_BLOCK_SIZE;
146+
int ret = storage_write_blocks(buffer, lba, block_count);
147+
if (ret != 0) {
148+
return -1;
149+
}
150+
return block_count * FLASH_BLOCK_SIZE;
151+
#if MICROPY_HW_ENABLE_SDCARD
152+
} else if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_SDCARD) {
153+
uint32_t block_count = bufsize / SDCARD_BLOCK_SIZE;
154+
if (sdcard_write_blocks(buffer, lba, block_count) != 0) {
155+
return -1;
156+
}
157+
return block_count * SDCARD_BLOCK_SIZE;
158+
#endif
159+
}
160+
161+
return -1;
162+
}
163+
164+
// Callback invoked on a SCSI command that's not handled by TinyUSB.
165+
int32_t tud_msc_scsi_cb(uint8_t lun, uint8_t const scsi_cmd[16], void *buffer, uint16_t bufsize) {
166+
(void)lun;
167+
(void)buffer;
168+
(void)bufsize;
169+
170+
int32_t resplen = 0;
171+
switch (scsi_cmd[0]) {
172+
case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
173+
// Sync the logical unit if needed.
174+
#if MICROPY_HW_ENABLE_STORAGE
175+
if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_FLASH) {
176+
storage_flush();
177+
}
178+
#endif
179+
break;
180+
181+
default:
182+
// Set Sense = Invalid Command Operation
183+
tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
184+
// negative means error -> tinyusb could stall and/or response with failed status
185+
resplen = -1;
186+
break;
187+
}
188+
return resplen;
189+
}
190+
191+
#endif // MICROPY_HW_USB_MSC && MICROPY_HW_TINYUSB_STACK

ports/stm32/storage.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@ bool storage_read_block(uint8_t *dest, uint32_t block) {
171171

172172
return true;
173173

174+
} else if (block < FLASH_PART1_START_BLOCK) {
175+
// Blocks between MBR and first partition: return zeros
176+
// (This handles blocks 1 to FLASH_PART1_START_BLOCK-1)
177+
memset(dest, 0, FLASH_BLOCK_SIZE);
178+
return true;
179+
174180
#if defined(MICROPY_HW_BDEV_READBLOCK)
175181
} else if (FLASH_PART1_START_BLOCK <= block && block < FLASH_PART1_START_BLOCK + MICROPY_HW_BDEV_IOCTL(BDEV_IOCTL_NUM_BLOCKS, 0)) {
176182
return MICROPY_HW_BDEV_READBLOCK(dest, block - FLASH_PART1_START_BLOCK);
@@ -184,6 +190,10 @@ bool storage_write_block(const uint8_t *src, uint32_t block) {
184190
if (block == 0) {
185191
// can't write MBR, but pretend we did
186192
return true;
193+
} else if (block < FLASH_PART1_START_BLOCK) {
194+
// Blocks between MBR and first partition: ignore writes
195+
// (This handles blocks 1 to FLASH_PART1_START_BLOCK-1)
196+
return true;
187197
#if defined(MICROPY_HW_BDEV_WRITEBLOCK)
188198
} else if (FLASH_PART1_START_BLOCK <= block && block < FLASH_PART1_START_BLOCK + MICROPY_HW_BDEV_IOCTL(BDEV_IOCTL_NUM_BLOCKS, 0)) {
189199
return MICROPY_HW_BDEV_WRITEBLOCK(src, block - FLASH_PART1_START_BLOCK);

ports/stm32/usb.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ typedef struct _usb_device_t {
9090
} usb_device_t;
9191

9292
usb_device_t usb_device = {0};
93+
#if MICROPY_HW_USB_MSC
9394
pyb_usb_storage_medium_t pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_NONE;
95+
#endif
9496

9597
#if !MICROPY_HW_USB_IS_MULTI_OTG
9698

ports/stm32/usb.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
#ifndef MICROPY_INCLUDED_STM32_USB_H
2727
#define MICROPY_INCLUDED_STM32_USB_H
2828

29+
#if MICROPY_HW_STM_USB_STACK
2930
#include "usbd_cdc_msc_hid0.h"
31+
#endif
3032

3133
#define PYB_USB_FLAG_USB_MODE_CALLED (0x0002)
3234

@@ -41,10 +43,16 @@ typedef enum {
4143
USB_PHY_HS_ID = 1,
4244
} USB_PHY_ID;
4345

46+
// Storage medium variable used by both USB stacks when MSC is enabled
47+
#if MICROPY_HW_USB_MSC
48+
extern pyb_usb_storage_medium_t pyb_usb_storage_medium;
49+
#endif
50+
51+
#if MICROPY_HW_STM_USB_STACK
52+
4453
typedef struct _pyb_usb_vcp_obj_t pyb_usb_vcp_obj_t;
4554

4655
extern mp_uint_t pyb_usb_flags;
47-
extern pyb_usb_storage_medium_t pyb_usb_storage_medium;
4856
extern const struct _mp_rom_obj_tuple_t pyb_usb_hid_mouse_obj;
4957
extern const struct _mp_rom_obj_tuple_t pyb_usb_hid_keyboard_obj;
5058
extern const mp_obj_type_t pyb_usb_vcp_type;
@@ -66,5 +74,6 @@ void usb_vcp_attach_to_repl(const pyb_usb_vcp_obj_t *self, bool attached);
6674
void pyb_usb_host_init(void);
6775
void pyb_usb_host_process(void);
6876
uint pyb_usb_host_get_keyboard(void);
77+
#endif
6978

7079
#endif // MICROPY_INCLUDED_STM32_USB_H

ports/stm32/usbdev/class/src/usbd_cdc_msc_hid.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include "usbd_ioreq.h"
2929
#include "usbd_cdc_msc_hid.h"
3030

31-
#if MICROPY_HW_ENABLE_USB
31+
#if MICROPY_HW_STM_USB_STACK
3232

3333
#define HEAD_DESC_SIZE (9)
3434
#define MSC_CLASS_DESC_SIZE (9 + 7 + 7)

0 commit comments

Comments
 (0)