Skip to content

Commit a502e39

Browse files
committed
robustness improvements for serial on esp32
* fix for serial port on esp32 V4 builds (see upstream wled#5501) * check that SERIAL_MAXTIME_MILLIS is below the task watchdog trigger time * pet esp32 watchdog once before massive reading of serial input (AdaLight/TPM2)
1 parent 3a01c00 commit a502e39

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

wled00/wled.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,12 @@ void WLED::setup()
482482
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detection
483483
#endif
484484

485-
#ifdef ARDUINO_ARCH_ESP32
486-
pinMode(hardwareRX, INPUT_PULLDOWN); delay(1); // suppress noise in case RX pin is floating (at low noise energy) - see issue #3128
485+
#if defined(ARDUINO_ARCH_ESP32) && !defined(ARDUINO_USB_CDC_ON_BOOT)
486+
#if ESP_IDF_VERSION_MAJOR > 3
487+
gpio_pulldown_en((gpio_num_t)hardwareRX); // pinMode() routes GPIO through the GPIO matrix and detaches UART0 RX - use gpio_pulldown_en() instead. See upstream issue #5501
488+
#else
489+
pinMode(hardwareRX, INPUT_PULLDOWN); delay(1); // suppress noise in case RX pin is floating (at low noise energy) - see issue #3128
490+
#endif
487491
#endif
488492
#ifdef WLED_BOOTUPDELAY
489493
delay(WLED_BOOTUPDELAY); // delay to let voltage stabilize, helps with boot issues on some setups

wled00/wled_serial.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#ifdef ARDUINO_ARCH_ESP32
33
#include "esp_ota_ops.h"
44
#endif
5+
#include "sdkconfig.h"
56

67
/*
78
* Adalight and TPM2 handler
@@ -10,6 +11,17 @@
1011
#define SERIAL_MAXTIME_MILLIS 100 // to avoid blocking other activities, do not spend more than 100ms with continuous reading
1112
// at 115200 baud, 100ms is enough to send/receive 1280 chars
1213

14+
#ifdef ARDUINO_ARCH_ESP32
15+
// CONFIG_INT_WDT_TIMEOUT_MS = 300 = interrupt watchdog timeout in milliseconds
16+
// CONFIG_TASK_WDT_TIMEOUT_S = 5 = idle task watchdog timeout in seconds
17+
// CONFIG_ESP_TASK_WDT_TIMEOUT_S (new name)
18+
#ifdef CONFIG_ESP_TASK_WDT_TIMEOUT_S
19+
static_assert(SERIAL_MAXTIME_MILLIS < (CONFIG_ESP_TASK_WDT_TIMEOUT_S * 1000 - 2 * portTICK_PERIOD_MS), "SERIAL_MAXTIME_MILLIS must be shorter than the IDLE watchdog timeout.");
20+
#else // arduino-esp32 1.0.x uses CONFIG_TASK_WDT_TIMEOUT_S
21+
static_assert(SERIAL_MAXTIME_MILLIS < (CONFIG_TASK_WDT_TIMEOUT_S * 1000 - 2 * portTICK_PERIOD_MS), "SERIAL_MAXTIME_MILLIS must be shorter than the IDLE watchdog timeout.");
22+
#endif
23+
#endif
24+
1325
enum class AdaState {
1426
Header_A,
1527
Header_d,
@@ -108,10 +120,12 @@ void handleSerial()
108120
static byte red = 0x00;
109121
static byte green = 0x00;
110122

123+
if (Serial.available() > 0) delay(1); // pet the watchdog
124+
111125
unsigned long startTime = millis();
112126
while ((Serial.available() > 0) && (millis() - startTime < SERIAL_MAXTIME_MILLIS))
113127
{
114-
yield();
128+
yield(); // useless on esp32
115129
byte next = Serial.peek();
116130
switch (state) {
117131
case AdaState::Header_A:

0 commit comments

Comments
 (0)