18 ESP32PWM(
int gpio_num, ledc_channel_t channel, ledc_timer_t timer = LEDC_TIMER_0,
19 ledc_timer_bit_t resolution =
static_cast<ledc_timer_bit_t
>(
20 static_cast<uint8_t
>(LEDC_TIMER_BIT_MAX) - 1))
21 : gpio_num_(gpio_num),
24 speed_mode_(LEDC_LOW_SPEED_MODE),
25 resolution_(resolution),
26 max_duty_((1U << static_cast<uint8_t>(resolution_)) - 1U)
28 ledc_hal_init(&hal_, speed_mode_);
30 ledc_channel_config_t channel_conf = {};
31 channel_conf.gpio_num = gpio_num_;
32 channel_conf.speed_mode = speed_mode_;
33 channel_conf.channel = channel_;
34 channel_conf.intr_type = LEDC_INTR_DISABLE;
35 channel_conf.timer_sel = timer_;
36 channel_conf.duty = 0;
37 channel_conf.hpoint = 0;
39 const esp_err_t err = ledc_channel_config(&channel_conf);
71 for (
int bits =
static_cast<int>(resolution_);
72 bits >=
static_cast<int>(LEDC_TIMER_1_BIT); --bits)
74 ledc_timer_config_t timer_conf = {};
75 timer_conf.speed_mode = speed_mode_;
76 timer_conf.duty_resolution =
static_cast<ledc_timer_bit_t
>(bits);
77 timer_conf.timer_num = timer_;
79 timer_conf.clk_cfg = LEDC_AUTO_CLK;
81 if (ledc_timer_config(&timer_conf) != ESP_OK)
86 resolution_ =
static_cast<ledc_timer_bit_t
>(bits);
87 ledc_hal_bind_channel_timer(&hal_, channel_, timer_);
88 ledc_hal_ls_timer_update(&hal_, timer_);
89 max_duty_ = (1U <<
static_cast<uint8_t
>(resolution_)) - 1U;