From 200fe298c7f0e3bf0d4762aa72eb774054d6828a Mon Sep 17 00:00:00 2001 From: Jan Prochazka <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Thu, 21 Dec 2023 15:30:30 +0100 Subject: [PATCH 1/4] feat(ledc): Allow custom channel selection --- cores/esp32/esp32-hal-ledc.c | 21 ++++++++++++++++----- cores/esp32/esp32-hal-ledc.h | 1 + 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/cores/esp32/esp32-hal-ledc.c b/cores/esp32/esp32-hal-ledc.c index c96d1b0a650..9f8afa0634f 100644 --- a/cores/esp32/esp32-hal-ledc.c +++ b/cores/esp32/esp32-hal-ledc.c @@ -56,12 +56,11 @@ static bool ledcDetachBus(void * bus){ return true; } -bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution) +bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, int channel) { - int free_channel = ~ledc_handle.used_channels & (ledc_handle.used_channels+1); - if (free_channel == 0 || resolution > LEDC_MAX_BIT_WIDTH) + if (channel > LEDC_CHANNELS || resolution > LEDC_MAX_BIT_WIDTH) { - log_e("No more LEDC channels available! (maximum %u) or bit width too big (maximum %u)", LEDC_CHANNELS, LEDC_MAX_BIT_WIDTH); + log_e("Channel %u is not available! (maximum %u) or bit width too big (maximum %u)", channel, LEDC_CHANNELS, LEDC_MAX_BIT_WIDTH); return false; } @@ -71,7 +70,6 @@ bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution) return false; } - int channel = log2(free_channel & -free_channel); uint8_t group=(channel/8), timer=((channel/2)%4); ledc_timer_config_t ledc_timer = { @@ -117,6 +115,19 @@ bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution) return true; } + +bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution) +{ + int free_channel = ~ledc_handle.used_channels & (ledc_handle.used_channels+1); + if (free_channel == 0 || resolution > LEDC_MAX_BIT_WIDTH){ + log_e("No more LEDC channels available! (maximum %u) or bit width too big (maximum %u)", LEDC_CHANNELS, LEDC_MAX_BIT_WIDTH); + return false; + } + int channel = log2(free_channel & -free_channel); + + return ledcAttachChannel(pin, freq, resolution, channel); +} + bool ledcWrite(uint8_t pin, uint32_t duty) { ledc_channel_handle_t *bus = (ledc_channel_handle_t*)perimanGetPinBus(pin, ESP32_BUS_TYPE_LEDC); diff --git a/cores/esp32/esp32-hal-ledc.h b/cores/esp32/esp32-hal-ledc.h index ca172844247..a0283c66039 100644 --- a/cores/esp32/esp32-hal-ledc.h +++ b/cores/esp32/esp32-hal-ledc.h @@ -45,6 +45,7 @@ typedef struct { //channel 0-15 resolution 1-16bits freq limits depend on resolution bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution); +bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, int channel); bool ledcWrite(uint8_t pin, uint32_t duty); uint32_t ledcWriteTone(uint8_t pin, uint32_t freq); uint32_t ledcWriteNote(uint8_t pin, note_t note, uint8_t octave); From 224dea961d9aa6a9746b73d06ddf897ec9228d8d Mon Sep 17 00:00:00 2001 From: Jan Prochazka <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Thu, 21 Dec 2023 15:59:33 +0100 Subject: [PATCH 2/4] fix(ledc): Fix check of maximum channel --- cores/esp32/esp32-hal-ledc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp32/esp32-hal-ledc.c b/cores/esp32/esp32-hal-ledc.c index 9f8afa0634f..8da9b03a1b1 100644 --- a/cores/esp32/esp32-hal-ledc.c +++ b/cores/esp32/esp32-hal-ledc.c @@ -58,7 +58,7 @@ static bool ledcDetachBus(void * bus){ bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, int channel) { - if (channel > LEDC_CHANNELS || resolution > LEDC_MAX_BIT_WIDTH) + if (channel >= LEDC_CHANNELS || resolution > LEDC_MAX_BIT_WIDTH) { log_e("Channel %u is not available! (maximum %u) or bit width too big (maximum %u)", channel, LEDC_CHANNELS, LEDC_MAX_BIT_WIDTH); return false; From 0fc3602a1eb34c61775f1067f718852d49166add Mon Sep 17 00:00:00 2001 From: Jan Prochazka <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Fri, 22 Dec 2023 13:33:25 +0100 Subject: [PATCH 3/4] docs(ledc): Add ledcAttachChannel to docs --- docs/source/api/ledc.rst | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/docs/source/api/ledc.rst b/docs/source/api/ledc.rst index b69f581f5bf..1abeea2f6ee 100644 --- a/docs/source/api/ledc.rst +++ b/docs/source/api/ledc.rst @@ -27,6 +27,7 @@ ledcAttach ********** This function is used to setup LEDC pin with given frequency and resolution. +LEDC channel will be selected automatically. .. code-block:: arduino @@ -41,6 +42,25 @@ This function is used to setup LEDC pin with given frequency and resolution. This function will return ``true`` if configuration is successful. If ``false`` is returned, error occurs and LEDC channel was not configured. +ledcAttachChannel +***************** + +This function is used to setup LEDC pin with given frequency, resolution and channel. + +.. code-block:: arduino + + bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t channel); + +* ``pin`` select LEDC pin. +* ``freq`` select frequency of pwm. +* ``resolution`` select resolution for LEDC channel. +* ``channel`` select LEDC channel. + + * range is 1-14 bits (1-20 bits for ESP32). + +This function will return ``true`` if configuration is successful. +If ``false`` is returned, error occurs and LEDC channel was not configured. + ledcWrite ********* From 42765f5ec92e999f1cda3ef19f718563863d2cff Mon Sep 17 00:00:00 2001 From: Jan Prochazka <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Fri, 22 Dec 2023 13:34:03 +0100 Subject: [PATCH 4/4] feat(ledc): Change channel to uint8_t + add log message --- cores/esp32/esp32-hal-ledc.c | 5 +++-- cores/esp32/esp32-hal-ledc.h | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/cores/esp32/esp32-hal-ledc.c b/cores/esp32/esp32-hal-ledc.c index 8da9b03a1b1..76d522c2313 100644 --- a/cores/esp32/esp32-hal-ledc.c +++ b/cores/esp32/esp32-hal-ledc.c @@ -56,7 +56,7 @@ static bool ledcDetachBus(void * bus){ return true; } -bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, int channel) +bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t channel) { if (channel >= LEDC_CHANNELS || resolution > LEDC_MAX_BIT_WIDTH) { @@ -113,12 +113,13 @@ bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, int chann return false; } + log_i("LEDC attached to pin %u (channel %u, resolution %u)", pin, channel, resolution); return true; } bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution) { - int free_channel = ~ledc_handle.used_channels & (ledc_handle.used_channels+1); + uint8_t free_channel = ~ledc_handle.used_channels & (ledc_handle.used_channels+1); if (free_channel == 0 || resolution > LEDC_MAX_BIT_WIDTH){ log_e("No more LEDC channels available! (maximum %u) or bit width too big (maximum %u)", LEDC_CHANNELS, LEDC_MAX_BIT_WIDTH); return false; diff --git a/cores/esp32/esp32-hal-ledc.h b/cores/esp32/esp32-hal-ledc.h index a0283c66039..c81da6dced2 100644 --- a/cores/esp32/esp32-hal-ledc.h +++ b/cores/esp32/esp32-hal-ledc.h @@ -45,7 +45,7 @@ typedef struct { //channel 0-15 resolution 1-16bits freq limits depend on resolution bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution); -bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, int channel); +bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t channel); bool ledcWrite(uint8_t pin, uint32_t duty); uint32_t ledcWriteTone(uint8_t pin, uint32_t freq); uint32_t ledcWriteNote(uint8_t pin, note_t note, uint8_t octave);