|
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 spidev;
|
20 | 19 | pub extern crate serial_unix;
|
21 | 20 | pub extern crate serial_core;
|
22 | 21 | pub extern crate nb;
|
23 | 22 |
|
24 |
| - |
25 | 23 | #[cfg(feature = "gpio_sysfs")]
|
26 | 24 | pub extern crate sysfs_gpio;
|
27 | 25 |
|
@@ -194,7 +192,10 @@ impl hal::blocking::i2c::WriteRead for I2cdev {
|
194 | 192 | buffer: &mut [u8],
|
195 | 193 | ) -> Result<(), Self::Error> {
|
196 | 194 | self.set_address(address)?;
|
197 |
| - let mut messages = [LinuxI2CMessage::write(bytes), LinuxI2CMessage::read(buffer)]; |
| 195 | + let mut messages = [ |
| 196 | + LinuxI2CMessage::write(bytes), |
| 197 | + LinuxI2CMessage::read(buffer), |
| 198 | + ]; |
198 | 199 | self.inner.transfer(&mut messages).map(drop)
|
199 | 200 | }
|
200 | 201 | }
|
@@ -249,6 +250,38 @@ impl hal::blocking::spi::Write<u8> for Spidev {
|
249 | 250 | }
|
250 | 251 | }
|
251 | 252 |
|
| 253 | +pub use hal::blocking::spi::{Operation as SpiOperation}; |
| 254 | + |
| 255 | +impl hal::blocking::spi::Transactional<u8> for Spidev { |
| 256 | + type Error = io::Error; |
| 257 | + |
| 258 | + fn exec<'a>(&mut self, operations: &mut [SpiOperation<'a, u8>]) -> Result<(), Self::Error> { |
| 259 | + |
| 260 | + // Map types from generic to linux objects |
| 261 | + let mut messages: Vec<_> = operations.iter_mut().map(|a| { |
| 262 | + match a { |
| 263 | + SpiOperation::Write(w) => SpidevTransfer::write(w), |
| 264 | + SpiOperation::Transfer(r) => { |
| 265 | + // TODO: is spidev okay with the same array pointer |
| 266 | + // being used twice? If not, need some kind of vector |
| 267 | + // pool that will outlive the transfer |
| 268 | + let w = unsafe { |
| 269 | + let p = r.as_ptr(); |
| 270 | + std::slice::from_raw_parts(p, r.len()) |
| 271 | + }; |
| 272 | + |
| 273 | + SpidevTransfer::read_write(w, r) |
| 274 | + }, |
| 275 | + } |
| 276 | + }).collect(); |
| 277 | + |
| 278 | + // Execute transfer |
| 279 | + self.0.transfer_multiple(&mut messages)?; |
| 280 | + |
| 281 | + Ok(()) |
| 282 | + } |
| 283 | +} |
| 284 | + |
252 | 285 | impl ops::Deref for Spidev {
|
253 | 286 | type Target = spidev::Spidev;
|
254 | 287 |
|
|
0 commit comments