ESP32-C3 - Access Built in USB CDC / Serial Port?

BrianP
Posts: 53
Joined: Mon Apr 13, 2020 1:48 pm

ESP32-C3 - Access Built in USB CDC / Serial Port?

Postby BrianP » Tue Aug 03, 2021 2:48 am

I know I can configure the ESP32-C3 to use the built-in serial port as console IO. This allows me to use things like printf and getchar. I have looked through the documentation and I can't find the API for the USB CDC serial port. I find this odd since the USB is a peripheral like a UART or I2C port but there doesn't seem to be an API or anything.

I started looking for this because it seems that if I do a printf and the terminal emulator is closed the Bluetooth connection is lost. I assume this is because of a block or perhaps a time out resulting in reset.

If nothing else it would be nice to know how to prevent this or even to determine whether a USB serial connection is active.

Also, minor point. I can't figure out a reliable way to only search for ESP32-C3. This returns hits for ESP32, which is pretty much all posts. Advanced search treats "C3" as "too short a word" for search and ignore it.

Thanks!

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32-C3 - Access Built in USB CDC / Serial Port?

Postby ESP_Sprite » Tue Aug 03, 2021 8:16 am

It doesn't have a low-level API as such, but it does implement a VFS node; if you [f]open() /dev/usbserjtag , you can read/write from that.

The bluetooth connection loss sounds odd... there is an existing issue in esp-idf where if the serial port is not opened or the usb/jtag thing is not plugged in on bootup of the C3, the bootup process hangs until it is... perhaps you're running into that?

BrianP
Posts: 53
Joined: Mon Apr 13, 2020 1:48 pm

Re: ESP32-C3 - Access Built in USB CDC / Serial Port?

Postby BrianP » Tue Aug 03, 2021 2:05 pm

If I select the config option the default for printf, getchar, etc., is the USB so I don't need the fopen but that is good to know.

I thought loss of Bluetooth was connected to printf but just the act of connecting/disconnecting via USB seems to kill the connection.

I connect to the ESP via an Android app called Serial Bluetooth Terminal (I haven't figure out how to connect via Windows - that is next). If I don't open a serial USB connection with Windows it works fine. If I do open the serial USB connection it works fine. If I then close the serial USB connection Bluetooth disconnects after a few seconds. It doesn't seem to matter whether I use Putty or something else a few seconds after I close the terminal emulator I lose the Bluetooth.

This is what the serial terminal sends before I shut it down

ESP-ROM:esp32c3-api1-20210207
E (15273) GATTS_SPP_DEMO: GAP_EVT, event 20 //On selecting connect in the bluetooth app

E (15563) GATTS_SPP_DEMO: GAP_EVT, event 20

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32-C3 - Access Built in USB CDC / Serial Port?

Postby ESP_Sprite » Wed Aug 04, 2021 1:38 am

Any chance you can see what happens on the UART0 Tx pin at that time? The USB-serial-JTAG thing has an auto-reset functionality using DTR/RTS; I'm wondering if closing Putty happens to trigger it, after which you would get stuck in the bug I mentioned earlier.

BrianP
Posts: 53
Joined: Mon Apr 13, 2020 1:48 pm

Re: ESP32-C3 - Access Built in USB CDC / Serial Port?

Postby BrianP » Wed Aug 04, 2021 2:12 am

I can try but I put a line into the code to add a "Reset" string to be sent via Bluetooth
(i.e.
Long delay to let me re-pair the link
Send out via Bluetooth "Restarted"
Loop
Send String "Hello Brian"
Long Delay
End Loop

The "Restarted" string is only sent once but I can see if I can verify if there is other traffic.

I am pretty sure that the ESP32-C3 mini devkit boards I have do not have the USB + and USB - lines connected to the CP2102 (i.e. R1 and R4 are missing, R2 and R3 are present) so it is hard to see how DTR/RTS could be triggered.

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32-C3 - Access Built in USB CDC / Serial Port?

Postby ESP_Sprite » Wed Aug 04, 2021 6:44 am

I don't mean the physical DTR/RTS lines; the CDC/ACM on the builtin USB port also has a 'virtual' RTS/DTR implementation; the chip will get reset if these are twiddled in a certain order.

BrianP
Posts: 53
Joined: Mon Apr 13, 2020 1:48 pm

Re: ESP32-C3 - Access Built in USB CDC / Serial Port?

Postby BrianP » Wed Aug 04, 2021 2:02 pm

Ah. I get it. In the interim I added

int count = 0;
char outbuf[100];

while( true ) {
DelaySeconds( 5 );
snprintf( outbuf, sizeof(outbuf), "\nHello %d", count++);
PrintBlueTooth(outbuf );
}

to app_main and count does indeed get reset to 0 when I close the serial terminal so that is the problem right there.

Btje1977
Posts: 1
Joined: Wed Nov 24, 2021 6:15 pm

Re: ESP32-C3 - Access Built in USB CDC / Serial Port?

Postby Btje1977 » Wed Nov 24, 2021 6:39 pm

Would it be possible to connect this interface to an android USB-OTG I would like to send settings from a phone to the ESP.

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32-C3 - Access Built in USB CDC / Serial Port?

Postby ESP_Sprite » Thu Nov 25, 2021 2:47 am

Possibly. I'm not too knowledgable on the android side of things, but if 1. the Android device can work as an USB host, 2. the kernel has the drivers for the standard USB TTY-ACM class built in, and 3. Android allows communicating with serial ports, you should be good to go. Given that Android is built on Linux and the C3 CDC-ACM is certainly compatible with that, I'd think you have a fair chance of it working.

wangshuoran
Posts: 37
Joined: Fri Dec 24, 2021 12:40 pm

Re: ESP32-C3 - Access Built in USB CDC / Serial Port?

Postby wangshuoran » Sun Feb 13, 2022 2:55 am

ESP_Sprite wrote:
Tue Aug 03, 2021 8:16 am
It doesn't have a low-level API as such, but it does implement a VFS node; if you [f]open() /dev/usbserjtag , you can read/write from that.

The bluetooth connection loss sounds odd... there is an existing issue in esp-idf where if the serial port is not opened or the usb/jtag thing is not plugged in on bootup of the C3, the bootup process hangs until it is... perhaps you're running into that?
Where is the /dev/usbserjtag? You mean upper computer behavior?
If I want to use esp32-c3 to monitor upper computer and I don't want to use scanf, gets, etc. How can I realization this behavior pass through VFS?

Who is online

Users browsing this forum: No registered users and 122 guests