diff --git a/Cargo.toml b/Cargo.toml index f3c5e99f..604ee42f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,8 +17,8 @@ keywords = ["raspberry", "pi", "embedded-hal", "embedded-hal-impl", "hal"] libc = "0.2" nb = { version = "0.1.1", optional = true } embedded-hal-0 = { version = "0.2.7", optional = true, package = "embedded-hal" } -embedded-hal = { version = "=1.0.0-alpha.9", optional = true } -embedded-hal-nb = { version = "=1.0.0-alpha.1", optional = true } +embedded-hal = { version = "=1.0.0-alpha.10", optional = true } +embedded-hal-nb = { version = "=1.0.0-alpha.2", optional = true } void = { version = "1.0.2", optional = true } spin_sleep = { version = "1.0.0", optional = true } diff --git a/src/hal.rs b/src/hal.rs index 530fbe8b..64058dd5 100644 --- a/src/hal.rs +++ b/src/hal.rs @@ -6,7 +6,6 @@ //! This module is only included when either the `hal` or `hal-unproven` feature //! flag is enabled. -use core::convert::Infallible; use std::time::{Duration, Instant}; use embedded_hal::delay::DelayUs; @@ -28,21 +27,21 @@ impl Delay { /// `DelayMs` trait implementation for `embedded-hal` v0.2.7. impl embedded_hal_0::blocking::delay::DelayMs for Delay { fn delay_ms(&mut self, ms: u8) { - DelayUs::delay_ms(self, ms as u32).unwrap() + DelayUs::delay_ms(self, ms as u32); } } /// `DelayMs` trait implementation for `embedded-hal` v0.2.7. impl embedded_hal_0::blocking::delay::DelayMs for Delay { fn delay_ms(&mut self, ms: u16) { - DelayUs::delay_ms(self, ms as u32).unwrap() + DelayUs::delay_ms(self, ms as u32); } } /// `DelayMs` trait implementation for `embedded-hal` v0.2.7. impl embedded_hal_0::blocking::delay::DelayMs for Delay { fn delay_ms(&mut self, ms: u32) { - DelayUs::delay_ms(self, ms).unwrap() + DelayUs::delay_ms(self, ms); } } @@ -51,46 +50,42 @@ impl embedded_hal_0::blocking::delay::DelayMs for Delay { fn delay_ms(&mut self, mut ms: u64) { while ms > (u32::MAX as u64) { ms -= u32::MAX as u64; - DelayUs::delay_ms(self, u32::MAX).unwrap(); + DelayUs::delay_ms(self, u32::MAX); } - DelayUs::delay_ms(self, ms as u32).unwrap() + DelayUs::delay_ms(self, ms as u32); } } /// `DelayUs` trait implementation for `embedded-hal` v0.2.7. impl embedded_hal_0::blocking::delay::DelayUs for Delay { fn delay_us(&mut self, us: u8) { - DelayUs::delay_us(self, us as u32).unwrap() + DelayUs::delay_us(self, us as u32); } } /// `DelayUs` trait implementation for `embedded-hal` v0.2.7. impl embedded_hal_0::blocking::delay::DelayUs for Delay { fn delay_us(&mut self, us: u16) { - DelayUs::delay_us(self, us as u32).unwrap() + DelayUs::delay_us(self, us as u32); } } /// `DelayUs` trait implementation for `embedded-hal` v1.0.0-alpha.9. impl DelayUs for Delay { - type Error = Infallible; - - fn delay_us(&mut self, us: u32) -> Result<(), Self::Error> { + fn delay_us(&mut self, us: u32) { sleep(Duration::from_micros(us.into())); - Ok(()) } - fn delay_ms(&mut self, ms: u32) -> Result<(), Self::Error> { + fn delay_ms(&mut self, ms: u32) { sleep(Duration::from_millis(u64::from(ms))); - Ok(()) } } /// `DelayUs` trait implementation for `embedded-hal` v0.2.7. impl embedded_hal_0::blocking::delay::DelayUs for Delay { fn delay_us(&mut self, us: u32) { - DelayUs::delay_us(self, us).unwrap() + DelayUs::delay_us(self, us); } } @@ -99,10 +94,10 @@ impl embedded_hal_0::blocking::delay::DelayUs for Delay { fn delay_us(&mut self, mut us: u64) { while us > (u32::MAX as u64) { us -= u32::MAX as u64; - DelayUs::delay_us(self, u32::MAX).unwrap(); + DelayUs::delay_us(self, u32::MAX); } - DelayUs::delay_us(self, us as u32).unwrap() + DelayUs::delay_us(self, us as u32); } } diff --git a/src/i2c/hal.rs b/src/i2c/hal.rs index 9ba0c2c2..09c09930 100644 --- a/src/i2c/hal.rs +++ b/src/i2c/hal.rs @@ -54,58 +54,8 @@ impl i2c::Error for Error { } } -/// `I2c` trait implementation for `embedded-hal` v1.0.0-alpha.9. +/// `I2c` trait implementation for `embedded-hal` v1.0.0-alpha.10. impl I2cHal for I2c { - fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> { - self.set_slave_address(u16::from(address))?; - I2c::write(self, bytes)?; - - Ok(()) - } - - fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { - self.set_slave_address(u16::from(address))?; - I2c::read(self, buffer)?; - - Ok(()) - } - - fn write_iter(&mut self, address: u8, bytes: B) -> Result<(), Self::Error> - where - B: IntoIterator, - { - let bytes: Vec<_> = bytes.into_iter().collect(); - I2cHal::write(self, address, &bytes) - } - - fn write_read( - &mut self, - address: u8, - bytes: &[u8], - buffer: &mut [u8], - ) -> Result<(), Self::Error> { - self.set_slave_address(u16::from(address))?; - I2c::write_read(self, bytes, buffer)?; - - Ok(()) - } - - fn write_iter_read( - &mut self, - address: u8, - bytes: B, - buffer: &mut [u8], - ) -> Result<(), Self::Error> - where - B: IntoIterator, - { - let bytes: Vec<_> = bytes.into_iter().collect(); - self.transaction( - address, - &mut [I2cOperation::Write(&bytes), I2cOperation::Read(buffer)], - ) - } - fn transaction( &mut self, address: u8, @@ -125,12 +75,4 @@ impl I2cHal for I2c { Ok(()) } - - fn transaction_iter<'a, O>(&mut self, address: u8, operations: O) -> Result<(), Self::Error> - where - O: IntoIterator>, - { - let mut ops: Vec<_> = operations.into_iter().collect(); - self.transaction(address, &mut ops) - } } diff --git a/src/spi/hal.rs b/src/spi/hal.rs index b26f8550..3eb7785a 100644 --- a/src/spi/hal.rs +++ b/src/spi/hal.rs @@ -1,4 +1,4 @@ -use embedded_hal::spi::{self, ErrorType, SpiBus, SpiBusFlush, SpiBusRead, SpiBusWrite, SpiDevice}; +use embedded_hal::spi::{self, ErrorType, SpiBus, SpiBusFlush, SpiBusRead, SpiBusWrite, SpiDevice, SpiDeviceWrite, SpiDeviceRead, Operation}; use embedded_hal_nb::spi::FullDuplex; use std::io; @@ -117,28 +117,82 @@ pub struct SimpleHalSpiDevice { bus: B, } -impl SimpleHalSpiDevice { +impl> SimpleHalSpiDevice { pub fn new(bus: B) -> SimpleHalSpiDevice { SimpleHalSpiDevice { bus } } } -impl SpiDevice for SimpleHalSpiDevice { - type Bus = B; +impl> SpiDeviceRead for SimpleHalSpiDevice +{ + fn read_transaction( + &mut self, + operations: &mut [&mut [u8]] + ) -> Result<(), Error> { + for op in operations { + self.transaction(&mut [Operation::Read(op)])?; + } + Ok(()) + } +} + +impl> SpiDeviceWrite for SimpleHalSpiDevice { + fn write_transaction( + &mut self, + operations: &[&[u8]] + ) -> Result<(), Error> { + for op in operations { + self.transaction(&mut [Operation::Write(op)])?; + } + Ok(()) + } +} - fn transaction( +impl> SpiDevice for SimpleHalSpiDevice { + fn transaction( &mut self, - f: impl FnOnce(&mut Self::Bus) -> Result::Error>, - ) -> Result { - f(&mut self.bus).map_err(|_| { - Error::Io(io::Error::new( - io::ErrorKind::Other, - "SimpleHalSpiDevice transaction error", - )) - }) + operations: &mut [Operation<'_, u8>] + ) -> Result<(), Error> { + for op in operations { + match op { + Operation::Read(read) => { + self.bus.read(read).map_err(|_| { + Error::Io(io::Error::new( + io::ErrorKind::Other, + "SimpleHalSpiDevice read transaction error", + )) + })?; + } + Operation::Write(write) => { + self.bus.write(write).map_err(|_| { + Error::Io(io::Error::new( + io::ErrorKind::Other, + "SimpleHalSpiDevice write transaction error", + )) + })?; + } + Operation::Transfer(read, write) => { + self.bus.transfer(read, write).map_err(|_| { + Error::Io(io::Error::new( + io::ErrorKind::Other, + "SimpleHalSpiDevice read/write transaction error", + )) + })?; + } + Operation::TransferInPlace(words) => { + self.bus.transfer_in_place(words).map_err(|_| { + Error::Io(io::Error::new( + io::ErrorKind::Other, + "SimpleHalSpiDevice in-place read/write transaction error", + )) + })?; + } + } + } + Ok(()) } } -impl ErrorType for SimpleHalSpiDevice { +impl> ErrorType for SimpleHalSpiDevice { type Error = Error; }