How to reinit ESP32S3 USB Serial/JTAG Controller

thinhchutien
Posts: 1
Joined: Tue Sep 12, 2023 9:22 am

How to reinit ESP32S3 USB Serial/JTAG Controller

Postby thinhchutien » Tue Sep 12, 2023 9:44 am

Hello,
I'm currently writing a program for the ESP32-S3 with esp-idf, in which the ESP32-S3 will switch between different USB modes such as USB CDC and USB HID through a display. After the program completes, the ESP32-S3's USB interface needs to return to its default JTAG/serial mode, just like when it's powered on, so that users can reprogram it as needed. I don't want to press the boot and reset buttons to enter bootloader mode because the ESP32-S3 automatically enters bootloader mode if USB is not initialized in the code.

So, how can I revert the USB interface to the default JTAG/serial mode without having to reset the device? Thank you all very much.

abellofiore
Posts: 10
Joined: Tue May 16, 2023 7:02 pm

Re: How to reinit ESP32S3 USB Serial/JTAG Controller

Postby abellofiore » Mon Sep 18, 2023 7:58 pm

On the S3 the USB PHY can be routed to the USB Serial/JTAG controller (factory default) or the CPU (in which case implement whatever you want using TinyUSB or such).
If you switched away from the built-in CDC/JTAG controller, just hit the HW registers to switch it back (look in the example projects that switch it away to figure this out or look in the datasheet).
BTW, you don't have to use the BOOT/RESET buttons at all when connected to the built-in controller after your first programming; you can use the "--before usb_reset" command to cycle through boot mode for you:

Code: Select all

esptool.py --before usb_reset --chip esp32c3 write_flash 0x0000 bootloader.bin

By the way, these APIs I made are handy for dealing with the CDC/JTAG device. Using them you can tell if you are: plugged into a host PC, you can turn-off the data connection to the PC (PC sees the USB go away), or give it back to the PC again:

Code: Select all

#include "hal/usb_serial_jtag_ll.h"
#include "hal/usb_phy_ll.h"

bool is_host_usb_device_connected(void)
{
	return *((volatile uint32_t *)USB_SERIAL_JTAG_OUT_EP0_ST_REG) != 0;
}

void disconnect_cdc_port(void)
{
	*((volatile uint32_t *)USB_SERIAL_JTAG_CONF0_REG) &= ~(1<<9);
}

void connect_cdc_port(void)
{
	*((volatile uint32_t *)USB_SERIAL_JTAG_CONF0_REG) |= (1<<9);
}

Who is online

Users browsing this forum: Bing [Bot] and 418 guests