|
13 | 13 | #![deny(missing_docs)]
|
14 | 14 |
|
15 | 15 | extern crate cast;
|
16 |
| -extern crate core; |
17 | 16 | extern crate embedded_hal as hal;
|
18 | 17 | pub extern crate i2cdev;
|
19 | 18 | pub extern crate nb;
|
@@ -202,7 +201,10 @@ impl hal::blocking::i2c::WriteRead for I2cdev {
|
202 | 201 | buffer: &mut [u8],
|
203 | 202 | ) -> Result<(), Self::Error> {
|
204 | 203 | self.set_address(address)?;
|
205 |
| - let mut messages = [LinuxI2CMessage::write(bytes), LinuxI2CMessage::read(buffer)]; |
| 204 | + let mut messages = [ |
| 205 | + LinuxI2CMessage::write(bytes), |
| 206 | + LinuxI2CMessage::read(buffer), |
| 207 | + ]; |
206 | 208 | self.inner.transfer(&mut messages).map(drop)
|
207 | 209 | }
|
208 | 210 | }
|
@@ -257,6 +259,40 @@ impl hal::blocking::spi::Write<u8> for Spidev {
|
257 | 259 | }
|
258 | 260 | }
|
259 | 261 |
|
| 262 | +#[cfg(feature = "transactional-spi")] |
| 263 | +pub use hal::blocking::spi::{Operation as SpiOperation}; |
| 264 | + |
| 265 | +#[cfg(feature = "transactional-spi")] |
| 266 | +impl hal::blocking::spi::Transactional<u8> for Spidev { |
| 267 | + type Error = io::Error; |
| 268 | + |
| 269 | + fn try_exec<'a>(&mut self, operations: &mut [SpiOperation<'a, u8>]) -> Result<(), Self::Error> { |
| 270 | + |
| 271 | + // Map types from generic to linux objects |
| 272 | + let mut messages: Vec<_> = operations.iter_mut().map(|a| { |
| 273 | + match a { |
| 274 | + SpiOperation::Write(w) => SpidevTransfer::write(w), |
| 275 | + SpiOperation::Transfer(r) => { |
| 276 | + // TODO: is spidev okay with the same array pointer |
| 277 | + // being used twice? If not, need some kind of vector |
| 278 | + // pool that will outlive the transfer |
| 279 | + let w = unsafe { |
| 280 | + let p = r.as_ptr(); |
| 281 | + std::slice::from_raw_parts(p, r.len()) |
| 282 | + }; |
| 283 | + |
| 284 | + SpidevTransfer::read_write(w, r) |
| 285 | + }, |
| 286 | + } |
| 287 | + }).collect(); |
| 288 | + |
| 289 | + // Execute transfer |
| 290 | + self.0.transfer_multiple(&mut messages)?; |
| 291 | + |
| 292 | + Ok(()) |
| 293 | + } |
| 294 | +} |
| 295 | + |
260 | 296 | impl ops::Deref for Spidev {
|
261 | 297 | type Target = spidev::Spidev;
|
262 | 298 |
|
|
0 commit comments