arduino-audio-tools
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
PWMComplementaryDriverESP32 Class Reference

Audio output to PWM pins for the ESP32. The ESP32 supports up to 16 channels. More...

#include <PWMComplementaryDriverESP32.h>

Inheritance diagram for PWMComplementaryDriverESP32:
DriverPWMBase

Public Member Functions

 PWMComplementaryDriverESP32 ()
 
PWMConfigaudioInfo ()
 
virtual int availableForWrite ()
 
bool begin (PWMConfig cfg)
 
virtual PWMConfig defaultConfig ()
 
virtual int effectiveOutputSampleRate ()
 Provides the effective sample rate.
 
virtual void end ()
 
uint32_t framesPerSecond ()
 
bool isTimerStarted ()
 
virtual void pwmWrite (int channel, int value)
 
void setBuffer (BaseBuffer< uint8_t > *buffer)
 
virtual void setupPWM ()
 Setup complementary MCPWM.
 
virtual void setupTimer ()
 Setup ESP32 timer with callback.
 
virtual void startTimer ()
 
uint32_t underflowsPerSecond ()
 
void updateStatistics ()
 
virtual size_t write (const uint8_t *data, size_t len)
 

Protected Member Functions

virtual int decimation ()
 Decimation factor to reduce the sample rate.
 
void deleteBuffer ()
 
float frequency (int resolution)
 determiens the PWM frequency based on the requested resolution
 
virtual bool isDecimateActive ()
 
virtual int maxChannels ()
 
virtual int maxOutputValue ()
 provides the max value for the configured resulution
 
virtual int maxSampleRate ()
 Provides the max working sample rate.
 
int maxUnsignedValue (int resolution)
 provides the max value for the indicated resulution
 
virtual int nextValue ()
 determines the next scaled value
 
void playNextFrame ()
 writes the next frame to the output pins
 

Static Protected Member Functions

static void pwm_callback (void *ptr)
 timer callback: write the next frame to the pins
 

Protected Attributes

AudioInfo actual_info
 
uint32_t actual_timer_frequency = 0
 
PWMConfig audio_config
 
BaseBuffer< uint8_t > * buffer = nullptr
 
Decimate decimate
 
int decimation_factor = 0
 
uint32_t frame_count = 0
 
uint32_t frame_size = 0
 
uint32_t frames_per_second = 0
 
bool is_blocking_write = true
 
bool is_timer_started = false
 
Vector< PinInfoESP32Complpins
 
uint32_t time_1_sec
 
TimerAlarmRepeating timer
 
uint32_t underflow_count = 0
 
uint32_t underflow_per_second = 0
 

Detailed Description

Audio output to PWM pins for the ESP32. The ESP32 supports up to 16 channels.

Author
Phil Schatzmann

Complementary (half‑bridge) PWM audio driver for ESP32 using the legacy MCPWM driver API.

This driver produces audio by modulating one or more complementary PWM pairs (high / low outputs) using the ESP32 MCPWM peripheral. Each audio channel occupies one MCPWM timer (up to 3 timers per MCPWM unit, and 2 units → maximum of 6 complementary channels). For every channel two GPIOs are driven 180° out of phase. Optional hardware dead time can be inserted to protect external half‑bridge power stages.

The duty cycle is derived from the (optionally decimated) audio samples in the base class buffer. The effective output sample rate equals the rate at which new duty values are pushed to the MCPWM hardware (timer interrupt / alarm frequency) and may differ from the nominal input sample rate due to decimation (see DriverPWMBase).

Resolution & PWM Frequency: The requested bit resolution (e.g. 8–11 bits) determines the PWM carrier frequency using empirically chosen values (see frequency()). If PWMConfig::pwm_frequency is left as 0 the driver derives it from the resolution. Otherwise the provided frequency is used.

Dead Time: If PWMConfig::dead_time_us > 0 the driver configures hardware complementary mode with symmetric dead time (rising & falling) in ticks computed from the APB clock (assumed 80 MHz). For very large dead time requests relative to the PWM period the value is limited to maintain a usable duty range. Without dead time the driver emulates complementary operation by inverting the B duty in software (B = 100% - A).

Pin Assignment: Provide either 2 * channels GPIO values (high0, low0, high1, low1, ...). If only one pin per channel is supplied the low pin is assumed to be high+1.

Limitations:

Thread Safety: Access from a single task context; ISR only invokes playNextFrame().

Constructor & Destructor Documentation

◆ PWMComplementaryDriverESP32()

Member Function Documentation

◆ audioInfo()

PWMConfig & audioInfo ( )
inlineinherited

◆ availableForWrite()

virtual int availableForWrite ( )
inlinevirtualinherited

◆ begin()

bool begin ( PWMConfig  cfg)
inlineinherited

◆ decimation()

virtual int decimation ( )
inlineprotectedvirtualinherited

Decimation factor to reduce the sample rate.

Reimplemented in PWMDriverRenesas.

◆ defaultConfig()

virtual PWMConfig defaultConfig ( )
inlinevirtualinherited

Reimplemented in PWMDriverRenesas.

◆ deleteBuffer()

void deleteBuffer ( )
inlineprotectedinherited

◆ effectiveOutputSampleRate()

virtual int effectiveOutputSampleRate ( )
inlinevirtualinherited

Provides the effective sample rate.

◆ end()

virtual void end ( )
inlinevirtual

Reimplemented from DriverPWMBase.

◆ framesPerSecond()

uint32_t framesPerSecond ( )
inlineinherited

◆ frequency()

float frequency ( int  resolution)
inlineprotected

determiens the PWM frequency based on the requested resolution

◆ isDecimateActive()

virtual bool isDecimateActive ( )
inlineprotectedvirtualinherited

The requested sampling rate is too hight: we only process half of the samples so we can half the sampling rate

◆ isTimerStarted()

bool isTimerStarted ( )
inlineinherited

◆ maxChannels()

virtual int maxChannels ( )
inlineprotectedvirtual

Implements DriverPWMBase.

◆ maxOutputValue()

virtual int maxOutputValue ( )
inlineprotectedvirtual

provides the max value for the configured resulution

Implements DriverPWMBase.

◆ maxSampleRate()

virtual int maxSampleRate ( )
inlineprotectedvirtualinherited

Provides the max working sample rate.

Reimplemented in PWMDriverRenesas.

◆ maxUnsignedValue()

int maxUnsignedValue ( int  resolution)
inlineprotected

provides the max value for the indicated resulution

◆ nextValue()

virtual int nextValue ( )
inlineprotectedvirtualinherited

determines the next scaled value

◆ playNextFrame()

void playNextFrame ( )
inlineprotectedinherited

writes the next frame to the output pins

◆ pwm_callback()

static void pwm_callback ( void ptr)
inlinestaticprotected

timer callback: write the next frame to the pins

◆ pwmWrite()

virtual void pwmWrite ( int  channel,
int  value 
)
inlinevirtual

write a pwm value to the indicated channel. The max value depends on the resolution

Implements DriverPWMBase.

◆ setBuffer()

void setBuffer ( BaseBuffer< uint8_t > *  buffer)
inlineinherited

You can assign your own custom buffer impelementation: must be allocated on the heap and will be cleaned up by this class

◆ setupPWM()

virtual void setupPWM ( )
inlinevirtual

Setup complementary MCPWM.

Implements DriverPWMBase.

◆ setupTimer()

virtual void setupTimer ( )
inlinevirtual

Setup ESP32 timer with callback.

Implements DriverPWMBase.

◆ startTimer()

virtual void startTimer ( )
inlinevirtual

when we get the first write -> we activate the timer to start with the output of data

Implements DriverPWMBase.

◆ underflowsPerSecond()

uint32_t underflowsPerSecond ( )
inlineinherited

◆ updateStatistics()

void updateStatistics ( )
inlineinherited

◆ write()

virtual size_t write ( const uint8_t data,
size_t  len 
)
inlinevirtualinherited

Member Data Documentation

◆ actual_info

AudioInfo actual_info
protectedinherited

◆ actual_timer_frequency

uint32_t actual_timer_frequency = 0
protected

◆ audio_config

PWMConfig audio_config
protectedinherited

◆ buffer

BaseBuffer<uint8_t>* buffer = nullptr
protectedinherited

◆ decimate

Decimate decimate
protectedinherited

◆ decimation_factor

int decimation_factor = 0
protectedinherited

◆ frame_count

uint32_t frame_count = 0
protectedinherited

◆ frame_size

uint32_t frame_size = 0
protectedinherited

◆ frames_per_second

uint32_t frames_per_second = 0
protectedinherited

◆ is_blocking_write

bool is_blocking_write = true
protectedinherited

◆ is_timer_started

bool is_timer_started = false
protectedinherited

◆ pins

Vector<PinInfoESP32Compl> pins
protected

◆ time_1_sec

uint32_t time_1_sec
protectedinherited

◆ timer

TimerAlarmRepeating timer
protected

◆ underflow_count

uint32_t underflow_count = 0
protectedinherited

◆ underflow_per_second

uint32_t underflow_per_second = 0
protectedinherited

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