From 2865630309c1cfe979e55ceacec355f862010b8f Mon Sep 17 00:00:00 2001 From: s-hadinger <49731213+s-hadinger@users.noreply.github.com> Date: Sun, 12 May 2024 21:12:08 +0200 Subject: [PATCH] Prepare for ESP32 WDT (#21383) * Prepare for ESP32 WDT * make `sleep()` reset WDT --- platformio_tasmota32.ini | 1 + tasmota/my_user_config.h | 2 ++ tasmota/tasmota.ino | 4 ++++ tasmota/tasmota_support/support.ino | 24 +++++++++++++++++++ .../xdrv_52_1_berry_native.ino | 1 + .../xdrv_52_3_berry_tasmota.ino | 3 ++- 6 files changed, 34 insertions(+), 1 deletion(-) diff --git a/platformio_tasmota32.ini b/platformio_tasmota32.ini index de7725220..4f1aab3a1 100644 --- a/platformio_tasmota32.ini +++ b/platformio_tasmota32.ini @@ -36,6 +36,7 @@ build_flags = ${esp_defaults.build_flags} -Wl,--wrap=panicHandler -Wl,--wrap=xt_unhandled_exception -Wl,--wrap=_Z11analogWritehi ; `analogWrite(unsigned char, int)` use the Tasmota version of analogWrite for deeper integration and phase control -Wl,--wrap=ledcReadFreq ; `uint32_t ledcReadFreq(uint8_t chan)` + -Wl,--wrap=delay ; void delay(uint32_t ms) lib_ignore = HTTPUpdateServer USB diff --git a/tasmota/my_user_config.h b/tasmota/my_user_config.h index 532682faf..faf3791c8 100644 --- a/tasmota/my_user_config.h +++ b/tasmota/my_user_config.h @@ -1122,6 +1122,8 @@ https://rya.nc/tasmota-fingerprint.html" #ifdef ESP32 +// #define USE_ESP32_WDT // Enable Watchdog for ESP32, trigger a restart if loop has not responded for 5s, and if `yield();` was not called + #define SET_ESP32_STACK_SIZE (8 * 1024) // Set the stack size for Tasmota. The default value is 8192 for Arduino, some builds might need to increase it #ifdef SOC_TOUCH_VERSION_1 // ESP32 diff --git a/tasmota/tasmota.ino b/tasmota/tasmota.ino index ced7f4fd3..122ae3eef 100644 --- a/tasmota/tasmota.ino +++ b/tasmota/tasmota.ino @@ -411,6 +411,10 @@ void setup(void) { #endif // CONFIG_IDF_TARGET_ESP32 #endif // ESP32 +#ifdef USE_ESP32_WDT + enableLoopWDT(); // enabled WDT Watchdog on Arduino `loop()` - must return before 5s or called `feedLoopWDT();` - included in `yield()` +#endif // USE_ESP32_WDT + RtcPreInit(); SettingsInit(); diff --git a/tasmota/tasmota_support/support.ino b/tasmota/tasmota_support/support.ino index 0064e70fa..f1c979a38 100755 --- a/tasmota/tasmota_support/support.ino +++ b/tasmota/tasmota_support/support.ino @@ -21,6 +21,30 @@ extern "C" { extern struct rst_info resetInfo; } +/*********************************************************************************************\ + * ESP32 Watchdog +\*********************************************************************************************/ +#ifdef ESP32 +// Watchdog - yield() resets the watchdog +#ifdef USE_ESP32_WDT + +extern "C" +void yield(void) { + vPortYield(); // was originally in `__yield` + feedLoopWDT(); +} + +// patching delay(uint32_t ms) +extern "C" void __real_delay(uint32_t ms); + +extern "C" void __wrap_delay(uint32_t ms) { + __real_delay(ms); + feedLoopWDT(); +} + +#endif // USE_ESP32_WDT +#endif // ESP32 + /*********************************************************************************************\ * Watchdog extension (https://github.com/esp8266/Arduino/issues/1532) \*********************************************************************************************/ diff --git a/tasmota/tasmota_xdrv_driver/xdrv_52_1_berry_native.ino b/tasmota/tasmota_xdrv_driver/xdrv_52_1_berry_native.ino index b0809c8d3..db11ac3e4 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_52_1_berry_native.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_52_1_berry_native.ino @@ -110,6 +110,7 @@ void BrTimeoutStart(void) { if (0 == berry.timeout) { berry.timeout = 1; // rare case when value accidentally computes to zero } + yield(); } void BrTimeoutYield(void) { diff --git a/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_tasmota.ino b/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_tasmota.ino index e23511bb2..eb87fede7 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_tasmota.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_tasmota.ino @@ -404,7 +404,8 @@ extern "C" { // ESP object int32_t l_yield(bvm *vm); int32_t l_yield(bvm *vm) { - return be_call_c_func(vm, (void*) &BrTimeoutYield, NULL, "-"); + BrTimeoutYield(); + be_return_nil(vm); } // Berry: tasmota.scale_uint(int * 5) -> int