Skip to content

Commit 3130424

Browse files
committed
stm32/usbdev: Add support for MSC-only USB device class.
Select this mode in boot.py via: pyb.usb_mode('MSC')
1 parent 72ca049 commit 3130424

3 files changed

Lines changed: 60 additions & 0 deletions

File tree

ports/stm32/usb.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,11 @@ STATIC mp_obj_t pyb_usb_mode(size_t n_args, const mp_obj_t *pos_args, mp_map_t *
293293
pid = USBD_PID_CDC;
294294
}
295295
mode = USBD_MODE_CDC;
296+
} else if (strcmp(mode_str, "MSC") == 0) {
297+
if (args[2].u_int == -1) {
298+
pid = USBD_PID_MSC;
299+
}
300+
mode = USBD_MODE_MSC;
296301
} else {
297302
goto bad_mode;
298303
}

ports/stm32/usb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#define USBD_PID_CDC_MSC (0x9800)
3636
#define USBD_PID_CDC_HID (0x9801)
3737
#define USBD_PID_CDC (0x9802)
38+
#define USBD_PID_MSC (0x9803)
3839

3940
typedef enum {
4041
PYB_USB_STORAGE_MEDIUM_NONE = 0,

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "usbd_ioreq.h"
2929
#include "usbd_cdc_msc_hid.h"
3030

31+
#define MSC_TEMPLATE_CONFIG_DESC_SIZE (32)
3132
#define CDC_TEMPLATE_CONFIG_DESC_SIZE (67)
3233
#define CDC_MSC_TEMPLATE_CONFIG_DESC_SIZE (98)
3334
#define CDC_HID_TEMPLATE_CONFIG_DESC_SIZE (107)
@@ -89,6 +90,54 @@ __ALIGN_BEGIN static uint8_t USBD_CDC_MSC_HID_DeviceQualifierDesc[USB_LEN_DEV_QU
8990
};
9091
*/
9192

93+
// USB MSC device Configuration Descriptor
94+
static const uint8_t msc_template_config_desc[MSC_TEMPLATE_CONFIG_DESC_SIZE] = {
95+
//--------------------------------------------------------------------------
96+
// Configuration Descriptor
97+
0x09, // bLength: Configuration Descriptor size
98+
USB_DESC_TYPE_CONFIGURATION, // bDescriptorType: Configuration
99+
LOBYTE(MSC_TEMPLATE_CONFIG_DESC_SIZE), // wTotalLength: no of returned bytes
100+
HIBYTE(MSC_TEMPLATE_CONFIG_DESC_SIZE),
101+
0x01, // bNumInterfaces: 1 interfaces
102+
0x01, // bConfigurationValue: Configuration value
103+
0x00, // iConfiguration: Index of string descriptor describing the configuration
104+
0x80, // bmAttributes: bus powered; 0xc0 for self powered
105+
0xfa, // bMaxPower: in units of 2mA
106+
107+
//==========================================================================
108+
// MSC only has 1 interface so doesn't need an IAD
109+
110+
//--------------------------------------------------------------------------
111+
// Interface Descriptor
112+
0x09, // bLength: Interface Descriptor size
113+
USB_DESC_TYPE_INTERFACE, // bDescriptorType: interface descriptor
114+
MSC_IFACE_NUM_WITH_CDC, // bInterfaceNumber: Number of Interface
115+
0x00, // bAlternateSetting: Alternate setting
116+
0x02, // bNumEndpoints
117+
0x08, // bInterfaceClass: MSC Class
118+
0x06, // bInterfaceSubClass : SCSI transparent
119+
0x50, // nInterfaceProtocol
120+
0x00, // iInterface:
121+
122+
// Endpoint IN descriptor
123+
0x07, // bLength: Endpoint descriptor length
124+
USB_DESC_TYPE_ENDPOINT, // bDescriptorType: Endpoint descriptor type
125+
MSC_IN_EP, // bEndpointAddress: IN, address 3
126+
0x02, // bmAttributes: Bulk endpoint type
127+
LOBYTE(MSC_MAX_PACKET), // wMaxPacketSize
128+
HIBYTE(MSC_MAX_PACKET),
129+
0x00, // bInterval: ignore for Bulk transfer
130+
131+
// Endpoint OUT descriptor
132+
0x07, // bLength: Endpoint descriptor length
133+
USB_DESC_TYPE_ENDPOINT, // bDescriptorType: Endpoint descriptor type
134+
MSC_OUT_EP, // bEndpointAddress: OUT, address 3
135+
0x02, // bmAttributes: Bulk endpoint type
136+
LOBYTE(MSC_MAX_PACKET), // wMaxPacketSize
137+
HIBYTE(MSC_MAX_PACKET),
138+
0x00, // bInterval: ignore for Bulk transfer
139+
};
140+
92141
// USB CDC MSC device Configuration Descriptor
93142
static const uint8_t cdc_msc_template_config_desc[CDC_MSC_TEMPLATE_CONFIG_DESC_SIZE] = {
94143
//--------------------------------------------------------------------------
@@ -554,6 +603,11 @@ int USBD_SelectMode(usbd_cdc_msc_hid_state_t *usbd, uint32_t mode, USBD_HID_Mode
554603

555604
// construct config desc
556605
switch (usbd->usbd_mode) {
606+
case USBD_MODE_MSC:
607+
usbd->usbd_config_desc_size = sizeof(msc_template_config_desc);
608+
memcpy(usbd->usbd_config_desc, msc_template_config_desc, sizeof(msc_template_config_desc));
609+
break;
610+
557611
case USBD_MODE_CDC_MSC:
558612
usbd->usbd_config_desc_size = sizeof(cdc_msc_template_config_desc);
559613
memcpy(usbd->usbd_config_desc, cdc_msc_template_config_desc, sizeof(cdc_msc_template_config_desc));

0 commit comments

Comments
 (0)