Skip to content

Commit 3fbb5ad

Browse files
committed
stm32/usb: Add TinyUSB Mass Storage Class support.
Implements USB MSC functionality for STM32 port when using TinyUSB stack, supporting both internal Flash and SD card storage mediums. Key changes: - Add msc_disk.c with TinyUSB MSC callback implementations following pattern used in rp2 and mimxrt ports - Configure FATFS sector size to match flash block size for TinyUSB - Enable MSC for TinyUSB in board config (MICROPY_HW_USB_MSC) - Fix usbd_cdc_msc_hid.c to only compile with legacy USB stack - Initialize pyb_usb_storage_medium for both USB stacks in main.c - Update usb.h to conditionally include headers for safe cross-stack usage - Handle blocks between MBR and first partition in storage.c to prevent assertion errors when host OS scans disk The pyb_usb_storage_medium variable is initialized at boot time in main.c (lines 287-290 and 651-653) when MICROPY_HW_USB_MSC is enabled, defaulting to SD card if mounted, otherwise internal flash. This initialization works for both compile-time and runtime MSC configurations. Tested with CDC+MSC composite device: device enumerates correctly, mounts successfully on Linux, and filesystem contents are accessible. Signed-off-by: Andrew Leech <[email protected]>
1 parent 1ae8e56 commit 3fbb5ad

File tree

8 files changed

+220
-6
lines changed

8 files changed

+220
-6
lines changed

ports/stm32/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ SRC_C += \
267267
usbd_hid_interface.c \
268268
usbd_msc_interface.c \
269269
mphalport.c \
270+
msc_disk.c \
270271
mpnetworkport.c \
271272
mpthreadport.c \
272273
irq.c \

ports/stm32/main.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@
9090
#if MICROPY_HW_TINYUSB_STACK
9191
#include "usbd_conf.h"
9292
#include "shared/tinyusb/mp_usbd.h"
93-
#else
93+
#endif
94+
95+
#if MICROPY_HW_ENABLE_USB
9496
#include "usb.h"
9597
#endif
9698

@@ -281,7 +283,7 @@ static bool init_sdcard_fs(void) {
281283
}
282284
}
283285

284-
#if MICROPY_HW_ENABLE_USB && !MICROPY_HW_TINYUSB_STACK
286+
#if MICROPY_HW_USB_MSC
285287
if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_NONE) {
286288
// if no USB MSC medium is selected then use the SD card
287289
pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_SDCARD;
@@ -640,7 +642,7 @@ void stm32_main(uint32_t reset_mode) {
640642
}
641643
#endif
642644

643-
#if MICROPY_HW_STM_USB_STACK
645+
#if MICROPY_HW_USB_MSC
644646
// if the SD card isn't used as the USB MSC medium then use the internal flash
645647
if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_NONE) {
646648
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
@@ -754,7 +754,7 @@
754754
#define MICROPY_HW_USB_CDC_NUM (1)
755755
#endif
756756
#ifndef MICROPY_HW_USB_MSC
757-
#define MICROPY_HW_USB_MSC (MICROPY_HW_STM_USB_STACK)
757+
#define MICROPY_HW_USB_MSC (MICROPY_HW_ENABLE_USB)
758758
#endif
759759
#ifndef MICROPY_HW_USB_HID
760760
#define MICROPY_HW_USB_HID (MICROPY_HW_STM_USB_STACK)

ports/stm32/mpconfigport.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,14 @@
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
179+
#if MICROPY_HW_USB_MSC && MICROPY_HW_TINYUSB_STACK
180+
// Set FatFS block size to flash sector size to avoid caching
181+
// the flash sector in memory to support smaller block sizes.
182+
#if defined(STM32N6)
180183
#define MICROPY_FATFS_MAX_SS (4096)
184+
#else
185+
#define MICROPY_FATFS_MAX_SS (512)
186+
#endif
181187
#endif
182188

183189
#if MICROPY_PY_PYB

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

Lines changed: 4 additions & 0 deletions
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

@@ -54,6 +56,7 @@ MP_DECLARE_CONST_FUN_OBJ_KW(pyb_usb_mode_obj);
5456
MP_DECLARE_CONST_FUN_OBJ_0(pyb_have_cdc_obj); // deprecated
5557
MP_DECLARE_CONST_FUN_OBJ_1(pyb_hid_send_report_obj); // deprecated
5658

59+
#if MICROPY_HW_STM_USB_STACK
5760
void pyb_usb_init0(void);
5861
int pyb_usb_dev_detect(void);
5962
bool pyb_usb_dev_init(int dev_id, uint16_t vid, uint16_t pid, uint8_t mode, size_t msc_n, const void *msc_unit, USBD_HID_ModeInfoTypeDef *hid_info);
@@ -66,5 +69,6 @@ void usb_vcp_attach_to_repl(const pyb_usb_vcp_obj_t *self, bool attached);
6669
void pyb_usb_host_init(void);
6770
void pyb_usb_host_process(void);
6871
uint pyb_usb_host_get_keyboard(void);
72+
#endif
6973

7074
#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)