HF-ADS7952 Driver 0.1.0-dev
HF-ADS7952 C++ Driver
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// =============================================================================
77enum class GPIO01AlarmMode : uint8_t {
78 GPIO = 0,
80 GPIO0_HighAlarm = 2,
81 GPIO1_HighAlarm = 4,
83};
84
85// =============================================================================
86// GPIO Pin Index Constants (usable with SetGPIOOutputs and GPIOConfig)
87// =============================================================================
93namespace gpio {
95 inline constexpr uint8_t kGPIO0 = 0x01;
97 inline constexpr uint8_t kGPIO1 = 0x02;
99 inline constexpr uint8_t kGPIO2 = 0x04;
101 inline constexpr uint8_t kGPIO3 = 0x08;
103 inline constexpr uint8_t kAll = 0x0F;
105 inline constexpr uint8_t kNone = 0x00;
106} // namespace gpio
107
108// =============================================================================
109// GPIO Configuration Structure
110// =============================================================================
129
130// =============================================================================
131// Single-Channel Read Result
132// =============================================================================
135 uint16_t count = 0;
136 float voltage = 0.0f;
137 uint8_t channel = 0;
139
141 bool ok() const noexcept { return error == Error::Ok; }
142};
143
144// =============================================================================
145// Multi-Channel Readings Container
146// =============================================================================
149 static constexpr uint8_t MAX_CHANNELS = 12;
150
151 uint16_t count[MAX_CHANNELS] = {};
152 float voltage[MAX_CHANNELS] = {};
153
167 uint16_t valid_mask = 0;
168
170
172 bool ok() const noexcept { return error == Error::Ok; }
173
175 bool hasChannel(uint8_t ch) const noexcept {
176 return ch < MAX_CHANNELS && (valid_mask & (1U << ch));
177 }
178
183 uint8_t validChannelCount() const noexcept {
184 uint16_t x = valid_mask;
185 x = x - ((x >> 1) & 0x5555);
186 x = (x & 0x3333) + ((x >> 2) & 0x3333);
187 return static_cast<uint8_t>((((x + (x >> 4)) & 0x0F0F) * 0x0101) >> 8);
188 }
189};
190
191// =============================================================================
192// Channel Mask Helpers
193// =============================================================================
215template <typename... Channels>
216constexpr uint16_t ChannelMask(Channels... channels) noexcept {
217 static_assert((std::is_integral_v<Channels> && ...),
218 "ChannelMask arguments must be integers");
219 uint16_t mask = 0;
220 ((mask |= static_cast<uint16_t>(1U << (static_cast<uint8_t>(channels) & 0x0F))), ...);
221 return mask & 0x0FFF;
222}
223
231constexpr uint16_t ChannelRangeMask(uint8_t first, uint8_t last) noexcept {
232 if (first > 11 || last > 11 || first > last) return 0;
233 // (1 << (last+1)) - (1 << first) generates contiguous set bits
234 return static_cast<uint16_t>(
235 ((1U << (last + 1)) - (1U << first)) & 0x0FFF);
236}
237
242inline constexpr uint16_t kAllChannels = 0x0FFF;
243inline constexpr uint16_t kEvenChannels = 0x0555;
244inline constexpr uint16_t kOddChannels = 0x0AAA;
245inline constexpr uint16_t kFirstFour = 0x000F;
246inline constexpr uint16_t kSecondFour = 0x00F0;
247inline constexpr uint16_t kThirdFour = 0x0F00;
249
250// =============================================================================
251// Voltage-Count Conversion Helpers
252// =============================================================================
276constexpr uint16_t VoltageToCount(float voltage, float vref) noexcept {
277 if (vref <= 0.0f || voltage <= 0.0f) return 0;
278 float raw = (voltage / vref) * 4095.0f;
279 if (raw > 4095.0f) return 4095;
280 return static_cast<uint16_t>(raw);
281}
282
283} // 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.
@ GPIO0_HighAndLowAlarm
GPIO0 = combined hi/lo alarm output.
@ GPIO0_HighAlarm
GPIO0 = high alarm output.
@ GPIO
Both as general-purpose I/O.
@ GPIO1_HighAlarm
GPIO1 = high alarm output.
@ GPIO1_LowAlarm_GPIO0_HighAlarm
GPIO1=low, GPIO0=high alarm.
@ 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.