Skip to content

Update embedded-hal #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Changed
- [breaking-change] Replace serial-rs with the serialport-rs crate. `Serial::open` now needs a baud-rate argument as well.
- 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))
- 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))
- Updated to `spidev` `0.5.2` release([API changes](https://github.com/rust-embedded/rust-spidev/blob/master/CHANGELOG.md#052--2023-08-02))

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

Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ spi = ["spidev"]
default = [ "gpio_cdev", "gpio_sysfs", "i2c", "spi" ]

[dependencies]
embedded-hal = "=1.0.0-alpha.10"
embedded-hal-nb = "=1.0.0-alpha.2"
embedded-hal = "=1.0.0-alpha.11"
embedded-hal-nb = "=1.0.0-alpha.3"
gpio-cdev = { version = "0.5.1", optional = true }
sysfs_gpio = { version = "0.6.1", optional = true }
i2cdev = { version = "0.5.1", optional = true }
nb = "1"
serialport = { version = "4.2.0", default-features = false }
spidev = { version = "0.5.1", optional = true }
spidev = { version = "0.5.2", optional = true }
nix = "0.23.1"

[dev-dependencies]
Expand Down
1 change: 1 addition & 0 deletions src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ impl fmt::Display for SerialError {
impl std::error::Error for SerialError {}

impl embedded_hal::serial::Error for SerialError {
#[allow(clippy::match_single_binding)]
fn kind(&self) -> embedded_hal::serial::ErrorKind {
use embedded_hal::serial::ErrorKind::*;
// TODO: match any errors here if we can find any that are relevant
Expand Down
63 changes: 20 additions & 43 deletions src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ use std::path::Path;

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

impl Spidev {
/// See [`spidev::Spidev::open`][0] for details.
///
/// [0]: https://docs.rs/spidev/0.5.0/spidev/struct.Spidev.html#method.open
/// [0]: https://docs.rs/spidev/0.5.2/spidev/struct.Spidev.html#method.open
pub fn open<P>(path: P) -> Result<Self, SPIError>
where
P: AsRef<Path>,
Expand All @@ -42,36 +45,24 @@ impl ops::DerefMut for Spidev {
mod embedded_hal_impl {
use super::*;
use embedded_hal::spi::ErrorType;
use embedded_hal::spi::{
Operation as SpiOperation, SpiBus, SpiBusFlush, SpiBusRead, SpiBusWrite, SpiDevice,
SpiDeviceRead, SpiDeviceWrite,
};
use embedded_hal::spi::{Operation as SpiOperation, SpiBus, SpiDevice};
use spidev::SpidevTransfer;
use std::convert::TryInto;
use std::io::{Read, Write};

impl ErrorType for Spidev {
type Error = SPIError;
}

impl SpiBusFlush for Spidev {
fn flush(&mut self) -> Result<(), Self::Error> {
self.0.flush().map_err(|err| SPIError { err })
}
}

impl SpiBusRead<u8> for Spidev {
impl SpiBus<u8> for Spidev {
fn read(&mut self, words: &mut [u8]) -> Result<(), Self::Error> {
self.0.read_exact(words).map_err(|err| SPIError { err })
}
}

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

impl SpiBus<u8> for Spidev {
fn transfer(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Self::Error> {
self.0
.transfer(&mut SpidevTransfer::read_write(write, read))
Expand All @@ -84,37 +75,19 @@ mod embedded_hal_impl {
.transfer(&mut SpidevTransfer::read_write(&tx, words))
.map_err(|err| SPIError { err })
}
}

impl SpiDeviceRead for Spidev {
fn read_transaction(&mut self, operations: &mut [&mut [u8]]) -> Result<(), Self::Error> {
let mut transfers: Vec<_> = operations
.iter_mut()
.map(|op| SpidevTransfer::read(op))
.collect();
self.0
.transfer_multiple(&mut transfers)
.map_err(|err| SPIError { err })?;
self.flush()?;
Ok(())
}
}

impl SpiDeviceWrite for Spidev {
fn write_transaction(&mut self, operations: &[&[u8]]) -> Result<(), Self::Error> {
let mut transfers: Vec<_> = operations
.iter()
.map(|op| SpidevTransfer::write(op))
.collect();
self.0
.transfer_multiple(&mut transfers)
.map_err(|err| SPIError { err })?;
self.flush()?;
Ok(())
fn flush(&mut self) -> Result<(), Self::Error> {
self.0.flush().map_err(|err| SPIError { err })
}
}

impl SpiDevice for Spidev {
///Perform a transaction against the device. [Read more][transaction]
///
/// [Delay operations][delay] are capped to 65535 microseconds.
///
/// [transaction]: SpiDevice::transaction
/// [delay]: SpiOperation::DelayUs
fn transaction(
&mut self,
operations: &mut [SpiOperation<'_, u8>],
Expand All @@ -132,6 +105,9 @@ mod embedded_hal_impl {
};
SpidevTransfer::read_write(tx, buf)
}
SpiOperation::DelayUs(us) => {
SpidevTransfer::delay((*us).try_into().unwrap_or(u16::MAX))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose to max out the delay to u16::MAX but that may not be what we want here.
Let me know what you think.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can leave at that here but please document in this method and in the Spidev struct that delays are capped to 65535 microseconds.

}
})
.collect();
self.0
Expand Down Expand Up @@ -163,6 +139,7 @@ impl From<io::Error> for SPIError {
}

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