Skip to content

Commit 1476c81

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 683e41e commit 1476c81

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1212
implement these `Error` traits, which implies providing a conversion to a common
1313
set of error kinds. Generic drivers using these interfaces can then convert the errors
1414
to this common set to act upon them.
15+
- The `NoAcknowledgeAddress` and `NoAcknowledgeData` variants of the I2C `Error`
16+
trait have been merged into `NoAcknowledge` with a `NoAcknowledgeSource` field
17+
to differentiate between the two events. An additional `Unknown` variant is
18+
provided in `NoAcknowledgeSource` for devices that can't differentiate between
19+
the two events.
1520

1621
### Removed
1722
- Removed `DelayMs` in favor of `DelayUs` with `u32` as type for clarity.

src/i2c.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,33 @@ 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+
/// I2C no acknowledge error source
137+
///
138+
/// In cases where it is possible, a device should indicate if a no acknowledge
139+
/// response was received to an address versus a no acknowledge to a data byte.
140+
/// Where it is not possible to differentiate, `Unknown` should be indicated.
141+
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
142+
pub enum NoAcknowledgeSource {
143+
/// The device did not acknowledge its address. The device may be missing.
144+
Address,
145+
/// The device did not acknowledge the data. It may not be ready to process
146+
/// requests at the moment.
147+
Data,
148+
/// Either the device did not acknowledge its address or the data, but it is
149+
/// unknown which.
150+
Unknown,
151+
}
152+
136153
impl Error for ErrorKind {
137154
fn kind(&self) -> ErrorKind {
138155
*self
@@ -144,8 +161,7 @@ impl core::fmt::Display for ErrorKind {
144161
match self {
145162
Self::Bus => write!(f, "Bus error occurred"),
146163
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"),
164+
Self::NoAcknowledge(s) => s.fmt(f),
149165
Self::Overrun => write!(f, "The peripheral receive buffer was overrun"),
150166
Self::Other => write!(
151167
f,
@@ -155,6 +171,16 @@ impl core::fmt::Display for ErrorKind {
155171
}
156172
}
157173

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

0 commit comments

Comments
 (0)