Skip to content

Commit dd4a7d6

Browse files
konzenP-R-O-C-H-Ypre-commit-ci-lite[bot]
authored
feat(LEDC): Adds the ability to set the clock source for the LEDC (espressif#10171)
* Adds the ability to set the clock source for the LEDC * feat(LEDC): Adjusting function names to more suitable * feat(LEDC): Fix clock_source to static * docs(ledc): Document ledc set and get clock source * docs(ledc): Update ledcSetClockSource description * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: Jan Prochazka <90197375+P-R-O-C-H-Y@users.noreply.github.com> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 683fea1 commit dd4a7d6

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

cores/esp32/esp32-hal-ledc.c

+18-3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ ledc_periph_t ledc_handle = {0};
4747

4848
static bool fade_initialized = false;
4949

50+
static ledc_clk_cfg_t clock_source = LEDC_DEFAULT_CLK;
51+
52+
ledc_clk_cfg_t ledcGetClockSource(void) {
53+
return clock_source;
54+
}
55+
56+
bool ledcSetClockSource(ledc_clk_cfg_t source) {
57+
if (ledc_handle.used_channels) {
58+
log_e("Cannot change LEDC clock source! LEDC channels in use.");
59+
return false;
60+
}
61+
clock_source = source;
62+
return true;
63+
}
64+
5065
static bool ledcDetachBus(void *bus) {
5166
ledc_channel_handle_t *handle = (ledc_channel_handle_t *)bus;
5267
bool channel_found = false;
@@ -111,7 +126,7 @@ bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, uint8_t c
111126
return false;
112127
}
113128
} else {
114-
ledc_timer_config_t ledc_timer = {.speed_mode = group, .timer_num = timer, .duty_resolution = resolution, .freq_hz = freq, .clk_cfg = LEDC_DEFAULT_CLK};
129+
ledc_timer_config_t ledc_timer = {.speed_mode = group, .timer_num = timer, .duty_resolution = resolution, .freq_hz = freq, .clk_cfg = clock_source};
115130
if (ledc_timer_config(&ledc_timer) != ESP_OK) {
116131
log_e("ledc setup failed!");
117132
return false;
@@ -241,7 +256,7 @@ uint32_t ledcWriteTone(uint8_t pin, uint32_t freq) {
241256

242257
uint8_t group = (bus->channel / 8), timer = ((bus->channel / 2) % 4);
243258

244-
ledc_timer_config_t ledc_timer = {.speed_mode = group, .timer_num = timer, .duty_resolution = 10, .freq_hz = freq, .clk_cfg = LEDC_DEFAULT_CLK};
259+
ledc_timer_config_t ledc_timer = {.speed_mode = group, .timer_num = timer, .duty_resolution = 10, .freq_hz = freq, .clk_cfg = clock_source};
245260

246261
if (ledc_timer_config(&ledc_timer) != ESP_OK) {
247262
log_e("ledcWriteTone configuration failed!");
@@ -292,7 +307,7 @@ uint32_t ledcChangeFrequency(uint8_t pin, uint32_t freq, uint8_t resolution) {
292307
}
293308
uint8_t group = (bus->channel / 8), timer = ((bus->channel / 2) % 4);
294309

295-
ledc_timer_config_t ledc_timer = {.speed_mode = group, .timer_num = timer, .duty_resolution = resolution, .freq_hz = freq, .clk_cfg = LEDC_DEFAULT_CLK};
310+
ledc_timer_config_t ledc_timer = {.speed_mode = group, .timer_num = timer, .duty_resolution = resolution, .freq_hz = freq, .clk_cfg = clock_source};
296311

297312
if (ledc_timer_config(&ledc_timer) != ESP_OK) {
298313
log_e("ledcChangeFrequency failed!");

cores/esp32/esp32-hal-ledc.h

+17
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ extern "C" {
2626
#include <stdbool.h>
2727
#include "freertos/FreeRTOS.h"
2828
#include "freertos/semphr.h"
29+
#include "hal/ledc_types.h"
2930

3031
typedef enum {
3132
NOTE_C,
@@ -57,6 +58,22 @@ typedef struct {
5758
#endif
5859
} ledc_channel_handle_t;
5960

61+
/**
62+
* @brief Get the LEDC clock source.
63+
*
64+
* @return LEDC clock source.
65+
*/
66+
ledc_clk_cfg_t ledcGetClockSource(void);
67+
68+
/**
69+
* @brief Set the LEDC clock source.
70+
*
71+
* @param source LEDC clock source to set.
72+
*
73+
* @return true if LEDC clock source was successfully set, false otherwise.
74+
*/
75+
bool ledcSetClockSource(ledc_clk_cfg_t source);
76+
6077
/**
6178
* @brief Attach a pin to the LEDC driver, with a given frequency and resolution.
6279
* Channel is automatically assigned.

docs/en/api/ledc.rst

+28
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,34 @@ ESP32-H2 6
2323
Arduino-ESP32 LEDC API
2424
----------------------
2525

26+
ledcSetCLockSource
27+
******************
28+
29+
This function is used to set the LEDC peripheral clock source. Must be called before any LEDC channel is used.
30+
The default clock source is XTAL clock (``LEDC_USE_XTAL_CLK``) if supported by the SoC, otherwise it is AUTO clock (``LEDC_AUTO_CLK``).
31+
32+
.. code-block:: arduino
33+
34+
bool ledcSetClockSource(ledc_clk_cfg_t source);
35+
36+
* ``source`` select the clock source for LEDC peripheral.
37+
38+
* ``LEDC_APB_CLK`` - APB clock.
39+
* ``LEDC_REF_CLK`` - REF clock.
40+
41+
This function will return ``true`` if setting the clock source is successful, otherwise it will return ``false``.
42+
43+
ledcGetClockSource
44+
******************
45+
46+
This function is used to get the LEDC peripheral clock source.
47+
48+
.. code-block:: arduino
49+
50+
ledc_clk_cfg_t ledcGetClockSource(void);
51+
52+
This function will return the clock source for the LEDC peripheral.
53+
2654
ledcAttach
2755
**********
2856

0 commit comments

Comments
 (0)