Fixed ESP32 LedPwmMode exception

Fixed ESP32 LedPwmMode exception (#14073)
This commit is contained in:
Theo Arends 2021-12-22 16:50:29 +01:00
parent 2b0344a787
commit 8943656729
7 changed files with 38 additions and 22 deletions

View File

@ -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]

View File

@ -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

View File

@ -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)
{

View File

@ -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

View File

@ -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
}
}

View File

@ -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 {

View File

@ -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);