Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
openmrn_arduino::Esp32Ledc Class Reference

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.
 
PWMget_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< Channelchannels_ [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.
 

Detailed Description

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:

OpenMRN openmrn(NODE_ID);
Esp32Ledc ledc({16, // Channel 0
17, // Channel 1
18}); // Channel 2
ServoConsumer servo_0(openmrn.stack()->node(), cfg.seg().servo(), 1000,
ledc.get_channel(1));
PWMGPO led_0(ledc.get_channel(0), 2500, 0);
void setup() {
...
ledc.hw_init();
openmrn.begin();
openmrn.stack()->set_tx_activity_led(&led_0);
ledc.fade_channel_over_time(2, 2500, SEC_TO_MSEC(5));
...
}
General Purpose Output specialization of a PWM Bit.
Definition PWM.hxx:101
ESP32 LEDC provider for PWM like output on GPIO pins.
Definition Esp32Ledc.hxx:83
#define SEC_TO_MSEC(_sec)
Convert a second value to a millisecond value.
Definition os.h:298

Definition at line 82 of file Esp32Ledc.hxx.

Constructor & Destructor Documentation

◆ Esp32Ledc()

openmrn_arduino::Esp32Ledc::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 
)
inline

Constructor.

Parameters
pinsis the collection of output pins to use for this instance.
first_channelis the first LEDC channel to use for this Esp32Ledc instance, default is LEDC_CHANNEL_0.
timer_resolutionis the resolution of the LEDC timer, default is 12bit.
timer_hzis the LEDC timer tick frequency, default is 5kHz.
timer_numis the LEDC timer to use, default is LEDC_TIMER_0.
timer_modeis the LED timer mode to use, default is LEDC_LOW_SPEED_MODE.
timer_clockis the LEDC timer clock source, default is LEDC_AUTO_CLK.

Note: For

Parameters
timer_modean 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.

Member Function Documentation

◆ fade_channel_over_time()

void openmrn_arduino::Esp32Ledc::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 
)
inline

Transitions a PWM output from the current duty to the target duty over the provided time period.

Parameters
idis the output number, zero based for this Esp32Ledc instance.
target_dutytarget duty value, default is zero.
fade_periodnumber of milliseconds to use for the transition, default is 1000 milliseconds.
fade_modecontrols 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.

◆ get_channel()

PWM * openmrn_arduino::Esp32Ledc::get_channel ( unsigned  id)
inline
Returns
one PWM output.
Parameters
idis the output number, zero based for this Esp32Ledc instance.

Definition at line 165 of file Esp32Ledc.hxx.

◆ get_duty()

uint32_t openmrn_arduino::Esp32Ledc::get_duty ( ledc_channel_t  channel)
inlineprivate

Gets the duty cycle.

Parameters
channelPWM channel to retrieve the duty cycle for.
Returns
counts duty cycle in counts

Definition at line 287 of file Esp32Ledc.hxx.

◆ get_period()

uint32_t openmrn_arduino::Esp32Ledc::get_period ( )
inlineprivate

Get PWM period.

Returns
PWM timer frequency in Hz.

Definition at line 269 of file Esp32Ledc.hxx.

◆ get_period_max()

uint32_t openmrn_arduino::Esp32Ledc::get_period_max ( )
inlineprivate

Get max period supported by the underlying LEDC timer.

Returns
period in counts.

Definition at line 294 of file Esp32Ledc.hxx.

◆ get_period_min()

uint32_t openmrn_arduino::Esp32Ledc::get_period_min ( )
inlineprivate

Get min period supported by the underlying LEDC timer.

Returns
period in counts.

Definition at line 301 of file Esp32Ledc.hxx.

◆ hw_init()

void openmrn_arduino::Esp32Ledc::hw_init ( )
inline

Initializes the LEDC peripheral.

Parameters
pinsare 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.

◆ ledc_fade_setup()

static void openmrn_arduino::Esp32Ledc::ledc_fade_setup ( )
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.

◆ set_duty()

void openmrn_arduino::Esp32Ledc::set_duty ( ledc_channel_t  channel,
uint32_t  counts 
)
inlineprivate

Sets the duty cycle.

Parameters
channelPWM channel to configure
countsduty cycle in counts

Definition at line 277 of file Esp32Ledc.hxx.

◆ set_period()

void openmrn_arduino::Esp32Ledc::set_period ( uint32_t  counts)
inlineprivate

Set PWM period.

Parameters
countsPWM 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.

Member Data Documentation

◆ channels_

uninitialized<Channel> openmrn_arduino::Esp32Ledc::channels_[LEDC_CHANNEL_MAX]
private

PWM instances connected to LEDC channels.

Definition at line 316 of file Esp32Ledc.hxx.

◆ firstChannel_

const ledc_channel_t openmrn_arduino::Esp32Ledc::firstChannel_
private

First LEDC Channel for this Esp32Ledc.

Definition at line 307 of file Esp32Ledc.hxx.

◆ ledcFadeOnce_

pthread_once_t openmrn_arduino::Esp32Ledc::ledcFadeOnce_
staticprivate

Protects the initialization of LEDC Fade ISR hook.

Definition at line 313 of file Esp32Ledc.hxx.

◆ pins_

std::vector<uint8_t> openmrn_arduino::Esp32Ledc::pins_
private

Collection of GPIO pins in use by this Esp32Ledc.

Definition at line 319 of file Esp32Ledc.hxx.

◆ timerConfig_

ledc_timer_config_t openmrn_arduino::Esp32Ledc::timerConfig_
private

LEDC Timer configuration settings.

Definition at line 310 of file Esp32Ledc.hxx.


The documentation for this class was generated from the following file: