From d9b5b9d4d6b257c4f4e82d880bef4f251603c07f Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Thu, 16 Mar 2023 11:30:12 +0100 Subject: [PATCH] handleSerial(): 100ms RX timeout (experimental) experimental - this should avoid lockups when ESP32 is constantly receiving data (noise, junk, excessive communication) --- wled00/wled.h | 2 +- wled00/wled_serial.cpp | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/wled00/wled.h b/wled00/wled.h index 1e8d81e9..fa4efe36 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -8,7 +8,7 @@ */ // version code in format yymmddb (b = daily build) -#define VERSION 2303141 +#define VERSION 2303160 //uncomment this if you have a "my_config.h" file you'd like to use //#define WLED_USE_MY_CONFIG diff --git a/wled00/wled_serial.cpp b/wled00/wled_serial.cpp index b6ae511b..35fce884 100644 --- a/wled00/wled_serial.cpp +++ b/wled00/wled_serial.cpp @@ -4,6 +4,9 @@ * Adalight and TPM2 handler */ +#define SERIAL_MAXTIME_MILLIS 100 // to avoid blocking other activities, do not spend more than 100ms with continouus reading +// at 115200 baud, 100ms is enough to send/receive 1280 chars + enum class AdaState { Header_A, Header_d, @@ -87,6 +90,7 @@ void handleSerial() { if (pinManager.isPinAllocated(hardwareRX)) return; if (!Serial) return; // WLEDMM - serial not connected (USB CDC) + if (((pinManager.isPinAllocated(hardwareTX)) && (pinManager.getPinOwner(hardwareTX) != PinOwner::DebugOut))) return; // WLEDMM serial TX is necessary for adalight / TPM2 #ifdef WLED_ENABLE_ADALIGHT static auto state = AdaState::Header_A; @@ -96,7 +100,8 @@ void handleSerial() static byte red = 0x00; static byte green = 0x00; - while (Serial.available() > 0) + unsigned long startTime = millis(); + while ((Serial.available() > 0) && (millis() - startTime < SERIAL_MAXTIME_MILLIS)) { yield(); byte next = Serial.peek(); @@ -217,6 +222,9 @@ void handleSerial() Serial.read(); //discard the byte } #endif + //#ifdef WLED_DEBUG + if ((millis() - startTime) > SERIAL_MAXTIME_MILLIS) { USER_PRINTLN(F("handleSerial(): need a break after >100ms of activity.")); } + //#endif // If Continuous Serial Streaming is enabled, send new LED data as bytes if (continuousSendLED && (lastUpdate != strip.getLastShow())){