Skip to content

Commit 86ab035

Browse files
authored
Merge pull request #94 from LehMaxence/update-embedded-hal
Update embedded-hal
2 parents 40dfc36 + 847e93e commit 86ab035

File tree

4 files changed

+26
-47
lines changed

4 files changed

+26
-47
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Changed
1111
- [breaking-change] Replace serial-rs with the serialport-rs crate. `Serial::open` now needs a baud-rate argument as well.
12-
- Updated to `embedded-hal` `1.0.0-alpha.10` release ([API changes](https://github.com/rust-embedded/embedded-hal/blob/master/embedded-hal/CHANGELOG.md#v100-alpha10---2023-04-04))
12+
- Updated to `embedded-hal` `1.0.0-alpha.11` release ([API changes](https://github.com/rust-embedded/embedded-hal/blob/master/embedded-hal/CHANGELOG.md#v100-alpha11---2023-07-04))
13+
- Updated to `spidev` `0.5.2` release([API changes](https://github.com/rust-embedded/rust-spidev/blob/master/CHANGELOG.md#052--2023-08-02))
1314

1415
## [v0.4.0-alpha.3] - 2022-08-04
1516

Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ spi = ["spidev"]
2222
default = [ "gpio_cdev", "gpio_sysfs", "i2c", "spi" ]
2323

2424
[dependencies]
25-
embedded-hal = "=1.0.0-alpha.10"
26-
embedded-hal-nb = "=1.0.0-alpha.2"
25+
embedded-hal = "=1.0.0-alpha.11"
26+
embedded-hal-nb = "=1.0.0-alpha.3"
2727
gpio-cdev = { version = "0.5.1", optional = true }
2828
sysfs_gpio = { version = "0.6.1", optional = true }
2929
i2cdev = { version = "0.5.1", optional = true }
3030
nb = "1"
3131
serialport = { version = "4.2.0", default-features = false }
32-
spidev = { version = "0.5.1", optional = true }
32+
spidev = { version = "0.5.2", optional = true }
3333
nix = "0.23.1"
3434

3535
[dev-dependencies]

src/serial.rs

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ impl fmt::Display for SerialError {
8181
impl std::error::Error for SerialError {}
8282

8383
impl embedded_hal::serial::Error for SerialError {
84+
#[allow(clippy::match_single_binding)]
8485
fn kind(&self) -> embedded_hal::serial::ErrorKind {
8586
use embedded_hal::serial::ErrorKind::*;
8687
// TODO: match any errors here if we can find any that are relevant

src/spi.rs

+20-43
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ use std::path::Path;
1010

1111
/// Newtype around [`spidev::Spidev`] that implements the `embedded-hal` traits
1212
///
13-
/// [`spidev::Spidev`]: https://docs.rs/spidev/0.5.0/spidev/struct.Spidev.html
13+
/// [Delay operations][delay] are capped to 65535 microseconds.
14+
///
15+
/// [`spidev::Spidev`]: https://docs.rs/spidev/0.5.2/spidev/struct.Spidev.html
16+
/// [delay]: embedded_hal::spi::Operation::DelayUs
1417
pub struct Spidev(pub spidev::Spidev);
1518

1619
impl Spidev {
1720
/// See [`spidev::Spidev::open`][0] for details.
1821
///
19-
/// [0]: https://docs.rs/spidev/0.5.0/spidev/struct.Spidev.html#method.open
22+
/// [0]: https://docs.rs/spidev/0.5.2/spidev/struct.Spidev.html#method.open
2023
pub fn open<P>(path: P) -> Result<Self, SPIError>
2124
where
2225
P: AsRef<Path>,
@@ -42,36 +45,24 @@ impl ops::DerefMut for Spidev {
4245
mod embedded_hal_impl {
4346
use super::*;
4447
use embedded_hal::spi::ErrorType;
45-
use embedded_hal::spi::{
46-
Operation as SpiOperation, SpiBus, SpiBusFlush, SpiBusRead, SpiBusWrite, SpiDevice,
47-
SpiDeviceRead, SpiDeviceWrite,
48-
};
48+
use embedded_hal::spi::{Operation as SpiOperation, SpiBus, SpiDevice};
4949
use spidev::SpidevTransfer;
50+
use std::convert::TryInto;
5051
use std::io::{Read, Write};
5152

5253
impl ErrorType for Spidev {
5354
type Error = SPIError;
5455
}
5556

56-
impl SpiBusFlush for Spidev {
57-
fn flush(&mut self) -> Result<(), Self::Error> {
58-
self.0.flush().map_err(|err| SPIError { err })
59-
}
60-
}
61-
62-
impl SpiBusRead<u8> for Spidev {
57+
impl SpiBus<u8> for Spidev {
6358
fn read(&mut self, words: &mut [u8]) -> Result<(), Self::Error> {
6459
self.0.read_exact(words).map_err(|err| SPIError { err })
6560
}
66-
}
6761

68-
impl SpiBusWrite<u8> for Spidev {
6962
fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> {
7063
self.0.write_all(words).map_err(|err| SPIError { err })
7164
}
72-
}
7365

74-
impl SpiBus<u8> for Spidev {
7566
fn transfer(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Self::Error> {
7667
self.0
7768
.transfer(&mut SpidevTransfer::read_write(write, read))
@@ -84,37 +75,19 @@ mod embedded_hal_impl {
8475
.transfer(&mut SpidevTransfer::read_write(&tx, words))
8576
.map_err(|err| SPIError { err })
8677
}
87-
}
88-
89-
impl SpiDeviceRead for Spidev {
90-
fn read_transaction(&mut self, operations: &mut [&mut [u8]]) -> Result<(), Self::Error> {
91-
let mut transfers: Vec<_> = operations
92-
.iter_mut()
93-
.map(|op| SpidevTransfer::read(op))
94-
.collect();
95-
self.0
96-
.transfer_multiple(&mut transfers)
97-
.map_err(|err| SPIError { err })?;
98-
self.flush()?;
99-
Ok(())
100-
}
101-
}
10278

103-
impl SpiDeviceWrite for Spidev {
104-
fn write_transaction(&mut self, operations: &[&[u8]]) -> Result<(), Self::Error> {
105-
let mut transfers: Vec<_> = operations
106-
.iter()
107-
.map(|op| SpidevTransfer::write(op))
108-
.collect();
109-
self.0
110-
.transfer_multiple(&mut transfers)
111-
.map_err(|err| SPIError { err })?;
112-
self.flush()?;
113-
Ok(())
79+
fn flush(&mut self) -> Result<(), Self::Error> {
80+
self.0.flush().map_err(|err| SPIError { err })
11481
}
11582
}
11683

11784
impl SpiDevice for Spidev {
85+
///Perform a transaction against the device. [Read more][transaction]
86+
///
87+
/// [Delay operations][delay] are capped to 65535 microseconds.
88+
///
89+
/// [transaction]: SpiDevice::transaction
90+
/// [delay]: SpiOperation::DelayUs
11891
fn transaction(
11992
&mut self,
12093
operations: &mut [SpiOperation<'_, u8>],
@@ -132,6 +105,9 @@ mod embedded_hal_impl {
132105
};
133106
SpidevTransfer::read_write(tx, buf)
134107
}
108+
SpiOperation::DelayUs(us) => {
109+
SpidevTransfer::delay((*us).try_into().unwrap_or(u16::MAX))
110+
}
135111
})
136112
.collect();
137113
self.0
@@ -163,6 +139,7 @@ impl From<io::Error> for SPIError {
163139
}
164140

165141
impl embedded_hal::spi::Error for SPIError {
142+
#[allow(clippy::match_single_binding)]
166143
fn kind(&self) -> embedded_hal::spi::ErrorKind {
167144
use embedded_hal::spi::ErrorKind;
168145
// TODO: match any errors here if we can find any that are relevant

0 commit comments

Comments
 (0)