Type-safe PWM controller for ESP32.
More...
|
| output | idfxx::pwm::start (idfxx::gpio gpio, const timer &tmr, enum channel ch) |
| | Starts PWM output on a GPIO pin.
|
| |
| output | idfxx::pwm::start (idfxx::gpio gpio, const timer &tmr, enum channel ch, const output_config &cfg) |
| | Starts PWM output on a GPIO pin with custom configuration.
|
| |
| output | idfxx::pwm::start (idfxx::gpio gpio, const timer::config &cfg) |
| | Starts PWM output with automatic timer and channel allocation.
|
| |
| output | idfxx::pwm::start (idfxx::gpio gpio, const timer::config &cfg, const output_config &out_cfg) |
| | Starts PWM output with automatic allocation and custom output configuration.
|
| |
| void | idfxx::pwm::stop (enum channel ch, enum speed_mode mode=speed_mode::low_speed, idfxx::gpio::level idle_level=idfxx::gpio::level::low) |
| | Stops PWM output on a channel and sets it to an idle level.
|
| |
| result< output > | idfxx::pwm::try_start (idfxx::gpio gpio, const timer &tmr, enum channel ch) |
| | Starts PWM output on a GPIO pin.
|
| |
| result< output > | idfxx::pwm::try_start (idfxx::gpio gpio, const timer &tmr, enum channel ch, const output_config &cfg) |
| | Starts PWM output on a GPIO pin with custom configuration.
|
| |
| result< output > | idfxx::pwm::try_start (idfxx::gpio gpio, const timer::config &cfg) |
| | Starts PWM output with automatic timer and channel allocation.
|
| |
| result< output > | idfxx::pwm::try_start (idfxx::gpio gpio, const timer::config &cfg, const output_config &out_cfg) |
| | Starts PWM output with automatic allocation and custom output configuration.
|
| |
| bool | idfxx::pwm::is_active (enum channel ch, enum speed_mode mode=speed_mode::low_speed) |
| | Returns true if the specified channel currently has an active output.
|
| |
| std::optional< timer > | idfxx::pwm::get_timer (enum channel ch, enum speed_mode mode=speed_mode::low_speed) |
| | Returns the timer associated with an active channel, if any.
|
| |
| result< void > | idfxx::pwm::try_stop (enum channel ch, enum speed_mode mode=speed_mode::low_speed, idfxx::gpio::level idle_level=idfxx::gpio::level::low) |
| | Stops PWM output on a channel and sets it to an idle level.
|
| |
Type-safe PWM controller for ESP32.
Provides PWM output management with lightweight timer identifiers and RAII-managed output bindings. Timers are fixed hardware resources configured via methods; outputs bind a timer, channel, and GPIO pin into an active PWM signal with automatic cleanup on destruction.
Depends on Core Utilities for error handling, GPIO Component for pin identifiers, and Hardware Support Component for interrupt allocation types.
◆ channel
PWM channel slot identifiers.
Selects the channel slot for PWM output. The number of available channels is target-dependent.
| Enumerator |
|---|
| ch_0 | Channel 0.
|
| ch_1 | Channel 1.
|
| ch_2 | Channel 2.
|
| ch_3 | Channel 3.
|
| ch_4 | Channel 4.
|
| ch_5 | Channel 5.
|
Definition at line 65 of file pwm.hpp.
◆ clk_source
PWM timer clock source.
Selects the clock source for the timer. The default auto_select lets the driver choose the best source for the requested frequency.
| Enumerator |
|---|
| auto_select | Automatic clock source selection.
|
Definition at line 87 of file pwm.hpp.
◆ fade_mode
Fade operation blocking mode.
| Enumerator |
|---|
| no_wait | Return immediately, fade runs in background.
|
| wait_done | Block until the fade completes.
|
Definition at line 107 of file pwm.hpp.
◆ sleep_mode
Channel behavior during light sleep.
| Enumerator |
|---|
| no_alive_no_pd | No output, keep power domain on (default)
|
| no_alive_allow_pd | No output, allow power domain off (saves power)
|
| keep_alive | Maintain PWM output during light sleep.
|
Definition at line 116 of file pwm.hpp.
◆ speed_mode
PWM speed mode selection.
Most targets only support low-speed mode. The original ESP32 also provides a high-speed mode with hardware-driven duty updates.
| Enumerator |
|---|
| low_speed | Low-speed mode (available on all targets)
|
Definition at line 51 of file pwm.hpp.
◆ get_timer()
Returns the timer associated with an active channel, if any.
- Parameters
-
| ch | Channel to check. |
| mode | Speed mode group (defaults to low_speed). |
- Returns
- The timer bound to the channel, or std::nullopt if the channel is not active.
◆ is_active()
Returns true if the specified channel currently has an active output.
- Parameters
-
| ch | Channel to check. |
| mode | Speed mode group (defaults to low_speed). |
- Returns
- True if the channel has an active output.
◆ start() [1/4]
Starts PWM output on a GPIO pin.
Configures the specified channel to output PWM on the given GPIO pin using the provided timer's frequency and resolution.
- Parameters
-
| gpio | GPIO pin for the output. |
| tmr | Configured timer to use. Must have been configured via timer::configure(). |
| ch | Channel slot to use. |
- Note
- Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
- Exceptions
-
| std::system_error | on failure. |
using namespace frequency_literals;
using namespace std::chrono_literals;
tmr.
configure({.frequency = 5_kHz, .resolution_bits = 13});
pwm.set_duty(0.5f);
pwm.fade_to(0.0f, 1s);
static void install_fade_service(idfxx::intr_levels levels=intr_level_lowmed, idfxx::flags< intr_flag > flags={})
Installs the PWM fade service.
void configure(const struct config &cfg)
Configures the timer with the given parameters.
output start(idfxx::gpio gpio, const timer &tmr, enum channel ch)
Starts PWM output on a GPIO pin.
Definition at line 1363 of file pwm.hpp.
References idfxx::pwm::output::try_start, and idfxx::unwrap().
◆ start() [2/4]
◆ start() [3/4]
Starts PWM output with automatic timer and channel allocation.
Finds a timer already configured with matching parameters, or allocates and configures a new one. Then finds a free channel and starts PWM output. Only low-speed mode timers and channels are considered.
- Parameters
-
| gpio | GPIO pin for the output. |
| cfg | Timer configuration (frequency, resolution, clock source). |
- Note
- Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled.
- Exceptions
-
| std::system_error | on failure. |
using namespace frequency_literals;
void set_duty(float duty)
Sets the PWM duty cycle as a ratio.
Definition at line 1369 of file pwm.hpp.
References idfxx::pwm::output::try_start, and idfxx::unwrap().
◆ start() [4/4]
◆ stop()
◆ try_start() [1/4]
Starts PWM output on a GPIO pin.
Configures the specified channel to output PWM on the given GPIO pin using the provided timer's frequency and resolution.
- Parameters
-
| gpio | GPIO pin for the output. |
| tmr | Configured timer to use. Must have been configured via timer::configure(). |
| ch | Channel slot to use. |
- Returns
- The active output, or an error.
- Return values
-
| invalid_state | Timer not configured. |
| invalid_arg | GPIO not connected or invalid channel. |
using namespace frequency_literals;
tmr.
configure({.frequency = 5_kHz, .resolution_bits = 13});
if (pwm) {
pwm->try_set_duty(0.5f);
}
result< output > try_start(idfxx::gpio gpio, const timer &tmr, enum channel ch)
Starts PWM output on a GPIO pin.
◆ try_start() [2/4]
Starts PWM output on a GPIO pin with custom configuration.
- Parameters
-
| gpio | GPIO pin for the output. |
| tmr | Configured timer to use. Must have been configured via timer::configure(). |
| ch | Channel slot to use. |
| cfg | Output configuration. |
- Returns
- The active output, or an error.
- Return values
-
| invalid_state | Timer not configured. |
| invalid_arg | GPIO not connected or invalid channel. |
◆ try_start() [3/4]
Starts PWM output with automatic timer and channel allocation.
Finds a timer already configured with matching parameters, or allocates and configures a new one. Then finds a free channel and starts PWM output. Only low-speed mode timers and channels are considered.
- Parameters
-
| gpio | GPIO pin for the output. |
| cfg | Timer configuration (frequency, resolution, clock source). |
- Returns
- The active output, or an error.
- Return values
-
| not_found | No free timer or channel available. |
| invalid_arg | GPIO not connected or invalid configuration. |
using namespace frequency_literals;
if (pwm) {
pwm->try_set_duty(0.5f);
}
◆ try_start() [4/4]
Starts PWM output with automatic allocation and custom output configuration.
- Parameters
-
| gpio | GPIO pin for the output. |
| cfg | Timer configuration (frequency, resolution, clock source). |
| out_cfg | Output configuration (initial duty, hpoint, inversion, sleep mode). |
- Returns
- The active output, or an error.
- Return values
-
| not_found | No free timer or channel available. |
| invalid_arg | GPIO not connected or invalid configuration. |
◆ try_stop()
Stops PWM output on a channel and sets it to an idle level.
Stops the channel regardless of whether an output object owns it. Any existing output for this channel will become inactive.
- Parameters
-
| ch | Channel to stop. |
| mode | Speed mode group (defaults to low_speed). |
| idle_level | Output level after stopping. |
- Returns
- Success, or an error.