Page 1 of 1

Esp32S3 : TinyUSB with MSC storage functionnality. Is it possible to detect USB unplug ?

Posted: Thu Nov 07, 2024 4:46 pm
by ThomasESP32
Good afternoon,

using the example tinyusb MSC storage, I managed to turn an Esp32S3 into a kind of USB Key. I mean I have defined a partition on the internal flash called "IntFlash" and when the device starts, this partition is mounted like an USB Key and is normally detected by Windows.

I have the possibility to put/delete files on it correctly.

Now, because my device is self powered, I would like to add the possibility to detect USB unplug and to manage an event when the unplug is detected.

Do you know how I can do it please ? Is it possible ?

Here is my program : (Before that a FileSystem is mounted and I get an IntFlashFlashHandle on it.

/* On initialise l'USBDrive sur la SPIFlash interne : */
storage_init_spiflash(&IntFlashFlashHandle);
tinyusb_msc_spiflash_config_t config_spi =
{
.wl_handle = IntFlashFlashHandle,
.callback_mount_changed = storage_mount_changed_cb,
.mount_config = IntFlash_MountCfg
};

/* On appelle les méthodes TinyUSB permettant d'initialiser la SPIFlash : */
tinyusb_msc_storage_init_spiflash(&config_spi);
tinyusb_msc_register_callback(TINYUSB_MSC_EVENT_MOUNT_CHANGED, storage_mount_changed_cb); /* Other way to register the callback i.e. registering using separate API. If the callback had been already registered, it will be overwritten. */

/* On monte le FileSystem : */
_mount(MOUNT_POINT_INTFLASH);

/* On monte le FileSystem : */
const tinyusb_config_t tusb_cfg = {
.device_descriptor = &descriptor_config,
.string_descriptor = string_desc_arr,
.string_descriptor_count = sizeof(string_desc_arr) / sizeof(string_desc_arr[0]),
.external_phy = false,
.configuration_descriptor = desc_configuration,
};

/* On installe le Driver TinyUSB : */
tinyusb_driver_install(&tusb_cfg);