HardFOC Core
API reference
Loading...
Searching...
No Matches
Ads7952Handler.h
Go to the documentation of this file.
1
26#ifndef COMPONENT_HANDLER_ADS7952_HANDLER_H_
27#define COMPONENT_HANDLER_ADS7952_HANDLER_H_
28
29#include <cstdint>
30#include <memory>
31#include <type_traits>
32#include <utility>
33#include "core/hf-core-drivers/external/hf-ads7952-driver/inc/ads7952.hpp"
34#include "base/BaseSpi.h"
35#include "base/BaseAdc.h"
36#include "RtosMutex.h"
37
38//======================================================//
39// ADS7952 SPI BRIDGE ADAPTER (CRTP — zero virtual overhead)
40//======================================================//
41
51class Ads7952SpiAdapter : public ads7952::SpiInterface<Ads7952SpiAdapter> {
52public:
57 explicit Ads7952SpiAdapter(BaseSpi& spi_interface) noexcept;
58
67 bool transfer(const uint8_t* tx, uint8_t* rx, std::size_t len) noexcept;
68
69private:
71};
72
73//======================================================//
74// ADS7952 HANDLER CONFIGURATION
75//======================================================//
76
81 float vref;
82 float va;
83 ads7952::Range range;
84 ads7952::Mode initial_mode;
87 uint8_t device_index;
88};
89
96 .vref = 2.5f,
97 .va = 5.0f,
98 .range = ads7952::Range::TwoVref,
99 .initial_mode = ads7952::Mode::Manual,
100 .auto1_channel_mask = ads7952::kAllChannels,
101 .auto2_last_channel = 11,
102 .device_index = 0
103 };
104}
105
106//======================================================//
107// ADS7952 HANDLER DIAGNOSTICS
108//======================================================//
109
116 ads7952::Mode current_mode;
117 ads7952::Range current_range;
118 float vref;
120 uint32_t total_reads;
121 uint32_t error_count;
122 uint8_t device_index;
123};
124
125//======================================================//
126// ADS7952 HANDLER CLASS — implements BaseAdc
127//======================================================//
128
149class Ads7952Handler : public BaseAdc {
150public:
151 //======================================================//
152 // CONSTRUCTION AND LIFECYCLE
153 //======================================================//
154
163 explicit Ads7952Handler(BaseSpi& spi_interface,
164 const Ads7952HandlerConfig& config = GetDefaultAds7952Config()) noexcept;
165
167 ~Ads7952Handler() noexcept override = default;
168
169 // Non-copyable, non-movable
171 Ads7952Handler& operator=(const Ads7952Handler&) = delete;
173 Ads7952Handler& operator=(Ads7952Handler&&) = delete;
174
175 //======================================================//
176 // BaseAdc INTERFACE IMPLEMENTATION
177 //======================================================//
178
186 bool Initialize() noexcept override;
187
192 bool Deinitialize() noexcept override;
193
198 hf_u8_t GetMaxChannels() const noexcept override;
199
205 bool IsChannelAvailable(hf_channel_id_t channel) const noexcept override;
206
215 hf_adc_err_t ReadChannelV(hf_channel_id_t channel, float& voltage,
216 hf_u8_t samples = 1, hf_time_t timeout_ms = 0) noexcept override;
217
226 hf_adc_err_t ReadChannelCount(hf_channel_id_t channel, hf_u32_t& count,
227 hf_u8_t samples = 1, hf_time_t timeout_ms = 0) noexcept override;
228
238 hf_adc_err_t ReadChannel(hf_channel_id_t channel, hf_u32_t& count, float& voltage,
239 hf_u8_t samples = 1, hf_time_t timeout_ms = 0) noexcept override;
240
249 hf_adc_err_t ReadMultipleChannels(const hf_channel_id_t* channels, hf_u8_t num_channels,
250 hf_u32_t* counts, float* voltages) noexcept override;
251
252 //======================================================//
253 // ADS7952-SPECIFIC METHODS
254 //======================================================//
255
261 bool ReadAllChannels(ads7952::ChannelReadings& readings) noexcept;
262
269 bool ReadAuto1Channels(uint16_t mask,
270 ads7952::ChannelReadings& readings) noexcept;
271
279 uint8_t RawManualFrames(uint8_t channel, uint16_t* out, uint8_t n) noexcept;
280
288 bool ProgramAlarm(uint8_t channel, ads7952::AlarmBound bound, uint16_t threshold_12bit) noexcept;
289
297 bool ProgramAlarmVoltage(uint8_t channel, ads7952::AlarmBound bound, float voltage) noexcept;
298
304 bool SetRange(ads7952::Range range) noexcept;
305
310 float GetActiveVref() const noexcept;
311
312 //======================================================//
313 // DRIVER ACCESS
314 //======================================================//
315
321 ads7952::ADS7952<Ads7952SpiAdapter>* GetDriver() noexcept;
322 const ads7952::ADS7952<Ads7952SpiAdapter>* GetDriver() const noexcept;
323
329 template <typename Fn>
330 auto visitDriver(Fn&& fn) noexcept -> decltype(fn(std::declval<ads7952::ADS7952<Ads7952SpiAdapter>&>())) {
331 using ReturnType = decltype(fn(std::declval<ads7952::ADS7952<Ads7952SpiAdapter>&>()));
332 MutexLockGuard lock(handler_mutex_);
334 if constexpr (std::is_void_v<ReturnType>) {
335 return;
336 } else {
337 return ReturnType{};
338 }
339 }
340 return fn(*adc_driver_);
341 }
342
343 //======================================================//
344 // DIAGNOSTICS
345 //======================================================//
346
348 const char* GetDescription() const noexcept;
349
351 bool GetHandlerDiagnostics(Ads7952Diagnostics& diag) const noexcept;
352
354 void DumpDiagnostics() const noexcept;
355
357 uint8_t GetDeviceIndex() const noexcept { return config_.device_index; }
358
359private:
360 //======================================================//
361 // PRIVATE MEMBERS
362 //======================================================//
363
364 BaseSpi& spi_ref_;
365 std::unique_ptr<Ads7952SpiAdapter> spi_adapter_;
366 std::unique_ptr<ads7952::ADS7952<Ads7952SpiAdapter>> adc_driver_;
368 mutable RtosMutex handler_mutex_;
369 char description_[64];
370
371 // Statistics
372 mutable uint32_t total_reads_{0};
373 mutable uint32_t error_count_{0};
374
375 //======================================================//
376 // PRIVATE HELPERS
377 //======================================================//
378
380 bool EnsureInitializedLocked() noexcept;
381
383 bool ApplyConfiguration() noexcept;
384
386 ads7952::ReadResult ReadChannelLocked(uint8_t channel) noexcept;
387};
388
389//======================================================//
390// FACTORY METHODS
391//======================================================//
392
399std::unique_ptr<Ads7952Handler> CreateAds7952Handler(
400 BaseSpi& spi_interface,
401 const Ads7952HandlerConfig& config = GetDefaultAds7952Config()) noexcept;
402
403#endif // COMPONENT_HANDLER_ADS7952_HANDLER_H_
std::unique_ptr< Ads7952Handler > CreateAds7952Handler(BaseSpi &spi_interface, const Ads7952HandlerConfig &config=GetDefaultAds7952Config()) noexcept
Create ADS7952 handler instance.
Definition Ads7952Handler.cpp:486
Ads7952HandlerConfig GetDefaultAds7952Config() noexcept
Default ADS7952 handler config (2.5 V Vref, 5.0 V VA, 2×Vref range).
Definition Ads7952Handler.h:94
Unified handler for ADS7952 12-channel SAR ADC with BaseAdc integration.
Definition Ads7952Handler.h:149
hf_adc_err_t ReadMultipleChannels(const hf_channel_id_t *channels, hf_u8_t num_channels, hf_u32_t *counts, float *voltages) noexcept override
Read multiple channels in a single batch using Auto-1 mode.
Definition Ads7952Handler.cpp:233
bool Initialize() noexcept override
Initialize the ADS7952 ADC (lazy initialization).
Definition Ads7952Handler.cpp:60
bool ProgramAlarm(uint8_t channel, ads7952::AlarmBound bound, uint16_t threshold_12bit) noexcept
Program alarm threshold for a channel (in 12-bit counts).
Definition Ads7952Handler.cpp:322
std::unique_ptr< Ads7952SpiAdapter > spi_adapter_
CRTP SPI adapter.
Definition Ads7952Handler.h:365
~Ads7952Handler() noexcept override=default
Destructor — automatically cleans up driver resources.
bool SetRange(ads7952::Range range) noexcept
Set the ADC input range.
Definition Ads7952Handler.cpp:336
bool ProgramAlarmVoltage(uint8_t channel, ads7952::AlarmBound bound, float voltage) noexcept
Program alarm threshold for a channel (in voltage).
Definition Ads7952Handler.cpp:329
BaseSpi & spi_ref_
Reference to SPI interface.
Definition Ads7952Handler.h:364
uint32_t total_reads_
Successful read count.
Definition Ads7952Handler.h:372
Ads7952HandlerConfig config_
Handler configuration.
Definition Ads7952Handler.h:367
bool ApplyConfiguration() noexcept
Apply configuration to the driver after init.
Definition Ads7952Handler.cpp:445
bool IsChannelAvailable(hf_channel_id_t channel) const noexcept override
Check if a channel is available (0–11).
Definition Ads7952Handler.cpp:147
bool EnsureInitializedLocked() noexcept
Ensure initialized while mutex is already held.
Definition Ads7952Handler.cpp:437
hf_adc_err_t ReadChannel(hf_channel_id_t channel, hf_u32_t &count, float &voltage, hf_u8_t samples=1, hf_time_t timeout_ms=0) noexcept override
Read a channel and return both raw count and voltage.
Definition Ads7952Handler.cpp:205
uint8_t GetDeviceIndex() const noexcept
Get the logical device index.
Definition Ads7952Handler.h:357
bool GetHandlerDiagnostics(Ads7952Diagnostics &diag) const noexcept
Get current diagnostics snapshot.
Definition Ads7952Handler.cpp:373
uint8_t RawManualFrames(uint8_t channel, uint16_t *out, uint8_t n) noexcept
Bring-up trace: raw 16-bit response words for a manual select.
Definition Ads7952Handler.cpp:315
RtosMutex handler_mutex_
Thread safety mutex.
Definition Ads7952Handler.h:368
auto visitDriver(Fn &&fn) noexcept -> decltype(fn(std::declval< ads7952::ADS7952< Ads7952SpiAdapter > & >()))
Visit the underlying ADS7952 driver under handler mutex protection.
Definition Ads7952Handler.h:330
ads7952::ADS7952< Ads7952SpiAdapter > * GetDriver() noexcept
Get raw pointer to the ADS7952 driver for advanced operations.
Definition Ads7952Handler.cpp:354
std::unique_ptr< ads7952::ADS7952< Ads7952SpiAdapter > > adc_driver_
ADS7952 driver.
Definition Ads7952Handler.h:366
hf_adc_err_t ReadChannelV(hf_channel_id_t channel, float &voltage, hf_u8_t samples=1, hf_time_t timeout_ms=0) noexcept override
Read a channel and return voltage.
Definition Ads7952Handler.cpp:155
char description_[64]
Description string.
Definition Ads7952Handler.h:369
float GetActiveVref() const noexcept
Get the current effective reference voltage.
Definition Ads7952Handler.cpp:344
void DumpDiagnostics() const noexcept
Dump comprehensive diagnostics to log.
Definition Ads7952Handler.cpp:387
hf_u8_t GetMaxChannels() const noexcept override
Get maximum number of ADC channels (always 12 for ADS7952).
Definition Ads7952Handler.cpp:143
ads7952::ReadResult ReadChannelLocked(uint8_t channel) noexcept
Perform a single-channel read under lock (no mutex acquire).
Definition Ads7952Handler.cpp:475
const char * GetDescription() const noexcept
Get handler description string.
Definition Ads7952Handler.cpp:369
hf_adc_err_t ReadChannelCount(hf_channel_id_t channel, hf_u32_t &count, hf_u8_t samples=1, hf_time_t timeout_ms=0) noexcept override
Read a channel and return raw count.
Definition Ads7952Handler.cpp:180
uint32_t error_count_
Failed read count.
Definition Ads7952Handler.h:373
bool ReadAllChannels(ads7952::ChannelReadings &readings) noexcept
Read all 12 channels using Auto-1 mode.
Definition Ads7952Handler.cpp:289
bool ReadAuto1Channels(uint16_t mask, ads7952::ChannelReadings &readings) noexcept
Auto-1 burst for a channel mask; stay in Auto-1 (no Manual hop).
Definition Ads7952Handler.cpp:293
bool Deinitialize() noexcept override
Deinitialize the ADC and release driver resources.
Definition Ads7952Handler.cpp:126
CRTP adapter connecting BaseSpi interface to ADS7952 SpiInterface.
Definition Ads7952Handler.h:51
BaseSpi & spi_interface_
Definition Ads7952Handler.h:70
bool transfer(const uint8_t *tx, uint8_t *rx, std::size_t len) noexcept
Perform full-duplex SPI transfer (CRTP dispatch target).
Definition Ads7952Handler.cpp:28
Diagnostic information for an ADS7952 handler instance.
Definition Ads7952Handler.h:113
ads7952::Range current_range
Current input range setting.
Definition Ads7952Handler.h:117
bool initialized
Handler initialization state.
Definition Ads7952Handler.h:114
uint8_t device_index
Logical device index.
Definition Ads7952Handler.h:122
bool driver_ready
Driver instance active and ready.
Definition Ads7952Handler.h:115
uint32_t error_count
Total failed read operations.
Definition Ads7952Handler.h:121
ads7952::Mode current_mode
Current operating mode.
Definition Ads7952Handler.h:116
float active_vref
Effective Vref based on range.
Definition Ads7952Handler.h:119
float vref
Configured Vref.
Definition Ads7952Handler.h:118
uint32_t total_reads
Total successful read operations.
Definition Ads7952Handler.h:120
Configuration for ADS7952 handler initialization.
Definition Ads7952Handler.h:80
float vref
External reference voltage (e.g. 2.5V)
Definition Ads7952Handler.h:81
ads7952::Mode initial_mode
Initial operating mode (Manual/Auto1/Auto2)
Definition Ads7952Handler.h:84
uint8_t device_index
Logical index for multi-device setups (0-based)
Definition Ads7952Handler.h:87
ads7952::Range range
Input range (Vref or 2*Vref clamped to VA)
Definition Ads7952Handler.h:83
uint16_t auto1_channel_mask
Channel mask for Auto-1 mode (bit N = CH N)
Definition Ads7952Handler.h:85
float va
Analog supply voltage (e.g. 5.0V)
Definition Ads7952Handler.h:82
uint8_t auto2_last_channel
Last channel for Auto-2 mode (0–11)
Definition Ads7952Handler.h:86