diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e1b8ff..c9ee5fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] - ReleaseDate ### Changed +- Update to `embedded-hal` 1.0. - The MSRV is now 1.62.0. ## [0.2.1] - 2020-08-13 diff --git a/Cargo.toml b/Cargo.toml index cebee3e..735b0f4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,14 +22,11 @@ include = [ ] [dependencies] -embedded-hal = "0.2" +embedded-hal = "1" [dev-dependencies] -linux-embedded-hal = "0.3" -embedded-hal-mock = "0.7" +linux-embedded-hal = "0.4" +embedded-hal-mock = { version = "0.11", default-features = false, features = ["eh1"] } [profile.release] lto = true - -[features] -default = [] diff --git a/examples/linux.rs b/examples/linux.rs index 3ba1cb3..c91ec60 100644 --- a/examples/linux.rs +++ b/examples/linux.rs @@ -1,4 +1,4 @@ -use embedded_hal::blocking::i2c::{Read, Write, WriteRead}; +use embedded_hal::i2c::I2c; use linux_embedded_hal::I2cdev; use xca9548a::{Error, SlaveAddr, Xca9548a}; @@ -51,7 +51,8 @@ struct Driver { impl Driver where - I2C: Write + Read + WriteRead, + I2C: I2c, + E: core::fmt::Debug, { pub fn new(i2c: I2C) -> Self { Driver { i2c } diff --git a/src/lib.rs b/src/lib.rs index a31ec82..cd2b84c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -83,7 +83,7 @@ //! ### Reading and writing to device connected to channel 0 (SD0/SC0 pins) //! //! ```no_run -//! use embedded_hal::blocking::i2c::{ Read, Write }; +//! use embedded_hal::i2c::I2c; //! use linux_embedded_hal::I2cdev; //! use xca9548a::{Xca9548a, SlaveAddr}; //! @@ -111,7 +111,7 @@ //! Switching will be done automatically as necessary. //! //! ```no_run -//! use embedded_hal::blocking::i2c::{Read, Write, WriteRead}; +//! use embedded_hal::i2c::I2c; //! use linux_embedded_hal::I2cdev; //! use xca9548a::{Xca9548a, SlaveAddr}; //! @@ -122,7 +122,7 @@ //! } //! //! impl Driver -//! where I2C: Write + Read + WriteRead { +//! where I2C: I2c { //! pub fn new(i2c: I2C) -> Self { //! Driver { i2c } //! } @@ -143,7 +143,7 @@ //! Switching will be done automatically as necessary. //! //! ```no_run -//! use embedded_hal::blocking::i2c::{ Read, Write }; +//! use embedded_hal::i2c::I2c; //! use linux_embedded_hal::I2cdev; //! use xca9548a::{Xca9548a, SlaveAddr}; //! @@ -169,11 +169,11 @@ #![no_std] use core::cell; -use embedded_hal::blocking::i2c; +use embedded_hal::i2c as ehal; /// All possible errors in this crate #[derive(Debug)] -pub enum Error { +pub enum Error { /// I²C bus error I2C(E), /// Could not acquire device. Maybe it is already acquired. @@ -221,7 +221,8 @@ pub struct Xca954xaData { impl SelectChannels for Xca954xaData where - I2C: i2c::Write, + I2C: ehal::I2c, + E: core::fmt::Debug, { type Error = Error; fn select_channels(&mut self, channels: u8) -> Result<(), Self::Error> { @@ -235,7 +236,7 @@ where #[doc(hidden)] pub trait DoOnAcquired: private::Sealed { - fn do_on_acquired( + fn do_on_acquired( &self, f: impl FnOnce(cell::RefMut>) -> Result>, ) -> Result>; @@ -260,14 +261,27 @@ pub struct Xca9543a { } /// Device driver for T/PCA9545A +#[derive(Debug, Default)] pub struct Xca9545a { pub(crate) data: cell::RefCell>, } +impl ehal::Error for Error +where + E: ehal::Error, +{ + fn kind(&self) -> ehal::ErrorKind { + match self { + Error::I2C(e) => e.kind(), + Error::CouldNotAcquireDevice => ehal::ErrorKind::Other, + } + } +} + macro_rules! i2c_traits { ( $name:ident ) => { impl DoOnAcquired for $name { - fn do_on_acquired( + fn do_on_acquired( &self, f: impl FnOnce(cell::RefMut>) -> Result>, ) -> Result> { @@ -279,44 +293,45 @@ macro_rules! i2c_traits { } } - impl i2c::Write for $name + impl ehal::ErrorType for $name where - I2C: i2c::Write, + I2C: ehal::I2c, + E: ehal::Error, { type Error = Error; - - fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> { - self.do_on_acquired(|mut dev| dev.i2c.write(address, bytes).map_err(Error::I2C)) - } } - impl i2c::Read for $name + impl ehal::I2c for $name where - I2C: i2c::Read, + I2C: ehal::I2c, + E: ehal::Error, { - type Error = Error; + fn transaction( + &mut self, + address: u8, + operations: &mut [ehal::Operation<'_>], + ) -> Result<(), Error> { + self.do_on_acquired(|mut dev| { + dev.i2c.transaction(address, operations).map_err(Error::I2C) + }) + } - fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { - self.do_on_acquired(|mut dev| dev.i2c.read(address, buffer).map_err(Error::I2C)) + fn read(&mut self, address: u8, read: &mut [u8]) -> Result<(), Self::Error> { + self.do_on_acquired(|mut dev| dev.i2c.read(address, read).map_err(Error::I2C)) } - } - impl i2c::WriteRead for $name - where - I2C: i2c::WriteRead, - { - type Error = Error; + fn write(&mut self, address: u8, write: &[u8]) -> Result<(), Self::Error> { + self.do_on_acquired(|mut dev| dev.i2c.write(address, write).map_err(Error::I2C)) + } fn write_read( &mut self, address: u8, - bytes: &[u8], - buffer: &mut [u8], + write: &[u8], + read: &mut [u8], ) -> Result<(), Self::Error> { self.do_on_acquired(|mut dev| { - dev.i2c - .write_read(address, bytes, buffer) - .map_err(Error::I2C) + dev.i2c.write_read(address, write, read).map_err(Error::I2C) }) } } @@ -358,7 +373,8 @@ macro_rules! impl_device { impl $name where - I2C: i2c::Read, + I2C: ehal::I2c, + E: ehal::Error, { /// Get status of channels. /// @@ -380,7 +396,8 @@ macro_rules! impl_device { impl $name where - I2C: i2c::Write, + I2C: ehal::I2c, + E: ehal::Error, { /// Select which channels are enabled. /// @@ -399,7 +416,8 @@ macro_rules! impl_device { impl $name where - I2C: i2c::Read, + I2C: ehal::I2c, + E: ehal::Error, { /// Get status of channels. /// @@ -437,7 +455,8 @@ macro_rules! impl_device { impl $name where - I2C: i2c::Write, + I2C: ehal::I2c, + E: ehal::Error, { /// Select which channels are enabled. /// diff --git a/src/parts.rs b/src/parts.rs index 73d76fb..1c5ddfb 100644 --- a/src/parts.rs +++ b/src/parts.rs @@ -1,6 +1,6 @@ use crate::{DoOnAcquired, Error, SelectChannels}; use core::marker::PhantomData; -use embedded_hal::blocking::i2c; +use embedded_hal::i2c as ehal; /// Slave I2C device pub struct I2cSlave<'a, DEV: 'a, I2C>(&'a DEV, u8, PhantomData); @@ -37,60 +37,63 @@ parts!( Parts4; i2c0, 0x01, i2c1, 0x02, i2c2, 0x04, i2c3, 0x08 ); -impl<'a, DEV, I2C, E> i2c::Write for I2cSlave<'a, DEV, I2C> +impl<'a, DEV, I2C, E> ehal::ErrorType for I2cSlave<'a, DEV, I2C> where DEV: DoOnAcquired, - I2C: i2c::Write, + I2C: ehal::I2c, + E: ehal::Error, { type Error = Error; +} - fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> { +impl<'a, DEV, I2C, E> ehal::I2c for I2cSlave<'a, DEV, I2C> +where + DEV: DoOnAcquired, + I2C: ehal::I2c, + E: ehal::Error, +{ + fn transaction( + &mut self, + address: u8, + operations: &mut [ehal::Operation<'_>], + ) -> Result<(), Self::Error> { self.0.do_on_acquired(|mut dev| { if dev.selected_channel_mask != self.1 { dev.select_channels(self.1)?; } - dev.i2c.write(address, bytes).map_err(Error::I2C) + dev.i2c.transaction(address, operations).map_err(Error::I2C) }) } -} - -impl<'a, DEV, I2C, E> i2c::Read for I2cSlave<'a, DEV, I2C> -where - DEV: DoOnAcquired, - I2C: i2c::Write + i2c::Read, -{ - type Error = Error; - fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { + fn read(&mut self, address: u8, read: &mut [u8]) -> Result<(), Self::Error> { self.0.do_on_acquired(|mut dev| { if dev.selected_channel_mask != self.1 { dev.select_channels(self.1)?; } - dev.i2c.read(address, buffer).map_err(Error::I2C) + dev.i2c.read(address, read).map_err(Error::I2C) }) } -} -impl<'a, DEV, I2C, E> i2c::WriteRead for I2cSlave<'a, DEV, I2C> -where - DEV: DoOnAcquired, - I2C: i2c::Write + i2c::WriteRead, -{ - type Error = Error; + fn write(&mut self, address: u8, write: &[u8]) -> Result<(), Self::Error> { + self.0.do_on_acquired(|mut dev| { + if dev.selected_channel_mask != self.1 { + dev.select_channels(self.1)?; + } + dev.i2c.write(address, write).map_err(Error::I2C) + }) + } fn write_read( &mut self, address: u8, - bytes: &[u8], - buffer: &mut [u8], + write: &[u8], + read: &mut [u8], ) -> Result<(), Self::Error> { self.0.do_on_acquired(|mut dev| { if dev.selected_channel_mask != self.1 { dev.select_channels(self.1)?; } - dev.i2c - .write_read(address, bytes, buffer) - .map_err(Error::I2C) + dev.i2c.write_read(address, write, read).map_err(Error::I2C) }) } } diff --git a/tests/tests.rs b/tests/tests.rs index 3b8ad08..c148c35 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1,4 +1,4 @@ -use embedded_hal_mock::i2c::{Mock as I2cMock, Transaction as I2cTrans}; +use embedded_hal_mock::eh1::i2c::{Mock as I2cMock, Transaction as I2cTrans}; use xca9548a::{SlaveAddr, Xca9543a, Xca9545a, Xca9548a}; const DEV_ADDR: u8 = 0b111_0000; @@ -159,14 +159,14 @@ macro_rules! test_device { mod test_xca9548a { use super::*; - use embedded_hal::blocking::i2c::{Read, Write, WriteRead}; + use embedded_hal::i2c::I2c; test_device!(Xca9548a, 0xff); } mod test_xca9545a { use super::*; - use embedded_hal::blocking::i2c::{Read, Write, WriteRead}; + use embedded_hal::i2c::I2c; test_device!(Xca9545a, 0x0f); test_interrupt!(Xca9545a, 0x0f); @@ -175,7 +175,7 @@ mod test_xca9545a { mod test_xca9543a { use super::*; - use embedded_hal::blocking::i2c::{Read, Write, WriteRead}; + use embedded_hal::i2c::I2c; test_device!(Xca9543a, 0x03); test_interrupt!(Xca9543a, 0x03);