7#include <driver/gpio.h>
8#include <driver/ledc.h>
10#include <esp_idf_version.h>
11#include <esp_private/periph_ctrl.h>
12#include <hal/ledc_ll.h>
14#define CLOCK_FREQUENCY 80e6f
16#ifdef SOC_LEDC_SUPPORT_APB_CLOCK
17#define DEFAULT_CLK LEDC_USE_APB_CLK
19#define DEFAULT_CLK LEDC_AUTO_CLK
22static const uint8_t SETUP_ATTEMPT_COUNT_MAX = 5;
27static bool ledc_peripheral_reset_done =
false;
29static const int MAX_RES_BITS = LEDC_TIMER_BIT_MAX - 1;
30#if SOC_LEDC_SUPPORT_HS_MODE
32inline ledc_mode_t
get_speed_mode(uint8_t channel) {
return channel < 8 ? LEDC_HIGH_SPEED_MODE : LEDC_LOW_SPEED_MODE; }
37inline ledc_mode_t
get_speed_mode(uint8_t) {
return LEDC_LOW_SPEED_MODE; }
40#if defined(USE_LEDC_DITHERING) || !defined(SOC_LEDC_SUPPORT_FADE_STOP)
41static inline ledc_dev_t *ledc_get_hw() {
42#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 1, 0)
43 return LEDC_LL_GET_HW(0);
45 return LEDC_LL_GET_HW();
50#ifdef USE_LEDC_DITHERING
51static void ledc_set_duty_fixed_point(ledc_mode_t speed_mode, ledc_channel_t chan_num,
uint32_t duty) {
52 auto *hw = ledc_get_hw();
54#if defined(USE_ESP32_VARIANT_ESP32C5) || defined(USE_ESP32_VARIANT_ESP32C61) || defined(USE_ESP32_VARIANT_ESP32P4) || \
55 defined(USE_ESP32_VARIANT_ESP32S31)
56 hw->channel_group[speed_mode].channel[chan_num].duty_init.duty = duty;
58 hw->channel_group[speed_mode].channel[chan_num].duty.duty = duty;
63#if !defined(SOC_LEDC_SUPPORT_FADE_STOP)
71#if defined(CONFIG_IDF_TARGET_ESP32)
76 "LEDC duty_start pending check assumes classic ESP32 register layout; "
77 "re-evaluate for this target");
79static bool ledc_duty_update_pending(ledc_mode_t speed_mode, ledc_channel_t chan_num) {
80 auto *hw = ledc_get_hw();
81 return hw->channel_group[speed_mode].channel[chan_num].conf1.duty_start != 0;
86 return static_cast<float>(CLOCK_FREQUENCY) /
static_cast<float>(1 << bit_depth);
90 const float max_div_num = ((1 << MAX_RES_BITS) - 1) / (low_frequency ? 32.0f : 256.0f);
91 return static_cast<float>(CLOCK_FREQUENCY) / (max_div_num *
static_cast<float>(1 << bit_depth));
95 ESP_LOGV(TAG,
"Calculating resolution bit-depth for frequency %f",
frequency);
96 for (
int i = MAX_RES_BITS; i >= 1; i--) {
100 ESP_LOGV(TAG,
"Resolution calculated as %d", i);
108 uint8_t channel, uint8_t &bit_depth,
float frequency) {
110 bit_depth = bit_depth_opt.value_or(0);
112 ESP_LOGE(TAG,
"Frequency %f can't be achieved with any bit depth",
frequency);
115 ledc_timer_config_t timer_conf{};
116 timer_conf.speed_mode = speed_mode;
117 timer_conf.duty_resolution =
static_cast<ledc_timer_bit_t
>(bit_depth);
118 timer_conf.timer_num = timer_num;
120 timer_conf.clk_cfg = DEFAULT_CLK;
123 int attempt_count_max = SETUP_ATTEMPT_COUNT_MAX;
124 esp_err_t init_result = ESP_FAIL;
125 while (attempt_count_max > 0 && init_result != ESP_OK) {
126 init_result = ledc_timer_config(&timer_conf);
127 if (init_result != ESP_OK) {
128 ESP_LOGW(TAG,
"Unable to initialize timer with frequency %.1f and bit depth of %u",
frequency, bit_depth);
129 if (bit_depth <= 1) {
133 timer_conf.duty_resolution =
static_cast<ledc_timer_bit_t
>(--bit_depth);
142 return static_cast<int>(angle * ((1U << bit_depth) - 1) / 360.0f);
147 ESP_LOGW(TAG,
"Not yet initialized");
152 state = 1.0f -
state;
161 uint8_t fractional_bits = 0;
162#ifdef USE_LEDC_DITHERING
173 ESP_LOGV(TAG,
"Setting duty: %" PRIu32
" on channel %u", duty, this->
channel_);
175 auto chan_num =
static_cast<ledc_channel_t
>(this->
channel_ % 8);
178 if (duty == max_duty) {
179 err = ledc_stop(speed_mode, chan_num, 1);
181#if !defined(SOC_LEDC_SUPPORT_FADE_STOP)
182 if (ledc_duty_update_pending(speed_mode, chan_num)) {
183 ESP_LOGV(TAG,
"Deferring duty update on channel %u while previous duty_start is still set", this->
channel_);
190 err = ledc_set_duty_with_hpoint(speed_mode, chan_num, duty >> fractional_bits, hpoint);
191#ifdef USE_LEDC_DITHERING
194 ledc_set_duty_fixed_point(speed_mode, chan_num, duty);
198 err = ledc_update_duty(speed_mode, chan_num);
202 ESP_LOGW(TAG,
"Setting duty on channel %u failed: %s", this->
channel_, esp_err_to_name(err));
215 if (!ledc_peripheral_reset_done) {
216 ESP_LOGV(TAG,
"Resetting LEDC peripheral to clear stale state after reboot");
218#if !defined(CLANG_TIDY)
219#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 1, 0)
220 PERIPH_RCC_ATOMIC() { ledc_ll_reset_register(0); }
221#elif ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0) || \
222 (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0) && !defined(__PERIPH_CTRL_ALLOW_LEGACY_API))
224 PERIPH_RCC_ATOMIC() {
225 ledc_ll_enable_reset_reg(
true);
226 ledc_ll_enable_reset_reg(
false);
229 periph_module_reset(PERIPH_LEDC_MODULE);
232 ledc_peripheral_reset_done =
true;
236 auto timer_num =
static_cast<ledc_timer_t
>((this->
channel_ % 8) / 2);
237 auto chan_num =
static_cast<ledc_channel_t
>(this->
channel_ % 8);
239 esp_err_t timer_init_result =
242 if (timer_init_result != ESP_OK) {
243 ESP_LOGE(TAG,
"Frequency %f can't be achieved with computed bit depth %u", this->
frequency_, this->
bit_depth_);
249 ESP_LOGV(TAG,
"Configured frequency %f with bit depth %u, angle %.1f° hpoint %u", this->
frequency_, this->
bit_depth_,
252 ledc_channel_config_t chan_conf{};
253 chan_conf.gpio_num =
static_cast<gpio_num_t
>(this->
pin_->
get_pin());
254 chan_conf.speed_mode = speed_mode;
255 chan_conf.channel = chan_num;
256#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(6, 0, 0)
257 chan_conf.intr_type = LEDC_INTR_DISABLE;
259 chan_conf.timer_sel = timer_num;
263 chan_conf.hpoint = hpoint;
264 ledc_channel_config(&chan_conf);
273 " PWM Frequency: %.1f Hz\n"
274 " Phase angle: %.1f°\n"
277#ifdef USE_LEDC_DITHERING
278 ESP_LOGCONFIG(TAG,
" Hardware dithering: %u fractional bits", this->
dithering_ ? 4 : 0);
280 LOG_PIN(
" Pin ", this->
pin_);
282 " Max frequency for bit depth: %f\n"
283 " Min frequency for bit depth: %f\n"
284 " Max frequency for bit depth-1: %f\n"
285 " Min frequency for bit depth-1: %f\n"
286 " Max frequency for bit depth+1: %f\n"
287 " Min frequency for bit depth+1: %f\n"
288 " Max res bits: %d\n"
289 " Clock frequency: %f",
301 if (!bit_depth_opt.has_value()) {
302 ESP_LOGE(TAG,
"Frequency %f can't be achieved with any bit depth", this->
frequency_);
309 ESP_LOGW(TAG,
"Not yet initialized");
314 auto timer_num =
static_cast<ledc_timer_t
>((this->
channel_ % 8) / 2);
315 auto chan_num =
static_cast<ledc_channel_t
>(this->
channel_ % 8);
317 esp_err_t timer_init_result =
320 if (timer_init_result != ESP_OK) {
321 ESP_LOGE(TAG,
"Frequency %f can't be achieved with computed bit depth %u", this->
frequency_, this->
bit_depth_);
void status_set_warning()
void status_clear_error()
void enable_loop()
Enable this component's loop.
bool status_has_warning() const
void disable_loop()
Disable this component's loop.
void status_clear_warning()
void update_frequency(float frequency) override
Dynamically change frequency at runtime.
void setup() override
Setup LEDC.
void write_state(float state) override
Override FloatOutput's write_state.
void dump_config() override
uint32_t full_scale_ticks(uint8_t bit_depth, uint8_t fractional_bits)
Ticks for a constant high output, one past the highest PWM level.
esp_err_t configure_timer_frequency(ledc_mode_t speed_mode, ledc_timer_t timer_num, ledc_channel_t chan_num, uint8_t channel, uint8_t &bit_depth, float frequency)
uint8_t next_ledc_channel
optional< uint8_t > ledc_bit_depth_for_frequency(float frequency)
constexpr int ledc_angle_to_htop(float angle, uint8_t bit_depth)
float ledc_min_frequency_for_bit_depth(uint8_t bit_depth, bool low_frequency)
ledc_mode_t get_speed_mode(uint8_t channel)
uint32_t duty_to_ticks(float state, uint8_t bit_depth, uint8_t fractional_bits)
Convert a 0-1 duty to timer ticks, with fractional_bits extra bits for dithering.
ESPHOME_LOG_TAG(TAG, "ledc.output")
float ledc_max_frequency_for_bit_depth(uint8_t bit_depth)