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