HF-ADS7952 Driver 0.1.0-dev
HF-ADS7952 C++ Driver
Loading...
Searching...
No Matches
ads7952.hpp
Go to the documentation of this file.
1
40#pragma once
41
42#define ADS7952_HEADER_INCLUDED
43
44#include <cstdint>
45#include <cstddef>
46#include <algorithm>
47
48#include "ads7952_config.hpp"
49#include "ads7952_registers.hpp"
51#include "ads7952_types.hpp"
52
53namespace ads7952 {
54
75template <typename SpiType>
76class ADS7952 {
77public:
78 // ===========================================================================
79 // Construction
80 // ===========================================================================
81
90 explicit ADS7952(SpiType &spi,
91 float vref = ADS7952_CFG::DEFAULT_VREF,
92 float va = ADS7952_CFG::DEFAULT_VA,
93 Range initial_range = ADS7952_CFG::DEFAULT_RANGE) noexcept;
94
95 ADS7952(const ADS7952 &) = delete;
96 ADS7952 &operator=(const ADS7952 &) = delete;
97
98 // ===========================================================================
99 // Initialization
100 // ===========================================================================
101
114 bool EnsureInitialized(bool force = false) noexcept;
115
117 bool IsInitialized() const noexcept { return initialized_; }
118
119 // ===========================================================================
120 // Channel Reading
121 // ===========================================================================
122
129 ReadResult ReadChannel(uint8_t channel) noexcept;
130
141
142 // ===========================================================================
143 // Voltage Conversion
144 // ===========================================================================
145
151 float CountToVoltage(uint16_t count) const noexcept;
152
159 static constexpr float CountToVoltage(uint16_t count, float vref) noexcept {
160 return (static_cast<float>(count) * vref) / static_cast<float>(reg::MAX_COUNT);
161 }
162
172 uint16_t VoltageToCount(float voltage) const noexcept;
173
174 // ===========================================================================
175 // Mode Control
176 // ===========================================================================
177
183 bool EnterManualMode(uint8_t channel = 0) noexcept;
184
190 bool EnterAuto1Mode(bool reset_counter = true) noexcept;
191
197 bool EnterAuto2Mode(bool reset_counter = true) noexcept;
198
200 Mode GetMode() const noexcept { return mode_; }
201
202 // ===========================================================================
203 // Programming
204 // ===========================================================================
205
223 bool ProgramAuto1Channels(uint16_t channel_mask) noexcept;
224
230 bool ProgramAuto2LastChannel(uint8_t last_channel) noexcept;
231
243 bool ProgramGPIO(const GPIOConfig &config) noexcept;
244
261 bool ProgramAlarm(uint8_t channel, AlarmBound bound,
262 uint16_t threshold_12bit) noexcept;
263
291 bool ProgramAlarmVoltage(uint8_t channel, AlarmBound bound,
292 float voltage) noexcept;
293
294 // ===========================================================================
295 // Range & Power
296 // ===========================================================================
297
315 bool SetRange(Range range) noexcept;
316
318 Range GetRange() const noexcept { return range_; }
319
325 bool SetPowerDown(PowerDown pd) noexcept;
326
327 // ===========================================================================
328 // GPIO Output Control
329 // ===========================================================================
330
343 void SetGPIOOutputs(uint8_t gpio_state) noexcept;
344
345 // ===========================================================================
346 // Diagnostics
347 // ===========================================================================
348
350 float GetActiveVref() const noexcept { return active_vref_; }
351
353 float GetVref() const noexcept { return vref_; }
354
356 float GetVA() const noexcept { return va_; }
357
367 void SetVref(float vref) noexcept;
368
379 void SetVA(float va) noexcept;
380
383 uint16_t GetAuto1ChannelMask() const noexcept { return auto1_mask_; }
384
386 uint8_t GetAuto2LastChannel() const noexcept { return auto2_last_ch_; }
387
388 // ===========================================================================
389 // Version
390 // ===========================================================================
392 static constexpr uint8_t GetDriverVersionMajor() noexcept { return 1; }
394 static constexpr uint8_t GetDriverVersionMinor() noexcept { return 0; }
396 static constexpr uint8_t GetDriverVersionPatch() noexcept { return 0; }
397
398private:
399 // ===========================================================================
400 // Internal Helpers
401 // ===========================================================================
402
404 uint16_t spiTransfer16(uint16_t command) noexcept;
405
410 bool Initialize() noexcept;
411
413 uint16_t commonControlBits() const noexcept;
414
416 static constexpr uint8_t popcount16(uint16_t x) noexcept;
417
418 // ===========================================================================
419 // State
420 // ===========================================================================
421 SpiType &spi_;
422
423 // Voltage references
424 float vref_;
425 float va_;
426 float two_vref_;
427 float active_vref_;
428
429 // Device state tracking
430 bool initialized_ = false;
431 Mode mode_ = Mode::Manual;
432 Range range_ = ADS7952_CFG::DEFAULT_RANGE;
433 PowerDown power_down_ = PowerDown::Normal;
434
435 // Programming state tracking
436 uint16_t auto1_mask_ = ADS7952_CFG::DEFAULT_AUTO1_MASK;
437 uint8_t auto2_last_ch_ = ADS7952_CFG::DEFAULT_AUTO2_LAST_CH;
438 uint8_t gpio_output_state_ = 0;
439};
440
441} // namespace ads7952
442
443// Template implementation — included only at end of this header
444#include "ads7952.ipp"
Compile-time configuration options for the ADS7952 driver.
Register definitions and SPI frame constants for the ADS7952.
CRTP-based SPI bus interface for ADS7952 driver.
Type definitions for the ADS7952 driver.
Main driver class for the ADS7952 ADC.
Definition ads7952.hpp:76
bool IsInitialized() const noexcept
Check if the driver has been initialized.
Definition ads7952.hpp:117
ADS7952 & operator=(const ADS7952 &)=delete
static constexpr uint8_t GetDriverVersionMinor() noexcept
Driver semantic minor version.
Definition ads7952.hpp:394
Range GetRange() const noexcept
Get the current input range setting.
Definition ads7952.hpp:318
Mode GetMode() const noexcept
Get the current operating mode.
Definition ads7952.hpp:200
float GetVA() const noexcept
Get the configured VA supply voltage.
Definition ads7952.hpp:356
float GetActiveVref() const noexcept
Get the active voltage reference (affected by range setting).
Definition ads7952.hpp:350
uint16_t GetAuto1ChannelMask() const noexcept
Get the Auto-1 programmed channel mask. Bit ordering: bit N = channel N (bit 0 = CH0,...
Definition ads7952.hpp:383
float GetVref() const noexcept
Get the configured Vref (REFP pin voltage).
Definition ads7952.hpp:353
uint8_t GetAuto2LastChannel() const noexcept
Get the Auto-2 programmed last channel.
Definition ads7952.hpp:386
static constexpr uint8_t GetDriverVersionMajor() noexcept
Driver semantic major version.
Definition ads7952.hpp:392
static constexpr uint8_t GetDriverVersionPatch() noexcept
Driver semantic patch version.
Definition ads7952.hpp:396
ADS7952(const ADS7952 &)=delete
constexpr float DEFAULT_VA
constexpr ads7952::Range DEFAULT_RANGE
constexpr float DEFAULT_VREF
float CountToVoltage(uint16_t count) const noexcept
Convert raw count to voltage using the currently active reference.
Definition ads7952.ipp:192
ChannelReadings ReadAllChannels() noexcept
Read all channels in the current Auto-1 sequence.
Definition ads7952.ipp:135
void SetVref(float vref) noexcept
Update the Vref value for voltage calculations.
Definition ads7952.ipp:45
bool ProgramAuto2LastChannel(uint8_t last_channel) noexcept
Program the Auto-2 last channel.
Definition ads7952.ipp:268
void SetGPIOOutputs(uint8_t gpio_state) noexcept
Set GPIO output pin levels (for pins configured as outputs).
Definition ads7952.ipp:414
bool ProgramGPIO(const GPIOConfig &config) noexcept
Program GPIO pin functions, direction, and alarm routing.
Definition ads7952.ipp:284
bool ProgramAlarm(uint8_t channel, AlarmBound bound, uint16_t threshold_12bit) noexcept
Program an alarm threshold for a single channel (count-based).
Definition ads7952.ipp:333
bool ProgramAlarmVoltage(uint8_t channel, AlarmBound bound, float voltage) noexcept
Program an alarm threshold for a single channel (voltage-based).
Definition ads7952.ipp:374
void SetVA(float va) noexcept
Update the VA supply voltage value.
Definition ads7952.ipp:53
uint16_t VoltageToCount(float voltage) const noexcept
Convert a voltage to a 12-bit ADC count using the active reference.
Definition ads7952.ipp:199
bool SetPowerDown(PowerDown pd) noexcept
Set the power-down mode.
Definition ads7952.ipp:399
ReadResult ReadChannel(uint8_t channel) noexcept
Read a single ADC channel (switches to Manual mode).
Definition ads7952.ipp:101
bool EnterManualMode(uint8_t channel=0) noexcept
Enter Manual mode, selecting the given channel for conversion.
Definition ads7952.ipp:212
bool EnterAuto2Mode(bool reset_counter=true) noexcept
Enter Auto-2 mode (sequences channels 0 through last_channel).
Definition ads7952.ipp:235
bool EnterAuto1Mode(bool reset_counter=true) noexcept
Enter Auto-1 mode (sequences through programmed channel mask).
Definition ads7952.ipp:224
bool ProgramAuto1Channels(uint16_t channel_mask) noexcept
Program the Auto-1 channel sequence.
Definition ads7952.ipp:250
bool SetRange(Range range) noexcept
Set the input voltage range and update active reference.
Definition ads7952.ipp:385
bool EnsureInitialized(bool force=false) noexcept
Ensure the driver is initialized — idempotent, safe to call repeatedly.
Definition ads7952.ipp:63
ADS7952(SpiType &spi, float vref=ADS7952_CFG::DEFAULT_VREF, float va=ADS7952_CFG::DEFAULT_VA, Range initial_range=ADS7952_CFG::DEFAULT_RANGE) noexcept
Construct a new ADS7952 driver instance.
Definition ads7952.ipp:32
@ Normal
Normal operation.
@ Manual
Host selects channel each frame.
constexpr uint16_t MAX_COUNT
Maximum 12-bit conversion value.