Skip to content

feat(LEDC): Adds the ability to set the clock source for the LEDC #10171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions cores/esp32/esp32-hal-ledc.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ ledc_periph_t ledc_handle = {0};

static bool fade_initialized = false;

ledc_clk_cfg_t clock_source = LEDC_DEFAULT_CLK;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be static

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you!


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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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!");
Expand Down Expand Up @@ -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!");
Expand Down
15 changes: 15 additions & 0 deletions cores/esp32/esp32-hal-ledc.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extern "C" {
#include <stdbool.h>
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "hal/ledc_types.h"

typedef enum {
NOTE_C,
Expand Down Expand Up @@ -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.
Expand Down