ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
ledc_output.cpp
Go to the documentation of this file.
1#include "ledc_output.h"
2#include "esphome/core/log.h"
3
4#ifdef USE_ESP32
5
6#include <driver/gpio.h>
7#include <driver/ledc.h>
8#include <cinttypes>
9#include <esp_idf_version.h>
10#include <esp_private/periph_ctrl.h>
11#include <hal/ledc_ll.h>
12
13#define CLOCK_FREQUENCY 80e6f
14
15#ifdef SOC_LEDC_SUPPORT_APB_CLOCK
16#define DEFAULT_CLK LEDC_USE_APB_CLK
17#else
18#define DEFAULT_CLK LEDC_AUTO_CLK
19#endif
20
21static const uint8_t SETUP_ATTEMPT_COUNT_MAX = 5;
22
23namespace esphome::ledc {
24
25static const char *const TAG = "ledc.output";
26static bool ledc_peripheral_reset_done = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
27
28static const int MAX_RES_BITS = LEDC_TIMER_BIT_MAX - 1;
29#if SOC_LEDC_SUPPORT_HS_MODE
30// Only ESP32 has LEDC_HIGH_SPEED_MODE
31inline ledc_mode_t get_speed_mode(uint8_t channel) { return channel < 8 ? LEDC_HIGH_SPEED_MODE : LEDC_LOW_SPEED_MODE; }
32#else
33// S2, C3, S3 only support LEDC_LOW_SPEED_MODE
34// See
35// https://docs.espressif.com/projects/esp-idf/en/latest/esp32c3/api-reference/peripherals/ledc.html#functionality-overview
36inline ledc_mode_t get_speed_mode(uint8_t) { return LEDC_LOW_SPEED_MODE; }
37#endif
38
39#if !defined(SOC_LEDC_SUPPORT_FADE_STOP)
40// Classic ESP32 (currently the only target without SOC_LEDC_SUPPORT_FADE_STOP) can block in
41// ledc_ll_set_duty_start() while duty_start is set. We check the same conf1.duty_start bit here
42// to defer updates and avoid entering IDF's unbounded wait loop.
43//
44// This intentionally depends on the classic ESP32 LEDC register layout used by IDF's own LL HAL.
45// If another target without SOC_LEDC_SUPPORT_FADE_STOP is introduced, revisit this helper.
46static_assert(
47#if defined(CONFIG_IDF_TARGET_ESP32)
48 true,
49#else
50 false,
51#endif
52 "LEDC duty_start pending check assumes classic ESP32 register layout; "
53 "re-evaluate for this target");
54
55static bool ledc_duty_update_pending(ledc_mode_t speed_mode, ledc_channel_t chan_num) {
56#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 1, 0)
57 auto *hw = LEDC_LL_GET_HW(0);
58#else
59 auto *hw = LEDC_LL_GET_HW();
60#endif
61 return hw->channel_group[speed_mode].channel[chan_num].conf1.duty_start != 0;
62}
63#endif
64
65float ledc_max_frequency_for_bit_depth(uint8_t bit_depth) {
66 return static_cast<float>(CLOCK_FREQUENCY) / static_cast<float>(1 << bit_depth);
67}
68
69float ledc_min_frequency_for_bit_depth(uint8_t bit_depth, bool low_frequency) {
70 const float max_div_num = ((1 << MAX_RES_BITS) - 1) / (low_frequency ? 32.0f : 256.0f);
71 return static_cast<float>(CLOCK_FREQUENCY) / (max_div_num * static_cast<float>(1 << bit_depth));
72}
73
74optional<uint8_t> ledc_bit_depth_for_frequency(float frequency) {
75 ESP_LOGV(TAG, "Calculating resolution bit-depth for frequency %f", frequency);
76 for (int i = MAX_RES_BITS; i >= 1; i--) {
77 const float min_frequency = ledc_min_frequency_for_bit_depth(i, (frequency < 100));
78 const float max_frequency = ledc_max_frequency_for_bit_depth(i);
79 if (min_frequency <= frequency && frequency <= max_frequency) {
80 ESP_LOGV(TAG, "Resolution calculated as %d", i);
81 return i;
82 }
83 }
84 return {};
85}
86
87esp_err_t configure_timer_frequency(ledc_mode_t speed_mode, ledc_timer_t timer_num, ledc_channel_t chan_num,
88 uint8_t channel, uint8_t &bit_depth, float frequency) {
89 auto bit_depth_opt = ledc_bit_depth_for_frequency(frequency);
90 bit_depth = bit_depth_opt.value_or(0);
91 if (bit_depth < 1) {
92 ESP_LOGE(TAG, "Frequency %f can't be achieved with any bit depth", frequency);
93 }
94
95 ledc_timer_config_t timer_conf{};
96 timer_conf.speed_mode = speed_mode;
97 timer_conf.duty_resolution = static_cast<ledc_timer_bit_t>(bit_depth);
98 timer_conf.timer_num = timer_num;
99 timer_conf.freq_hz = (uint32_t) frequency;
100 timer_conf.clk_cfg = DEFAULT_CLK;
101
102 // Configure the time with fallback in case of error
103 int attempt_count_max = SETUP_ATTEMPT_COUNT_MAX;
104 esp_err_t init_result = ESP_FAIL;
105 while (attempt_count_max > 0 && init_result != ESP_OK) {
106 init_result = ledc_timer_config(&timer_conf);
107 if (init_result != ESP_OK) {
108 ESP_LOGW(TAG, "Unable to initialize timer with frequency %.1f and bit depth of %u", frequency, bit_depth);
109 if (bit_depth <= 1) {
110 break;
111 }
112 // try again with a lower bit depth
113 timer_conf.duty_resolution = static_cast<ledc_timer_bit_t>(--bit_depth);
114 }
115 attempt_count_max--;
116 }
117
118 return init_result;
119}
120
121constexpr int ledc_angle_to_htop(float angle, uint8_t bit_depth) {
122 return static_cast<int>(angle * ((1U << bit_depth) - 1) / 360.0f);
123}
124
126 if (!this->initialized_) {
127 ESP_LOGW(TAG, "Not yet initialized");
128 return;
129 }
130
131 if (this->pin_->is_inverted())
132 state = 1.0f - state;
133
134 this->duty_ = state;
135 const uint32_t max_duty = (uint32_t(1) << this->bit_depth_) - 1;
136 const float duty_rounded = roundf(state * max_duty);
137 auto duty = static_cast<uint32_t>(duty_rounded);
138 if (duty == this->last_duty_) {
139 return;
140 }
141
142 ESP_LOGV(TAG, "Setting duty: %" PRIu32 " on channel %u", duty, this->channel_);
143 auto speed_mode = get_speed_mode(this->channel_);
144 auto chan_num = static_cast<ledc_channel_t>(this->channel_ % 8);
145 int hpoint = ledc_angle_to_htop(this->phase_angle_, this->bit_depth_);
146 if (duty == max_duty) {
147 ledc_stop(speed_mode, chan_num, 1);
148 this->last_duty_ = duty;
149 } else if (duty == 0) {
150 ledc_stop(speed_mode, chan_num, 0);
151 this->last_duty_ = duty;
152 } else {
153#if !defined(SOC_LEDC_SUPPORT_FADE_STOP)
154 if (ledc_duty_update_pending(speed_mode, chan_num)) {
155 ESP_LOGV(TAG, "Skipping LEDC duty update on channel %u while previous duty_start is still set", this->channel_);
156 return;
157 }
158#endif
159 ledc_set_duty_with_hpoint(speed_mode, chan_num, duty, hpoint);
160 ledc_update_duty(speed_mode, chan_num);
161 this->last_duty_ = duty;
162 }
163}
164
166 if (!ledc_peripheral_reset_done) {
167 ESP_LOGV(TAG, "Resetting LEDC peripheral to clear stale state after reboot");
168 // Skip under clang-tidy: the inlined HAL MMIO writes trip clang-analyzer-core.FixedAddressDereference
169#if !defined(CLANG_TIDY)
170#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 1, 0)
171 PERIPH_RCC_ATOMIC() { ledc_ll_reset_register(0); }
172#elif ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
173 PERIPH_RCC_ATOMIC() {
174 ledc_ll_enable_reset_reg(true);
175 ledc_ll_enable_reset_reg(false);
176 }
177#else
178 periph_module_reset(PERIPH_LEDC_MODULE);
179#endif
180#endif
181 ledc_peripheral_reset_done = true;
182 }
183
184 auto speed_mode = get_speed_mode(this->channel_);
185 auto timer_num = static_cast<ledc_timer_t>((this->channel_ % 8) / 2);
186 auto chan_num = static_cast<ledc_channel_t>(this->channel_ % 8);
187
188 esp_err_t timer_init_result =
189 configure_timer_frequency(speed_mode, timer_num, chan_num, this->channel_, this->bit_depth_, this->frequency_);
190
191 if (timer_init_result != ESP_OK) {
192 ESP_LOGE(TAG, "Frequency %f can't be achieved with computed bit depth %u", this->frequency_, this->bit_depth_);
193 this->status_set_error();
194 return;
195 }
196 int hpoint = ledc_angle_to_htop(this->phase_angle_, this->bit_depth_);
197
198 ESP_LOGV(TAG, "Configured frequency %f with bit depth %u, angle %.1f° hpoint %u", this->frequency_, this->bit_depth_,
199 this->phase_angle_, hpoint);
200
201 ledc_channel_config_t chan_conf{};
202 chan_conf.gpio_num = static_cast<gpio_num_t>(this->pin_->get_pin());
203 chan_conf.speed_mode = speed_mode;
204 chan_conf.channel = chan_num;
205#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(6, 0, 0)
206 chan_conf.intr_type = LEDC_INTR_DISABLE;
207#endif
208 chan_conf.timer_sel = timer_num;
209 chan_conf.duty = this->inverted_ == this->pin_->is_inverted() ? 0 : (1U << this->bit_depth_);
210 chan_conf.hpoint = hpoint;
211 ledc_channel_config(&chan_conf);
212 this->initialized_ = true;
213 this->status_clear_error();
214}
215
217 ESP_LOGCONFIG(TAG,
218 "Output:\n"
219 " Channel: %u\n"
220 " PWM Frequency: %.1f Hz\n"
221 " Phase angle: %.1f°\n"
222 " Bit depth: %u",
223 this->channel_, this->frequency_, this->phase_angle_, this->bit_depth_);
224 LOG_PIN(" Pin ", this->pin_);
225 ESP_LOGV(TAG,
226 " Max frequency for bit depth: %f\n"
227 " Min frequency for bit depth: %f\n"
228 " Max frequency for bit depth-1: %f\n"
229 " Min frequency for bit depth-1: %f\n"
230 " Max frequency for bit depth+1: %f\n"
231 " Min frequency for bit depth+1: %f\n"
232 " Max res bits: %d\n"
233 " Clock frequency: %f",
239 ledc_min_frequency_for_bit_depth(this->bit_depth_ + 1, (this->frequency_ < 100)), MAX_RES_BITS,
240 CLOCK_FREQUENCY);
241}
242
244 auto bit_depth_opt = ledc_bit_depth_for_frequency(frequency);
245 if (!bit_depth_opt.has_value()) {
246 ESP_LOGE(TAG, "Frequency %f can't be achieved with any bit depth", this->frequency_);
247 this->status_set_error();
248 }
249 this->bit_depth_ = bit_depth_opt.value_or(8);
250 this->frequency_ = frequency;
251
252 if (!this->initialized_) {
253 ESP_LOGW(TAG, "Not yet initialized");
254 return;
255 }
256
257 auto speed_mode = get_speed_mode(this->channel_);
258 auto timer_num = static_cast<ledc_timer_t>((this->channel_ % 8) / 2);
259 auto chan_num = static_cast<ledc_channel_t>(this->channel_ % 8);
260
261 esp_err_t timer_init_result =
262 configure_timer_frequency(speed_mode, timer_num, chan_num, this->channel_, this->bit_depth_, this->frequency_);
263
264 if (timer_init_result != ESP_OK) {
265 ESP_LOGE(TAG, "Frequency %f can't be achieved with computed bit depth %u", this->frequency_, this->bit_depth_);
266 this->status_set_error();
267 return;
268 }
269
270 this->status_clear_error();
271
272 // re-apply duty
273 this->last_duty_ = UINT32_MAX;
274 this->write_state(this->duty_);
275}
276
277uint8_t next_ledc_channel = 0; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
278
279} // namespace esphome::ledc
280
281#endif
uint16_le_t frequency
Definition bl0942.h:6
void status_clear_error()
Definition component.h:295
virtual uint8_t get_pin() const =0
virtual bool is_inverted() const =0
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
InternalGPIOPin * pin_
Definition ledc_output.h:36
bool state
Definition fan.h:2
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)
float ledc_max_frequency_for_bit_depth(uint8_t bit_depth)
static void uint32_t