ad7124_rs/
errors.rs

1//! Error handling for AD7124 driver
2//!
3//! Provides a unified error model with family-consistent naming following v3.0 standards
4
5/// Core AD7124 errors (transport-layer independent)
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum AD7124CoreError {
8    /// Invalid parameter provided
9    InvalidParameter,
10    /// Invalid configuration
11    InvalidConfiguration,
12    /// Device not responding
13    DeviceNotResponding,
14    /// Invalid device ID
15    InvalidDeviceId,
16    /// Unsupported operation for this device variant
17    UnsupportedOperation,
18    /// Data corruption detected
19    DataCorruption,
20    /// Buffer overflow in command sequence
21    BufferOverflow,
22    /// Value out of valid range
23    ValueOutOfRange,
24    /// Device not initialized
25    NotInitialized,
26    /// Communication timeout
27    Timeout,
28    /// CRC validation failed
29    CrcError,
30    /// ADC calibration failed
31    CalibrationFailed,
32    /// ADC conversion error
33    ConversionError,
34    /// Channel not enabled
35    ChannelNotEnabled,
36    /// Reference voltage error
37    ReferenceVoltageError,
38    /// Power supply fault
39    PowerSupplyFault,
40}
41
42/// AD7124 driver error type (supports transport and pin errors)
43#[derive(Debug)]
44pub enum AD7124Error<TransportE = (), PinE = ()> {
45    /// Core driver error
46    Core(AD7124CoreError),
47    /// Transport error (SPI)
48    Transport(TransportE),
49    /// Pin control error
50    Pin(PinE),
51    /// Operation timeout
52    Timeout,
53}
54
55impl<TransportE, PinE> From<AD7124CoreError> for AD7124Error<TransportE, PinE> {
56    fn from(err: AD7124CoreError) -> Self {
57        AD7124Error::Core(err)
58    }
59}
60
61// Note: From<TransportE> implementation would conflict with From<AD7124CoreError>
62// Users must wrap transport errors manually: AD7124Error::Transport(transport_err)
63
64impl<TransportE, PinE> core::fmt::Display for AD7124Error<TransportE, PinE>
65where
66    TransportE: core::fmt::Display,
67    PinE: core::fmt::Display,
68{
69    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
70        match self {
71            AD7124Error::Core(err) => match err {
72                AD7124CoreError::InvalidParameter => write!(f, "Invalid parameter"),
73                AD7124CoreError::InvalidConfiguration => write!(f, "Invalid configuration"),
74                AD7124CoreError::DeviceNotResponding => write!(f, "Device not responding"),
75                AD7124CoreError::InvalidDeviceId => write!(f, "Invalid device ID"),
76                AD7124CoreError::UnsupportedOperation => write!(f, "Unsupported operation"),
77                AD7124CoreError::DataCorruption => write!(f, "Data corruption detected"),
78                AD7124CoreError::BufferOverflow => write!(f, "Buffer overflow"),
79                AD7124CoreError::ValueOutOfRange => write!(f, "Value out of range"),
80                AD7124CoreError::NotInitialized => write!(f, "Device not initialized"),
81                AD7124CoreError::Timeout => write!(f, "Communication timeout"),
82                AD7124CoreError::CrcError => write!(f, "CRC validation failed"),
83                AD7124CoreError::CalibrationFailed => write!(f, "ADC calibration failed"),
84                AD7124CoreError::ConversionError => write!(f, "ADC conversion error"),
85                AD7124CoreError::ChannelNotEnabled => write!(f, "Channel not enabled"),
86                AD7124CoreError::ReferenceVoltageError => write!(f, "Reference voltage error"),
87                AD7124CoreError::PowerSupplyFault => write!(f, "Power supply fault"),
88            },
89            AD7124Error::Transport(err) => write!(f, "Transport error: {}", err),
90            AD7124Error::Pin(err) => write!(f, "Pin error: {}", err),
91            AD7124Error::Timeout => write!(f, "Operation timeout"),
92        }
93    }
94}
95
96#[cfg(feature = "defmt")]
97impl<TransportE, PinE> defmt::Format for AD7124Error<TransportE, PinE>
98where
99    TransportE: defmt::Format,
100    PinE: defmt::Format,
101{
102    fn format(&self, f: defmt::Formatter<'_>) {
103        match self {
104            AD7124Error::Core(err) => match err {
105                AD7124CoreError::InvalidParameter => defmt::write!(f, "Invalid parameter"),
106                AD7124CoreError::InvalidConfiguration => defmt::write!(f, "Invalid configuration"),
107                AD7124CoreError::DeviceNotResponding => defmt::write!(f, "Device not responding"),
108                AD7124CoreError::InvalidDeviceId => defmt::write!(f, "Invalid device ID"),
109                AD7124CoreError::UnsupportedOperation => defmt::write!(f, "Unsupported operation"),
110                AD7124CoreError::DataCorruption => defmt::write!(f, "Data corruption detected"),
111                AD7124CoreError::BufferOverflow => defmt::write!(f, "Buffer overflow"),
112                AD7124CoreError::ValueOutOfRange => defmt::write!(f, "Value out of range"),
113                AD7124CoreError::NotInitialized => defmt::write!(f, "Device not initialized"),
114                AD7124CoreError::Timeout => defmt::write!(f, "Communication timeout"),
115                AD7124CoreError::CrcError => defmt::write!(f, "CRC validation failed"),
116                AD7124CoreError::CalibrationFailed => defmt::write!(f, "ADC calibration failed"),
117                AD7124CoreError::ConversionError => defmt::write!(f, "ADC conversion error"),
118                AD7124CoreError::ChannelNotEnabled => defmt::write!(f, "Channel not enabled"),
119                AD7124CoreError::ReferenceVoltageError => {
120                    defmt::write!(f, "Reference voltage error")
121                }
122                AD7124CoreError::PowerSupplyFault => defmt::write!(f, "Power supply fault"),
123            },
124            AD7124Error::Transport(err) => defmt::write!(f, "Transport error: {:?}", err),
125            AD7124Error::Pin(err) => defmt::write!(f, "Pin error: {:?}", err),
126            AD7124Error::Timeout => defmt::write!(f, "Operation timeout"),
127        }
128    }
129}