From 0faef8e4dd0943a2bca42128b1d33ebef7e9ee21 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Fri, 16 Feb 2024 18:57:03 +0100 Subject: [PATCH] Fix PWM channels after restart (#20732) --- .../ESP32-to-ESP8266-compat/src/esp8266toEsp32.cpp | 8 ++++++++ lib/libesp32/ESP32-to-ESP8266-compat/src/esp8266toEsp32.h | 2 ++ tasmota/tasmota_xdrv_driver/xdrv_01_9_webserver.ino | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/lib/libesp32/ESP32-to-ESP8266-compat/src/esp8266toEsp32.cpp b/lib/libesp32/ESP32-to-ESP8266-compat/src/esp8266toEsp32.cpp index dc2908eec..a876133c0 100644 --- a/lib/libesp32/ESP32-to-ESP8266-compat/src/esp8266toEsp32.cpp +++ b/lib/libesp32/ESP32-to-ESP8266-compat/src/esp8266toEsp32.cpp @@ -323,6 +323,14 @@ int32_t analogAttach(uint32_t pin, bool output_invert) { // returns ledc chan return chan; } +void analogDetach(void) { + for (uint32_t pin = 0; pin < SOC_GPIO_PIN_COUNT; pin++) { + if (pin_to_channel[pin] > 0) { + ledcDetachPin(pin); + } + } +} + extern "C" uint32_t ledcReadFreq2(uint8_t chan) { // extern "C" uint32_t __wrap_ledcReadFreq(uint8_t chan) { if (chan > MAX_PWMS) { diff --git a/lib/libesp32/ESP32-to-ESP8266-compat/src/esp8266toEsp32.h b/lib/libesp32/ESP32-to-ESP8266-compat/src/esp8266toEsp32.h index 659e34b55..325654247 100644 --- a/lib/libesp32/ESP32-to-ESP8266-compat/src/esp8266toEsp32.h +++ b/lib/libesp32/ESP32-to-ESP8266-compat/src/esp8266toEsp32.h @@ -55,6 +55,8 @@ uint8_t ledcReadResolution(uint8_t chan); // Returns: hardware channel number, or -1 if it failed int32_t analogAttach(uint32_t pin, bool output_invert = false); // returns the ledc channel, or -1 if failed. This is implicitly called by analogWrite if the channel was not already allocated +void analogDetach(void); + // change both freq and range // `0`: set to global value // `-1`: keep unchanged diff --git a/tasmota/tasmota_xdrv_driver/xdrv_01_9_webserver.ino b/tasmota/tasmota_xdrv_driver/xdrv_01_9_webserver.ino index 1124a60ea..263a42043 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_01_9_webserver.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_01_9_webserver.ino @@ -1816,6 +1816,11 @@ void HandleModuleConfiguration(void) { if (Webserver->hasArg(F("save"))) { ModuleSaveSettings(); + +#ifdef CONFIG_IDF_TARGET_ESP32 + analogDetach(); +#endif + WebRestart(1); return; }