[Console] Prevent long delay when USB-HWCDC is plugged but not used

This commit is contained in:
TD-er
2026-07-20 22:55:00 +02:00
parent 413a22ff8e
commit 406a62421f
2 changed files with 45 additions and 28 deletions
@@ -162,7 +162,7 @@ size_t Port_ESPEasySerial_USB_HWCDC_t::read(uint8_t *buffer,
void Port_ESPEasySerial_USB_HWCDC_t::flush(void) void Port_ESPEasySerial_USB_HWCDC_t::flush(void)
{ {
if (isConnected()) { if (isConnected()) {
return _hwcdc_serial->flush(); _hwcdc_serial->flush();
} }
} }
@@ -190,16 +190,12 @@ size_t Port_ESPEasySerial_USB_HWCDC_t::write(const uint8_t *buffer,
Port_ESPEasySerial_USB_HWCDC_t::operator bool() const Port_ESPEasySerial_USB_HWCDC_t::operator bool() const
{ {
if (isConnected()) { return _hwcdc_serial != nullptr && _hwcdc_serial->isPlugged();
// return usbActive;
return true;
}
return false;
} }
void Port_ESPEasySerial_USB_HWCDC_t::setDebugOutput(bool enabled) { void Port_ESPEasySerial_USB_HWCDC_t::setDebugOutput(bool enabled) {
if (_hwcdc_serial != nullptr) { if (_hwcdc_serial != nullptr) {
return _hwcdc_serial->setDebugOutput(enabled); _hwcdc_serial->setDebugOutput(enabled);
} }
} }
+42 -21
View File
@@ -117,25 +117,28 @@ void EspEasy_Console_Port::begin(uint32_t baudrate)
updateActiveTaskUseSerial0(); updateActiveTaskUseSerial0();
if (_serial != nullptr) { if (_serial != nullptr) {
#if FEATURE_IMPROV #if FEATURE_IMPROV
_improv.init(); _improv.init();
#endif #endif
_serial->begin(baudrate); _serial->begin(baudrate);
#if FEATURE_DEFINE_SERIAL_CONSOLE_PORT #if FEATURE_DEFINE_SERIAL_CONSOLE_PORT
_config.baud = baudrate; _config.baud = baudrate;
#endif #endif
_serial->flush();
#ifdef ESP32 #ifdef ESP32
// Allow to flush data from the serial buffers // Allow to flush data from the serial buffers
// When not opening the USB serial port, the ESP may hang at boot. // When not opening the USB serial port, the ESP may hang at boot.
delay(10);
_serial->end();
delay(10);
_serial->begin(baudrate);
_serial->flush();
/*
delay(10);
_serial->end();
delay(10);
_serial->begin(baudrate);
_serial->flush();
*/
# if FEATURE_DEFINE_SERIAL_CONSOLE_PORT # if FEATURE_DEFINE_SERIAL_CONSOLE_PORT
@@ -202,15 +205,15 @@ void EspEasy_Console_Port::readInput()
ESPEasySerialPort EspEasy_Console_Port::getPortType() const ESPEasySerialPort EspEasy_Console_Port::getPortType() const
{ {
# if FEATURE_DEFINE_SERIAL_CONSOLE_PORT # if FEATURE_DEFINE_SERIAL_CONSOLE_PORT
if (_serial == nullptr) { return ESPEasySerialPort::not_set; } if (_serial == nullptr) { return ESPEasySerialPort::not_set; }
return _config.port; return _config.port;
# else // if FEATURE_DEFINE_SERIAL_CONSOLE_PORT # else // if FEATURE_DEFINE_SERIAL_CONSOLE_PORT
// TODO TD-er: Should I also try to see if we're using serial0_swapped? // TODO TD-er: Should I also try to see if we're using serial0_swapped?
return ESPEasySerialPort::serial0; return ESPEasySerialPort::serial0;
# endif // if FEATURE_DEFINE_SERIAL_CONSOLE_PORT # endif // if FEATURE_DEFINE_SERIAL_CONSOLE_PORT
} }
#endif // if FEATURE_DEFINE_SERIAL_CONSOLE_PORT #endif // if FEATURE_DEFINE_SERIAL_CONSOLE_PORT
@@ -218,6 +221,7 @@ ESPEasySerialPort EspEasy_Console_Port::getPortType() const
bool EspEasy_Console_Port::process_serialWriteBuffer() bool EspEasy_Console_Port::process_serialWriteBuffer()
{ {
if (_serialWriteBuffer.getNrMessages() == 0) { return false; } if (_serialWriteBuffer.getNrMessages() == 0) { return false; }
if (_serial == nullptr) { return false; } if (_serial == nullptr) { return false; }
#ifdef ESP32 #ifdef ESP32
@@ -251,8 +255,8 @@ bool EspEasy_Console_Port::process_serialWriteBuffer()
} }
const size_t chunkSize = availableForWrite > CONSOLE_MAX_WRITE_CHUNKSIZE const size_t chunkSize = availableForWrite > CONSOLE_MAX_WRITE_CHUNKSIZE
? CONSOLE_MAX_WRITE_CHUNKSIZE ? CONSOLE_MAX_WRITE_CHUNKSIZE
: availableForWrite; : availableForWrite;
availableForWrite -= chunkSize; availableForWrite -= chunkSize;
@@ -317,13 +321,13 @@ bool EspEasy_Console_Port::process_consoleInput(uint8_t SerialInByte)
String EspEasy_Console_Port::getPortDescription() const String EspEasy_Console_Port::getPortDescription() const
{ {
if (_serial != nullptr) { if (_serial != nullptr) {
#if FEATURE_DEFINE_SERIAL_CONSOLE_PORT #if FEATURE_DEFINE_SERIAL_CONSOLE_PORT
return _serial->getPortDescription(); return _serial->getPortDescription();
#else // if FEATURE_DEFINE_SERIAL_CONSOLE_PORT #else // if FEATURE_DEFINE_SERIAL_CONSOLE_PORT
String res = F("HW Serial0 @ "); String res = F("HW Serial0 @ ");
res += _serial->baudRate(); res += _serial->baudRate();
return res; return res;
#endif // if FEATURE_DEFINE_SERIAL_CONSOLE_PORT #endif // if FEATURE_DEFINE_SERIAL_CONSOLE_PORT
} }
return F("-"); return F("-");
@@ -364,15 +368,32 @@ bool EspEasy_Console_Port::updateSerialPort(
} }
if ((_serial == nullptr) && mustHaveSerial) { if ((_serial == nullptr) && mustHaveSerial) {
# ifdef USE_SECOND_HEAP
HeapSelectDram ephemeral;
# endif // ifdef USE_SECOND_HEAP
{ {
# ifdef USE_SECOND_HEAP # ifdef USE_SECOND_HEAP
HeapSelectDram ephemeral; HeapSelectDram ephemeral;
# endif // ifdef USE_SECOND_HEAP # endif // ifdef USE_SECOND_HEAP
_serial = new (std::nothrow) ESPeasySerial(_config); _serial = new (std::nothrow) ESPeasySerial(_config);
# if USES_HWCDC
// Check to see if the port is plugged.
// If not, we can remove it again and save resources.
if ((_config.port == ESPEasySerialPort::usb_hw_cdc) && (_serial != nullptr)) {
bool isPlugged{};
for (uint32_t i = 0; !isPlugged && i < 5; i++) {
isPlugged = _serial->operator bool();
delay(50);
}
if (!isPlugged) {
delete _serial;
_serial = nullptr;
}
}
# endif // if USES_HWCDC
} }
somethingChanged = true; somethingChanged = true;