HF Interface Wrapper 0.1.0-dev
Embedded C++ hardware abstraction layer
Loading...
Searching...
No Matches
BasePwm Class Referenceabstract

Abstract base class for PWM implementations. More...

#include <BasePwm.h>

Inheritance diagram for BasePwm:
[legend]
Collaboration diagram for BasePwm:
[legend]

Public Member Functions

virtual ~BasePwm () noexcept=default
 
virtual hf_pwm_err_t Initialize () noexcept=0
 Initialize the PWM system.
 
virtual hf_pwm_err_t Deinitialize () noexcept=0
 Deinitialize the PWM system.
 
bool IsInitialized () const noexcept
 Check if PWM system is initialized.
 
bool EnsureInitialized () noexcept
 Ensure PWM is initialized (lazy initialization pattern)
 
bool EnsureDeinitialized () noexcept
 Ensure PWM is deinitialized (lazy deinitialization pattern)
 
virtual hf_pwm_err_t EnableChannel (hf_channel_id_t channel_id) noexcept=0
 Enable a PWM channel.
 
virtual hf_pwm_err_t DisableChannel (hf_channel_id_t channel_id) noexcept=0
 Disable a PWM channel.
 
virtual bool IsChannelEnabled (hf_channel_id_t channel_id) const noexcept=0
 Check if a channel is enabled.
 
virtual hf_pwm_err_t SetDutyCycle (hf_channel_id_t channel_id, float duty_cycle) noexcept=0
 Set duty cycle for a channel.
 
virtual hf_pwm_err_t SetDutyCycleRaw (hf_channel_id_t channel_id, hf_u32_t raw_value) noexcept=0
 Set raw duty value for a channel.
 
virtual hf_pwm_err_t SetFrequency (hf_channel_id_t channel_id, hf_frequency_hz_t frequency_hz) noexcept=0
 Set frequency for a channel.
 
virtual hf_pwm_err_t SetPhaseShift (hf_channel_id_t channel_id, float phase_shift_degrees) noexcept=0
 Set phase shift for a channel (if supported)
 
virtual hf_pwm_err_t StartAll () noexcept=0
 Start all enabled channels simultaneously.
 
virtual hf_pwm_err_t StopAll () noexcept=0
 Stop all channels.
 
virtual hf_pwm_err_t UpdateAll () noexcept=0
 Update all channel outputs simultaneously (for synchronized updates)
 
virtual hf_pwm_err_t SetComplementaryOutput (hf_channel_id_t primary_channel, hf_channel_id_t complementary_channel, hf_u32_t deadtime_ns) noexcept=0
 Set complementary output configuration (for motor control)
 
virtual float GetDutyCycle (hf_channel_id_t channel_id) const noexcept=0
 Get current duty cycle for a channel.
 
virtual hf_frequency_hz_t GetFrequency (hf_channel_id_t channel_id) const noexcept=0
 Get current frequency for a channel.
 
virtual hf_pwm_err_t GetStatistics (hf_pwm_statistics_t &statistics) const noexcept
 Get PWM statistics.
 
virtual hf_pwm_err_t GetDiagnostics (hf_pwm_diagnostics_t &diagnostics) const noexcept
 Get PWM diagnostics.
 
virtual hf_pwm_err_t ResetStatistics () noexcept
 Reset PWM operation statistics.
 
virtual hf_pwm_err_t ResetDiagnostics () noexcept
 Reset PWM diagnostic information.
 

Static Public Member Functions

static constexpr hf_u32_t DutyCycleToRaw (float duty_cycle, hf_u8_t resolution_bits) noexcept
 Calculate raw duty value from percentage.
 
static constexpr float RawToDutyCycle (hf_u32_t raw_value, hf_u8_t resolution_bits) noexcept
 Calculate duty cycle percentage from raw value.
 
static constexpr bool IsValidDutyCycle (float duty_cycle) noexcept
 Validate duty cycle range.
 
static constexpr bool IsValidFrequency (hf_u32_t frequency_hz, hf_u32_t min_freq_hz, hf_u32_t max_freq_hz) noexcept
 Validate frequency range.
 
static constexpr bool IsValidRawDuty (hf_u32_t raw_value, hf_u8_t resolution_bits) noexcept
 Validate raw duty value against resolution.
 
static constexpr float CalculateFrequencyAccuracy (hf_u32_t target_freq, hf_u32_t actual_freq) noexcept
 Calculate frequency accuracy percentage.
 
static constexpr float ClampDutyCycle (float duty_cycle) noexcept
 Clamp duty cycle to valid range.
 

Protected Member Functions

 BasePwm () noexcept
 
 BasePwm (const BasePwm &)=delete
 
BasePwmoperator= (const BasePwm &)=delete
 
 BasePwm (BasePwm &&)=delete
 
BasePwmoperator= (BasePwm &&)=delete
 

Protected Attributes

bool initialized_
 Initialization state.
 
hf_pwm_statistics_t statistics_
 PWM operation statistics.
 
hf_pwm_diagnostics_t diagnostics_
 PWM diagnostic information.
 

Detailed Description

Abstract base class for PWM implementations.

This class defines the common interface that all PWM implementations must provide. It supports both on-chip PWM peripherals and external PWM controllers.

Key features:

  • Multi-channel PWM support
  • Configurable frequency and resolution
  • Hardware abstraction for different PWM sources
  • Event callbacks for period and fault events
  • Comprehensive error handling
  • Thread-safe design when used with SfPwm wrapper

Possible implementations include on-chip controllers or dedicated PWM chips.

  • CustomPwm: For custom/proprietary PWM implementations

Constructor & Destructor Documentation

◆ ~BasePwm()

virtual BasePwm::~BasePwm ( )
virtualdefaultnoexcept

◆ BasePwm() [1/3]

BasePwm::BasePwm ( )
inlineprotectednoexcept

◆ BasePwm() [2/3]

BasePwm::BasePwm ( const BasePwm & )
protecteddelete

◆ BasePwm() [3/3]

BasePwm::BasePwm ( BasePwm && )
protecteddelete

Member Function Documentation

◆ CalculateFrequencyAccuracy()

static constexpr float BasePwm::CalculateFrequencyAccuracy ( hf_u32_t target_freq,
hf_u32_t actual_freq )
inlinestaticconstexprnoexcept

Calculate frequency accuracy percentage.

Parameters
target_freqTarget frequency
actual_freqActual achieved frequency
Returns
Accuracy percentage (0.0-1.0)

◆ ClampDutyCycle()

static constexpr float BasePwm::ClampDutyCycle ( float duty_cycle)
inlinestaticconstexprnoexcept

Clamp duty cycle to valid range.

Parameters
duty_cycleDuty cycle to clamp
Returns
Clamped duty cycle (0.0 - 1.0)

◆ Deinitialize()

virtual hf_pwm_err_t BasePwm::Deinitialize ( )
pure virtualnoexcept

Deinitialize the PWM system.

Returns
PWM_SUCCESS on success, error code on failure

Implemented in EspPwm, and StmPwm.

◆ DisableChannel()

virtual hf_pwm_err_t BasePwm::DisableChannel ( hf_channel_id_t channel_id)
pure virtualnoexcept

Disable a PWM channel.

Parameters
channel_idChannel to disable
Returns
PWM_SUCCESS on success, error code on failure

Implemented in EspPwm, and StmPwm.

◆ DutyCycleToRaw()

static constexpr hf_u32_t BasePwm::DutyCycleToRaw ( float duty_cycle,
hf_u8_t resolution_bits )
inlinestaticconstexprnoexcept

Calculate raw duty value from percentage.

Parameters
duty_cycleDuty cycle (0.0 - 1.0)
resolution_bitsPWM resolution in bits
Returns
Raw duty value

◆ EnableChannel()

virtual hf_pwm_err_t BasePwm::EnableChannel ( hf_channel_id_t channel_id)
pure virtualnoexcept

Enable a PWM channel.

Parameters
channel_idChannel to enable
Returns
PWM_SUCCESS on success, error code on failure

Implemented in EspPwm, and StmPwm.

◆ EnsureDeinitialized()

bool BasePwm::EnsureDeinitialized ( )
inlinenoexcept

Ensure PWM is deinitialized (lazy deinitialization pattern)

Returns
true if deinitialized successfully, false on failure
Note
This method can be called to ensure proper cleanup when needed.

◆ EnsureInitialized()

bool BasePwm::EnsureInitialized ( )
inlinenoexcept

Ensure PWM is initialized (lazy initialization pattern)

Returns
true if initialized successfully, false on failure
Note
This method should be called at the beginning of all public methods that require initialization. It implements lazy initialization.

◆ GetDiagnostics()

virtual hf_pwm_err_t BasePwm::GetDiagnostics ( hf_pwm_diagnostics_t & diagnostics) const
inlinevirtualnoexcept

Get PWM diagnostics.

Parameters
diagnosticsDiagnostics structure to fill
Returns
PWM_SUCCESS on success, error code on failure

Reimplemented in EspPwm.

◆ GetDutyCycle()

virtual float BasePwm::GetDutyCycle ( hf_channel_id_t channel_id) const
pure virtualnoexcept

Get current duty cycle for a channel.

Parameters
channel_idChannel identifier
Returns
Current duty cycle (0.0 - 1.0), or -1.0 on error

Implemented in EspPwm, and StmPwm.

◆ GetFrequency()

virtual hf_frequency_hz_t BasePwm::GetFrequency ( hf_channel_id_t channel_id) const
pure virtualnoexcept

Get current frequency for a channel.

Parameters
channel_idChannel identifier
Returns
Current frequency in Hz, or 0 on error

Implemented in EspPwm, and StmPwm.

◆ GetStatistics()

virtual hf_pwm_err_t BasePwm::GetStatistics ( hf_pwm_statistics_t & statistics) const
inlinevirtualnoexcept

Get PWM statistics.

Parameters
statisticsStatistics structure to fill
Returns
PWM_SUCCESS on success, error code on failure

Reimplemented in EspPwm.

◆ Initialize()

virtual hf_pwm_err_t BasePwm::Initialize ( )
pure virtualnoexcept

Initialize the PWM system.

Returns
PWM_SUCCESS on success, error code on failure

Implemented in EspPwm, and StmPwm.

◆ IsChannelEnabled()

virtual bool BasePwm::IsChannelEnabled ( hf_channel_id_t channel_id) const
pure virtualnoexcept

Check if a channel is enabled.

Parameters
channel_idChannel to check
Returns
true if enabled, false otherwise

Implemented in EspPwm, and StmPwm.

◆ IsInitialized()

bool BasePwm::IsInitialized ( ) const
inlinenoexcept

Check if PWM system is initialized.

Returns
true if initialized, false otherwise

◆ IsValidDutyCycle()

static constexpr bool BasePwm::IsValidDutyCycle ( float duty_cycle)
inlinestaticconstexprnoexcept

Validate duty cycle range.

Parameters
duty_cycleDuty cycle to validate
Returns
true if valid (0.0 - 1.0), false otherwise

◆ IsValidFrequency()

static constexpr bool BasePwm::IsValidFrequency ( hf_u32_t frequency_hz,
hf_u32_t min_freq_hz,
hf_u32_t max_freq_hz )
inlinestaticconstexprnoexcept

Validate frequency range.

Parameters
frequency_hzFrequency to validate
min_freq_hzMinimum allowed frequency
max_freq_hzMaximum allowed frequency
Returns
true if valid, false otherwise

◆ IsValidRawDuty()

static constexpr bool BasePwm::IsValidRawDuty ( hf_u32_t raw_value,
hf_u8_t resolution_bits )
inlinestaticconstexprnoexcept

Validate raw duty value against resolution.

Parameters
raw_valueRaw duty value to validate
resolution_bitsPWM resolution in bits
Returns
true if valid, false otherwise

◆ operator=() [1/2]

BasePwm & BasePwm::operator= ( BasePwm && )
protecteddelete

◆ operator=() [2/2]

BasePwm & BasePwm::operator= ( const BasePwm & )
protecteddelete

◆ RawToDutyCycle()

static constexpr float BasePwm::RawToDutyCycle ( hf_u32_t raw_value,
hf_u8_t resolution_bits )
inlinestaticconstexprnoexcept

Calculate duty cycle percentage from raw value.

Parameters
raw_valueRaw duty value
resolution_bitsPWM resolution in bits
Returns
Duty cycle (0.0 - 1.0)

◆ ResetDiagnostics()

virtual hf_pwm_err_t BasePwm::ResetDiagnostics ( )
inlinevirtualnoexcept

Reset PWM diagnostic information.

Returns
hf_pwm_err_t::PWM_SUCCESS if successful, error code otherwise
Note
Override this method to provide platform-specific diagnostics reset

◆ ResetStatistics()

virtual hf_pwm_err_t BasePwm::ResetStatistics ( )
inlinevirtualnoexcept

Reset PWM operation statistics.

Returns
hf_pwm_err_t::PWM_SUCCESS if successful, error code otherwise
Note
Override this method to provide platform-specific statistics reset

◆ SetComplementaryOutput()

virtual hf_pwm_err_t BasePwm::SetComplementaryOutput ( hf_channel_id_t primary_channel,
hf_channel_id_t complementary_channel,
hf_u32_t deadtime_ns )
pure virtualnoexcept

Set complementary output configuration (for motor control)

Parameters
primary_channelPrimary channel
complementary_channelComplementary channel
deadtime_nsDeadtime in nanoseconds
Returns
PWM_SUCCESS on success, error code on failure

Implemented in EspPwm, and StmPwm.

◆ SetDutyCycle()

virtual hf_pwm_err_t BasePwm::SetDutyCycle ( hf_channel_id_t channel_id,
float duty_cycle )
pure virtualnoexcept

Set duty cycle for a channel.

Parameters
channel_idChannel identifier
duty_cycleDuty cycle (0.0 - 1.0)
Returns
PWM_SUCCESS on success, error code on failure

Implemented in EspPwm, and StmPwm.

◆ SetDutyCycleRaw()

virtual hf_pwm_err_t BasePwm::SetDutyCycleRaw ( hf_channel_id_t channel_id,
hf_u32_t raw_value )
pure virtualnoexcept

Set raw duty value for a channel.

Parameters
channel_idChannel identifier
raw_valueRaw duty register value
Returns
PWM_SUCCESS on success, error code on failure

Implemented in EspPwm, and StmPwm.

◆ SetFrequency()

virtual hf_pwm_err_t BasePwm::SetFrequency ( hf_channel_id_t channel_id,
hf_frequency_hz_t frequency_hz )
pure virtualnoexcept

Set frequency for a channel.

Parameters
channel_idChannel identifier
frequency_hzFrequency in Hz
Returns
PWM_SUCCESS on success, error code on failure

Implemented in EspPwm, and StmPwm.

◆ SetPhaseShift()

virtual hf_pwm_err_t BasePwm::SetPhaseShift ( hf_channel_id_t channel_id,
float phase_shift_degrees )
pure virtualnoexcept

Set phase shift for a channel (if supported)

Parameters
channel_idChannel identifier
phase_shift_degreesPhase shift in degrees (0-360)
Returns
PWM_SUCCESS on success, error code on failure

Implemented in EspPwm, and StmPwm.

◆ StartAll()

virtual hf_pwm_err_t BasePwm::StartAll ( )
pure virtualnoexcept

Start all enabled channels simultaneously.

Returns
PWM_SUCCESS on success, error code on failure

Implemented in EspPwm, and StmPwm.

◆ StopAll()

virtual hf_pwm_err_t BasePwm::StopAll ( )
pure virtualnoexcept

Stop all channels.

Returns
PWM_SUCCESS on success, error code on failure

Implemented in EspPwm, and StmPwm.

◆ UpdateAll()

virtual hf_pwm_err_t BasePwm::UpdateAll ( )
pure virtualnoexcept

Update all channel outputs simultaneously (for synchronized updates)

Returns
PWM_SUCCESS on success, error code on failure

Implemented in EspPwm, and StmPwm.

Member Data Documentation

◆ diagnostics_

hf_pwm_diagnostics_t BasePwm::diagnostics_
protected

PWM diagnostic information.

◆ initialized_

bool BasePwm::initialized_
protected

Initialization state.

◆ statistics_

hf_pwm_statistics_t BasePwm::statistics_
protected

PWM operation statistics.


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