@@ -68,9 +68,11 @@ static int __usb_task_irq;
6868#define USBD_STR_SERIAL (0x03 )
6969#define USBD_STR_CDC (0x04 )
7070
71-
7271#define EPNUM_HID 0x83
7372
73+ #define USBD_MSC_EPOUT 0x03
74+ #define USBD_MSC_EPIN 0x84
75+ #define USBD_MSC_EPSIZE 64
7476
7577const uint8_t *tud_descriptor_device_cb (void ) {
7678 static tusb_desc_device_t usbd_desc_device = {
@@ -89,7 +91,7 @@ const uint8_t *tud_descriptor_device_cb(void) {
8991 .iSerialNumber = USBD_STR_SERIAL,
9092 .bNumConfigurations = 1
9193 };
92- if (__USBInstallSerial && !__USBInstallKeyboard && !__USBInstallMouse && !__USBInstallJoystick) {
94+ if (__USBInstallSerial && !__USBInstallKeyboard && !__USBInstallMouse && !__USBInstallJoystick && !__USBInstallMassStorage ) {
9395 // Can use as-is, this is the default USB case
9496 return (const uint8_t *)&usbd_desc_device;
9597 }
@@ -103,6 +105,9 @@ const uint8_t *tud_descriptor_device_cb(void) {
103105 if (__USBInstallJoystick) {
104106 usbd_desc_device.idProduct |= 0x0100 ;
105107 }
108+ if (__USBInstallMassStorage) {
109+ usbd_desc_device.idProduct ^= 0x2000 ;
110+ }
106111 // Set the device class to 0 to indicate multiple device classes
107112 usbd_desc_device.bDeviceClass = 0 ;
108113 usbd_desc_device.bDeviceSubClass = 0 ;
@@ -223,7 +228,7 @@ void __SetupUSBDescriptor() {
223228 if (!usbd_desc_cfg) {
224229 bool hasHID = __USBInstallKeyboard || __USBInstallMouse || __USBInstallJoystick;
225230
226- uint8_t interface_count = (__USBInstallSerial ? 2 : 0 ) + (hasHID ? 1 : 0 );
231+ uint8_t interface_count = (__USBInstallSerial ? 2 : 0 ) + (hasHID ? 1 : 0 ) + (__USBInstallMassStorage ? 1 : 0 ) ;
227232
228233 uint8_t cdc_desc[TUD_CDC_DESC_LEN] = {
229234 // Interface number, string index, protocol, report descriptor len, EP In & Out address, size & polling interval
@@ -238,7 +243,12 @@ void __SetupUSBDescriptor() {
238243 TUD_HID_DESCRIPTOR (hid_itf, 0 , HID_ITF_PROTOCOL_NONE, hid_report_len, EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 10 )
239244 };
240245
241- int usbd_desc_len = TUD_CONFIG_DESC_LEN + (__USBInstallSerial ? sizeof (cdc_desc) : 0 ) + (hasHID ? sizeof (hid_desc) : 0 );
246+ uint8_t msd_itf = interface_count - 1 ;
247+ uint8_t msd_desc[TUD_MSC_DESC_LEN] = {
248+ TUD_MSC_DESCRIPTOR (msd_itf, 0 , USBD_MSC_EPOUT, USBD_MSC_EPIN, USBD_MSC_EPSIZE)
249+ };
250+
251+ int usbd_desc_len = TUD_CONFIG_DESC_LEN + (__USBInstallSerial ? sizeof (cdc_desc) : 0 ) + (hasHID ? sizeof (hid_desc) : 0 ) + (__USBInstallMassStorage ? sizeof (msd_desc) : 0 );
242252
243253 uint8_t tud_cfg_desc[TUD_CONFIG_DESC_LEN] = {
244254 // Config number, interface count, string index, total length, attribute, power in mA
@@ -260,6 +270,10 @@ void __SetupUSBDescriptor() {
260270 memcpy (ptr, hid_desc, sizeof (hid_desc));
261271 ptr += sizeof (hid_desc);
262272 }
273+ if (__USBInstallMassStorage) {
274+ memcpy (ptr, msd_desc, sizeof (msd_desc));
275+ ptr += sizeof (msd_desc);
276+ }
263277 }
264278 }
265279}
@@ -367,4 +381,55 @@ extern "C" void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id, hid_r
367381 (void ) bufsize;
368382}
369383
384+ extern " C" int32_t tud_msc_read10_cb (uint8_t lun, uint32_t lba, uint32_t offset, void * buffer, uint32_t bufsize) __attribute__((weak));
385+ extern " C" int32_t tud_msc_read10_cb (uint8_t lun, uint32_t lba, uint32_t offset, void * buffer, uint32_t bufsize) {
386+ (void ) lun;
387+ (void ) lba;
388+ (void ) offset;
389+ (void ) buffer;
390+ (void ) bufsize;
391+ return -1 ;
392+ }
393+
394+ extern " C" bool tud_msc_test_unit_ready_cb (uint8_t lun) __attribute__((weak));
395+ extern " C" bool tud_msc_test_unit_ready_cb (uint8_t lun) {
396+ (void ) lun;
397+ return false ;
398+ }
399+
400+ extern " C" int32_t tud_msc_write10_cb (uint8_t lun, uint32_t lba, uint32_t offset, uint8_t * buffer, uint32_t bufsize) __attribute__((weak));
401+ extern " C" int32_t tud_msc_write10_cb (uint8_t lun, uint32_t lba, uint32_t offset, uint8_t * buffer, uint32_t bufsize) {
402+ (void ) lun;
403+ (void ) lba;
404+ (void ) offset;
405+ (void ) buffer;
406+ (void ) bufsize;
407+ return -1 ;
408+ }
409+
410+ extern " C" int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16 ], void * buffer, uint16_t bufsize) __attribute__((weak));
411+ extern " C" int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16 ], void * buffer, uint16_t bufsize) {
412+ (void ) lun;
413+ (void ) scsi_cmd;
414+ (void ) buffer;
415+ (void ) bufsize;
416+ return 0 ;
417+ }
418+
419+ extern " C" void tud_msc_capacity_cb (uint8_t lun, uint32_t * block_count, uint16_t * block_size) __attribute__((weak));
420+ extern " C" void tud_msc_capacity_cb (uint8_t lun, uint32_t * block_count, uint16_t * block_size) {
421+ (void ) lun;
422+ *block_count = 0 ;
423+ *block_size = 0 ;
424+ }
425+
426+ extern " C" void tud_msc_inquiry_cb (uint8_t lun, uint8_t vendor_id[8 ], uint8_t product_id[16 ], uint8_t product_rev[4 ]) __attribute__((weak));
427+ extern " C" void tud_msc_inquiry_cb (uint8_t lun, uint8_t vendor_id[8 ], uint8_t product_id[16 ], uint8_t product_rev[4 ]) {
428+ (void ) lun;
429+ vendor_id[0 ] = 0 ;
430+ product_id[0 ] = 0 ;
431+ product_rev[0 ] = 0 ;
432+ }
433+
434+
370435#endif
0 commit comments