|
Open Model Railroad Network (OpenMRN)
|
ESP32 LEDC provider for PWM like output on GPIO pins. More...
#include <Esp32Ledc.hxx>
Classes | |
| class | Channel |
Public Member Functions | |
| 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. | |
| void | hw_init () |
| Initializes the LEDC peripheral. | |
| PWM * | get_channel (unsigned id) |
| 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. | |
Static Public Member Functions | |
| static void | ledc_fade_setup () |
| Static entry point for configuring the LEDC hardware fade controller. | |
Private Member Functions | |
| void | set_period (uint32_t counts) |
| Set PWM period. | |
| uint32_t | get_period () |
| Get PWM period. | |
| void | set_duty (ledc_channel_t channel, uint32_t counts) |
| Sets the duty cycle. | |
| uint32_t | get_duty (ledc_channel_t channel) |
| Gets the duty cycle. | |
| 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. | |
| DISALLOW_COPY_AND_ASSIGN (Esp32Ledc) | |
Private Attributes | |
| const ledc_channel_t | firstChannel_ |
| First LEDC Channel for this Esp32Ledc. | |
| ledc_timer_config_t | timerConfig_ |
| LEDC Timer configuration settings. | |
| uninitialized< Channel > | channels_ [LEDC_CHANNEL_MAX] |
| PWM instances connected to LEDC channels. | |
| std::vector< uint8_t > | pins_ |
| Collection of GPIO pins in use by this Esp32Ledc. | |
Static Private Attributes | |
| static pthread_once_t | ledcFadeOnce_ |
| Protects the initialization of LEDC Fade ISR hook. | |
ESP32 LEDC provider for PWM like output on GPIO pins.
This class allows creation of up to eight PWM outputs using a single PWM frequency. All outputs in a single Esp32Ledc instance will share the same PWM frequency and will be assigned a channel sequentially.
When the more than one PWM frequency is needed for outputs it is required to create multiple Esp32Ledc instances, each with a unique LEDC timer (LEDC_TIMER_0 through LEDC_TIMER_3) and a unique first channel (LED_CHANNEL_0 through LED_CHANNEL_5 or LED_CHANNEL_7 depending on ESP32 variant).
The ESP32-C3 only supports six channels, whereas other variants support eight. Creating more than one Esp32Ledc instance will not increase the number of outputs.
Example of usage:
Definition at line 82 of file Esp32Ledc.hxx.
|
inline |
Constructor.
| pins | is the collection of output pins to use for this instance. |
| first_channel | is the first LEDC channel to use for this Esp32Ledc instance, default is LEDC_CHANNEL_0. |
| timer_resolution | is the resolution of the LEDC timer, default is 12bit. |
| timer_hz | is the LEDC timer tick frequency, default is 5kHz. |
| timer_num | is the LEDC timer to use, default is LEDC_TIMER_0. |
| timer_mode | is the LED timer mode to use, default is LEDC_LOW_SPEED_MODE. |
| timer_clock | is the LEDC timer clock source, default is LEDC_AUTO_CLK. |
Note: For
| timer_mode | an additional value of LEDC_HIGH_SPEED_MODE is supported ONLY on the base ESP32 variant. Other variants of the ESP32 only support LEDC_LOW_SPEED_MODE. |
Definition at line 102 of file Esp32Ledc.hxx.
|
inline |
Transitions a PWM output from the current duty to the target duty over the provided time period.
| id | is the output number, zero based for this Esp32Ledc instance. |
| target_duty | target duty value, default is zero. |
| fade_period | number of milliseconds to use for the transition, default is 1000 milliseconds. |
| fade_mode | controls if this call is blocking or non-blocking, default is non-blocking. |
NOTE: One a fade request has been submitted to the hardware it can not be canceled and no other requests can be submitted or processed until the previous request has completed.
Definition at line 185 of file Esp32Ledc.hxx.
|
inline |
| id | is the output number, zero based for this Esp32Ledc instance. |
Definition at line 165 of file Esp32Ledc.hxx.
|
inlineprivate |
Gets the duty cycle.
| channel | PWM channel to retrieve the duty cycle for. |
Definition at line 287 of file Esp32Ledc.hxx.
|
inlineprivate |
|
inlineprivate |
Get max period supported by the underlying LEDC timer.
Definition at line 294 of file Esp32Ledc.hxx.
|
inlineprivate |
Get min period supported by the underlying LEDC timer.
Definition at line 301 of file Esp32Ledc.hxx.
|
inline |
Initializes the LEDC peripheral.
| pins | are the gpio pins to assign to the LEDC channels. |
NOTE: Depending on the target ESP32 device the number of LEDC channels available will be either six or eight. Exceeding this number of pins will generate a runtime failure.
Definition at line 132 of file Esp32Ledc.hxx.
|
inlinestatic |
Static entry point for configuring the LEDC hardware fade controller.
NOTE: This method should not be invoked by the user code, it will be called automatically the first time fade_channel_over_time is called on any instance of Esp32Ledc.
Definition at line 203 of file Esp32Ledc.hxx.
|
inlineprivate |
Sets the duty cycle.
| channel | PWM channel to configure |
| counts | duty cycle in counts |
Definition at line 277 of file Esp32Ledc.hxx.
|
inlineprivate |
Set PWM period.
| counts | PWM timer frequency in Hz. |
NOTE: This will apply to ALL PWM outputs that use the same timer.
Definition at line 260 of file Esp32Ledc.hxx.
|
private |
PWM instances connected to LEDC channels.
Definition at line 316 of file Esp32Ledc.hxx.
|
private |
First LEDC Channel for this Esp32Ledc.
Definition at line 307 of file Esp32Ledc.hxx.
|
staticprivate |
Protects the initialization of LEDC Fade ISR hook.
Definition at line 313 of file Esp32Ledc.hxx.
|
private |
Collection of GPIO pins in use by this Esp32Ledc.
Definition at line 319 of file Esp32Ledc.hxx.
|
private |
LEDC Timer configuration settings.
Definition at line 310 of file Esp32Ledc.hxx.