diff --git a/cores/esp32/esp32-hal-ledc.c b/cores/esp32/esp32-hal-ledc.c index a02431b58fb..08dba67104c 100644 --- a/cores/esp32/esp32-hal-ledc.c +++ b/cores/esp32/esp32-hal-ledc.c @@ -215,26 +215,32 @@ static int cnt_channel = LEDC_CHANNELS; static uint8_t analog_resolution = 8; static int analog_frequency = 1000; void analogWrite(uint8_t pin, int value) { - // Use ledc hardware for internal pins - if (pin < SOC_GPIO_PIN_COUNT) { - if (pin_to_channel[pin] == 0) { - if (!cnt_channel) { - log_e("No more analogWrite channels available! You can have maximum %u", LEDC_CHANNELS); - return; - } - if(ledcSetup(cnt_channel - 1, analog_frequency, analog_resolution) == 0){ - log_e("analogWrite setup failed (freq = %u, resolution = %u). Try setting different resolution or frequency"); - return; - } - ledcAttachPin(pin, cnt_channel - 1); - pin_to_channel[pin] = cnt_channel--; + // Use ledc hardware for internal pins + if (pin < SOC_GPIO_PIN_COUNT) { + int8_t channel = -1; + if (pin_to_channel[pin] == 0) { + if (!cnt_channel) { + log_e("No more analogWrite channels available! You can have maximum %u", LEDC_CHANNELS); + return; + } + cnt_channel--; + channel = cnt_channel; + } else { + channel = analogGetChannel(pin); + } + log_v("GPIO %d - Using Channel %d, Value = %d", pin, channel, value); + if(ledcSetup(channel, analog_frequency, analog_resolution) == 0){ + log_e("analogWrite setup failed (freq = %u, resolution = %u). Try setting different resolution or frequency"); + return; + } + ledcAttachPin(pin, channel); + pin_to_channel[pin] = channel; + ledcWrite(channel, value); } - ledcWrite(pin_to_channel[pin] - 1, value); - } } int8_t analogGetChannel(uint8_t pin) { - return pin_to_channel[pin] - 1; + return pin_to_channel[pin]; } void analogWriteFrequency(uint32_t freq) {