handling of Serial on CDC USB board

... like the typical -C3
* Replaced a few direct Serial.printf with macros
* Always check if Serial is connected before printing (CDC sometimes hangs  when trying to send/receive without connection)
This commit is contained in:
Frank
2023-04-13 22:59:09 +02:00
parent 0081122f13
commit 94a7f5623d
6 changed files with 27 additions and 25 deletions

View File

@@ -320,9 +320,9 @@ bool deserializeConfig(JsonObject doc, bool fromFS) {
Wire.setPins(i2c_sda, i2c_scl); // this will fail if Wire is initilised (Wire.begin() called prior)
#endif
// Wire.begin(); // WLEDMM moved into pinManager
Serial.printf("pinmgr success for global i2c %d %d\n", i2c_sda, i2c_scl);
DEBUG_PRINTF("pinmgr success for global i2c %d %d\n", i2c_sda, i2c_scl);
} else {
Serial.printf("pinmgr not success for global i2c %d %d\n", i2c_sda, i2c_scl);
DEBUG_PRINTF("pinmgr not success for global i2c %d %d\n", i2c_sda, i2c_scl);
}
JsonArray hw_if_spi = hw[F("if")][F("spi-pin")];
CJSON(spi_mosi, hw_if_spi[0]);
@@ -335,9 +335,9 @@ bool deserializeConfig(JsonObject doc, bool fromFS) {
#else
SPI.begin();
#endif
Serial.printf("pinmgr success for global spi %d %d %d\n", spi_mosi, spi_miso, spi_sclk);
DEBUG_PRINTF("pinmgr success for global spi %d %d %d\n", spi_mosi, spi_miso, spi_sclk);
} else {
Serial.printf("pinmgr not success for global spi %d %d %d\n", spi_mosi, spi_miso, spi_sclk);
DEBUG_PRINTF("pinmgr not success for global spi %d %d %d\n", spi_mosi, spi_miso, spi_sclk);
}
//int hw_status_pin = hw[F("status")]["pin"]; // -1

View File

@@ -767,7 +767,7 @@ void handleIR()
if (results.value != 0) // only print results if anything is received ( != 0 )
{
if (!pinManager.isPinAllocated(hardwareTX) || pinManager.getPinOwner(hardwareTX) == PinOwner::DebugOut) // Serial TX pin (GPIO 1 on ESP32 and ESP8266)
Serial.printf_P(PSTR("IR recv: 0x%lX\n"), (unsigned long)results.value);
if (Serial) Serial.printf_P(PSTR("IR recv: 0x%lX\n"), (unsigned long)results.value);
}
decodeIR(results.value);
irrecv->resume();

View File

@@ -579,7 +579,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
DEBUG_PRINTLN(F("handleSettingsSet(): Could not allocate I2C pins - deallocating."));
uint8_t i2c[2] = { static_cast<uint8_t>(i2c_scl), static_cast<uint8_t>(i2c_sda) };
pinManager.deallocateMultiplePins(i2c, 2, PinOwner::HW_I2C); // just in case deallocation of old pins
Serial.printf("pinmgr not success for global i2c %d %d\n", i2c_sda, i2c_scl);
DEBUG_PRINTF("pinmgr not success for global i2c %d %d\n", i2c_sda, i2c_scl);
}
#ifdef ESP8266
@@ -610,7 +610,7 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
DEBUG_PRINTLN(F("Could not allocate SPI pins."));
uint8_t spi[3] = { static_cast<uint8_t>(spi_mosi), static_cast<uint8_t>(spi_miso), static_cast<uint8_t>(spi_sclk) };
pinManager.deallocateMultiplePins(spi, 3, PinOwner::HW_SPI); // just in case deallocation of old pins
Serial.printf("pinmgr not success for global spi %d %d %d\n", spi_mosi, spi_miso, spi_sclk);
DEBUG_PRINTF("pinmgr not success for global spi %d %d %d\n", spi_mosi, spi_miso, spi_sclk);
}
JsonObject um = doc.createNestedObject("um");

View File

@@ -156,6 +156,6 @@ class Toki {
}
void printTime(const Time& t) {
Serial.printf_P(PSTR("%u,%03u\n"),t.sec,t.ms);
if (Serial) Serial.printf_P(PSTR("%u,%03u\n"),t.sec,t.ms);
}
};

View File

@@ -343,27 +343,29 @@ void WLED::setup()
Serial.begin(115200);
if (!Serial) delay(1000); // WLEDMM make sure that Serial has initalized
#if !ARDUINO_USB_CDC_ON_BOOT
Serial.setTimeout(50); // this causes troubles on new MCUs that have a "virtual" USB Serial (HWCDC)
#else
#endif
#if defined(WLED_DEBUG) && defined(ARDUINO_ARCH_ESP32) && (defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32C3) || ARDUINO_USB_CDC_ON_BOOT)
#ifdef ARDUINO_ARCH_ESP32
#if defined(WLED_DEBUG) && (defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32C3) || ARDUINO_USB_CDC_ON_BOOT)
if (!Serial) delay(2500); // WLEDMM allow CDC USB serial to initialise
#endif
#if ARDUINO_USB_CDC_ON_BOOT
#if ARDUINO_USB_CDC_ON_BOOT || ARDUINO_USB_MODE
if (!Serial) delay(2500); // WLEDMM: always allow CDC USB serial to initialise
Serial.println("wait 1"); // waiting a bit longer ensures that a debug messages are shown in serial monitor
if (Serial) Serial.println("wait 1"); // waiting a bit longer ensures that a debug messages are shown in serial monitor
if (!Serial) delay(2500);
Serial.println("wait 2");
if (Serial) Serial.println("wait 2");
if (!Serial) delay(2500);
if (Serial) Serial.flush(); // WLEDMM
Serial.setTimeout(350); // WLEDMM: don't change timeout, as it causes crashes later
//Serial.setTimeout(350); // WLEDMM: don't change timeout, as it causes crashes later
// WLEDMM: redirect debug output to HWCDC
#if defined(WLED_DEBUG) || defined (SR_DEBUG)
Serial0.setDebugOutput(false);
Serial.setDebugOutput(true);
#else
#endif
// WLEDMM don't touch serial timeout when we use CDC USB or tinyUSB
#else // "standard" serial-to-USB chip
if (Serial) Serial.setTimeout(50); // WLEDMM - only when serial is initialized
#endif
#else // 8266
if (Serial) Serial.setTimeout(50); // WLEDMM - only when serial is initialized
#endif
@@ -587,7 +589,7 @@ void WLED::setup()
//Serial RX (Adalight, Improv, Serial JSON) only possible if GPIO3 unused
//Serial TX (Debug, Improv, Serial JSON) only possible if GPIO1 unused
if (!pinManager.isPinAllocated(hardwareRX) && !pinManager.isPinAllocated(hardwareTX)) {
Serial.println(F("Ada"));
if (Serial) Serial.println(F("Ada"));
}
#endif
@@ -599,7 +601,7 @@ void WLED::setup()
#endif
#ifdef WLED_ENABLE_ADALIGHT
if (Serial.available() > 0 && Serial.peek() == 'I') handleImprovPacket();
if (Serial && (Serial.available() > 0) && (Serial.peek() == 'I')) handleImprovPacket();
#endif
strip.service(); // why?
@@ -626,7 +628,7 @@ void WLED::setup()
#endif
#ifdef WLED_ENABLE_ADALIGHT
if (Serial.available() > 0 && Serial.peek() == 'I') handleImprovPacket();
if (Serial && (Serial.available() > 0) && (Serial.peek() == 'I')) handleImprovPacket();
#endif
// HTTP server page init
@@ -704,7 +706,7 @@ void WLED::setup()
// repeat Ada prompt
#ifdef WLED_ENABLE_ADALIGHT
if (!pinManager.isPinAllocated(hardwareRX) && !pinManager.isPinAllocated(hardwareTX)) {
Serial.println(F("Ada"));
if (Serial) Serial.println(F("Ada"));
}
#endif

View File

@@ -32,10 +32,10 @@ void updateBaudRate(uint32_t rate){
currentBaud = rate100;
if (!pinManager.isPinAllocated(hardwareTX) || pinManager.getPinOwner(hardwareTX) == PinOwner::DebugOut){
Serial.print(F("Baud is now ")); Serial.println(rate);
if (Serial) { Serial.print(F("Baud is now ")); Serial.println(rate); }
}
Serial.flush();
if (Serial) Serial.flush();
Serial.begin(rate);
}