Skip to content

Commit fe36df8

Browse files
committed
Adds the ability to set the clock source for the LEDC
1 parent a7399e2 commit fe36df8

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

Diff for: 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+
ledc_clk_cfg_t clock_source = LEDC_DEFAULT_CLK;
51+
52+
ledc_clk_cfg_t ledcReadClockSource(void) {
53+
return clock_source;
54+
}
55+
56+
bool ledcWriteClockSource(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!");

Diff for: cores/esp32/esp32-hal-ledc.h

+15
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,20 @@ typedef struct {
5758
#endif
5859
} ledc_channel_handle_t;
5960

61+
/**
62+
* @brief Read the LEDC clock source.
63+
*
64+
* @return LEDC clock source.
65+
*/
66+
ledc_clk_cfg_t ledcReadClockSource(void);
67+
68+
/**
69+
* @brief Set the LEDC clock source.
70+
*
71+
* @return true if LEDC clock source was successfully set, false otherwise.
72+
*/
73+
bool ledcWriteClockSource(ledc_clk_cfg_t source);
74+
6075
/**
6176
* @brief Attach a pin to the LEDC driver, with a given frequency and resolution.
6277
* Channel is automatically assigned.

0 commit comments

Comments
 (0)