Skip to content

Commit c9f823e

Browse files
committed
use ErrorKind-like error names for I2c::Error
1 parent e6cee3b commit c9f823e

File tree

4 files changed

+45
-54
lines changed

4 files changed

+45
-54
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2828
### Added
2929

3030
- Missing `DelayMs<u8>` / `DelayUs<u8>` impls for fugit::Delay
31-
- Support of embedded-hal 1.0.0-alpha.7
31+
- Support of embedded-hal 1.0.0-alpha.7 [#443]
3232
- Aliases for peripheral wrappers [#434]
3333
- `WithPwm` trait implemented for timers with channels (internals) [#425]
3434
- `Pwm` struct with `split` method and implementation of embedded-hal::Pwm (similar to f1xx-hal) [#425]
@@ -51,6 +51,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
5151
[#438]: https://github.com/stm32-rs/stm32f4xx-hal/pull/438
5252
[#439]: https://github.com/stm32-rs/stm32f4xx-hal/pull/439
5353
[#440]: https://github.com/stm32-rs/stm32f4xx-hal/pull/440
54+
[#443]: https://github.com/stm32-rs/stm32f4xx-hal/pull/443
5455

5556
### Changed
5657

src/fmpi2c.rs

+13-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use core::ops::Deref;
22

3-
use crate::i2c::{Error, Pins};
3+
use crate::i2c::{Error, NoAcknowledgeSource, Pins};
44
use crate::pac::{fmpi2c1, FMPI2C1, RCC};
55
use crate::rcc::{Enable, Reset};
66
use crate::time::{Hertz, U32Ext};
@@ -168,12 +168,19 @@ where
168168
self.i2c
169169
.icr
170170
.write(|w| w.stopcf().set_bit().nackcf().set_bit());
171-
return Err(Error::NACK);
171+
return Err(Error::NoAcknowledge(NoAcknowledgeSource::Unknown));
172172
}
173173

174174
Ok(())
175175
}
176176

177+
fn end_transaction(&self) -> Result<(), Error> {
178+
// Check and clear flags if they somehow ended up set
179+
self.check_and_clear_error_flags(&self.i2c.isr.read())
180+
.map_err(Error::nack_data)?;
181+
Ok(())
182+
}
183+
177184
fn send_byte(&self, byte: u8) -> Result<(), Error> {
178185
// Wait until we're ready for sending
179186
while {
@@ -186,9 +193,7 @@ where
186193
// Push out a byte of data
187194
self.i2c.txdr.write(|w| unsafe { w.bits(u32::from(byte)) });
188195

189-
self.check_and_clear_error_flags(&self.i2c.isr.read())
190-
.map_err(Error::nack_data)?;
191-
Ok(())
196+
self.end_transaction()
192197
}
193198

194199
fn recv_byte(&self) -> Result<u8, Error> {
@@ -225,11 +230,7 @@ where
225230
*c = self.recv_byte()?;
226231
}
227232

228-
// Check and clear flags if they somehow ended up set
229-
self.check_and_clear_error_flags(&self.i2c.isr.read())
230-
.map_err(Error::nack_data)?;
231-
232-
Ok(())
233+
self.end_transaction()
233234
}
234235

235236
pub fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Error> {
@@ -253,11 +254,7 @@ where
253254
self.send_byte(*c)?;
254255
}
255256

256-
// Check and clear flags if they somehow ended up set
257-
self.check_and_clear_error_flags(&self.i2c.isr.read())
258-
.map_err(Error::nack_data)?;
259-
260-
Ok(())
257+
self.end_transaction()
261258
}
262259

263260
pub fn write_read(&mut self, addr: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Error> {
@@ -318,10 +315,6 @@ where
318315
*c = self.recv_byte()?;
319316
}
320317

321-
// Check and clear flags if they somehow ended up set
322-
self.check_and_clear_error_flags(&self.i2c.isr.read())
323-
.map_err(Error::nack_data)?;
324-
325-
Ok(())
318+
self.end_transaction()
326319
}
327320
}

src/i2c.rs

+23-24
Original file line numberDiff line numberDiff line change
@@ -99,31 +99,34 @@ where
9999
}
100100
}
101101

102+
pub use embedded_hal_one::i2c::NoAcknowledgeSource;
103+
102104
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
103105
#[non_exhaustive]
104106
pub enum Error {
105-
OVERRUN,
106-
NACK,
107-
NACK_ADDR,
108-
NACK_DATA,
109-
TIMEOUT,
110-
// Note: The BUS error type is not currently returned, but is maintained for backwards
111-
// compatibility.
112-
BUS,
113-
CRC,
114-
ARBITRATION,
107+
Overrun,
108+
NoAcknowledge(NoAcknowledgeSource),
109+
Timeout,
110+
// Note: The Bus error type is not currently returned, but is maintained for compatibility.
111+
Bus,
112+
Crc,
113+
ArbitrationLoss,
115114
}
116115

117116
impl Error {
118117
pub(crate) fn nack_addr(self) -> Self {
119118
match self {
120-
Error::NACK => Error::NACK_ADDR,
119+
Error::NoAcknowledge(NoAcknowledgeSource::Unknown) => {
120+
Error::NoAcknowledge(NoAcknowledgeSource::Address)
121+
}
121122
e => e,
122123
}
123124
}
124125
pub(crate) fn nack_data(self) -> Self {
125126
match self {
126-
Error::NACK => Error::NACK_DATA,
127+
Error::NoAcknowledge(NoAcknowledgeSource::Unknown) => {
128+
Error::NoAcknowledge(NoAcknowledgeSource::Data)
129+
}
127130
e => e,
128131
}
129132
}
@@ -243,27 +246,27 @@ impl<I2C: Instance, PINS> I2c<I2C, PINS> {
243246

244247
if sr1.timeout().bit_is_set() {
245248
self.i2c.sr1.modify(|_, w| w.timeout().clear_bit());
246-
return Err(Error::TIMEOUT);
249+
return Err(Error::Timeout);
247250
}
248251

249252
if sr1.pecerr().bit_is_set() {
250253
self.i2c.sr1.modify(|_, w| w.pecerr().clear_bit());
251-
return Err(Error::CRC);
254+
return Err(Error::Crc);
252255
}
253256

254257
if sr1.ovr().bit_is_set() {
255258
self.i2c.sr1.modify(|_, w| w.ovr().clear_bit());
256-
return Err(Error::OVERRUN);
259+
return Err(Error::Overrun);
257260
}
258261

259262
if sr1.af().bit_is_set() {
260263
self.i2c.sr1.modify(|_, w| w.af().clear_bit());
261-
return Err(Error::NACK);
264+
return Err(Error::NoAcknowledge(NoAcknowledgeSource::Unknown));
262265
}
263266

264267
if sr1.arlo().bit_is_set() {
265268
self.i2c.sr1.modify(|_, w| w.arlo().clear_bit());
266-
return Err(Error::ARBITRATION);
269+
return Err(Error::ArbitrationLoss);
267270
}
268271

269272
// The errata indicates that BERR may be incorrectly detected. It recommends ignoring and
@@ -414,7 +417,7 @@ impl<I2C: Instance, PINS> I2c<I2C, PINS> {
414417
// Fallthrough is success
415418
Ok(())
416419
} else {
417-
Err(Error::OVERRUN)
420+
Err(Error::Overrun)
418421
}
419422
}
420423

@@ -449,18 +452,14 @@ impl<I2C: Instance, PINS> I2c<I2C, PINS> {
449452

450453
pub fn write_read(&mut self, addr: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Error> {
451454
self.write_bytes(addr, bytes.iter().cloned())?;
452-
self.read(addr, buffer)?;
453-
454-
Ok(())
455+
self.read(addr, buffer)
455456
}
456457

457458
pub fn write_iter_read<B>(&mut self, addr: u8, bytes: B, buffer: &mut [u8]) -> Result<(), Error>
458459
where
459460
B: IntoIterator<Item = u8>,
460461
{
461462
self.write_bytes(addr, bytes.into_iter())?;
462-
self.read(addr, buffer)?;
463-
464-
Ok(())
463+
self.read(addr, buffer)
465464
}
466465
}

src/i2c/hal_1.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
use embedded_hal_one::i2c::{Error, ErrorKind, ErrorType, NoAcknowledgeSource};
1+
use embedded_hal_one::i2c::{Error, ErrorKind, ErrorType};
22

33
impl Error for super::Error {
44
fn kind(&self) -> ErrorKind {
5-
match self {
6-
Self::OVERRUN => ErrorKind::Overrun,
7-
Self::BUS => ErrorKind::Bus,
8-
Self::ARBITRATION => ErrorKind::ArbitrationLoss,
9-
Self::NACK_ADDR => ErrorKind::NoAcknowledge(NoAcknowledgeSource::Address),
10-
Self::NACK_DATA => ErrorKind::NoAcknowledge(NoAcknowledgeSource::Data),
11-
Self::NACK => ErrorKind::NoAcknowledge(NoAcknowledgeSource::Unknown),
12-
Self::CRC | Self::TIMEOUT => ErrorKind::Other,
5+
match *self {
6+
Self::Overrun => ErrorKind::Overrun,
7+
Self::Bus => ErrorKind::Bus,
8+
Self::ArbitrationLoss => ErrorKind::ArbitrationLoss,
9+
Self::NoAcknowledge(nack) => ErrorKind::NoAcknowledge(nack),
10+
Self::Crc | Self::Timeout => ErrorKind::Other,
1311
}
1412
}
1513
}

0 commit comments

Comments
 (0)