35#ifndef _DRIVERS_ESP32LEDC_HXX_
36#define _DRIVERS_ESP32LEDC_HXX_
43#include <driver/ledc.h>
45namespace openmrn_arduino
103 const ledc_channel_t first_channel = LEDC_CHANNEL_0,
104 const ledc_timer_bit_t timer_resolution = LEDC_TIMER_12_BIT,
105 const uint32_t timer_hz = 5000,
106 const ledc_timer_t timer_num = LEDC_TIMER_0,
107 const ledc_mode_t timer_mode = LEDC_LOW_SPEED_MODE,
108 const ledc_clk_cfg_t timer_clock = LEDC_AUTO_CLK)
114 pins_.size() <= (LEDC_CHANNEL_MAX - first_channel));
135 "[Esp32Ledc:%d] Configuring timer (resolution:%d, frequency:%"
142 for (uint8_t pin :
pins_)
144 HASSERT(GPIO_IS_VALID_OUTPUT_GPIO(pin));
146 ledc_channel_t led_channel =
148 LOG(
INFO,
"[Esp32Ledc:%d] Configuring LEDC channel %d on GPIO %d",
150 ledc_channel_config_t config;
151 memset(&config, 0,
sizeof(ledc_channel_config_t));
152 config.gpio_num = pin;
154 config.channel = led_channel;
156 ESP_ERROR_CHECK(ledc_channel_config(&config));
157 channels_[count].emplace(
this, led_channel, pin);
186 uint32_t fade_period = 1000,
187 ledc_fade_mode_t fade_mode = LEDC_FADE_NO_WAIT)
190 ledc_channel_t channel =
194 ledc_set_fade_time_and_start(
timerConfig_.speed_mode, channel,
195 target_duty, fade_period, fade_mode));
207 const int INTR_MODE_FLAGS =
208 ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_SHARED;
209 LOG(
VERBOSE,
"[Esp32Ledc] Initializing LED Fade ISR");
210 ESP_ERROR_CHECK(ledc_fade_func_install(INTR_MODE_FLAGS));
234 parent_->
set_duty(channel_, counts);
253 ledc_channel_t channel_;
277 void set_duty(ledc_channel_t channel, uint32_t counts)
280 ledc_set_duty(
timerConfig_.speed_mode, channel, counts));
281 ESP_ERROR_CHECK(ledc_update_duty(
timerConfig_.speed_mode, channel));
296 return ((1 << (uint8_t)
timerConfig_.duty_resolution) - 1);
Abstract interface for a PWM driver.
void set_duty(uint32_t counts) override
Sets the duty cycle.
uint32_t get_period_max() override
Get max period supported.
uint32_t get_period() override
Get PWM period.
uint32_t get_period_min() override
Get min period supported.
void set_period(uint32_t counts) override
Set PWM period.
uint32_t get_duty() override
Gets the duty cycle.
ESP32 LEDC provider for PWM like output on GPIO pins.
void set_period(uint32_t counts)
Set PWM period.
PWM * get_channel(unsigned id)
ledc_timer_config_t timerConfig_
LEDC Timer configuration settings.
uint32_t get_period()
Get PWM period.
Esp32Ledc(const std::initializer_list< uint8_t > &pins, const ledc_channel_t first_channel=LEDC_CHANNEL_0, const ledc_timer_bit_t timer_resolution=LEDC_TIMER_12_BIT, const uint32_t timer_hz=5000, const ledc_timer_t timer_num=LEDC_TIMER_0, const ledc_mode_t timer_mode=LEDC_LOW_SPEED_MODE, const ledc_clk_cfg_t timer_clock=LEDC_AUTO_CLK)
Constructor.
std::vector< uint8_t > pins_
Collection of GPIO pins in use by this Esp32Ledc.
static void ledc_fade_setup()
Static entry point for configuring the LEDC hardware fade controller.
const ledc_channel_t firstChannel_
First LEDC Channel for this Esp32Ledc.
void set_duty(ledc_channel_t channel, uint32_t counts)
Sets the duty cycle.
static pthread_once_t ledcFadeOnce_
Protects the initialization of LEDC Fade ISR hook.
uint32_t get_duty(ledc_channel_t channel)
Gets the duty cycle.
uninitialized< Channel > channels_[LEDC_CHANNEL_MAX]
PWM instances connected to LEDC channels.
uint32_t get_period_max()
Get max period supported by the underlying LEDC timer.
uint32_t get_period_min()
Get min period supported by the underlying LEDC timer.
void fade_channel_over_time(unsigned id, uint32_t target_duty=0, uint32_t fade_period=1000, ledc_fade_mode_t fade_mode=LEDC_FADE_NO_WAIT)
Transitions a PWM output from the current duty to the target duty over the provided time period.
void hw_init()
Initializes the LEDC peripheral.
Template class that allows allocating storage for an object but not calling its constructor.
#define LOG(level, message...)
Conditionally write a message to the logging output.
static const int VERBOSE
Loglevel that is usually not printed, reporting debugging information.
static const int INFO
Loglevel that is printed by default, reporting some status information.
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Removes default copy-constructor and assignment added by C++.