Skip to content

Commit

Permalink
Use Spidev 0.5.2
Browse files Browse the repository at this point in the history
This allows using the delay transfer support.
  • Loading branch information
LehMaxence committed Aug 8, 2023
1 parent 1a5d611 commit 3b57244
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 54 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ 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
72 changes: 19 additions & 53 deletions src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,20 @@ use std::io;
use std::ops;
use std::path::Path;

use crate::Delay;
use spidev::SpidevTransfer;

/// Newtype around [`spidev::Spidev`] that implements the `embedded-hal` traits
///
/// [`spidev::Spidev`]: https://docs.rs/spidev/0.5.spidev/spidev/struct.Spidev.html
pub struct Spidev(pub spidev::Spidev, Delay);
/// [`spidev::Spidev`]: https://docs.rs/spidev/0.5.2/spidev/struct.Spidev.html
pub struct Spidev(pub spidev::Spidev);

impl Spidev {
/// See [`spidev::Spidev::open`][0] for details.
///
/// [0]: https://docs.rs/spidev/0.5.spidev/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>,
{
spidev::Spidev::open(path)
.map(|spidev| Spidev(spidev, Delay {}))
.map_err(|e| e.into())
spidev::Spidev::open(path).map(Spidev).map_err(|e| e.into())
}
}

Expand All @@ -46,10 +41,10 @@ impl ops::DerefMut for Spidev {

mod embedded_hal_impl {
use super::*;
use embedded_hal::delay::DelayUs;
use embedded_hal::spi::ErrorType;
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 {
Expand Down Expand Up @@ -88,62 +83,33 @@ mod embedded_hal_impl {
&mut self,
operations: &mut [SpiOperation<'_, u8>],
) -> Result<(), Self::Error> {
let mut spidev_ops: Vec<Operation> = vec![];
let mut spidev_transfers: Vec<SpidevTransfer> = vec![];

for op in operations {
match op {
SpiOperation::Read(buf) => spidev_transfers.push(SpidevTransfer::read(buf)),
SpiOperation::Write(buf) => spidev_transfers.push(SpidevTransfer::write(buf)),
SpiOperation::Transfer(read, write) => {
spidev_transfers.push(SpidevTransfer::read_write(write, read))
}
let mut transfers: Vec<_> = operations
.iter_mut()
.map(|op| match op {
SpiOperation::Read(buf) => SpidevTransfer::read(buf),
SpiOperation::Write(buf) => SpidevTransfer::write(buf),
SpiOperation::Transfer(read, write) => SpidevTransfer::read_write(write, read),
SpiOperation::TransferInPlace(buf) => {
let tx = unsafe {
let p = buf.as_ptr();
std::slice::from_raw_parts(p, buf.len())
};
spidev_transfers.push(SpidevTransfer::read_write(tx, buf))
SpidevTransfer::read_write(tx, buf)
}
SpiOperation::DelayUs(us) => {
if !spidev_transfers.is_empty() {
let mut transfers: Vec<SpidevTransfer> = vec![];
std::mem::swap(&mut transfers, &mut spidev_transfers);
spidev_ops.push(Operation::Transfers(transfers));
}
spidev_ops.push(Operation::Delay(us.to_owned()));
}
}
}

if !spidev_transfers.is_empty() {
spidev_ops.push(Operation::Transfers(spidev_transfers));
}

for op in spidev_ops {
match op {
Operation::Transfers(mut transfers) => {
self.0
.transfer_multiple(&mut transfers)
.map_err(|err| SPIError { err })?;
self.flush()?;
}
Operation::Delay(us) => {
self.1.delay_us(us);
SpidevTransfer::delay((*us).try_into().unwrap_or(u16::MAX))
}
}
}

})
.collect();
self.0
.transfer_multiple(&mut transfers)
.map_err(|err| SPIError { err })?;
self.flush()?;
Ok(())
}
}
}

enum Operation<'a, 'b> {
Transfers(Vec<SpidevTransfer<'a, 'b>>),
Delay(u32),
}

/// Error type wrapping [io::Error](io::Error) to implement [embedded_hal::spi::ErrorKind]
#[derive(Debug)]
pub struct SPIError {
Expand Down

0 comments on commit 3b57244

Please sign in to comment.