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 ## [Unreleased] - Development
## [10.1.0.1] ## [10.1.0.1]
### Fixed
- ESP32 LedPwmMode exception (#14073)
## [Released] ## [Released]

View File

@ -108,5 +108,6 @@ The latter links can be used for OTA upgrades too like ``OtaUrl http://ota.tasmo
### Changed ### Changed
### Fixed ### Fixed
- ESP32 LedPwmMode exception [#14073](https://github.com/arendst/Tasmota/issues/14073)
### Removed ### Removed

View File

@ -88,12 +88,37 @@ inline void analogWriteFreq(uint32_t freq) {
_analogWriteFreqRange(); _analogWriteFreqRange();
} }
/*
inline void analogAttach(uint32_t pin, uint32_t channel) { inline void analogAttach(uint32_t pin, uint32_t channel) {
_pwm_channel[channel &7] = pin; _pwm_channel[channel &7] = pin;
ledcAttachPin(pin, channel + PWM_CHANNEL_OFFSET); ledcAttachPin(pin, channel + PWM_CHANNEL_OFFSET);
ledcSetup(channel + PWM_CHANNEL_OFFSET, _pwm_frequency, _pwm_bit_num); ledcSetup(channel + PWM_CHANNEL_OFFSET, _pwm_frequency, _pwm_bit_num);
// Serial.printf("attach %d - %d\n", channel, pin); // 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) inline void analogWrite(uint8_t pin, int val)
{ {

View File

@ -439,6 +439,9 @@ void SetLedPowerIdx(uint32_t led, uint32_t state)
#else //USE_LIGHT #else //USE_LIGHT
pwm = changeUIntScale((uint16_t)(state ? Settings->ledpwm_on : Settings->ledpwm_off), 0, 255, 0, Settings->pwm_range); // linear pwm = changeUIntScale((uint16_t)(state ? Settings->ledpwm_on : Settings->ledpwm_off), 0, 255, 0, Settings->pwm_range); // linear
#endif //USE_LIGHT #endif //USE_LIGHT
#ifdef ESP32
if (analogAttach(Pin(GPIO_LED1, led)))
#endif
analogWrite(Pin(GPIO_LED1, led), bitRead(TasmotaGlobal.led_inverted, led) ? Settings->pwm_range - pwm : pwm); analogWrite(Pin(GPIO_LED1, led), bitRead(TasmotaGlobal.led_inverted, led) ? Settings->pwm_range - pwm : pwm);
} else { } else {
DigitalWrite(GPIO_LED1, led, bitRead(TasmotaGlobal.led_inverted, led) ? !state : state); DigitalWrite(GPIO_LED1, led, bitRead(TasmotaGlobal.led_inverted, led) ? !state : state);
@ -1996,7 +1999,7 @@ void GpioInit(void)
pinMode(Pin(GPIO_PWM1, i), OUTPUT); pinMode(Pin(GPIO_PWM1, i), OUTPUT);
#endif // ESP8266 #endif // ESP8266
#ifdef ESP32 #ifdef ESP32
analogAttach(Pin(GPIO_PWM1, i), i); analogAttach(Pin(GPIO_PWM1, i));
#endif // ESP32 #endif // ESP32
if (TasmotaGlobal.light_type) { if (TasmotaGlobal.light_type) {
// force PWM GPIOs to low or high mode, see #7165 // 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); pinMode(Pin(GPIO_PWM1, i), OUTPUT);
#endif // ESP8266 #endif // ESP8266
#ifdef ESP32 #ifdef ESP32
analogAttach(Pin(GPIO_PWM1, i), i); analogAttach(Pin(GPIO_PWM1, i));
#endif // ESP32 #endif // ESP32
} }
} }

View File

@ -51,7 +51,7 @@ void BuzzerSet(uint32_t state) {
static uint8_t last_state = 0; static uint8_t last_state = 0;
if (last_state != state) { if (last_state != state) {
#ifdef ESP32 #ifdef ESP32
analogAttach(Pin(GPIO_BUZZER), 7); if (analogAttach(Pin(GPIO_BUZZER)))
#endif // ESP32 #endif // ESP32
// Set 50% 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 // Set 0% (or 100% for inverted PWM) duty cycle which turns off frequency output either way

View File

@ -143,21 +143,6 @@ void Sm16716ModuleSelected(void)
Sm16716.pin_dat = Pin(GPIO_SM16716_DAT); Sm16716.pin_dat = Pin(GPIO_SM16716_DAT);
Sm16716.pin_sel = Pin(GPIO_SM16716_SEL); 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 // init sm16716
pinMode(Sm16716.pin_clk, OUTPUT); pinMode(Sm16716.pin_clk, OUTPUT);
digitalWrite(Sm16716.pin_clk, LOW); digitalWrite(Sm16716.pin_clk, LOW);