Skip to content

Commit 60c2c41

Browse files
committed
Update embedded-hal and backport transactional SPI implementation
1 parent 25bf2be commit 60c2c41

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ gpio_cdev = ["gpio-cdev"]
1515
default = [ "gpio_cdev", "gpio_sysfs" ]
1616

1717
[dependencies]
18-
embedded-hal = { version = "0.2.3", features = ["unproven"] }
18+
embedded-hal = { version = "0.2.6", features = ["unproven"] }
1919
gpio-cdev = { version = "0.3", optional = true }
2020
sysfs_gpio = { version = "0.5", optional = true }
2121

src/lib.rs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,19 @@
1313
#![deny(missing_docs)]
1414

1515
extern crate cast;
16-
extern crate core;
1716
extern crate embedded_hal as hal;
1817
pub extern crate i2cdev;
19-
pub extern crate spidev;
20-
pub extern crate serial_unix;
21-
pub extern crate serial_core;
2218
pub extern crate nb;
23-
19+
pub extern crate serial_core;
20+
pub extern crate serial_unix;
21+
pub extern crate spidev;
2422

2523
#[cfg(feature = "gpio_sysfs")]
2624
pub extern crate sysfs_gpio;
2725

2826
#[cfg(feature = "gpio_cdev")]
2927
pub extern crate gpio_cdev;
3028

31-
3229
use std::io::{self, Write};
3330
use std::path::{Path, PathBuf};
3431
use std::time::Duration;
@@ -60,7 +57,6 @@ pub use cdev_pin::CdevPin;
6057
/// Sysfs pin re-export
6158
pub use sysfs_pin::SysfsPin;
6259

63-
6460
/// Empty struct that provides delay functionality on top of `thread::sleep`
6561
pub struct Delay;
6662

@@ -118,7 +114,6 @@ impl hal::blocking::delay::DelayMs<u64> for Delay {
118114
}
119115
}
120116

121-
122117
/// Newtype around [`i2cdev::linux::LinuxI2CDevice`] that implements the `embedded-hal` traits
123118
///
124119
/// [`i2cdev::linux::LinuxI2CDevice`]: https://docs.rs/i2cdev/0.3.1/i2cdev/linux/struct.LinuxI2CDevice.html
@@ -236,6 +231,39 @@ impl hal::blocking::spi::Write<u8> for Spidev {
236231
}
237232
}
238233

234+
pub use hal::blocking::spi::Operation as SpiOperation;
235+
236+
impl hal::blocking::spi::Transactional<u8> for Spidev {
237+
type Error = io::Error;
238+
239+
fn exec<'a>(&mut self, operations: &mut [SpiOperation<'a, u8>]) -> Result<(), Self::Error> {
240+
// Map types from generic to linux objects
241+
let mut messages: Vec<_> = operations
242+
.iter_mut()
243+
.map(|a| {
244+
match a {
245+
SpiOperation::Write(w) => SpidevTransfer::write(w),
246+
SpiOperation::Transfer(r) => {
247+
// Clone read to write pointer
248+
// SPIdev is okay with having w == r but this is tricky to achieve in safe rust
249+
let w = unsafe {
250+
let p = r.as_ptr();
251+
std::slice::from_raw_parts(p, r.len())
252+
};
253+
254+
SpidevTransfer::read_write(w, r)
255+
}
256+
}
257+
})
258+
.collect();
259+
260+
// Execute transfer
261+
self.0.transfer_multiple(&mut messages)?;
262+
263+
Ok(())
264+
}
265+
}
266+
239267
impl ops::Deref for Spidev {
240268
type Target = spidev::Spidev;
241269

0 commit comments

Comments
 (0)