From cbeb12be57ca83762a05d7297761a38172d841ac Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Fri, 28 Apr 2023 22:27:48 -0300 Subject: [PATCH 1/3] Fixes analogWrite --- cores/esp32/esp32-hal-ledc.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/cores/esp32/esp32-hal-ledc.c b/cores/esp32/esp32-hal-ledc.c index a02431b58fb..3e3ce5c7e8c 100644 --- a/cores/esp32/esp32-hal-ledc.c +++ b/cores/esp32/esp32-hal-ledc.c @@ -217,24 +217,29 @@ static int analog_frequency = 1000; void analogWrite(uint8_t pin, int value) { // 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; } - 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--; + channel = cnt_channel - 1; + } 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; } - ledcWrite(pin_to_channel[pin] - 1, value); + ledcAttachPin(pin, channel); + pin_to_channel[pin] = channel; + ledcWrite(channel, value); } } int8_t analogGetChannel(uint8_t pin) { - return pin_to_channel[pin] - 1; + return pin_to_channel[pin]; } void analogWriteFrequency(uint32_t freq) { From b153dca6ee3928efa6a9da241af5f4b7596500bb Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Fri, 28 Apr 2023 22:43:00 -0300 Subject: [PATCH 2/3] sets cnt_channel index --- cores/esp32/esp32-hal-ledc.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cores/esp32/esp32-hal-ledc.c b/cores/esp32/esp32-hal-ledc.c index 3e3ce5c7e8c..85a53d07e11 100644 --- a/cores/esp32/esp32-hal-ledc.c +++ b/cores/esp32/esp32-hal-ledc.c @@ -223,7 +223,8 @@ void analogWrite(uint8_t pin, int value) { log_e("No more analogWrite channels available! You can have maximum %u", LEDC_CHANNELS); return; } - channel = cnt_channel - 1; + cnt_channel--; + channel = cnt_channel; } else { channel = analogGetChannel(pin); } From 6c8e03a40c5cf79c3d3793bc60b7bc7e70809dbb Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Fri, 28 Apr 2023 23:50:53 -0300 Subject: [PATCH 3/3] fixes TAB alligment --- cores/esp32/esp32-hal-ledc.c | 42 ++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/cores/esp32/esp32-hal-ledc.c b/cores/esp32/esp32-hal-ledc.c index 85a53d07e11..08dba67104c 100644 --- a/cores/esp32/esp32-hal-ledc.c +++ b/cores/esp32/esp32-hal-ledc.c @@ -215,28 +215,28 @@ 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) { - 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; + // 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); } - ledcAttachPin(pin, channel); - pin_to_channel[pin] = channel; - ledcWrite(channel, value); - } } int8_t analogGetChannel(uint8_t pin) {