From e04c322da3d34427849eb4eb51eb6ba69f8fe87b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Procha=CC=81zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Mon, 14 Nov 2022 09:43:28 +0100 Subject: [PATCH 1/4] Added log errors + returns --- cores/esp32/esp32-hal-ledc.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/cores/esp32/esp32-hal-ledc.c b/cores/esp32/esp32-hal-ledc.c index 4b58c1d9a91..a02431b58fb 100644 --- a/cores/esp32/esp32-hal-ledc.c +++ b/cores/esp32/esp32-hal-ledc.c @@ -222,9 +222,12 @@ void analogWrite(uint8_t pin, int value) { 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--; - ledcSetup(cnt_channel, analog_frequency, analog_resolution); - ledcAttachPin(pin, cnt_channel); } ledcWrite(pin_to_channel[pin] - 1, value); } @@ -237,7 +240,10 @@ int8_t analogGetChannel(uint8_t pin) { void analogWriteFrequency(uint32_t freq) { if (cnt_channel != LEDC_CHANNELS) { for (int channel = LEDC_CHANNELS - 1; channel >= cnt_channel; channel--) { - ledcChangeFrequency(channel, freq, analog_resolution); + if (ledcChangeFrequency(channel, freq, analog_resolution) == 0){ + log_e("analogWrite frequency cant be set due to selected resolution! Try to adjust resolution first"); + return; + } } } analog_frequency = freq; @@ -250,7 +256,10 @@ void analogWriteResolution(uint8_t bits) { } if (cnt_channel != LEDC_CHANNELS) { for (int channel = LEDC_CHANNELS - 1; channel >= cnt_channel; channel--) { - ledcChangeFrequency(channel, analog_frequency, bits); + if (ledcChangeFrequency(channel, analog_frequency, bits) == 0){ + log_e("analogWrite resolution cant be set due to selected frequency! Try to adjust frequency first"); + return; + } } } analog_resolution = bits; From a6dc813cd6eaa689913c05c67e563f37a291c511 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Procha=CC=81zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Mon, 14 Nov 2022 13:18:43 +0100 Subject: [PATCH 2/4] fixed missing functions in header file --- cores/esp32/esp32-hal-ledc.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cores/esp32/esp32-hal-ledc.h b/cores/esp32/esp32-hal-ledc.h index 4b8bc7d712a..f00f44c8b59 100644 --- a/cores/esp32/esp32-hal-ledc.h +++ b/cores/esp32/esp32-hal-ledc.h @@ -37,6 +37,8 @@ void ledcAttachPin(uint8_t pin, uint8_t channel); void ledcDetachPin(uint8_t pin); uint32_t ledcChangeFrequency(uint8_t channel, uint32_t freq, uint8_t resolution_bits); +void analogWriteFrequency(uint32_t freq); +void analogWriteResolution(uint8_t bits); #ifdef __cplusplus } From 0cab4b3d2926bcb5537c8df7c527ee5ed55d78e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Procha=CC=81zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Mon, 14 Nov 2022 13:19:02 +0100 Subject: [PATCH 3/4] Update LEDC docs --- docs/source/api/ledc.rst | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/docs/source/api/ledc.rst b/docs/source/api/ledc.rst index b6c72821304..a23092e3886 100644 --- a/docs/source/api/ledc.rst +++ b/docs/source/api/ledc.rst @@ -169,6 +169,28 @@ It is compatible with Arduinos analogWrite function. * ``value`` select the duty cycle of pwm. * range is from 0 (always off) to 255 (always on). +analogWriteResolution +********************* + +This function is used to set resolution for all analogWrite channels. + +.. code-block:: arduino + + void analogWriteResolution(uint8_t bits); + +* ``bits`` select resolution for analog channels. + +analogWriteFrequency +******************** + +This function is used to set frequency for all analogWrite channels. + +.. code-block:: arduino + + void analogWriteFrequency(uint32_t freq); + +* ``freq`` select frequency of pwm. + Example Applications ******************** From 55ff85db956aee0d63a2963b88dbf8dd286f28a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Procha=CC=81zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Mon, 14 Nov 2022 13:35:09 +0100 Subject: [PATCH 4/4] Revert "fixed missing functions in header file" This reverts commit a6dc813cd6eaa689913c05c67e563f37a291c511. --- cores/esp32/esp32-hal-ledc.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/cores/esp32/esp32-hal-ledc.h b/cores/esp32/esp32-hal-ledc.h index f00f44c8b59..4b8bc7d712a 100644 --- a/cores/esp32/esp32-hal-ledc.h +++ b/cores/esp32/esp32-hal-ledc.h @@ -37,8 +37,6 @@ void ledcAttachPin(uint8_t pin, uint8_t channel); void ledcDetachPin(uint8_t pin); uint32_t ledcChangeFrequency(uint8_t channel, uint32_t freq, uint8_t resolution_bits); -void analogWriteFrequency(uint32_t freq); -void analogWriteResolution(uint8_t bits); #ifdef __cplusplus }