Skip to content

ESP32-C3 in USB CDC mode can hang if Serial.flush() called #9172

Closed
@6v6gt-duino

Description

@6v6gt-duino

Board

ESP32-C3 Supermini (no USB/UART adaptor)

Device Description

ESP32-C3 Supermini is basically an ESP32-C3 chip with a USB-C socket and little else.
https://www.tindie.com/products/adz1122/esp32-c3-development-board-esp32-supermini/

Hardware Configuration

USB-C cable to either a PC (windows 10) or a simple phone charger

Version

v2.0.14

IDE Name

Arduino IDE 1.8.19

Operating System

Windows 10

Flash frequency

80MHz

PSRAM enabled

no

Upload speed

921600

Description

Using "USB CDC on boot : enabled":
If the supermini is powered via a phone charger with no PC connection, Serial.flush() appears to cause a hang.

Sketch

/*
    esp32-c3 supermini test
    similar to: https://www.tindie.com/products/adz1122/esp32-c3-development-board-esp32-supermini/

    set up IDE with
    (a) Board ESP32-C3 dev module
    (b) "USB CDC on boot : enabled"
    (c) JTAG adapter disabled

    Test generates lots of serial output and then forces a Serial.flush

    This test appears to function correctly whether or not Serial.setTxTimeoutMs(0) is set (but see comment below for case(c)) :
    (a) if the supermini is connected via USB to a PC and the serial monitor is active. 
    (c) if the supermini is powered via a phone charger with no PC connection.

    This test appear to work only if Serial.setTxTimeoutMs(0) is set :
    (b) if the supermini is connected via USB to a PC and the serial monitor is NOT active. 
    
    Serial.flush() appears to cause a hang in case (c)

*/


const uint8_t led = 8 ;  // led wired HIGH side





void setup() {

  pinMode( led, OUTPUT ) ;
  digitalWrite( led, HIGH ) ;

  Serial.begin( 115200 ) ;

  // only defined if option "USB CDC on boot" selected in IDE
  // Serial.setTxTimeoutMs(0) ; // https://github.com/espressif/arduino-esp32/pull/7583

  while ( !Serial && millis() < 10000 ) {
    delay(500) ;
  }

  // pulse led for 4 seconds if serial connection found.
  if ( millis() < 10000 ) {
    Serial.println( "found Serial") ;
    digitalWrite( led, LOW ) ;
    delay( 4000 ) ;
    digitalWrite( led, HIGH ) ;
    delay( 1000 ) ;
  }
  else {
    // no Serial found
  }

}



void loop() {

  for ( int j = 0; j < 10 ; j++ ) {
    for ( int i = 0; i < 500 ; i++ ) {
      // Serial.print( i ) ;
      // Serial.print( '\t' ) ;
      // Serial.println(  "the quick brown fox jumps over the lazy dog" ) ;
      Serial.printf( "%4d \t %s \r\n",  i, "the quick brown fox jumps over the lazy dog" ) ;
      delay(1) ;
    }
    digitalWrite( led, LOW ) ;
    delay( 1000 ) ;
    digitalWrite( led, HIGH ) ;
    delay( 1000 ) ;
    if ( j == 9 ) { 
     // this kills it if there is no serial monitor connected 
     Serial.flush() ;
    }
  }
}

Debug Message

No debug messages with this use case (no serial monitor)

Other Steps to Reproduce

image

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

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions