HF-Core Platform 0.1.0-dev
Hardware-Agnostic Handler Layer & RTOS Utilities for HardFOC
Loading...
Searching...
No Matches
Pca9685PwmAdapter Class Reference

BasePwm adapter for all 16 PCA9685 PWM channels. More...

#include <Pca9685Handler.h>

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

Public Member Functions

 Pca9685PwmAdapter (Pca9685Handler *parent_handler) noexcept
 Construct the PWM adapter.
 
 ~Pca9685PwmAdapter () noexcept override=default
 Default destructor.
 
BasePwm Interface Implementation
hf_pwm_err_t Initialize () noexcept override
 Initialize the PWM adapter (ensures parent handler is initialized).
 
hf_pwm_err_t Deinitialize () noexcept override
 Deinitialize (marks adapter as uninitialized).
 
hf_pwm_err_t EnableChannel (hf_channel_id_t channel_id) noexcept override
 Enable a PWM channel (restore its duty cycle from cache).
 
hf_pwm_err_t DisableChannel (hf_channel_id_t channel_id) noexcept override
 Disable a PWM channel (set to full-off).
 
bool IsChannelEnabled (hf_channel_id_t channel_id) const noexcept override
 Check if a channel is enabled.
 
hf_pwm_err_t SetDutyCycle (hf_channel_id_t channel_id, float duty_cycle) noexcept override
 Set duty cycle for a channel (0.0-1.0).
 
hf_pwm_err_t SetDutyCycleRaw (hf_channel_id_t channel_id, hf_u32_t raw_value) noexcept override
 Set raw PWM tick value for a channel.
 
hf_pwm_err_t SetFrequency (hf_channel_id_t channel_id, hf_frequency_hz_t frequency_hz) noexcept override
 Set PWM frequency (global – affects ALL channels).
 
hf_pwm_err_t SetPhaseShift (hf_channel_id_t channel_id, float phase_shift_degrees) noexcept override
 Set phase shift for a channel (via PCA9685 on-time offset).
 
hf_pwm_err_t StartAll () noexcept override
 Wake the PCA9685 from sleep (start all channels).
 
hf_pwm_err_t StopAll () noexcept override
 Put the PCA9685 into sleep mode (stop all outputs).
 
hf_pwm_err_t UpdateAll () noexcept override
 No-op (PCA9685 updates immediately on register write).
 
hf_pwm_err_t SetComplementaryOutput (hf_channel_id_t primary_channel, hf_channel_id_t complementary_channel, hf_u32_t deadtime_ns) noexcept override
 Not supported by PCA9685.
 
float GetDutyCycle (hf_channel_id_t channel_id) const noexcept override
 Get the cached duty cycle for a channel.
 
hf_frequency_hz_t GetFrequency (hf_channel_id_t channel_id) const noexcept override
 Get the current PWM frequency (global).
 

Static Public Attributes

static constexpr uint8_t kMaxChannels = 16
 Number of PWM channels on the PCA9685.
 
static constexpr uint8_t kResolutionBits = 12
 PWM resolution in bits.
 
static constexpr uint16_t kMaxRawValue = 4095
 Maximum raw PWM value (2^12 - 1 = 4095).
 
static constexpr uint32_t kMinFrequencyHz = 24
 Minimum PWM frequency in Hz.
 
static constexpr uint32_t kMaxFrequencyHz = 1526
 Maximum PWM frequency in Hz.
 

Private Member Functions

bool validateChannel (hf_channel_id_t channel_id) const noexcept
 Validate channel ID is in range 0-15.
 

Private Attributes

Pca9685Handlerparent_handler_
 Owning handler (not owned).
 
hf_frequency_hz_t current_frequency_hz_ = 200
 Current global frequency.
 
Per-Channel State Cache
std::array< float, kMaxChannelsduty_cache_
 Cached duty cycles (0.0-1.0).
 
std::array< uint16_t, kMaxChannelson_time_cache_
 Cached on-time values (phase offset).
 
std::array< bool, kMaxChannelschannel_enabled_
 Channel enable state.
 

Friends

class Pca9685Handler
 Allow handler to access internals.
 

Detailed Description

BasePwm adapter for all 16 PCA9685 PWM channels.

Wraps the PCA9685's 16 PWM channels as a HardFOC BasePwm instance. All hardware operations delegate to the parent Pca9685Handler, which in turn calls the typed PCA9685 driver.

Hardware Constraints

  • Global frequency: The PCA9685 has a single oscillator with one prescaler. SetFrequency() on any channel changes the frequency for ALL channels. The adapter tracks the current frequency and returns it for any channel.
  • 12-bit resolution: PWM values are 0-4095 (12-bit).
  • Phase shift: Supported via the PCA9685's on-time offset. Each channel has a programmable on-time (0-4095) which creates a phase offset.
  • No complementary outputs: SetComplementaryOutput() is unsupported.
Note
Created via Pca9685Handler::GetPwmAdapter(). The handler owns this instance.

Constructor & Destructor Documentation

◆ Pca9685PwmAdapter()

Pca9685PwmAdapter::Pca9685PwmAdapter ( Pca9685Handler * parent_handler)
explicitnoexcept

Construct the PWM adapter.

Parameters
parent_handlerPointer to the owning handler (must not be null).

◆ ~Pca9685PwmAdapter()

Pca9685PwmAdapter::~Pca9685PwmAdapter ( )
overridedefaultnoexcept

Default destructor.

Member Function Documentation

◆ Deinitialize()

hf_pwm_err_t Pca9685PwmAdapter::Deinitialize ( )
overridenoexcept

Deinitialize (marks adapter as uninitialized).

◆ DisableChannel()

hf_pwm_err_t Pca9685PwmAdapter::DisableChannel ( hf_channel_id_t channel_id)
overridenoexcept

Disable a PWM channel (set to full-off).

Parameters
channel_idChannel number (0-15).
Returns
PWM_SUCCESS or error code.

◆ EnableChannel()

hf_pwm_err_t Pca9685PwmAdapter::EnableChannel ( hf_channel_id_t channel_id)
overridenoexcept

Enable a PWM channel (restore its duty cycle from cache).

Parameters
channel_idChannel number (0-15).
Returns
PWM_SUCCESS or error code.

◆ GetDutyCycle()

float Pca9685PwmAdapter::GetDutyCycle ( hf_channel_id_t channel_id) const
overridenoexcept

Get the cached duty cycle for a channel.

Parameters
channel_idChannel number (0-15).
Returns
Duty cycle (0.0-1.0), or -1.0 on error.

◆ GetFrequency()

hf_frequency_hz_t Pca9685PwmAdapter::GetFrequency ( hf_channel_id_t channel_id) const
overridenoexcept

Get the current PWM frequency (global).

Parameters
channel_idIgnored (frequency is global).
Returns
Frequency in Hz, or 0 on error.

◆ Initialize()

hf_pwm_err_t Pca9685PwmAdapter::Initialize ( )
overridenoexcept

Initialize the PWM adapter (ensures parent handler is initialized).

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsChannelEnabled()

bool Pca9685PwmAdapter::IsChannelEnabled ( hf_channel_id_t channel_id) const
overridenoexcept

Check if a channel is enabled.

Parameters
channel_idChannel number (0-15).
Returns
true if the channel is active.

◆ SetComplementaryOutput()

hf_pwm_err_t Pca9685PwmAdapter::SetComplementaryOutput ( hf_channel_id_t primary_channel,
hf_channel_id_t complementary_channel,
hf_u32_t deadtime_ns )
overridenoexcept

Not supported by PCA9685.

Returns
PWM_ERR_UNSUPPORTED_OPERATION.

◆ SetDutyCycle()

hf_pwm_err_t Pca9685PwmAdapter::SetDutyCycle ( hf_channel_id_t channel_id,
float duty_cycle )
overridenoexcept

Set duty cycle for a channel (0.0-1.0).

Parameters
channel_idChannel number (0-15).
duty_cycleDuty cycle (0.0 = off, 1.0 = full on).
Returns
PWM_SUCCESS or error code.

◆ SetDutyCycleRaw()

hf_pwm_err_t Pca9685PwmAdapter::SetDutyCycleRaw ( hf_channel_id_t channel_id,
hf_u32_t raw_value )
overridenoexcept

Set raw PWM tick value for a channel.

Parameters
channel_idChannel number (0-15).
raw_valueOff-time tick count (0-4095).
Returns
PWM_SUCCESS or error code.

◆ SetFrequency()

hf_pwm_err_t Pca9685PwmAdapter::SetFrequency ( hf_channel_id_t channel_id,
hf_frequency_hz_t frequency_hz )
overridenoexcept

Set PWM frequency (global – affects ALL channels).

The PCA9685 has a single prescaler so frequency is shared across all 16 channels. The channel_id parameter is accepted for API conformance but the frequency change applies globally.

Parameters
channel_idIgnored (frequency is global).
frequency_hzFrequency in Hz (24-1526).
Returns
PWM_SUCCESS or error code.

◆ SetPhaseShift()

hf_pwm_err_t Pca9685PwmAdapter::SetPhaseShift ( hf_channel_id_t channel_id,
float phase_shift_degrees )
overridenoexcept

Set phase shift for a channel (via PCA9685 on-time offset).

The PCA9685 supports staggered outputs by programming different on-time values per channel. This maps to phase shift: on_time = (phase_degrees / 360.0) * 4096

Parameters
channel_idChannel number (0-15).
phase_shift_degreesPhase offset in degrees (0-360).
Returns
PWM_SUCCESS or error code.

◆ StartAll()

hf_pwm_err_t Pca9685PwmAdapter::StartAll ( )
overridenoexcept

Wake the PCA9685 from sleep (start all channels).

Returns
PWM_SUCCESS or error code.
Here is the call graph for this function:

◆ StopAll()

hf_pwm_err_t Pca9685PwmAdapter::StopAll ( )
overridenoexcept

Put the PCA9685 into sleep mode (stop all outputs).

Returns
PWM_SUCCESS or error code.
Here is the call graph for this function:

◆ UpdateAll()

hf_pwm_err_t Pca9685PwmAdapter::UpdateAll ( )
overridenoexcept

No-op (PCA9685 updates immediately on register write).

Returns
PWM_SUCCESS.

◆ validateChannel()

bool Pca9685PwmAdapter::validateChannel ( hf_channel_id_t channel_id) const
inlineprivatenoexcept

Validate channel ID is in range 0-15.

Friends And Related Symbol Documentation

◆ Pca9685Handler

friend class Pca9685Handler
friend

Allow handler to access internals.

Member Data Documentation

◆ channel_enabled_

std::array<bool, kMaxChannels> Pca9685PwmAdapter::channel_enabled_
private

Channel enable state.

◆ current_frequency_hz_

hf_frequency_hz_t Pca9685PwmAdapter::current_frequency_hz_ = 200
private

Current global frequency.

◆ duty_cache_

std::array<float, kMaxChannels> Pca9685PwmAdapter::duty_cache_
private

Cached duty cycles (0.0-1.0).

◆ kMaxChannels

constexpr uint8_t Pca9685PwmAdapter::kMaxChannels = 16
staticconstexpr

Number of PWM channels on the PCA9685.

◆ kMaxFrequencyHz

constexpr uint32_t Pca9685PwmAdapter::kMaxFrequencyHz = 1526
staticconstexpr

Maximum PWM frequency in Hz.

◆ kMaxRawValue

constexpr uint16_t Pca9685PwmAdapter::kMaxRawValue = 4095
staticconstexpr

Maximum raw PWM value (2^12 - 1 = 4095).

◆ kMinFrequencyHz

constexpr uint32_t Pca9685PwmAdapter::kMinFrequencyHz = 24
staticconstexpr

Minimum PWM frequency in Hz.

◆ kResolutionBits

constexpr uint8_t Pca9685PwmAdapter::kResolutionBits = 12
staticconstexpr

PWM resolution in bits.

◆ on_time_cache_

std::array<uint16_t, kMaxChannels> Pca9685PwmAdapter::on_time_cache_
private

Cached on-time values (phase offset).

◆ parent_handler_

Pca9685Handler* Pca9685PwmAdapter::parent_handler_
private

Owning handler (not owned).


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