espressif / arduino-esp32

Arduino core for the ESP32
GNU Lesser General Public License v2.1
13.03k stars 7.3k forks source link

Is it possible to switch the USB mode at runtime programmatically? #8277

Open sblantipodi opened 1 year ago

sblantipodi commented 1 year ago

Board

Lolin ESP32-S3 mini

Device Description

ESP32-S3

Hardware Configuration

no gpio in use

Version

latest master (checkout manually)

IDE Name

CLION

Operating System

Windows 11

Flash frequency

80MHz

PSRAM enabled

yes

Upload speed

115200

Description

esptool.js does not work with TinyUSB devices like S2 and S3, https://github.com/espressif/esptool-js/issues/97 this means that you cannot flash this devices using a web tool when they are in TinyUSB mode.

as you can imagine TinyUSB is much better than CDC since CDC creates lags on S3, it consumes more memory and it's not as stable as TinyUSB.

is there a way to flash a CDC firmware using the esptool.js and then switch to TinyUSB at runtime using some lines of code inside our firmware?

Sketch

no sketch

Debug Message

no debug

Other Steps to Reproduce

No response

I have checked existing issues, online documentation and the Troubleshooting Guide

me-no-dev commented 1 year ago

you could manually boot into download mode, which will expose the CDC/JTAG interface and allow you to flash.

sblantipodi commented 1 year ago

@me-no-dev this is not really a solution... most open source projects are using web installers like esp-web-tools

to let users flash the firmware on their devices and then configure the wifi connection on the same webpage before accessing the web interface of the firmware itself.

if you set the device in CDC mode and then you flash a TinyUSB firmware, you can't setup the wifi connection as described before because the improv-wifi does not work since it tries to Serial.print() using the TinyUSB method on a CDC connection.

me-no-dev commented 1 year ago

seems that you mention a few different problems. Getting TinyUSB's CDC to reboot into download, when attempted by esptool.js is not an issue. I made a couple small modifications and got it to work. The problems come afterwards:

sblantipodi commented 1 year ago

@me-no-dev thanks for the answer.

the idea is to

it would be cool to have a function like the ESP.restart() that works like ESP.restartInCdcMode() or ESP.restartInTinyUsbMode()

is it possible to do this? this could be really useful since the only method that works with web tools is CDC.

me-no-dev commented 1 year ago

Switching USB controllers is not all that straight-forward. We can and should look into this in the future, but as of now is not possible.

sblantipodi commented 1 year ago

if you agree please leave this issue open as a reminder, this feature would be really really useful for a lot of use cases in addition to the one I explained before.

me-no-dev commented 1 year ago

done :)

sblantipodi commented 1 year ago

thank you very much, I appreciate it. :)

0xBERNDOG commented 6 months ago

Related to this, is it possible to change the device type programmatically before initialising? I would like to select either CDC or MSC (depending on a user input at boot), and it's okay to reboot in order to change between them

VojtechBartoska commented 5 months ago

Postponing to 3.1.0 milestone.