Skip to content

Arduino IDE ESP32-C3 USB CDC problems #6089

Closed
@specternecter

Description

@specternecter

Board

ESP32-C3

Device Description

ESP32-C3-DevKitC-02

Hardware Configuration

D- attached to GPIO18, D+ attached to GPIO19

Version

latest master

IDE Name

Arduino IDE

Operating System

Windows 10

Flash frequency

80 MHz

PSRAM enabled

yes

Upload speed

921600

Description

The first issue is that the USBSerial example for ESP32C3 in the Arduino IDE absolutely does not work for the C3. It works for the S2, but the C3 is different. I managed to get it working on the C3, but it took a few days and it still has issues I can't solve.

With the sketch I included, one of the issues is that after a reset, the USB doesn't work on the first opening of the port. This can be visualized with the led on GPIO9. On reset, the led is on, but it should be off. When the COM port is opened, the led stays lit. When you then close the COM port you can see the led flash and go off. When the COM port is opened afterward, it works properly. COM port opened, led on; COM port closed, led off.

The major issue is that every time you first write to the USB port, it's somehow taking your written data and joining it after "ESP-ROM:esp32c3-api1-20210207\r\n", so you end up with "ESP-ROM:esp32c3-api1-20210207\r\n[YOUR DESIRED DATA]". This only happens on the first write after opening the USB port. This issue renders the ESP32C3 useless for my application, because it's main purpose is as a USB-UART bridge and that data I didn't ask for gets sent to the PC software, and the software then can't do it's job. There's no workaround for the software it's talking to because I don't control it. It needs to only be sending the data I tell it to. I would imagine anybody trying to use the ESP32C3 as a USB-UART bridge is also going to also see this as a problem.

Sketch

#include "USB.h"

uint8_t led = 9;
bool uart_enabled = false;

static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data){
  if(event_base == ARDUINO_HW_CDC_EVENTS){
    arduino_hw_cdc_event_data_t * data = (arduino_hw_cdc_event_data_t*)event_data;
    switch (event_id){
      case ARDUINO_HW_CDC_CONNECTED_EVENT:
        usb_connected();
        break;
      /*case ARDUINO_HW_CDC_BUS_RESET_EVENT:
        // might be useful later
        break;*/
      case ARDUINO_HW_CDC_RX_EVENT:
        {
            uint8_t buf[data->rx.len];
            size_t len = Serial.read(buf, data->rx.len);
            Serial0.write(buf, len);
        }
        break;
       /*case ARDUINO_HW_CDC_TX_EVENT:
         // might be useful later
        break;*/
      
      default:
        break;
    }
  }
}

void setup() {
  //Serial0.setDebugOutput(true);
  //Serial.setDebugOutput(true);
  Serial.begin();
  Serial.onEvent(usbEventCallback);
  pinMode(led, OUTPUT);
  Serial0.begin(230400);
}

void loop() {
  while(Serial0.available()){
    size_t l = Serial0.available();
    uint8_t b[l];
    l = Serial0.read(b, l);
    Serial.write(b, l);
  }
}

void usb_connected(){
  uart_enabled = true;
  digitalWrite(led, HIGH);
}

Debug Message

Enabling or disabling debug messages makes no difference

Other Steps to Reproduce

No response

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

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions