espressif / idf-extra-components

Additional components for ESP-IDF, maintained by Espressif
147 stars 89 forks source link

Unplugging the usb cable from the esp32s3 did not trigger the corresponding event (IEC-47) #234

Closed hongquan-prog closed 12 months ago

hongquan-prog commented 1 year ago

Answers checklist.

Which component are you using? If you choose Other, provide details in More Information.

usb

ESP-IDF version.

ESP-IDF v5.1.1-233-gf0437b945f

Development Kit.

Custom

Used Component version.

espressif/esp_tinyusb: 1.4.0 espressif/tinyusb:0.14.3

More Information.

// Invoked when device is unmounted
void tud_umount_cb(void)
{
    printf("tud_umount_cb: %p\n", s_storage_handle->base_path);

    if (tinyusb_msc_storage_mount(s_storage_handle->base_path) != ESP_OK) {
        ESP_LOGW(TAG, "tud_umount_cb() mount Fails");
    }
}

// Invoked when device is mounted (configured)
void tud_mount_cb(void)
{
    tinyusb_msc_storage_unmount();
    printf("tinyusb_msc_storage_unmount : %p\n", s_storage_handle->base_path);
}

I did this by adding a print filesystem mount path to the code, and when the USB cable was removed from the computer, the mount path became a null pointer and had to be remounted manually for it to work.the serial output is as follows:

I (5856) TinyUSB: TinyUSB Driver installed
I (5861) main: USB initialization DONE
I (5864) main_task: Returned from app_main()
tinyusb_msc_storage_unmount : 0x0
tinyusb_msc_storage_unmount : 0x0
tinyusb_msc_storage_unmount : 0x0
tinyusb_msc_storage_unmount : 0x0
tinyusb_msc_storage_unmount : 0x0
tinyusb_msc_storage_unmount : 0x0
tinyusb_msc_storage_unmount : 0x0
tinyusb_msc_storage_unmount : 0x0
tinyusb_msc_storage_unmount : 0x0
tinyusb_msc_storage_unmount : 0x0
tinyusb_msc_storage_unmount : 0x0
tinyusb_msc_storage_unmount : 0x0
tinyusb_msc_storage_unmount : 0x0

By printing I also found that tud_umount_cb is not called after I unplugged the usb, the event type triggered after unplugging the usb is DCD_EVENT_SUSPEND, not DCD_EVENT_UNPLUGGED.

leeebo commented 1 year ago

@hongquan-prog For a self-powered USB device, the VBUS monitor must be enabled. please refer the docs

hongquan-prog commented 1 year ago

@hongquan-prog For a self-powered USB device, the VBUS monitor must be enabled. please refer the docs

Thank you very much for your help, I didn't consider making a standalone powered device at first, I just added a serial port, which will still power the esp32 when the USB is unplugged!