Skip to content

USB stack will not reinitialize after deinit. (IDFGH-14332) #15122

@marchingband

Description

@marchingband

Answers checklist.

  • I have read the documentation ESP-IDF Programming Guide and the issue is not addressed there.
  • I have updated my IDF branch (master or release) to the latest version and checked that the issue is present there.
  • I have searched the issue tracker for a similar issue and not found a similar issue.

IDF version.

5.1.2

Espressif SoC revision.

esp32-s3

Operating System used.

macOS

How did you build your project?

Command line with idf.py

If you are using Windows, please specify command line type.

None

Development Kit.

custom

Power Supply used.

External 3.3V

What is the expected behavior?

I expect to be able to

  1. initialize the MSC interface
  2. deinitialize it
  3. initialize the CDC interface
    etc.

What is the actual behavior?

CDC reports no errors, but does not work.
However if I start with the CDC it works well, so I know my config is correct.

Steps to reproduce.

esp_err_t usb_msc_init( void ){
    static wl_handle_t wl_handle = WL_INVALID_HANDLE;
    ESP_ERROR_CHECK(storage_init(&wl_handle));
    const tinyusb_msc_spiflash_config_t config_spi = {
        .wl_handle = wl_handle
    };
    ESP_ERROR_CHECK(tinyusb_msc_storage_init_spiflash(&config_spi));
    mount();
    ESP_LOGI(TAG, "USB MSC initialization");
    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,
    };
    ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
    return ESP_OK;
}

void usb_reset(void) {
    ESP_LOGI(TAG, "Resetting USB hardware");
    periph_module_disable(PERIPH_USB_MODULE);
    vTaskDelay(pdMS_TO_TICKS(100)); 
    periph_module_enable(PERIPH_USB_MODULE);
    ESP_LOGI(TAG, "USB hardware reset complete");
}

esp_err_t usb_msc_deinit( void ){
    tinyusb_msc_storage_deinit();
    ESP_ERROR_CHECK(tinyusb_driver_uninstall());
    ESP_ERROR_CHECK(tusb_stop_task());
//    usb_reset();
    return ESP_OK;
}

void usb_cdc_init(void){
    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 = descriptor_cfg,
    };
    ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));

    tinyusb_config_cdcacm_t acm_cfg = {
        .usb_dev = TINYUSB_USBDEV_0,
        .cdc_port = TINYUSB_CDC_ACM_0,
        .rx_unread_buf_sz = 64,
        .callback_rx = &tinyusb_cdc_rx_callback, // the first way to register a callback
        .callback_rx_wanted_char = NULL,
        .callback_line_state_changed = tinyusb_cdc_line_state_changed_callback,
        .callback_line_coding_changed = NULL
    };
    ESP_ERROR_CHECK(tusb_cdc_acm_init(&acm_cfg));
}

void app_main(void){
    ESP_LOGI(TAG, "start MSC");
    usb_msc_init();
    vTaskDelay(5000 / portTICK_PERIOD_MS);
    ESP_LOGI(TAG, "stop MSC");
    usb_msc_deinit();
    vTaskDelay(1000 / portTICK_PERIOD_MS);
    ESP_LOGI(TAG, "start CDC");
    usb_cdc_init();
    while(1){
        vTaskDelay(10000 / portTICK_PERIOD_MS);
        ESP_LOGI(TAG, "main");
    }
}

### Debug Logs.

```plain
I (349) NBMS: start MSC
I (352) usb_msc: Initializing storage...
I (356) usb_msc: Initializing wear levelling
I (363) usb_msc: Mount storage...
I (366) usb_msc: 
ls command output:
.fseventsd
I (371) usb_msc: USB MSC initialization
I (375) tusb_desc: 
┌─────────────────────────────────┐
│  USB Device Descriptor Summary  │
├───────────────────┬─────────────┤
│bDeviceClass       │ 239         │
├───────────────────┼─────────────┤
│bDeviceSubClass    │ 2           │
├───────────────────┼─────────────┤
│bDeviceProtocol    │ 1           │
├───────────────────┼─────────────┤
│bMaxPacketSize0    │ 64          │
├───────────────────┼─────────────┤
│idVendor           │ 0x303a      │
├───────────────────┼─────────────┤
│idProduct          │ 0x4002      │
├───────────────────┼─────────────┤
│bcdDevice          │ 0x100       │
├───────────────────┼─────────────┤
│iManufacturer      │ 0x1         │
├───────────────────┼─────────────┤
│iProduct           │ 0x2         │
├───────────────────┼─────────────┤
│iSerialNumber      │ 0x3         │
├───────────────────┼─────────────┤
│bNumConfigurations │ 0x1         │
└───────────────────┴─────────────┘
I (544) TinyUSB: TinyUSB Driver installed
I (548) usb_msc: USB MSC initialization DONE
I (5553) NBMS: stop MSC
I (5553) usb_msc: calling tinyusb_msc_storage_deinit
I (5553) usb_msc: calling tusb_stop_task
I (5555) usb_msc: calling tinyusb_driver_uninstall
I (5561) usb_msc: calling usb_reset
I (5565) usb_msc: Resetting USB hardware
I (5669) usb_msc: USB hardware reset complete
I (5669) usb_msc: usb_cdc_deinit done!
I (6669) NBMS: start CDC
I (6669) tusb_desc: 
┌─────────────────────────────────┐
│  USB Device Descriptor Summary  │
├───────────────────┬─────────────┤
│bDeviceClass       │ 2           │
├───────────────────┼─────────────┤
│bDeviceSubClass    │ 0           │
├───────────────────┼─────────────┤
│bDeviceProtocol    │ 0           │
├───────────────────┼─────────────┤
│bMaxPacketSize0    │ 64          │
├───────────────────┼─────────────┤
│idVendor           │ 0x303a      │
├───────────────────┼─────────────┤
│idProduct          │ 0x4002      │
├───────────────────┼─────────────┤
│bcdDevice          │ 0x100       │
├───────────────────┼─────────────┤
│iManufacturer      │ 0x1         │
├───────────────────┼─────────────┤
│iProduct           │ 0x2         │
├───────────────────┼─────────────┤
│iSerialNumber      │ 0x3         │
├───────────────────┼─────────────┤
│bNumConfigurations │ 0x1         │
└───────────────────┴─────────────┘
I (6829) TinyUSB: TinyUSB Driver installed
I (6834) usb_cdc: USB Composite initialization DONE


### More Information.

_No response_

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions