ad7124_rs/
device_type.rs

1//! Device type detection and capability management for AD7124 family
2//!
3//! Enhanced v3.0 with family-wide device support
4
5/// Supported device variants in the AD7124 family
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum DeviceType {
8    /// AD7124-4: 4-channel, 24-bit ADC
9    AD7124_4,
10    /// AD7124-8: 8-channel, 24-bit ADC  
11    AD7124_8,
12    /// Unknown or unsupported device
13    Unknown,
14}
15
16impl DeviceType {
17    /// Get device type from device ID register
18    pub fn from_device_id(device_id: u8) -> Self {
19        match device_id {
20            0x04 => DeviceType::AD7124_4,
21            0x12 => DeviceType::AD7124_8,
22            _ => DeviceType::Unknown,
23        }
24    }
25
26    /// Get expected device ID for this variant
27    pub fn device_id(self) -> Option<u8> {
28        match self {
29            DeviceType::AD7124_4 => Some(0x04),
30            DeviceType::AD7124_8 => Some(0x12),
31            DeviceType::Unknown => None,
32        }
33    }
34
35    /// Check if device supports specific feature
36    pub fn supports_feature(self, feature: DeviceFeature) -> bool {
37        use DeviceFeature::*;
38        match (self, feature) {
39            (_, BasicOperation) => true, // All devices support basic ADC operation
40            (DeviceType::AD7124_4, MultiChannel) => true, // 4 channels
41            (DeviceType::AD7124_8, MultiChannel) => true, // 8 channels
42            (_, InternalReference) => true, // Both have internal 2.5V reference
43            (_, ProgrammableGain) => true, // Both have PGA
44            (_, SelfTest) => true,       // Both support internal test modes
45            (_, LowPowerMode) => true,   // Both support power modes
46            (_, HighPrecision) => true,  // Both are 24-bit precision
47            (_, DiagnosticMode) => true, // Both support diagnostic features
48            (_, BurnoutCurrent) => true, // Both support burnout current sources
49            _ => false,
50        }
51    }
52
53    /// Get maximum number of channels for this device
54    pub fn max_channels(self) -> u8 {
55        match self {
56            DeviceType::AD7124_4 => 4,
57            DeviceType::AD7124_8 => 8,
58            DeviceType::Unknown => 0,
59        }
60    }
61
62    /// Get maximum SPI frequency for this device
63    pub fn max_spi_frequency_hz(self) -> u32 {
64        match self {
65            DeviceType::AD7124_4 | DeviceType::AD7124_8 => 5_000_000, // 5 MHz max
66            DeviceType::Unknown => 1_000_000,                         // Conservative value
67        }
68    }
69
70    /// Get device description string
71    pub fn description(self) -> &'static str {
72        match self {
73            DeviceType::AD7124_4 => "AD7124-4: 4-channel, 24-bit Sigma-Delta ADC",
74            DeviceType::AD7124_8 => "AD7124-8: 8-channel, 24-bit Sigma-Delta ADC",
75            DeviceType::Unknown => "Unknown or unsupported AD7124 device",
76        }
77    }
78}
79
80/// Device feature enumeration
81#[derive(Debug, Clone, Copy, PartialEq, Eq)]
82pub enum DeviceFeature {
83    /// Basic ADC operation
84    BasicOperation,
85    /// Multiple channel support
86    MultiChannel,
87    /// Internal reference voltage
88    InternalReference,
89    /// Programmable gain amplifier
90    ProgrammableGain,
91    /// Built-in self-test
92    SelfTest,
93    /// Low-power operation modes
94    LowPowerMode,
95    /// High-precision measurements (24-bit)
96    HighPrecision,
97    /// Diagnostic mode
98    DiagnosticMode,
99    /// Burnout current sources
100    BurnoutCurrent,
101    /// Continuous conversion mode
102    ContinuousMode,
103    /// Single conversion mode
104    SingleShotMode,
105    /// Digital filtering
106    DigitalFiltering,
107}
108
109/// Device capabilities structure (runtime capability detection)
110#[derive(Debug, Clone, Copy)]
111pub struct DeviceCapabilities {
112    /// The specific device type (AD7124-4, AD7124-8, etc.)
113    pub device_type: DeviceType,
114    /// Maximum number of analog input channels supported
115    pub max_channels: u8,
116    /// Maximum SPI clock frequency supported in Hz
117    pub max_spi_frequency_hz: u32,
118    /// Whether device has built-in voltage reference
119    pub internal_reference: bool,
120    /// Whether device supports programmable gain amplification
121    pub programmable_gain: bool,
122    /// Whether device supports built-in self-test functionality
123    pub self_test: bool,
124    /// Whether device supports low-power operation modes
125    pub low_power_mode: bool,
126    /// Whether device provides high-precision (24-bit) measurements
127    pub high_precision: bool,
128    /// Whether device supports diagnostic and status monitoring
129    pub diagnostic_mode: bool,
130    /// Whether device supports burnout current sources for sensor diagnostics
131    pub burnout_current: bool,
132    /// Whether device supports continuous conversion mode
133    pub continuous_mode: bool,
134    /// Whether device supports single-shot conversion mode
135    pub single_shot_mode: bool,
136    /// Whether device supports digital filtering options
137    pub digital_filtering: bool,
138}
139
140impl DeviceCapabilities {
141    /// Create capabilities for detected device type
142    pub fn for_device(device_type: DeviceType) -> Self {
143        Self {
144            device_type,
145            max_channels: device_type.max_channels(),
146            max_spi_frequency_hz: device_type.max_spi_frequency_hz(),
147            internal_reference: device_type.supports_feature(DeviceFeature::InternalReference),
148            programmable_gain: device_type.supports_feature(DeviceFeature::ProgrammableGain),
149            self_test: device_type.supports_feature(DeviceFeature::SelfTest),
150            low_power_mode: device_type.supports_feature(DeviceFeature::LowPowerMode),
151            high_precision: device_type.supports_feature(DeviceFeature::HighPrecision),
152            diagnostic_mode: device_type.supports_feature(DeviceFeature::DiagnosticMode),
153            burnout_current: device_type.supports_feature(DeviceFeature::BurnoutCurrent),
154            continuous_mode: device_type.supports_feature(DeviceFeature::ContinuousMode),
155            single_shot_mode: device_type.supports_feature(DeviceFeature::SingleShotMode),
156            digital_filtering: device_type.supports_feature(DeviceFeature::DigitalFiltering),
157        }
158    }
159
160    /// Check if device supports a specific feature
161    pub fn supports(&self, feature: DeviceFeature) -> bool {
162        self.device_type.supports_feature(feature)
163    }
164
165    /// Validate channel number against device capabilities
166    pub fn validate_channel(&self, channel: u8) -> bool {
167        channel < self.max_channels
168    }
169}
170
171impl Default for DeviceCapabilities {
172    fn default() -> Self {
173        Self::for_device(DeviceType::Unknown)
174    }
175}