From 8943656729c2bb27a78b4850c046cf1d5a42633b Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Wed, 22 Dec 2021 16:50:29 +0100 Subject: [PATCH] Fixed ESP32 LedPwmMode exception Fixed ESP32 LedPwmMode exception (#14073) --- CHANGELOG.md | 2 ++ RELEASENOTES.md | 1 + .../src/esp8266toEsp32.h | 25 +++++++++++++++++++ tasmota/support_tasmota.ino | 7 ++++-- tasmota/xdrv_04_light.ino | 2 +- tasmota/xdrv_24_buzzer.ino | 8 +++--- tasmota/xlgt_03_sm16716.ino | 15 ----------- 7 files changed, 38 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ca69c83c..969bea353 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file. ## [Unreleased] - Development ## [10.1.0.1] +### Fixed +- ESP32 LedPwmMode exception (#14073) ## [Released] diff --git a/RELEASENOTES.md b/RELEASENOTES.md index e7b2dcdb3..eb9ca66ad 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -108,5 +108,6 @@ The latter links can be used for OTA upgrades too like ``OtaUrl http://ota.tasmo ### Changed ### Fixed +- ESP32 LedPwmMode exception [#14073](https://github.com/arendst/Tasmota/issues/14073) ### Removed diff --git a/lib/libesp32/ESP32-to-ESP8266-compat/src/esp8266toEsp32.h b/lib/libesp32/ESP32-to-ESP8266-compat/src/esp8266toEsp32.h index 40abad981..d5c747485 100644 --- a/lib/libesp32/ESP32-to-ESP8266-compat/src/esp8266toEsp32.h +++ b/lib/libesp32/ESP32-to-ESP8266-compat/src/esp8266toEsp32.h @@ -88,12 +88,37 @@ inline void analogWriteFreq(uint32_t freq) { _analogWriteFreqRange(); } +/* inline void analogAttach(uint32_t pin, uint32_t channel) { _pwm_channel[channel &7] = pin; ledcAttachPin(pin, channel + PWM_CHANNEL_OFFSET); ledcSetup(channel + PWM_CHANNEL_OFFSET, _pwm_frequency, _pwm_bit_num); // Serial.printf("attach %d - %d\n", channel, pin); } +*/ +inline bool analogAttach(uint32_t pin) { + // Find if pin is already attached + uint32_t channel; + for (channel = 0; channel < PWM_SUPPORTED_CHANNELS; channel++) { + if (_pwm_channel[channel] == pin) { + // Already attached +// Serial.printf("PWM: Already attached pin %d to channel %d\n", pin, channel); + return true; + } + } + // Find an empty channel + for (channel = 0; channel < PWM_SUPPORTED_CHANNELS; channel++) { + if (99 == _pwm_channel[channel]) { + _pwm_channel[channel] = pin; + ledcAttachPin(pin, channel + PWM_CHANNEL_OFFSET); + ledcSetup(channel + PWM_CHANNEL_OFFSET, _pwm_frequency, _pwm_bit_num); +// Serial.printf("PWM: New attach pin %d to channel %d\n", pin, channel); + return true; + } + } + // No more channels available + return false; +} inline void analogWrite(uint8_t pin, int val) { diff --git a/tasmota/support_tasmota.ino b/tasmota/support_tasmota.ino index 72a1b7ced..9bd9169a7 100644 --- a/tasmota/support_tasmota.ino +++ b/tasmota/support_tasmota.ino @@ -439,7 +439,10 @@ void SetLedPowerIdx(uint32_t led, uint32_t state) #else //USE_LIGHT pwm = changeUIntScale((uint16_t)(state ? Settings->ledpwm_on : Settings->ledpwm_off), 0, 255, 0, Settings->pwm_range); // linear #endif //USE_LIGHT - analogWrite(Pin(GPIO_LED1, led), bitRead(TasmotaGlobal.led_inverted, led) ? Settings->pwm_range - pwm : pwm); +#ifdef ESP32 + if (analogAttach(Pin(GPIO_LED1, led))) +#endif + analogWrite(Pin(GPIO_LED1, led), bitRead(TasmotaGlobal.led_inverted, led) ? Settings->pwm_range - pwm : pwm); } else { DigitalWrite(GPIO_LED1, led, bitRead(TasmotaGlobal.led_inverted, led) ? !state : state); } @@ -1996,7 +1999,7 @@ void GpioInit(void) pinMode(Pin(GPIO_PWM1, i), OUTPUT); #endif // ESP8266 #ifdef ESP32 - analogAttach(Pin(GPIO_PWM1, i), i); + analogAttach(Pin(GPIO_PWM1, i)); #endif // ESP32 if (TasmotaGlobal.light_type) { // force PWM GPIOs to low or high mode, see #7165 diff --git a/tasmota/xdrv_04_light.ino b/tasmota/xdrv_04_light.ino index a5db3d58a..8be733e82 100644 --- a/tasmota/xdrv_04_light.ino +++ b/tasmota/xdrv_04_light.ino @@ -1158,7 +1158,7 @@ void LightInit(void) pinMode(Pin(GPIO_PWM1, i), OUTPUT); #endif // ESP8266 #ifdef ESP32 - analogAttach(Pin(GPIO_PWM1, i), i); + analogAttach(Pin(GPIO_PWM1, i)); #endif // ESP32 } } diff --git a/tasmota/xdrv_24_buzzer.ino b/tasmota/xdrv_24_buzzer.ino index 377c2fee6..d7b8ef0f3 100644 --- a/tasmota/xdrv_24_buzzer.ino +++ b/tasmota/xdrv_24_buzzer.ino @@ -51,11 +51,11 @@ void BuzzerSet(uint32_t state) { static uint8_t last_state = 0; if (last_state != state) { #ifdef ESP32 - analogAttach(Pin(GPIO_BUZZER), 7); + if (analogAttach(Pin(GPIO_BUZZER))) #endif // ESP32 - // Set 50% duty cycle for frequency output - // Set 0% (or 100% for inverted PWM) duty cycle which turns off frequency output either way - analogWrite(Pin(GPIO_BUZZER), (state) ? Settings->pwm_range / 2 : 0); // set duty cycle for frequency output + // Set 50% duty cycle for frequency output + // Set 0% (or 100% for inverted PWM) duty cycle which turns off frequency output either way + analogWrite(Pin(GPIO_BUZZER), (state) ? Settings->pwm_range / 2 : 0); // set duty cycle for frequency output last_state = state; } } else { diff --git a/tasmota/xlgt_03_sm16716.ino b/tasmota/xlgt_03_sm16716.ino index c08df6a90..b1d4207b8 100644 --- a/tasmota/xlgt_03_sm16716.ino +++ b/tasmota/xlgt_03_sm16716.ino @@ -143,21 +143,6 @@ void Sm16716ModuleSelected(void) Sm16716.pin_dat = Pin(GPIO_SM16716_DAT); Sm16716.pin_sel = Pin(GPIO_SM16716_SEL); -/* - // init PWM - for (uint32_t i = 0; i < Light.subtype; i++) { - Settings->pwm_value[i] = 0; // Disable direct PWM control - if (PinUsed(GPIO_PWM1, i)) { -#ifdef ESP8266 - pinMode(Pin(GPIO_PWM1, i), OUTPUT); -#endif // ESP8266 -#ifdef ESP32 - analogAttach(Pin(GPIO_PWM1, i), i); -#endif // ESP32 - } - } -*/ - // init sm16716 pinMode(Sm16716.pin_clk, OUTPUT); digitalWrite(Sm16716.pin_clk, LOW);