ADS7952 Driver
API reference
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
80template <typename SpiType>
81class ADS7952 {
82public:
83 // ===========================================================================
84 // Construction
85 // ===========================================================================
86
95 explicit ADS7952(SpiType &spi,
96 float vref = ADS7952_CFG::DEFAULT_VREF,
97 float va = ADS7952_CFG::DEFAULT_VA,
98 Range initial_range = ADS7952_CFG::DEFAULT_RANGE) noexcept;
99
100 ADS7952(const ADS7952 &) = delete;
101 ADS7952 &operator=(const ADS7952 &) = delete;
102
103 // ===========================================================================
104 // Initialization
105 // ===========================================================================
106
119 bool EnsureInitialized(bool force = false) noexcept;
120
122 bool IsInitialized() const noexcept { return initialized_; }
123
124 // ===========================================================================
125 // Channel Reading
126 // ===========================================================================
127
134 ReadResult ReadChannel(uint8_t channel) noexcept;
135
146
147 // ===========================================================================
148 // Voltage Conversion
149 // ===========================================================================
150
156 float CountToVoltage(uint16_t count) const noexcept;
157
164 static constexpr float CountToVoltage(uint16_t count, float vref) noexcept {
165 return (static_cast<float>(count) * vref) / static_cast<float>(reg::MAX_COUNT);
166 }
167
177 uint16_t VoltageToCount(float voltage) const noexcept;
178
179 // ===========================================================================
180 // Mode Control
181 // ===========================================================================
182
188 bool EnterManualMode(uint8_t channel = 0) noexcept;
189
195 bool EnterAuto1Mode(bool reset_counter = true) noexcept;
196
202 bool EnterAuto2Mode(bool reset_counter = true) noexcept;
203
205 Mode GetMode() const noexcept { return mode_; }
206
207 // ===========================================================================
208 // Programming
209 // ===========================================================================
210
228 bool ProgramAuto1Channels(uint16_t channel_mask) noexcept;
229
235 bool ProgramAuto2LastChannel(uint8_t last_channel) noexcept;
236
248 bool ProgramGPIO(const GPIOConfig &config) noexcept;
249
266 bool ProgramAlarm(uint8_t channel, AlarmBound bound,
267 uint16_t threshold_12bit) noexcept;
268
296 bool ProgramAlarmVoltage(uint8_t channel, AlarmBound bound,
297 float voltage) noexcept;
298
299 // ===========================================================================
300 // Range & Power
301 // ===========================================================================
302
329 bool SetRange(Range range) noexcept;
330
332 Range GetRange() const noexcept { return range_; }
333
346 bool SetPowerDown(PowerDown pd) noexcept;
347
349 PowerDown GetPowerDown() const noexcept {
350 return power_down_;
351 }
352
353 // ===========================================================================
354 // GPIO Output Control
355 // ===========================================================================
356
376 bool SetGPIOOutputs(uint8_t gpio_state) noexcept;
377
379 uint8_t GetGPIOOutputs() const noexcept {
380 return gpio_output_state_;
381 }
382
383 // ===========================================================================
384 // Diagnostics
385 // ===========================================================================
386
388 float GetActiveVref() const noexcept { return active_vref_; }
389
391 float GetVref() const noexcept { return vref_; }
392
394 float GetVA() const noexcept { return va_; }
395
405 void SetVref(float vref) noexcept;
406
417 void SetVA(float va) noexcept;
418
421 uint16_t GetAuto1ChannelMask() const noexcept { return auto1_mask_; }
422
424 uint8_t GetAuto2LastChannel() const noexcept { return auto2_last_ch_; }
425
426 // ===========================================================================
427 // Raw frame trace (bring-up)
428 // ===========================================================================
445 uint8_t RawManualFrames(uint8_t channel, uint16_t* out, uint8_t n) noexcept;
446
447 // ===========================================================================
448 // Version
449 // ===========================================================================
451 static constexpr uint8_t GetDriverVersionMajor() noexcept { return 1; }
453 static constexpr uint8_t GetDriverVersionMinor() noexcept { return 0; }
455 static constexpr uint8_t GetDriverVersionPatch() noexcept { return 0; }
456
457private:
458 // ===========================================================================
459 // Internal Helpers
460 // ===========================================================================
461
468 bool spiTransfer16(uint16_t command, uint16_t* response = nullptr) noexcept;
469
474 bool Initialize() noexcept;
475
477 uint16_t commonControlBits() const noexcept;
478
487 template <typename Commit>
488 bool programControlBits(uint16_t ctrl, Commit commit) noexcept;
489
491 static constexpr uint8_t popcount16(uint16_t x) noexcept;
492
493 // ===========================================================================
494 // State
495 // ===========================================================================
496 SpiType &spi_;
497
498 // Voltage references
499 float vref_;
500 float va_;
501 float two_vref_;
502 float active_vref_;
503
504 // Device state tracking
505 bool initialized_ = false;
506 Mode mode_ = Mode::Manual;
507 Range range_ = ADS7952_CFG::DEFAULT_RANGE;
508 PowerDown power_down_ = PowerDown::Normal;
509
510 // Programming state tracking
511 uint16_t auto1_mask_ = ADS7952_CFG::DEFAULT_AUTO1_MASK;
512 uint8_t auto2_last_ch_ = ADS7952_CFG::DEFAULT_AUTO2_LAST_CH;
513 uint8_t gpio_output_state_ = 0;
514 uint8_t manual_channel_ = 0;
515};
516
517} // namespace ads7952
518
519// Template implementation — included only at end of this header
520#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:81
bool IsInitialized() const noexcept
Check if the driver has been initialized.
Definition ads7952.hpp:122
ADS7952 & operator=(const ADS7952 &)=delete
static constexpr uint8_t GetDriverVersionMinor() noexcept
Driver semantic minor version.
Definition ads7952.hpp:453
Range GetRange() const noexcept
Get the current input range setting.
Definition ads7952.hpp:332
uint8_t GetGPIOOutputs() const noexcept
Get the GPIO output levels last written (bits [3:0]).
Definition ads7952.hpp:379
Mode GetMode() const noexcept
Get the current operating mode.
Definition ads7952.hpp:205
float GetVA() const noexcept
Get the configured VA supply voltage.
Definition ads7952.hpp:394
float GetActiveVref() const noexcept
Get the active voltage reference (affected by range setting).
Definition ads7952.hpp:388
uint16_t GetAuto1ChannelMask() const noexcept
Get the Auto-1 programmed channel mask. Bit ordering: bit N = channel N (bit 0 = CH0,...
Definition ads7952.hpp:421
float GetVref() const noexcept
Get the configured Vref (REFP pin voltage).
Definition ads7952.hpp:391
PowerDown GetPowerDown() const noexcept
Get the current power-down setting.
Definition ads7952.hpp:349
uint8_t GetAuto2LastChannel() const noexcept
Get the Auto-2 programmed last channel.
Definition ads7952.hpp:424
static constexpr uint8_t GetDriverVersionMajor() noexcept
Driver semantic major version.
Definition ads7952.hpp:451
static constexpr uint8_t GetDriverVersionPatch() noexcept
Driver semantic patch version.
Definition ads7952.hpp:455
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:244
bool SetGPIOOutputs(uint8_t gpio_state) noexcept
Set GPIO output pin levels (for pins configured as outputs).
Definition ads7952.ipp:508
ChannelReadings ReadAllChannels() noexcept
Read all channels in the current Auto-1 sequence.
Definition ads7952.ipp:175
void SetVref(float vref) noexcept
Update the Vref value for voltage calculations.
Definition ads7952.ipp:59
bool ProgramAuto2LastChannel(uint8_t last_channel) noexcept
Program the Auto-2 last channel.
Definition ads7952.ipp:331
uint8_t RawManualFrames(uint8_t channel, uint16_t *out, uint8_t n) noexcept
Clock raw 16-bit frames and return every response word unparsed.
Definition ads7952.ipp:520
bool ProgramGPIO(const GPIOConfig &config) noexcept
Program GPIO pin functions, direction, and alarm routing.
Definition ads7952.ipp:349
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:397
bool ProgramAlarmVoltage(uint8_t channel, AlarmBound bound, float voltage) noexcept
Program an alarm threshold for a single channel (voltage-based).
Definition ads7952.ipp:440
void SetVA(float va) noexcept
Update the VA supply voltage value.
Definition ads7952.ipp:67
uint16_t VoltageToCount(float voltage) const noexcept
Convert a voltage to a 12-bit ADC count using the active reference.
Definition ads7952.ipp:251
bool SetPowerDown(PowerDown pd) noexcept
Set the power-down mode; in effect on the chip when this returns.
Definition ads7952.ipp:496
ReadResult ReadChannel(uint8_t channel) noexcept
Read a single ADC channel (switches to Manual mode).
Definition ads7952.ipp:117
bool EnterManualMode(uint8_t channel=0) noexcept
Enter Manual mode, selecting the given channel for conversion.
Definition ads7952.ipp:264
bool EnterAuto2Mode(bool reset_counter=true) noexcept
Enter Auto-2 mode (sequences channels 0 through last_channel).
Definition ads7952.ipp:292
bool EnterAuto1Mode(bool reset_counter=true) noexcept
Enter Auto-1 mode (sequences through programmed channel mask).
Definition ads7952.ipp:279
bool ProgramAuto1Channels(uint16_t channel_mask) noexcept
Program the Auto-1 channel sequence.
Definition ads7952.ipp:309
bool SetRange(Range range) noexcept
Set the input voltage range; in effect on the chip when this returns.
Definition ads7952.ipp:485
bool EnsureInitialized(bool force=false) noexcept
Ensure the driver is initialized — idempotent, safe to call repeatedly.
Definition ads7952.ipp:77
@ Normal
Normal operation.
@ Manual
Host selects channel each frame.
constexpr uint16_t MAX_COUNT
Maximum 12-bit conversion value.