HardFOC Core
API reference
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
Tmc9660Handler::Adc Class Reference

BaseAdc adapter for all TMC9660 ADC channels. More...

#include <Tmc9660Handler.h>

Inheritance diagram for Tmc9660Handler::Adc:

Public Member Functions

 Adc (Tmc9660Handler &parent)
 Construct the ADC wrapper.
 
 ~Adc () noexcept override=default
 Default destructor.
 
BaseAdc Interface Implementation
bool Initialize () noexcept override
 Initialize ADC wrapper (no-op; TMC9660 ADC is always available).
 
bool Deinitialize () noexcept override
 Deinitialize ADC wrapper (no-op).
 
hf_u8_t GetMaxChannels () const noexcept override
 Returns the total number of readable channels (15).
 
bool IsChannelAvailable (hf_channel_id_t channel_id) const noexcept override
 Check if a channel ID is valid per the TMC9660 channel scheme.
 
hf_adc_err_t ReadChannelV (hf_channel_id_t channel_id, float &channel_reading_v, hf_u8_t numOfSamplesToAvg=1, hf_time_t timeBetweenSamples=0) noexcept override
 Read a channel and return the voltage.
 
hf_adc_err_t ReadChannelCount (hf_channel_id_t channel_id, hf_u32_t &channel_reading_count, hf_u8_t numOfSamplesToAvg=1, hf_time_t timeBetweenSamples=0) noexcept override
 Read a channel and return the raw count.
 
hf_adc_err_t ReadChannel (hf_channel_id_t channel_id, hf_u32_t &channel_reading_count, float &channel_reading_v, hf_u8_t numOfSamplesToAvg=1, hf_time_t timeBetweenSamples=0) noexcept override
 Read a channel and return both raw count and voltage.
 
hf_adc_err_t ReadMultipleChannels (const hf_channel_id_t *channel_ids, hf_u8_t num_channels, hf_u32_t *readings, float *voltages) noexcept override
 Read multiple channels in a batch.
 
hf_adc_err_t GetStatistics (hf_adc_statistics_t &statistics) noexcept override
 Get accumulated ADC statistics (conversion counts, timing).
 
hf_adc_err_t GetDiagnostics (hf_adc_diagnostics_t &diagnostics) noexcept override
 Get ADC health diagnostics (error counts, health status).
 
hf_adc_err_t ResetStatistics () noexcept override
 Reset all accumulated statistics to zero.
 
hf_adc_err_t ResetDiagnostics () noexcept override
 Reset diagnostics counters and health flags.
 
TMC9660-Specific Channel Methods

Direct accessors for specific TMC9660 ADC channel types.

hf_adc_err_t ReadAinChannel (uint8_t ain_channel, hf_u32_t &raw_value, float &voltage) noexcept
 Read an external analog input (AIN) channel.
 
hf_adc_err_t ReadCurrentSenseChannel (uint8_t current_channel, hf_u32_t &raw_value, float &voltage) noexcept
 Read a phase current sense channel.
 
hf_adc_err_t ReadVoltageChannel (uint8_t voltage_channel, hf_u32_t &raw_value, float &voltage) noexcept
 Read a voltage monitoring channel.
 
hf_adc_err_t ReadTemperatureChannel (uint8_t temp_channel, hf_u32_t &raw_value, float &voltage) noexcept
 Read a temperature channel.
 
hf_adc_err_t ReadMotorDataChannel (uint8_t motor_channel, hf_u32_t &raw_value, float &voltage) noexcept
 Read a motor data channel.
 

Private Member Functions

hf_adc_err_t ValidateChannelId (hf_channel_id_t channel_id) const noexcept
 Validate a channel ID against all valid ranges.
 
hf_adc_err_t ReadChannelLocked (hf_channel_id_t channel_id, hf_u32_t &raw, float &voltage) noexcept
 Internal channel read that returns both raw count and correct voltage.
 
hf_adc_err_t UpdateStatistics (hf_adc_err_t result, uint64_t start_time_us) noexcept
 Update conversion timing statistics.
 
uint64_t GetCurrentTimeUs () const noexcept
 Get current time in microseconds from RTOS tick counter.
 
void UpdateDiagnostics (hf_adc_err_t error) noexcept
 Update error diagnostics (consecutive error tracking, health flag).
 
const char * GetChannelTypeString (hf_channel_id_t channel_id) const noexcept
 Get a human-readable string for a channel type (for logging).
 

Private Attributes

Tmc9660Handler & parent_
 Owning handler.
 
RtosMutex mutex_
 Thread-safety for statistics.
 
hf_adc_statistics_t statistics_
 Accumulated conversion statistics.
 
hf_adc_diagnostics_t diagnostics_
 Health and error diagnostics.
 
std::atomic< hf_adc_err_t > last_error_
 Most recent error code.
 

Detailed Description

BaseAdc adapter for all TMC9660 ADC channels.

Provides a unified BaseAdc interface for reading the TMC9660's various analog and telemetry data channels. Channels are identified by a numeric ID scheme:

Channel ID Range Type Description
0 - 3 AIN channels External analog inputs (GPIO5)
10 - 13 Current sense Phase current ADC (I0-I3)
20 - 21 Voltage monitor 20=supply, 21=driver voltage
30 - 31 Temperature 30=chip, 31=external NTC
40 - 42 Motor data 40=current, 41=velocity, 42=pos

The wrapper includes thread-safe statistics and diagnostics tracking via RtosMutex. Voltage conversions use a default 3.3V / 16-bit scale; for temperature and motor data channels, the "voltage" field contains the physical value directly (degrees Celsius, mA, etc.).

Note
This inner class is owned by the handler. For host ownership of a BaseAdc*, use Tmc9660AdcWrapper which delegates to this instance.
See also
Tmc9660AdcWrapper Thin delegation wrapper for host BaseAdc ownership

Constructor & Destructor Documentation

◆ Adc()

Tmc9660Handler::Adc::Adc ( Tmc9660Handler &  parent)

Construct the ADC wrapper.

Parameters
parentReference to the owning Tmc9660Handler.

◆ ~Adc()

Tmc9660Handler::Adc::~Adc ( )
overridedefaultnoexcept

Default destructor.

Member Function Documentation

◆ Deinitialize()

bool Tmc9660Handler::Adc::Deinitialize ( )
overridenoexcept

Deinitialize ADC wrapper (no-op).

◆ GetChannelTypeString()

const char * Tmc9660Handler::Adc::GetChannelTypeString ( hf_channel_id_t  channel_id) const
privatenoexcept

Get a human-readable string for a channel type (for logging).

◆ GetCurrentTimeUs()

uint64_t Tmc9660Handler::Adc::GetCurrentTimeUs ( ) const
privatenoexcept

Get current time in microseconds from RTOS tick counter.

◆ GetDiagnostics()

hf_adc_err_t Tmc9660Handler::Adc::GetDiagnostics ( hf_adc_diagnostics_t &  diagnostics)
overridenoexcept

Get ADC health diagnostics (error counts, health status).

◆ GetMaxChannels()

hf_u8_t Tmc9660Handler::Adc::GetMaxChannels ( ) const
overridenoexcept

Returns the total number of readable channels (15).

The TMC9660 uses a non-contiguous channel ID scheme (0-3, 10-13, 20-21, 30-31, 40-42). Since IDs are sparse, callers should use IsChannelAvailable(channel_id) to validate specific channel IDs rather than iterating 0..GetMaxChannels()-1.

Returns
15 (the count of all valid channel IDs across all ranges).

◆ GetStatistics()

hf_adc_err_t Tmc9660Handler::Adc::GetStatistics ( hf_adc_statistics_t &  statistics)
overridenoexcept

Get accumulated ADC statistics (conversion counts, timing).

◆ Initialize()

bool Tmc9660Handler::Adc::Initialize ( )
overridenoexcept

Initialize ADC wrapper (no-op; TMC9660 ADC is always available).

◆ IsChannelAvailable()

bool Tmc9660Handler::Adc::IsChannelAvailable ( hf_channel_id_t  channel_id) const
overridenoexcept

Check if a channel ID is valid per the TMC9660 channel scheme.

Parameters
channel_idChannel ID to validate.
Returns
true if the channel ID is within any valid range.

◆ ReadAinChannel()

hf_adc_err_t Tmc9660Handler::Adc::ReadAinChannel ( uint8_t  ain_channel,
hf_u32_t &  raw_value,
float &  voltage 
)
noexcept

Read an external analog input (AIN) channel.

Parameters
ain_channelAIN channel number (0-3).
[out]raw_valueRaw 16-bit ADC count.
[out]voltageConverted voltage (3.3V / 65535 scale).
Returns
ADC_SUCCESS or ADC_ERR_CHANNEL_READ_ERR.

◆ ReadChannel()

hf_adc_err_t Tmc9660Handler::Adc::ReadChannel ( hf_channel_id_t  channel_id,
hf_u32_t &  channel_reading_count,
float &  channel_reading_v,
hf_u8_t  numOfSamplesToAvg = 1,
hf_time_t  timeBetweenSamples = 0 
)
overridenoexcept

Read a channel and return both raw count and voltage.

Parameters
channel_idChannel ID.
[out]channel_reading_countRaw count.
[out]channel_reading_vVoltage / physical value.
numOfSamplesToAvgNumber of samples to average.
timeBetweenSamplesDelay between samples in ms.
Returns
ADC_SUCCESS or error code.

◆ ReadChannelCount()

hf_adc_err_t Tmc9660Handler::Adc::ReadChannelCount ( hf_channel_id_t  channel_id,
hf_u32_t &  channel_reading_count,
hf_u8_t  numOfSamplesToAvg = 1,
hf_time_t  timeBetweenSamples = 0 
)
overridenoexcept

Read a channel and return the raw count.

Parameters
channel_idChannel ID.
[out]channel_reading_countRaw ADC count or integer value.
numOfSamplesToAvgNumber of samples to average.
timeBetweenSamplesDelay between samples in ms.
Returns
ADC_SUCCESS or error code.

◆ ReadChannelLocked()

hf_adc_err_t Tmc9660Handler::Adc::ReadChannelLocked ( hf_channel_id_t  channel_id,
hf_u32_t &  raw,
float &  voltage 
)
privatenoexcept

Internal channel read that returns both raw count and correct voltage.

Dispatches to the appropriate sub-reader (AIN, current, voltage, temperature, motor data) and returns both the raw count and the correctly-converted voltage. Caller must hold mutex_.

Parameters
channel_idChannel ID per the TMC9660 channel scheme.
[out]rawRaw ADC count or integer value.
[out]voltageCorrectly-converted voltage or physical value.
Returns
ADC_SUCCESS or error code.

◆ ReadChannelV()

hf_adc_err_t Tmc9660Handler::Adc::ReadChannelV ( hf_channel_id_t  channel_id,
float &  channel_reading_v,
hf_u8_t  numOfSamplesToAvg = 1,
hf_time_t  timeBetweenSamples = 0 
)
overridenoexcept

Read a channel and return the voltage.

Parameters
channel_idChannel ID per the scheme above.
[out]channel_reading_vVoltage reading (or physical value for temp/motor channels).
numOfSamplesToAvgNumber of samples to average (currently unused).
timeBetweenSamplesDelay between samples in ms (currently unused).
Returns
ADC_SUCCESS or an appropriate error code.

◆ ReadCurrentSenseChannel()

hf_adc_err_t Tmc9660Handler::Adc::ReadCurrentSenseChannel ( uint8_t  current_channel,
hf_u32_t &  raw_value,
float &  voltage 
)
noexcept

Read a phase current sense channel.

Parameters
current_channelCurrent channel number (0=I0, 1=I1, 2=I2, 3=I3).
[out]raw_valueRaw ADC count.
[out]voltageConverted voltage.
Returns
ADC_SUCCESS or error code.

◆ ReadMotorDataChannel()

hf_adc_err_t Tmc9660Handler::Adc::ReadMotorDataChannel ( uint8_t  motor_channel,
hf_u32_t &  raw_value,
float &  voltage 
)
noexcept

Read a motor data channel.

Parameters
motor_channel0=current (mA), 1=velocity (internal), 2=position (counts).
[out]raw_valueRaw integer value.
[out]voltageFloat representation of the same value.
Returns
ADC_SUCCESS or ADC_ERR_INVALID_CHANNEL.

◆ ReadMultipleChannels()

hf_adc_err_t Tmc9660Handler::Adc::ReadMultipleChannels ( const hf_channel_id_t *  channel_ids,
hf_u8_t  num_channels,
hf_u32_t *  readings,
float *  voltages 
)
overridenoexcept

Read multiple channels in a batch.

Parameters
channel_idsArray of channel IDs to read.
num_channelsNumber of channels.
[out]readingsArray to store raw counts.
[out]voltagesArray to store voltages.
Returns
ADC_SUCCESS if all channels read successfully.

◆ ReadTemperatureChannel()

hf_adc_err_t Tmc9660Handler::Adc::ReadTemperatureChannel ( uint8_t  temp_channel,
hf_u32_t &  raw_value,
float &  voltage 
)
noexcept

Read a temperature channel.

Parameters
temp_channel0=chip temperature (Celsius), 1=external NTC raw.
[out]raw_valueRaw value (temp*100 for chip, raw ADC for external).
[out]voltageTemperature in Celsius (chip) or voltage (external).
Returns
ADC_SUCCESS or ADC_ERR_INVALID_CHANNEL.

◆ ReadVoltageChannel()

hf_adc_err_t Tmc9660Handler::Adc::ReadVoltageChannel ( uint8_t  voltage_channel,
hf_u32_t &  raw_value,
float &  voltage 
)
noexcept

Read a voltage monitoring channel.

Parameters
voltage_channel0=supply voltage, 1=driver voltage.
[out]raw_valueRaw value (voltage * 1000 as integer mV).
[out]voltageVoltage in volts.
Returns
ADC_SUCCESS or ADC_ERR_INVALID_CHANNEL.

◆ ResetDiagnostics()

hf_adc_err_t Tmc9660Handler::Adc::ResetDiagnostics ( )
overridenoexcept

Reset diagnostics counters and health flags.

◆ ResetStatistics()

hf_adc_err_t Tmc9660Handler::Adc::ResetStatistics ( )
overridenoexcept

Reset all accumulated statistics to zero.

◆ UpdateDiagnostics()

void Tmc9660Handler::Adc::UpdateDiagnostics ( hf_adc_err_t  error)
privatenoexcept

Update error diagnostics (consecutive error tracking, health flag).

◆ UpdateStatistics()

hf_adc_err_t Tmc9660Handler::Adc::UpdateStatistics ( hf_adc_err_t  result,
uint64_t  start_time_us 
)
privatenoexcept

Update conversion timing statistics.

◆ ValidateChannelId()

hf_adc_err_t Tmc9660Handler::Adc::ValidateChannelId ( hf_channel_id_t  channel_id) const
privatenoexcept

Validate a channel ID against all valid ranges.

Member Data Documentation

◆ diagnostics_

hf_adc_diagnostics_t Tmc9660Handler::Adc::diagnostics_
mutableprivate

Health and error diagnostics.

◆ last_error_

std::atomic<hf_adc_err_t> Tmc9660Handler::Adc::last_error_
private

Most recent error code.

◆ mutex_

RtosMutex Tmc9660Handler::Adc::mutex_
mutableprivate

Thread-safety for statistics.

◆ parent_

Tmc9660Handler& Tmc9660Handler::Adc::parent_
private

Owning handler.

◆ statistics_

hf_adc_statistics_t Tmc9660Handler::Adc::statistics_
mutableprivate

Accumulated conversion statistics.


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