From 9075978146fb62dd00edeb206d377595c2d904cb Mon Sep 17 00:00:00 2001 From: Ricardo Lipas Augusto Date: Thu, 10 Aug 2023 15:32:28 +0100 Subject: [PATCH 1/2] Fix analogWrite channel available verification The last channel allocated is number 0, which conflicted with the value given to an uninitialized pin, giving the "No more analogWrite channels available!" error when trying to use it Pins are now given the value -1 to indicate that they are not used so channel 0 can be used without errors. --- cores/esp32/esp32-hal-ledc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cores/esp32/esp32-hal-ledc.c b/cores/esp32/esp32-hal-ledc.c index 08dba67104c..de75f273be2 100644 --- a/cores/esp32/esp32-hal-ledc.c +++ b/cores/esp32/esp32-hal-ledc.c @@ -210,7 +210,7 @@ uint32_t ledcChangeFrequency(uint8_t chan, uint32_t freq, uint8_t bit_num) return ledc_get_freq(group,timer); } -static int8_t pin_to_channel[SOC_GPIO_PIN_COUNT] = { 0 }; +static int8_t pin_to_channel[SOC_GPIO_PIN_COUNT] = { -1 }; static int cnt_channel = LEDC_CHANNELS; static uint8_t analog_resolution = 8; static int analog_frequency = 1000; @@ -218,7 +218,7 @@ 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 (pin_to_channel[pin] == -1) { if (!cnt_channel) { log_e("No more analogWrite channels available! You can have maximum %u", LEDC_CHANNELS); return; From d4d7a68d90c25926b10e7c6f94d83a9f9f0cf276 Mon Sep 17 00:00:00 2001 From: Ricardo Lipas Augusto Date: Sun, 13 Aug 2023 09:55:25 +0100 Subject: [PATCH 2/2] Fix incorrect array initialization Keeping array of zeros for `pin_to_channel` and shifting stored channel values by +1 to keep the pin with channel 0 from being interpreted as unused. ref: https://github.com/espressif/arduino-esp32/pull/8509#issuecomment-1676103452 --- cores/esp32/esp32-hal-ledc.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cores/esp32/esp32-hal-ledc.c b/cores/esp32/esp32-hal-ledc.c index de75f273be2..db14f04f9c3 100644 --- a/cores/esp32/esp32-hal-ledc.c +++ b/cores/esp32/esp32-hal-ledc.c @@ -210,7 +210,7 @@ uint32_t ledcChangeFrequency(uint8_t chan, uint32_t freq, uint8_t bit_num) return ledc_get_freq(group,timer); } -static int8_t pin_to_channel[SOC_GPIO_PIN_COUNT] = { -1 }; +static int8_t pin_to_channel[SOC_GPIO_PIN_COUNT] = { 0 }; static int cnt_channel = LEDC_CHANNELS; static uint8_t analog_resolution = 8; static int analog_frequency = 1000; @@ -218,7 +218,7 @@ 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] == -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; @@ -234,13 +234,13 @@ void analogWrite(uint8_t pin, int value) { return; } ledcAttachPin(pin, channel); - pin_to_channel[pin] = channel; + pin_to_channel[pin] = channel + 1; ledcWrite(channel, value); } } int8_t analogGetChannel(uint8_t pin) { - return pin_to_channel[pin]; + return pin_to_channel[pin] - 1; } void analogWriteFrequency(uint32_t freq) {