ADS7952 Driver
API reference
Loading...
Searching...
No Matches
ads7952_types.hpp
Go to the documentation of this file.
1
10#pragma once
11#include <cstdint>
12#include <type_traits>
13
14namespace ads7952 {
15
22// =============================================================================
23// Operating Mode
24// =============================================================================
26enum class Mode : uint8_t {
27 Manual,
28 Auto1,
29 Auto2,
30};
31
32// =============================================================================
33// Input Voltage Range
34// =============================================================================
36enum class Range : uint8_t {
37 Vref,
38 TwoVref,
39};
40
41// =============================================================================
42// Power-Down Control
43// =============================================================================
45enum class PowerDown : uint8_t {
46 Normal,
47 PowerDown,
48};
49
50// =============================================================================
51// Alarm Bound Selection
52// =============================================================================
54enum class AlarmBound : uint8_t {
55 Low = 0,
56 High = 1,
57};
58
59// =============================================================================
60// Error Codes
61// =============================================================================
63enum class Error : uint8_t {
64 Ok = 0,
67 SpiError,
70 Timeout,
71};
72
73// =============================================================================
74// GPIO 0/1 Alarm Assignment Modes
75// =============================================================================
82enum class GPIO01AlarmMode : uint8_t {
83 GPIO = 0,
85 GPIO0_HighAlarm = 2,
86 GPIO1_LowAlarm = 4,
89 GPIO1_HighAlarm [[deprecated("DI06:04 = 100 is the GPIO1 LOW alarm (SLAS605C Table 11); use "
90 "GPIO1_LowAlarm")]] = GPIO1_LowAlarm,
91};
92
93// =============================================================================
94// GPIO Pin Index Constants (usable with SetGPIOOutputs and GPIOConfig)
95// =============================================================================
101namespace gpio {
103 inline constexpr uint8_t kGPIO0 = 0x01;
105 inline constexpr uint8_t kGPIO1 = 0x02;
107 inline constexpr uint8_t kGPIO2 = 0x04;
109 inline constexpr uint8_t kGPIO3 = 0x08;
111 inline constexpr uint8_t kAll = 0x0F;
113 inline constexpr uint8_t kNone = 0x00;
114} // namespace gpio
115
116// =============================================================================
117// GPIO Configuration Structure
118// =============================================================================
137
138// =============================================================================
139// Single-Channel Read Result
140// =============================================================================
143 uint16_t count = 0;
144 float voltage = 0.0f;
145 uint8_t channel = 0;
147
149 bool ok() const noexcept { return error == Error::Ok; }
150};
151
152// =============================================================================
153// Multi-Channel Readings Container
154// =============================================================================
157 static constexpr uint8_t MAX_CHANNELS = 12;
158
159 uint16_t count[MAX_CHANNELS] = {};
160 float voltage[MAX_CHANNELS] = {};
161
175 uint16_t valid_mask = 0;
176
178
180 bool ok() const noexcept { return error == Error::Ok; }
181
183 bool hasChannel(uint8_t ch) const noexcept {
184 return ch < MAX_CHANNELS && (valid_mask & (1U << ch));
185 }
186
191 uint8_t validChannelCount() const noexcept {
192 uint16_t x = valid_mask;
193 x = x - ((x >> 1) & 0x5555);
194 x = (x & 0x3333) + ((x >> 2) & 0x3333);
195 return static_cast<uint8_t>((((x + (x >> 4)) & 0x0F0F) * 0x0101) >> 8);
196 }
197};
198
199// =============================================================================
200// Channel Mask Helpers
201// =============================================================================
223template <typename... Channels>
224constexpr uint16_t ChannelMask(Channels... channels) noexcept {
225 static_assert((std::is_integral_v<Channels> && ...),
226 "ChannelMask arguments must be integers");
227 uint16_t mask = 0;
228 ((mask |= static_cast<uint16_t>(1U << (static_cast<uint8_t>(channels) & 0x0F))), ...);
229 return mask & 0x0FFF;
230}
231
239constexpr uint16_t ChannelRangeMask(uint8_t first, uint8_t last) noexcept {
240 if (first > 11 || last > 11 || first > last) return 0;
241 // (1 << (last+1)) - (1 << first) generates contiguous set bits
242 return static_cast<uint16_t>(
243 ((1U << (last + 1)) - (1U << first)) & 0x0FFF);
244}
245
250inline constexpr uint16_t kAllChannels = 0x0FFF;
251inline constexpr uint16_t kEvenChannels = 0x0555;
252inline constexpr uint16_t kOddChannels = 0x0AAA;
253inline constexpr uint16_t kFirstFour = 0x000F;
254inline constexpr uint16_t kSecondFour = 0x00F0;
255inline constexpr uint16_t kThirdFour = 0x0F00;
257
258// =============================================================================
259// Voltage-Count Conversion Helpers
260// =============================================================================
284constexpr uint16_t VoltageToCount(float voltage, float vref) noexcept {
285 if (vref <= 0.0f || voltage <= 0.0f) return 0;
286 float raw = (voltage / vref) * 4095.0f;
287 if (raw > 4095.0f) return 4095;
288 return static_cast<uint16_t>(raw);
289}
290
291} // namespace ads7952
constexpr uint16_t ChannelRangeMask(uint8_t first, uint8_t last) noexcept
Build a contiguous channel range mask from first to last (inclusive).
constexpr uint16_t ChannelMask(Channels... channels) noexcept
Construct a channel enable bitmask from a list of channel numbers.
constexpr uint16_t VoltageToCount(float voltage, float vref) noexcept
Convert a voltage to a 12-bit ADC count.
constexpr uint8_t kGPIO0
Bit 0 — GPIO pin 0.
constexpr uint8_t kGPIO1
Bit 1 — GPIO pin 1.
constexpr uint8_t kAll
All 4 GPIO pins.
constexpr uint8_t kGPIO2
Bit 2 — GPIO pin 2.
constexpr uint8_t kNone
No GPIO pins.
constexpr uint8_t kGPIO3
Bit 3 — GPIO pin 3.
GPIO01AlarmMode
GPIO0/GPIO1 alarm functions, the DI06:04 codes of the GPIO program register (SLAS605C Table 11)....
@ GPIO0_HighAndLowAlarm
xx1: GPIO0 = combined high-or-low alarm output
@ GPIO0_HighAlarm
010: GPIO0 = high alarm output
@ GPIO
000: both as general-purpose I/O
@ GPIO1_LowAlarm
100: GPIO1 = low alarm output
@ Normal
Normal operation.
@ Auto2
Device sequences channels 0 to last_channel.
@ Auto1
Device sequences through programmed channel mask.
@ Manual
Host selects channel each frame.
@ Vref
0 to Vref (RANGE bit = 0)
@ TwoVref
0 to 2*Vref, clamped to VA (RANGE bit = 1)
@ Low
Low alarm threshold register.
@ High
High alarm threshold register.
@ SpiError
SPI transfer failure.
@ ProgrammingFailed
Device programming did not complete.
@ InvalidChannel
Channel number out of range (0-11)
@ NotInitialized
Driver not initialized.
@ Ok
Operation succeeded.
@ Timeout
Operation timed out waiting for data.
@ ModeChangeFailed
Mode transition did not complete.
constexpr uint16_t kOddChannels
CH1, CH3, CH5, CH7, CH9, CH11.
constexpr uint16_t kFirstFour
CH0–CH3.
constexpr uint16_t kThirdFour
CH8–CH11.
constexpr uint16_t kSecondFour
CH4–CH7.
constexpr uint16_t kAllChannels
CH0–CH11 (all 12)
constexpr uint16_t kEvenChannels
CH0, CH2, CH4, CH6, CH8, CH10.
uint16_t valid_mask
Bitmask indicating which channels contain valid data.
bool hasChannel(uint8_t ch) const noexcept
Check if a specific channel has valid data in this reading.
static constexpr uint8_t MAX_CHANNELS
uint16_t count[MAX_CHANNELS]
Raw counts per channel.
bool ok() const noexcept
True when error equals Error::Ok.
uint8_t validChannelCount() const noexcept
Count channels currently marked valid.
float voltage[MAX_CHANNELS]
Converted voltages per channel.
bool gpio3_as_powerdown_input
GPIO3 controls power-down externally.
uint8_t direction_mask
GPIO direction control — bits [3:0], bit N = GPIO pin N.
bool gpio2_as_range_input
GPIO2 controls RANGE externally.
GPIO01AlarmMode alarm_mode
bool reset_all_registers
Reset all programming registers.
uint8_t channel
Channel that was read (from response)
uint16_t count
Raw 12-bit ADC count (0-4095)
bool ok() const noexcept
True when error equals Error::Ok.
float voltage
Converted voltage.