From fe36df8c5a5ef14463940ab9899e3a1539a13460 Mon Sep 17 00:00:00 2001 From: Adriano Konzen Date: Thu, 15 Aug 2024 09:56:55 -0300 Subject: [PATCH 1/6] Adds the ability to set the clock source for the LEDC --- cores/esp32/esp32-hal-ledc.c | 21 ++++++++++++++++++--- cores/esp32/esp32-hal-ledc.h | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/cores/esp32/esp32-hal-ledc.c b/cores/esp32/esp32-hal-ledc.c index 15466390eb8..2feabbc7ecc 100644 --- a/cores/esp32/esp32-hal-ledc.c +++ b/cores/esp32/esp32-hal-ledc.c @@ -47,6 +47,21 @@ ledc_periph_t ledc_handle = {0}; static bool fade_initialized = false; +ledc_clk_cfg_t clock_source = LEDC_DEFAULT_CLK; + +ledc_clk_cfg_t ledcReadClockSource(void) { + return clock_source; +} + +bool ledcWriteClockSource(ledc_clk_cfg_t source) { + if (ledc_handle.used_channels) { + log_e("Cannot change LEDC clock source! LEDC channels in use."); + return false; + } + clock_source = source; + return true; +} + static bool ledcDetachBus(void *bus) { ledc_channel_handle_t *handle = (ledc_channel_handle_t *)bus; bool channel_found = false; @@ -111,7 +126,7 @@ bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t c return false; } } else { - ledc_timer_config_t ledc_timer = {.speed_mode = group, .timer_num = timer, .duty_resolution = resolution, .freq_hz = freq, .clk_cfg = LEDC_DEFAULT_CLK}; + ledc_timer_config_t ledc_timer = {.speed_mode = group, .timer_num = timer, .duty_resolution = resolution, .freq_hz = freq, .clk_cfg = clock_source}; if (ledc_timer_config(&ledc_timer) != ESP_OK) { log_e("ledc setup failed!"); return false; @@ -241,7 +256,7 @@ uint32_t ledcWriteTone(uint8_t pin, uint32_t freq) { uint8_t group = (bus->channel / 8), timer = ((bus->channel / 2) % 4); - ledc_timer_config_t ledc_timer = {.speed_mode = group, .timer_num = timer, .duty_resolution = 10, .freq_hz = freq, .clk_cfg = LEDC_DEFAULT_CLK}; + ledc_timer_config_t ledc_timer = {.speed_mode = group, .timer_num = timer, .duty_resolution = 10, .freq_hz = freq, .clk_cfg = clock_source}; if (ledc_timer_config(&ledc_timer) != ESP_OK) { log_e("ledcWriteTone configuration failed!"); @@ -292,7 +307,7 @@ uint32_t ledcChangeFrequency(uint8_t pin, uint32_t freq, uint8_t resolution) { } uint8_t group = (bus->channel / 8), timer = ((bus->channel / 2) % 4); - ledc_timer_config_t ledc_timer = {.speed_mode = group, .timer_num = timer, .duty_resolution = resolution, .freq_hz = freq, .clk_cfg = LEDC_DEFAULT_CLK}; + ledc_timer_config_t ledc_timer = {.speed_mode = group, .timer_num = timer, .duty_resolution = resolution, .freq_hz = freq, .clk_cfg = clock_source}; if (ledc_timer_config(&ledc_timer) != ESP_OK) { log_e("ledcChangeFrequency failed!"); diff --git a/cores/esp32/esp32-hal-ledc.h b/cores/esp32/esp32-hal-ledc.h index efa7bf1fe69..de2e932da26 100644 --- a/cores/esp32/esp32-hal-ledc.h +++ b/cores/esp32/esp32-hal-ledc.h @@ -26,6 +26,7 @@ extern "C" { #include #include "freertos/FreeRTOS.h" #include "freertos/semphr.h" +#include "hal/ledc_types.h" typedef enum { NOTE_C, @@ -57,6 +58,20 @@ typedef struct { #endif } ledc_channel_handle_t; +/** + * @brief Read the LEDC clock source. + * + * @return LEDC clock source. + */ +ledc_clk_cfg_t ledcReadClockSource(void); + +/** + * @brief Set the LEDC clock source. + * + * @return true if LEDC clock source was successfully set, false otherwise. + */ +bool ledcWriteClockSource(ledc_clk_cfg_t source); + /** * @brief Attach a pin to the LEDC driver, with a given frequency and resolution. * Channel is automatically assigned. From 5a97087333865b47e0feb29075c6b6af3fa535fe Mon Sep 17 00:00:00 2001 From: Adriano Konzen Date: Wed, 28 Aug 2024 08:27:24 -0300 Subject: [PATCH 2/6] feat(LEDC): Adjusting function names to more suitable --- cores/esp32/esp32-hal-ledc.c | 4 ++-- cores/esp32/esp32-hal-ledc.h | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cores/esp32/esp32-hal-ledc.c b/cores/esp32/esp32-hal-ledc.c index 2feabbc7ecc..d7e21ee07ea 100644 --- a/cores/esp32/esp32-hal-ledc.c +++ b/cores/esp32/esp32-hal-ledc.c @@ -49,11 +49,11 @@ static bool fade_initialized = false; ledc_clk_cfg_t clock_source = LEDC_DEFAULT_CLK; -ledc_clk_cfg_t ledcReadClockSource(void) { +ledc_clk_cfg_t ledcGetClockSource(void) { return clock_source; } -bool ledcWriteClockSource(ledc_clk_cfg_t source) { +bool ledcSetClockSource(ledc_clk_cfg_t source) { if (ledc_handle.used_channels) { log_e("Cannot change LEDC clock source! LEDC channels in use."); return false; diff --git a/cores/esp32/esp32-hal-ledc.h b/cores/esp32/esp32-hal-ledc.h index de2e932da26..07e2884f997 100644 --- a/cores/esp32/esp32-hal-ledc.h +++ b/cores/esp32/esp32-hal-ledc.h @@ -59,18 +59,18 @@ typedef struct { } ledc_channel_handle_t; /** - * @brief Read the LEDC clock source. + * @brief Get the LEDC clock source. * * @return LEDC clock source. */ -ledc_clk_cfg_t ledcReadClockSource(void); +ledc_clk_cfg_t ledcGetClockSource(void); /** * @brief Set the LEDC clock source. * * @return true if LEDC clock source was successfully set, false otherwise. */ -bool ledcWriteClockSource(ledc_clk_cfg_t source); +bool ledcSetClockSource(ledc_clk_cfg_t source); /** * @brief Attach a pin to the LEDC driver, with a given frequency and resolution. From 90a0d51cc8045876fa8c32c65b26ceccb7d947ec Mon Sep 17 00:00:00 2001 From: Adriano Konzen Date: Wed, 28 Aug 2024 08:32:40 -0300 Subject: [PATCH 3/6] feat(LEDC): Fix clock_source to static --- 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 d7e21ee07ea..7d748f98a56 100644 --- a/cores/esp32/esp32-hal-ledc.c +++ b/cores/esp32/esp32-hal-ledc.c @@ -47,7 +47,7 @@ ledc_periph_t ledc_handle = {0}; static bool fade_initialized = false; -ledc_clk_cfg_t clock_source = LEDC_DEFAULT_CLK; +static ledc_clk_cfg_t clock_source = LEDC_DEFAULT_CLK; ledc_clk_cfg_t ledcGetClockSource(void) { return clock_source; From becf54bb622e72b211ee1bf5fd32ffdbe0400cf7 Mon Sep 17 00:00:00 2001 From: Jan Prochazka <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Wed, 28 Aug 2024 14:52:04 +0200 Subject: [PATCH 4/6] docs(ledc): Document ledc set and get clock source --- cores/esp32/esp32-hal-ledc.h | 2 ++ docs/en/api/ledc.rst | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/cores/esp32/esp32-hal-ledc.h b/cores/esp32/esp32-hal-ledc.h index 07e2884f997..153d284af1e 100644 --- a/cores/esp32/esp32-hal-ledc.h +++ b/cores/esp32/esp32-hal-ledc.h @@ -67,6 +67,8 @@ ledc_clk_cfg_t ledcGetClockSource(void); /** * @brief Set the LEDC clock source. + * + * @param source LEDC clock source to set. * * @return true if LEDC clock source was successfully set, false otherwise. */ diff --git a/docs/en/api/ledc.rst b/docs/en/api/ledc.rst index b7fd7b25537..b1235169950 100644 --- a/docs/en/api/ledc.rst +++ b/docs/en/api/ledc.rst @@ -23,6 +23,34 @@ ESP32-H2 6 Arduino-ESP32 LEDC API ---------------------- +ledcSetCLockSource +****************** + +This function is used to set the LEDC peripheral clock source. The default clock source is XTAL clock (``LEDC_USE_XTAL_CLK``) if supported by the SoC, +otherwise it is AUTO clock (``LEDC_AUTO_CLK``). + +.. code-block:: arduino + + bool ledcSetClockSource(ledc_clk_cfg_t source); + +* ``source`` select the clock source for LEDC peripheral. + + * ``LEDC_APB_CLK`` - APB clock. + * ``LEDC_REF_CLK`` - REF clock. + +This function will return ``true`` if setting the clock source is successful, otherwise it will return ``false``. + +ledcGetClockSource +****************** + +This function is used to get the LEDC peripheral clock source. + +.. code-block:: arduino + + ledc_clk_cfg_t ledcGetClockSource(void); + +This function will return the clock source for the LEDC peripheral. + ledcAttach ********** From c61436a2eb46395ddb9acdc8272202b7fd8d781f Mon Sep 17 00:00:00 2001 From: Jan Prochazka <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Wed, 28 Aug 2024 15:15:36 +0200 Subject: [PATCH 5/6] docs(ledc): Update ledcSetClockSource description --- docs/en/api/ledc.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/en/api/ledc.rst b/docs/en/api/ledc.rst index b1235169950..a20682174bd 100644 --- a/docs/en/api/ledc.rst +++ b/docs/en/api/ledc.rst @@ -26,8 +26,8 @@ Arduino-ESP32 LEDC API ledcSetCLockSource ****************** -This function is used to set the LEDC peripheral clock source. The default clock source is XTAL clock (``LEDC_USE_XTAL_CLK``) if supported by the SoC, -otherwise it is AUTO clock (``LEDC_AUTO_CLK``). +This function is used to set the LEDC peripheral clock source. Must be called before any LEDC channel is used. +The default clock source is XTAL clock (``LEDC_USE_XTAL_CLK``) if supported by the SoC, otherwise it is AUTO clock (``LEDC_AUTO_CLK``). .. code-block:: arduino From 5bed7e022b496b527935d7e65c231d48e3ee6315 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Wed, 28 Aug 2024 13:21:06 +0000 Subject: [PATCH 6/6] ci(pre-commit): Apply automatic fixes --- cores/esp32/esp32-hal-ledc.h | 2 +- docs/en/api/ledc.rst | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cores/esp32/esp32-hal-ledc.h b/cores/esp32/esp32-hal-ledc.h index 153d284af1e..5b44aaad452 100644 --- a/cores/esp32/esp32-hal-ledc.h +++ b/cores/esp32/esp32-hal-ledc.h @@ -67,7 +67,7 @@ ledc_clk_cfg_t ledcGetClockSource(void); /** * @brief Set the LEDC clock source. - * + * * @param source LEDC clock source to set. * * @return true if LEDC clock source was successfully set, false otherwise. diff --git a/docs/en/api/ledc.rst b/docs/en/api/ledc.rst index a20682174bd..6ea3437bbf5 100644 --- a/docs/en/api/ledc.rst +++ b/docs/en/api/ledc.rst @@ -33,7 +33,7 @@ The default clock source is XTAL clock (``LEDC_USE_XTAL_CLK``) if supported by t bool ledcSetClockSource(ledc_clk_cfg_t source); -* ``source`` select the clock source for LEDC peripheral. +* ``source`` select the clock source for LEDC peripheral. * ``LEDC_APB_CLK`` - APB clock. * ``LEDC_REF_CLK`` - REF clock.