Skip to main content

Module usb

Module usb 

Source
Expand description

USB-CDC-ACM device with esptool auto-reset support.

The Lolin S2 Mini has no USB-UART bridge, so esptool cannot toggle GPIO0/EN via physical DTR/RTS pins. Instead, the firmware exposes the native USB-OTG port as a CDC-ACM device and interprets the CDC control-line state the way the classic devkit reset sequence does:

DTRRTSMeaningAction
10IO0 lowset FORCE_DOWNLOAD_BOOT
01EN low (reset)software reset
00idleclear FORCE_DOWNLOAD_BOOT
11transientno-op

esptool’s --before usb-reset walk is (0,0) -> (1,0) -> (0,1) -> (0,0): the (1,0) state arms FORCE_DOWNLOAD_BOOT, then (0,1) resets into the ROM bootloader.

The flag lives in the RTC domain and survives software resets. The ROM bootloader does not clear it on entry β€” after a flash the chip would stay stuck in the bootloader forever (until power-on). inv flash therefore clears the flag itself after flashing (esptool write-reg 0x3F408128 0 0x1 through the stub) and then resets via the RTC watchdog, which boots the application.

The same task also drains the log ring from crate::logging over the CDC data endpoint, so inv monitor shows the firmware log output.

ConstantsΒ§

LOG_CHUNK πŸ”’
Log chunk size handed to the CDC endpoint (matches max_packet_size).
POLL_INTERVAL πŸ”’
Line-poll interval of the DTR/RTS state machine.
USB_PID πŸ”’
Espressif generic CDC-ACM product ID.
USB_VID πŸ”’
Espressif USB vendor ID (same as the ROM bootloader).

StaticsΒ§

BOS_DESCRIPTOR πŸ”’
Buffer for the generated USB configuration descriptor.
CDC_STATE πŸ”’
Scratch buffer for USB control transfers.
CONFIG_DESCRIPTOR πŸ”’
Bulk OUT endpoint buffer for the CDC data endpoint.
CONTROL_BUF πŸ”’
Buffer for the generated USB BOS descriptor.
EP_OUT_BUFFER πŸ”’
The S2 peripheral types are invariant in their lifetime, which pins the USB driver to 'static; the embassy-usb buffers therefore must be 'static too. They are initialized exactly once at task start.

FunctionsΒ§

control_and_log πŸ”’
Runs the DTR/RTS state machine and drains the log ring into the host.
enter_bootloader πŸ”’
Resets into the ROM USB-CDC download mode (bootloader).
flush_logs πŸ”’
Copies pending log lines from the ring into the CDC endpoint until the ring is empty or the endpoint errors out. See control_and_log for the cancellation-safety argument.
set_force_download πŸ”’
Set or clear the ROM FORCE_DOWNLOAD_BOOT flag in RTC_CNTL_OPTION1.
task
Runs the USB-CDC-ACM device and the esptool reset protocol.