Skip to content

Commit 8d22a05

Browse files
committed
i2c: Merge NoAcknowledgeData and NoAcknowledgeAddress into one variant
A source enum was added to indicate if the NACK was from an address or a data byte. Some drivers may not be able to differentiate so they should use `Unknown`.
1 parent b72255c commit 8d22a05

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

src/i2c.rs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,34 @@ pub enum ErrorKind {
123123
Bus,
124124
/// The arbitration was lost, e.g. electrical problems with the clock signal
125125
ArbitrationLoss,
126-
/// The device did not acknowledge its address. The device may be missing.
127-
NoAcknowledgeAddress,
128-
/// The device did not acknowled the data. It may not be ready to process requests at the moment.
129-
NoAcknowledgeData,
126+
/// A bus operation was not acknowledged, e.g. due to the addressed device not
127+
/// being available on the bus or the device not being ready to process requests
128+
/// at the moment
129+
NoAcknowledge(NoAcknowledgeSource),
130130
/// The peripheral receive buffer was overrun
131131
Overrun,
132132
/// A different error occurred. The original error may contain more information.
133133
Other,
134134
}
135135

136+
137+
/// I2C no acknowledge error source
138+
///
139+
/// In cases where it is possible, a device should indicate if a no acknowledge
140+
/// response was received to an address versus a no acknowledge to a data byte.
141+
/// Where it is not possible to differentiate, `Unknown` should be indicated.
142+
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
143+
pub enum NoAcknowledgeSource {
144+
/// The device did not acknowledge its address. The device may be missing.
145+
Address,
146+
/// The device did not acknowledge the data. It may not be ready to process
147+
/// requests at the moment.
148+
Data,
149+
/// Either the device did not acknowledge its address or the data, but it is
150+
/// unknown which.
151+
Unknown,
152+
}
153+
136154
impl Error for ErrorKind {
137155
fn kind(&self) -> ErrorKind {
138156
*self
@@ -144,8 +162,7 @@ impl core::fmt::Display for ErrorKind {
144162
match self {
145163
Self::Bus => write!(f, "Bus error occurred"),
146164
Self::ArbitrationLoss => write!(f, "The arbitration was lost"),
147-
Self::NoAcknowledgeAddress => write!(f, "The device did not acknowledge its address"),
148-
Self::NoAcknowledgeData => write!(f, "The device did not acknowledge the data"),
165+
Self::NoAcknowledge(s) => s.fmt(f),
149166
Self::Overrun => write!(f, "The peripheral receive buffer was overrun"),
150167
Self::Other => write!(
151168
f,
@@ -155,6 +172,16 @@ impl core::fmt::Display for ErrorKind {
155172
}
156173
}
157174

175+
impl core::fmt::Display for NoAcknowledgeSource {
176+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
177+
match self {
178+
Self::Address => write!(f, "The device did not acknowledge its address"),
179+
Self::Data => write!(f, "The device did not acknowledge the data"),
180+
Self::Unknown => write!(f, "The device did not acknowledge its address or the data"),
181+
}
182+
}
183+
}
184+
158185
/// Address mode (7-bit / 10-bit)
159186
///
160187
/// Note: This trait is sealed and should not be implemented outside of this crate.

0 commit comments

Comments
 (0)