58 uint8_t channel, uint8_t &bit_depth,
float frequency) {
61 ESP_LOGE(TAG,
"Frequency %f can't be achieved with any bit depth",
frequency);
64 ledc_timer_config_t timer_conf{};
65 timer_conf.speed_mode = speed_mode;
66 timer_conf.duty_resolution =
static_cast<ledc_timer_bit_t
>(bit_depth);
67 timer_conf.timer_num = timer_num;
68 timer_conf.freq_hz = (uint32_t)
frequency;
69 timer_conf.clk_cfg = DEFAULT_CLK;
72 int attempt_count_max = SETUP_ATTEMPT_COUNT_MAX;
73 esp_err_t init_result = ESP_FAIL;
74 while (attempt_count_max > 0 && init_result != ESP_OK) {
75 init_result = ledc_timer_config(&timer_conf);
76 if (init_result != ESP_OK) {
77 ESP_LOGW(TAG,
"Unable to initialize timer with frequency %.1f and bit depth of %u",
frequency, bit_depth);
79 timer_conf.duty_resolution =
static_cast<ledc_timer_bit_t
>(--bit_depth);
93 ESP_LOGW(TAG,
"Not yet initialized");
101 const uint32_t max_duty = (uint32_t(1) << this->
bit_depth_) - 1;
102 const float duty_rounded = roundf(
state * max_duty);
103 auto duty =
static_cast<uint32_t
>(duty_rounded);
104 ESP_LOGV(TAG,
"Setting duty: %" PRIu32
" on channel %u", duty, this->
channel_);
106 auto chan_num =
static_cast<ledc_channel_t
>(this->
channel_ % 8);
108 if (duty == max_duty) {
109 ledc_stop(speed_mode, chan_num, 1);
110 }
else if (duty == 0) {
111 ledc_stop(speed_mode, chan_num, 0);
113 ledc_set_duty_with_hpoint(speed_mode, chan_num, duty, hpoint);
114 ledc_update_duty(speed_mode, chan_num);
120 auto timer_num =
static_cast<ledc_timer_t
>((this->
channel_ % 8) / 2);
121 auto chan_num =
static_cast<ledc_channel_t
>(this->
channel_ % 8);
123 esp_err_t timer_init_result =
126 if (timer_init_result != ESP_OK) {
127 ESP_LOGE(TAG,
"Frequency %f can't be achieved with computed bit depth %u", this->
frequency_, this->
bit_depth_);
133 ESP_LOGV(TAG,
"Configured frequency %f with a bit depth of %u bits", this->
frequency_, this->
bit_depth_);
134 ESP_LOGV(TAG,
"Angle of %.1f° results in hpoint %u", this->
phase_angle_, hpoint);
136 ledc_channel_config_t chan_conf{};
138 chan_conf.speed_mode = speed_mode;
139 chan_conf.channel = chan_num;
140 chan_conf.intr_type = LEDC_INTR_DISABLE;
141 chan_conf.timer_sel = timer_num;
143 chan_conf.hpoint = hpoint;
144 ledc_channel_config(&chan_conf);