ESPHome  2023.5.4
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 #ifdef USE_ARDUINO
7 #include <esp32-hal-ledc.h>
8 #endif
9 #include <driver/ledc.h>
10 
11 #define CLOCK_FREQUENCY 80e6f
12 
13 #ifdef USE_ARDUINO
14 #ifdef SOC_LEDC_SUPPORT_XTAL_CLOCK
15 #undef CLOCK_FREQUENCY
16 // starting with ESP32 Arduino 2.0.2, the 40MHz crystal is used as clock by default if supported
17 #define CLOCK_FREQUENCY 40e6f
18 #endif
19 #else
20 #define DEFAULT_CLK LEDC_USE_APB_CLK
21 #endif
22 
23 static const uint8_t SETUP_ATTEMPT_COUNT_MAX = 5;
24 
25 namespace esphome {
26 namespace ledc {
27 
28 static const char *const TAG = "ledc.output";
29 
30 static const int MAX_RES_BITS = LEDC_TIMER_BIT_MAX - 1;
31 #ifdef USE_ESP_IDF
32 #if SOC_LEDC_SUPPORT_HS_MODE
33 // Only ESP32 has LEDC_HIGH_SPEED_MODE
34 inline ledc_mode_t get_speed_mode(uint8_t channel) { return channel < 8 ? LEDC_HIGH_SPEED_MODE : LEDC_LOW_SPEED_MODE; }
35 #else
36 // S2, C3, S3 only support LEDC_LOW_SPEED_MODE
37 // See
38 // https://docs.espressif.com/projects/esp-idf/en/latest/esp32c3/api-reference/peripherals/ledc.html#functionality-overview
39 inline ledc_mode_t get_speed_mode(uint8_t) { return LEDC_LOW_SPEED_MODE; }
40 #endif
41 #endif
42 
43 float ledc_max_frequency_for_bit_depth(uint8_t bit_depth) { return CLOCK_FREQUENCY / float(1 << bit_depth); }
44 
45 float ledc_min_frequency_for_bit_depth(uint8_t bit_depth, bool low_frequency) {
46  const float max_div_num = ((1 << MAX_RES_BITS) - 1) / (low_frequency ? 32.0f : 256.0f);
47  return CLOCK_FREQUENCY / (max_div_num * float(1 << bit_depth));
48 }
49 
51  ESP_LOGD(TAG, "Calculating resolution bit-depth for frequency %f", frequency);
52  for (int i = MAX_RES_BITS; i >= 1; i--) {
53  const float min_frequency = ledc_min_frequency_for_bit_depth(i, (frequency < 100));
54  const float max_frequency = ledc_max_frequency_for_bit_depth(i);
55  if (min_frequency <= frequency && frequency <= max_frequency) {
56  ESP_LOGD(TAG, "Resolution calculated as %d", i);
57  return i;
58  }
59  }
60  return {};
61 }
62 
63 #ifdef USE_ESP_IDF
64 esp_err_t configure_timer_frequency(ledc_mode_t speed_mode, ledc_timer_t timer_num, ledc_channel_t chan_num,
65  uint8_t channel, uint8_t &bit_depth, float frequency) {
66  bit_depth = *ledc_bit_depth_for_frequency(frequency);
67  if (bit_depth < 1) {
68  ESP_LOGE(TAG, "Frequency %f can't be achieved with any bit depth", frequency);
69  }
70 
71  ledc_timer_config_t timer_conf{};
72  timer_conf.speed_mode = speed_mode;
73  timer_conf.duty_resolution = static_cast<ledc_timer_bit_t>(bit_depth);
74  timer_conf.timer_num = timer_num;
75  timer_conf.freq_hz = (uint32_t) frequency;
76  timer_conf.clk_cfg = DEFAULT_CLK;
77 
78  // Configure the time with fallback in case of error
79  int attempt_count_max = SETUP_ATTEMPT_COUNT_MAX;
80  esp_err_t init_result = ESP_FAIL;
81  while (attempt_count_max > 0 && init_result != ESP_OK) {
82  init_result = ledc_timer_config(&timer_conf);
83  if (init_result != ESP_OK) {
84  ESP_LOGW(TAG, "Unable to initialize timer with frequency %.1f and bit depth of %u", frequency, bit_depth);
85  // try again with a lower bit depth
86  timer_conf.duty_resolution = static_cast<ledc_timer_bit_t>(--bit_depth);
87  }
88  attempt_count_max--;
89  }
90 
91  return init_result;
92 }
93 #endif
94 
96  if (!initialized_) {
97  ESP_LOGW(TAG, "LEDC output hasn't been initialized yet!");
98  return;
99  }
100 
101  if (this->pin_->is_inverted())
102  state = 1.0f - state;
103 
104  this->duty_ = state;
105  const uint32_t max_duty = (uint32_t(1) << this->bit_depth_) - 1;
106  const float duty_rounded = roundf(state * max_duty);
107  auto duty = static_cast<uint32_t>(duty_rounded);
108 
109 #ifdef USE_ARDUINO
110  ESP_LOGV(TAG, "Setting duty: %u on channel %u", duty, this->channel_);
111  ledcWrite(this->channel_, duty);
112 #endif
113 #ifdef USE_ESP_IDF
114  auto speed_mode = get_speed_mode(channel_);
115  auto chan_num = static_cast<ledc_channel_t>(channel_ % 8);
116  ledc_set_duty(speed_mode, chan_num, duty);
117  ledc_update_duty(speed_mode, chan_num);
118 #endif
119 }
120 
122  ESP_LOGV(TAG, "Entering setup...");
123 #ifdef USE_ARDUINO
124  this->update_frequency(this->frequency_);
125  this->turn_off();
126  // Attach pin after setting default value
127  ledcAttachPin(this->pin_->get_pin(), this->channel_);
128 #endif
129 #ifdef USE_ESP_IDF
130  auto speed_mode = get_speed_mode(channel_);
131  auto timer_num = static_cast<ledc_timer_t>((channel_ % 8) / 2);
132  auto chan_num = static_cast<ledc_channel_t>(channel_ % 8);
133 
134  esp_err_t timer_init_result =
135  configure_timer_frequency(speed_mode, timer_num, chan_num, this->channel_, this->bit_depth_, this->frequency_);
136 
137  if (timer_init_result != ESP_OK) {
138  ESP_LOGE(TAG, "Frequency %f can't be achieved with computed bit depth %u", this->frequency_, this->bit_depth_);
139  this->status_set_error();
140  return;
141  }
142 
143  ESP_LOGV(TAG, "Configured frequency %f with a bit depth of %u bits", this->frequency_, this->bit_depth_);
144 
145  ledc_channel_config_t chan_conf{};
146  chan_conf.gpio_num = pin_->get_pin();
147  chan_conf.speed_mode = speed_mode;
148  chan_conf.channel = chan_num;
149  chan_conf.intr_type = LEDC_INTR_DISABLE;
150  chan_conf.timer_sel = timer_num;
151  chan_conf.duty = inverted_ == pin_->is_inverted() ? 0 : (1U << bit_depth_);
152  chan_conf.hpoint = 0;
153  ledc_channel_config(&chan_conf);
154  initialized_ = true;
155  this->status_clear_error();
156 #endif
157 }
158 
160  ESP_LOGCONFIG(TAG, "LEDC Output:");
161  LOG_PIN(" Pin ", this->pin_);
162  ESP_LOGCONFIG(TAG, " LEDC Channel: %u", this->channel_);
163  ESP_LOGCONFIG(TAG, " PWM Frequency: %.1f Hz", this->frequency_);
164  ESP_LOGCONFIG(TAG, " Bit depth: %u", this->bit_depth_);
165  ESP_LOGV(TAG, " Max frequency for bit depth: %f", ledc_max_frequency_for_bit_depth(this->bit_depth_));
166  ESP_LOGV(TAG, " Min frequency for bit depth: %f",
168  ESP_LOGV(TAG, " Max frequency for bit depth-1: %f", ledc_max_frequency_for_bit_depth(this->bit_depth_ - 1));
169  ESP_LOGV(TAG, " Min frequency for bit depth-1: %f",
170  ledc_min_frequency_for_bit_depth(this->bit_depth_ - 1, (this->frequency_ < 100)));
171  ESP_LOGV(TAG, " Max frequency for bit depth+1: %f", ledc_max_frequency_for_bit_depth(this->bit_depth_ + 1));
172  ESP_LOGV(TAG, " Min frequency for bit depth+1: %f",
173  ledc_min_frequency_for_bit_depth(this->bit_depth_ + 1, (this->frequency_ < 100)));
174  ESP_LOGV(TAG, " Max res bits: %d", MAX_RES_BITS);
175  ESP_LOGV(TAG, " Clock frequency: %f", CLOCK_FREQUENCY);
176 }
177 
179  auto bit_depth_opt = ledc_bit_depth_for_frequency(frequency);
180  if (!bit_depth_opt.has_value()) {
181  ESP_LOGE(TAG, "Frequency %f can't be achieved with any bit depth", this->frequency_);
182  this->status_set_error();
183  }
184  this->bit_depth_ = bit_depth_opt.value_or(8);
185  this->frequency_ = frequency;
186 #ifdef USE_ARDUINO
187  ESP_LOGV(TAG, "Using Arduino API - Trying to define channel, frequency and bit depth...");
188  u_int32_t configured_frequency = 0;
189 
190  // Configure LEDC channel, frequency and bit depth with fallback
191  int attempt_count_max = SETUP_ATTEMPT_COUNT_MAX;
192  while (attempt_count_max > 0 && configured_frequency == 0) {
193  ESP_LOGV(TAG, "Trying initialize channel %u with frequency %.1f and bit depth of %u...", this->channel_,
194  this->frequency_, this->bit_depth_);
195  configured_frequency = ledcSetup(this->channel_, frequency, this->bit_depth_);
196  if (configured_frequency != 0) {
197  initialized_ = true;
198  this->status_clear_error();
199  ESP_LOGV(TAG, "Configured frequency: %u with bit depth: %u", configured_frequency, this->bit_depth_);
200  } else {
201  ESP_LOGW(TAG, "Unable to initialize channel %u with frequency %.1f and bit depth of %u", this->channel_,
202  this->frequency_, this->bit_depth_);
203  // try again with a lower bit depth
204  this->bit_depth_--;
205  }
206  attempt_count_max--;
207  }
208 
209  if (configured_frequency == 0) {
210  ESP_LOGE(TAG, "Permanently failed to initialize channel %u with frequency %.1f and bit depth of %u", this->channel_,
211  this->frequency_, this->bit_depth_);
212  this->status_set_error();
213  return;
214  }
215 
216 #endif // USE_ARDUINO
217 #ifdef USE_ESP_IDF
218  if (!initialized_) {
219  ESP_LOGW(TAG, "LEDC output hasn't been initialized yet!");
220  return;
221  }
222 
223  auto speed_mode = get_speed_mode(channel_);
224  auto timer_num = static_cast<ledc_timer_t>((channel_ % 8) / 2);
225  auto chan_num = static_cast<ledc_channel_t>(channel_ % 8);
226 
227  esp_err_t timer_init_result =
228  configure_timer_frequency(speed_mode, timer_num, chan_num, this->channel_, this->bit_depth_, this->frequency_);
229 
230  if (timer_init_result != ESP_OK) {
231  ESP_LOGE(TAG, "Frequency %f can't be achieved with computed bit depth %u", this->frequency_, this->bit_depth_);
232  this->status_set_error();
233  return;
234  }
235 
236  this->status_clear_error();
237 #endif
238  // re-apply duty
239  this->write_state(this->duty_);
240 }
241 
242 uint8_t next_ledc_channel = 0; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
243 
244 } // namespace ledc
245 } // namespace esphome
246 
247 #endif
virtual void turn_off()
Disable this binary output.
Definition: binary_output.h:51
uint8_t next_ledc_channel
void update_frequency(float frequency) override
Dynamically change frequency at runtime.
virtual uint8_t get_pin() const =0
ledc_mode_t get_speed_mode(uint8_t channel)
Definition: ledc_output.cpp:34
void write_state(float state) override
Override FloatOutput&#39;s write_state.
Definition: ledc_output.cpp:95
uint16_le_t frequency
Definition: bl0942.h:21
void status_clear_error()
Definition: component.cpp:154
void setup() override
Setup LEDC.
void dump_config() override
float ledc_min_frequency_for_bit_depth(uint8_t bit_depth, bool low_frequency)
Definition: ledc_output.cpp:45
optional< uint8_t > ledc_bit_depth_for_frequency(float frequency)
Definition: ledc_output.cpp:50
InternalGPIOPin * pin_
Definition: ledc_output.h:35
Definition: a4988.cpp:4
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)
Definition: ledc_output.cpp:64
float ledc_max_frequency_for_bit_depth(uint8_t bit_depth)
Definition: ledc_output.cpp:43
virtual bool is_inverted() const =0
bool state
Definition: fan.h:34