From 2524c33f14418ef910ff491ee277d89d8d639295 Mon Sep 17 00:00:00 2001 From: Sycrosity <72102935+Sycrosity@users.noreply.github.com> Date: Fri, 12 Jan 2024 13:32:59 +0000 Subject: [PATCH 1/4] update to eh v1.0.0 --- Cargo.toml | 8 +- src/lib.rs | 2 +- src/manager.rs | 50 ++++---- src/proxies.rs | 317 +++++++++++++++++++++++-------------------------- 4 files changed, 176 insertions(+), 201 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 98bb2d3..380099d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "shared-bus" version = "0.3.1" authors = ["Rahix "] -edition = "2018" +edition = "2021" description = "Abstraction for sharing a bus between multiple devices." repository = "https://github.com/Rahix/shared-bus" @@ -16,7 +16,7 @@ license = "MIT OR Apache-2.0" all-features = true [dependencies] -embedded-hal = { version = "0.2.3", features = ["unproven"] } +embedded-hal = "1.0.0" nb = "1.0.0" once_cell = { version = "1.4.0", optional = true } cortex-m = { version = "0.7.7", optional = true } @@ -24,13 +24,11 @@ xtensa-lx = { version = "0.8.0", optional = true, features = ["spin"] } spin = { version = "0.9.8", optional = true } atomic-polyfill = { version = "1.0.1", optional = true } -embedded-hal-alpha = { package = "embedded-hal", version = "=1.0.0-alpha.9", optional = true } [dev-dependencies] -embedded-hal-mock = "0.9" +embedded-hal-mock = "0.10.0" [features] std = ["once_cell"] xtensa = ["xtensa-lx", "spin"] cortex-m = ["dep:cortex-m", "atomic-polyfill"] -eh-alpha = ["embedded-hal-alpha"] diff --git a/src/lib.rs b/src/lib.rs index 0395dd3..8e10edc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -143,7 +143,7 @@ pub use mutex::CortexMMutex; pub use mutex::NullMutex; #[cfg(feature = "xtensa")] pub use mutex::XtensaMutex; -pub use proxies::AdcProxy; +// pub use proxies::AdcProxy; pub use proxies::I2cProxy; pub use proxies::SpiProxy; diff --git a/src/manager.rs b/src/manager.rs index 68ca3c7..13fef45 100644 --- a/src/manager.rs +++ b/src/manager.rs @@ -127,34 +127,34 @@ impl BusManager { /// my_device.do_something_on_the_bus(); /// # } /// ``` - pub fn acquire_i2c<'a>(&'a self) -> crate::I2cProxy<'a, M> { + pub fn acquire_i2c(&self) -> crate::I2cProxy<'_, M> { crate::I2cProxy { mutex: &self.mutex } } - /// Acquire an [`AdcProxy`] for this hardware block. - /// - /// [`AdcProxy`]: ./struct.AdcProxy.html - /// - /// The returned proxy object can then be used for accessing the bus by e.g. a driver: - /// - /// ```ignore - /// // For example: - /// // let ch0 = gpioa.pa0.into_analog(&mut gpioa.crl); - /// // let ch1 = gpioa.pa1.into_analog(&mut gpioa.crl); - /// // let adc = Adc::adc1(p.ADC1, &mut rcc.apb2, clocks); - /// - /// let adc_bus: &'static _ = shared_bus::new_cortexm!(Adc = adc).unwrap(); - /// let mut proxy1 = adc_bus.acquire_adc(); - /// let mut proxy2 = adc_bus.acquire_adc(); - /// - /// proxy1.read(ch0).unwrap(); - /// proxy2.read(ch1).unwrap(); - /// - /// ``` + // /// Acquire an [`AdcProxy`] for this hardware block. + // /// + // /// [`AdcProxy`]: ./struct.AdcProxy.html + // /// + // /// The returned proxy object can then be used for accessing the bus by e.g. a driver: + // /// + // /// ```ignore + // /// // For example: + // /// // let ch0 = gpioa.pa0.into_analog(&mut gpioa.crl); + // /// // let ch1 = gpioa.pa1.into_analog(&mut gpioa.crl); + // /// // let adc = Adc::adc1(p.ADC1, &mut rcc.apb2, clocks); + // /// + // /// let adc_bus: &'static _ = shared_bus::new_cortexm!(Adc = adc).unwrap(); + // /// let mut proxy1 = adc_bus.acquire_adc(); + // /// let mut proxy2 = adc_bus.acquire_adc(); + // /// + // /// proxy1.read(ch0).unwrap(); + // /// proxy2.read(ch1).unwrap(); + // /// + // /// ``` - pub fn acquire_adc<'a>(&'a self) -> crate::AdcProxy<'a, M> { - crate::AdcProxy { mutex: &self.mutex } - } + // pub fn acquire_adc<'a>(&'a self) -> crate::AdcProxy<'a, M> { + // crate::AdcProxy { mutex: &self.mutex } + // } } impl BusManager> { @@ -195,7 +195,7 @@ impl BusManager> { /// my_device.do_something_on_the_bus(); /// # } /// ``` - pub fn acquire_spi<'a>(&'a self) -> crate::SpiProxy<'a, crate::NullMutex> { + pub fn acquire_spi(&self) -> crate::SpiProxy<'_, crate::NullMutex> { crate::SpiProxy { mutex: &self.mutex, _u: core::marker::PhantomData, diff --git a/src/proxies.rs b/src/proxies.rs index d154ada..c32a3a0 100644 --- a/src/proxies.rs +++ b/src/proxies.rs @@ -1,9 +1,5 @@ -#[cfg(feature = "eh-alpha")] -use embedded_hal_alpha::i2c as i2c_alpha; - -use embedded_hal::adc; -use embedded_hal::blocking::i2c; -use embedded_hal::blocking::spi; +use embedded_hal::i2c; +use embedded_hal::spi; /// Proxy type for I2C bus sharing. /// @@ -21,97 +17,94 @@ pub struct I2cProxy<'a, M> { impl<'a, M: crate::BusMutex> Clone for I2cProxy<'a, M> { fn clone(&self) -> Self { - Self { mutex: &self.mutex } - } -} - -impl<'a, M: crate::BusMutex> i2c::Write for I2cProxy<'a, M> -where - M::Bus: i2c::Write, -{ - type Error = ::Error; - - fn write(&mut self, addr: u8, buffer: &[u8]) -> Result<(), Self::Error> { - self.mutex.lock(|bus| bus.write(addr, buffer)) - } -} - -impl<'a, M: crate::BusMutex> i2c::Read for I2cProxy<'a, M> -where - M::Bus: i2c::Read, -{ - type Error = ::Error; - - fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { - self.mutex.lock(|bus| bus.read(addr, buffer)) - } -} - -impl<'a, M: crate::BusMutex> i2c::WriteRead for I2cProxy<'a, M> -where - M::Bus: i2c::WriteRead, -{ - type Error = ::Error; - - fn write_read( - &mut self, - addr: u8, - buffer_in: &[u8], - buffer_out: &mut [u8], - ) -> Result<(), Self::Error> { - self.mutex - .lock(|bus| bus.write_read(addr, buffer_in, buffer_out)) - } -} - -impl<'a, M: crate::BusMutex> i2c::WriteIterRead for I2cProxy<'a, M> -where - M::Bus: i2c::WriteIterRead, -{ - type Error = ::Error; - - fn write_iter_read( - &mut self, - address: u8, - bytes: B, - buffer: &mut [u8], - ) -> Result<(), Self::Error> - where - B: IntoIterator, - { - self.mutex - .lock(|bus| bus.write_iter_read(address, bytes, buffer)) - } -} - -impl<'a, M: crate::BusMutex> i2c::WriteIter for I2cProxy<'a, M> -where - M::Bus: i2c::WriteIter, -{ - type Error = ::Error; - - fn write(&mut self, address: u8, bytes: B) -> Result<(), Self::Error> - where - B: IntoIterator, - { - self.mutex.lock(|bus| bus.write(address, bytes)) - } -} + Self { mutex: self.mutex } + } +} + +// impl<'a, M: crate::BusMutex> i2c::Write for I2cProxy<'a, M> +// where +// M::Bus: i2c::Write, +// { +// type Error = ::Error; + +// fn write(&mut self, addr: u8, buffer: &[u8]) -> Result<(), Self::Error> { +// self.mutex.lock(|bus| bus.write(addr, buffer)) +// } +// } + +// impl<'a, M: crate::BusMutex> i2c::Read for I2cProxy<'a, M> +// where +// M::Bus: i2c::Read, +// { +// type Error = ::Error; + +// fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { +// self.mutex.lock(|bus| bus.read(addr, buffer)) +// } +// } + +// impl<'a, M: crate::BusMutex> i2c::WriteRead for I2cProxy<'a, M> +// where +// M::Bus: i2c::WriteRead, +// { +// type Error = ::Error; + +// fn write_read( +// &mut self, +// addr: u8, +// buffer_in: &[u8], +// buffer_out: &mut [u8], +// ) -> Result<(), Self::Error> { +// self.mutex +// .lock(|bus| bus.write_read(addr, buffer_in, buffer_out)) +// } +// } + +// impl<'a, M: crate::BusMutex> i2c::WriteIterRead for I2cProxy<'a, M> +// where +// M::Bus: i2c::WriteIterRead, +// { + +// fn write_iter_read( +// &mut self, +// address: u8, +// bytes: B, +// buffer: &mut [u8], +// ) -> Result<(), Self::Error> +// where +// B: IntoIterator, +// { +// self.mutex +// .lock(|bus| bus.write_iter_read(address, bytes, buffer)) +// } +// } + +// impl<'a, M: crate::BusMutex> i2c::WriteIter for I2cProxy<'a, M> +// where +// M::Bus: i2c::WriteIter, +// { +// type Error = ::Error; + +// fn write(&mut self, address: u8, bytes: B) -> Result<(), Self::Error> +// where +// B: IntoIterator, +// { +// self.mutex.lock(|bus| bus.write(address, bytes)) +// } +// } // Implementations for the embedded_hal alpha -#[cfg(feature = "eh-alpha")] -impl<'a, M: crate::BusMutex> i2c_alpha::ErrorType for I2cProxy<'a, M> +impl<'a, M: crate::BusMutex> i2c::ErrorType for I2cProxy<'a, M> where - M::Bus: i2c_alpha::ErrorType, + M::Bus: i2c::ErrorType, { - type Error = ::Error; + type Error = ::Error; } -#[cfg(feature = "eh-alpha")] -impl<'a, M: crate::BusMutex> i2c_alpha::I2c for I2cProxy<'a, M> +impl<'a, M: crate::BusMutex> i2c::I2c for I2cProxy<'a, M> where - M::Bus: i2c_alpha::I2c, + M::Bus: i2c::I2c, { fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { self.mutex.lock(|bus| bus.read(address, buffer)) @@ -121,13 +114,6 @@ where self.mutex.lock(|bus| bus.write(address, bytes)) } - fn write_iter(&mut self, address: u8, bytes: B) -> Result<(), Self::Error> - where - B: IntoIterator, - { - self.mutex.lock(|bus| bus.write_iter(address, bytes)) - } - fn write_read( &mut self, address: u8, @@ -138,34 +124,13 @@ where .lock(|bus| bus.write_read(address, bytes, buffer)) } - fn write_iter_read( - &mut self, - address: u8, - bytes: B, - buffer: &mut [u8], - ) -> Result<(), Self::Error> - where - B: IntoIterator, - { - self.mutex - .lock(|bus| bus.write_iter_read(address, bytes, buffer)) - } - - fn transaction<'b>( + fn transaction( &mut self, address: u8, - operations: &mut [i2c_alpha::Operation<'b>], + operations: &mut [i2c::Operation<'_>], ) -> Result<(), Self::Error> { self.mutex.lock(|bus| bus.transaction(address, operations)) } - - fn transaction_iter<'b, O>(&mut self, address: u8, operations: O) -> Result<(), Self::Error> - where - O: IntoIterator>, - { - self.mutex - .lock(|bus| bus.transaction_iter(address, operations)) - } } /// Proxy type for SPI bus sharing. @@ -189,71 +154,83 @@ pub struct SpiProxy<'a, M> { impl<'a, M: crate::BusMutex> Clone for SpiProxy<'a, M> { fn clone(&self) -> Self { Self { - mutex: &self.mutex, + mutex: self.mutex, _u: core::marker::PhantomData, } } } -impl<'a, M: crate::BusMutex> spi::Transfer for SpiProxy<'a, M> +impl<'a, M: crate::BusMutex> spi::ErrorType for SpiProxy<'a, M> where - M::Bus: spi::Transfer, + M::Bus: spi::ErrorType, { - type Error = >::Error; - - fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Self::Error> { - self.mutex.lock(move |bus| bus.transfer(words)) - } + type Error = ::Error; } -impl<'a, M: crate::BusMutex> spi::Write for SpiProxy<'a, M> +impl<'a, M: crate::BusMutex> spi::SpiDevice for SpiProxy<'a, M> where - M::Bus: spi::Write, + M::Bus: spi::SpiDevice, { - type Error = >::Error; fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> { self.mutex.lock(|bus| bus.write(words)) } -} - -/// Proxy type for ADC sharing. -/// -/// The `AdcProxy` implements OneShot trait so it can be passed to drivers instead of -/// ADC instance. Internally, it holds reference to the bus via a mutex, ensuring -/// that all accesses are strictly synchronized. -/// -/// An `AdcProxy` is created by calling [`BusManager::acquire_adc()`][acquire_adc]. -/// -/// **Note**: The [`adc::OneShot`] trait proxied by this type describes a -/// non-blocking contract for ADC read operation. However access to a shared ADC -/// unit can not be arbitrated in a completely non-blocking and concurrency safe way. -/// Any reading from a channel shall be completed before `shared-bus` can allow the -/// next read from the same or another channel. So the current implementation breaks -/// the non-blocking contract of the trait and just busy-spins until a sample is -/// returned. -/// -/// [acquire_adc]: ./struct.BusManager.html#method.acquire_adc -#[derive(Debug)] -pub struct AdcProxy<'a, M> { - pub(crate) mutex: &'a M, -} -impl<'a, M: crate::BusMutex> Clone for AdcProxy<'a, M> { - fn clone(&self) -> Self { - Self { mutex: &self.mutex } - } -} - -impl<'a, M: crate::BusMutex, ADC, Word, Pin> adc::OneShot for AdcProxy<'a, M> -where - Pin: adc::Channel, - M::Bus: adc::OneShot, -{ - type Error = >::Error; - - fn read(&mut self, pin: &mut Pin) -> nb::Result { + fn transaction(&mut self, operations: &mut [spi::Operation<'_, u8>]) -> Result<(), Self::Error> { self.mutex - .lock(|bus| nb::block!(bus.read(pin)).map_err(nb::Error::Other)) - } -} + .lock(|bus| bus.transaction(operations)) + } + + fn read(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> { + self.transaction(&mut [spi::Operation::Read(buf)]) + } + + fn transfer(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Self::Error> { + self.transaction(&mut [spi::Operation::Transfer(read, write)]) + } + + fn transfer_in_place(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> { + self.transaction(&mut [spi::Operation::TransferInPlace(buf)]) + } +} + +// /// Proxy type for ADC sharing. +// /// +// /// The `AdcProxy` implements OneShot trait so it can be passed to drivers instead of +// /// ADC instance. Internally, it holds reference to the bus via a mutex, ensuring +// /// that all accesses are strictly synchronized. +// /// +// /// An `AdcProxy` is created by calling [`BusManager::acquire_adc()`][acquire_adc]. +// /// +// /// **Note**: The [`adc::OneShot`] trait proxied by this type describes a +// /// non-blocking contract for ADC read operation. However access to a shared ADC +// /// unit can not be arbitrated in a completely non-blocking and concurrency safe way. +// /// Any reading from a channel shall be completed before `shared-bus` can allow the +// /// next read from the same or another channel. So the current implementation breaks +// /// the non-blocking contract of the trait and just busy-spins until a sample is +// /// returned. +// /// +// /// [acquire_adc]: ./struct.BusManager.html#method.acquire_adc +// #[derive(Debug)] +// pub struct AdcProxy<'a, M> { +// pub(crate) mutex: &'a M, +// } + +// impl<'a, M: crate::BusMutex> Clone for AdcProxy<'a, M> { +// fn clone(&self) -> Self { +// Self { mutex: &self.mutex } +// } +// } + +// impl<'a, M: crate::BusMutex, ADC, Word, Pin> adc::OneShot for AdcProxy<'a, M> +// where +// Pin: adc::Channel, +// M::Bus: adc::OneShot, +// { +// type Error = >::Error; + +// fn read(&mut self, pin: &mut Pin) -> nb::Result { +// self.mutex +// .lock(|bus| nb::block!(bus.read(pin)).map_err(nb::Error::Other)) +// } +// } From 80b86afdadb6a34361549795abd13b80f95d9022 Mon Sep 17 00:00:00 2001 From: Sycrosity <72102935+Sycrosity@users.noreply.github.com> Date: Fri, 12 Jan 2024 13:33:42 +0000 Subject: [PATCH 2/4] update to v0.4.0 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 380099d..32ec12c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "shared-bus" -version = "0.3.1" +version = "0.4.0" authors = ["Rahix "] edition = "2021" From cf1143d61d3575d2449253eafafb1ae3fcd8e3e3 Mon Sep 17 00:00:00 2001 From: Sycrosity <72102935+Sycrosity@users.noreply.github.com> Date: Sat, 13 Jan 2024 01:26:48 +0000 Subject: [PATCH 3/4] remove commented out code --- src/proxies.rs | 115 +------------------------------------------------ 1 file changed, 1 insertion(+), 114 deletions(-) diff --git a/src/proxies.rs b/src/proxies.rs index c32a3a0..9eb4b85 100644 --- a/src/proxies.rs +++ b/src/proxies.rs @@ -21,78 +21,6 @@ impl<'a, M: crate::BusMutex> Clone for I2cProxy<'a, M> { } } -// impl<'a, M: crate::BusMutex> i2c::Write for I2cProxy<'a, M> -// where -// M::Bus: i2c::Write, -// { -// type Error = ::Error; - -// fn write(&mut self, addr: u8, buffer: &[u8]) -> Result<(), Self::Error> { -// self.mutex.lock(|bus| bus.write(addr, buffer)) -// } -// } - -// impl<'a, M: crate::BusMutex> i2c::Read for I2cProxy<'a, M> -// where -// M::Bus: i2c::Read, -// { -// type Error = ::Error; - -// fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { -// self.mutex.lock(|bus| bus.read(addr, buffer)) -// } -// } - -// impl<'a, M: crate::BusMutex> i2c::WriteRead for I2cProxy<'a, M> -// where -// M::Bus: i2c::WriteRead, -// { -// type Error = ::Error; - -// fn write_read( -// &mut self, -// addr: u8, -// buffer_in: &[u8], -// buffer_out: &mut [u8], -// ) -> Result<(), Self::Error> { -// self.mutex -// .lock(|bus| bus.write_read(addr, buffer_in, buffer_out)) -// } -// } - -// impl<'a, M: crate::BusMutex> i2c::WriteIterRead for I2cProxy<'a, M> -// where -// M::Bus: i2c::WriteIterRead, -// { - -// fn write_iter_read( -// &mut self, -// address: u8, -// bytes: B, -// buffer: &mut [u8], -// ) -> Result<(), Self::Error> -// where -// B: IntoIterator, -// { -// self.mutex -// .lock(|bus| bus.write_iter_read(address, bytes, buffer)) -// } -// } - -// impl<'a, M: crate::BusMutex> i2c::WriteIter for I2cProxy<'a, M> -// where -// M::Bus: i2c::WriteIter, -// { -// type Error = ::Error; - -// fn write(&mut self, address: u8, bytes: B) -> Result<(), Self::Error> -// where -// B: IntoIterator, -// { -// self.mutex.lock(|bus| bus.write(address, bytes)) -// } -// } - // Implementations for the embedded_hal alpha impl<'a, M: crate::BusMutex> i2c::ErrorType for I2cProxy<'a, M> @@ -192,45 +120,4 @@ where fn transfer_in_place(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> { self.transaction(&mut [spi::Operation::TransferInPlace(buf)]) } -} - -// /// Proxy type for ADC sharing. -// /// -// /// The `AdcProxy` implements OneShot trait so it can be passed to drivers instead of -// /// ADC instance. Internally, it holds reference to the bus via a mutex, ensuring -// /// that all accesses are strictly synchronized. -// /// -// /// An `AdcProxy` is created by calling [`BusManager::acquire_adc()`][acquire_adc]. -// /// -// /// **Note**: The [`adc::OneShot`] trait proxied by this type describes a -// /// non-blocking contract for ADC read operation. However access to a shared ADC -// /// unit can not be arbitrated in a completely non-blocking and concurrency safe way. -// /// Any reading from a channel shall be completed before `shared-bus` can allow the -// /// next read from the same or another channel. So the current implementation breaks -// /// the non-blocking contract of the trait and just busy-spins until a sample is -// /// returned. -// /// -// /// [acquire_adc]: ./struct.BusManager.html#method.acquire_adc -// #[derive(Debug)] -// pub struct AdcProxy<'a, M> { -// pub(crate) mutex: &'a M, -// } - -// impl<'a, M: crate::BusMutex> Clone for AdcProxy<'a, M> { -// fn clone(&self) -> Self { -// Self { mutex: &self.mutex } -// } -// } - -// impl<'a, M: crate::BusMutex, ADC, Word, Pin> adc::OneShot for AdcProxy<'a, M> -// where -// Pin: adc::Channel, -// M::Bus: adc::OneShot, -// { -// type Error = >::Error; - -// fn read(&mut self, pin: &mut Pin) -> nb::Result { -// self.mutex -// .lock(|bus| nb::block!(bus.read(pin)).map_err(nb::Error::Other)) -// } -// } +} \ No newline at end of file From e495929cb39445275058cb57d6559d5f890f98c1 Mon Sep 17 00:00:00 2001 From: Sycrosity <72102935+Sycrosity@users.noreply.github.com> Date: Sat, 13 Jan 2024 01:27:17 +0000 Subject: [PATCH 4/4] delete adc tests --- tests/adc.rs | 107 --------------------------------------------------- tests/i2c.rs | 12 +++--- tests/spi.rs | 16 ++++---- 3 files changed, 14 insertions(+), 121 deletions(-) delete mode 100644 tests/adc.rs diff --git a/tests/adc.rs b/tests/adc.rs deleted file mode 100644 index 5eca709..0000000 --- a/tests/adc.rs +++ /dev/null @@ -1,107 +0,0 @@ -use embedded_hal::prelude::*; -use embedded_hal_mock::adc; -use std::thread; - -#[test] -fn adc_mock_device() { - let expectations = [ - adc::Transaction::read(0, 0xabcd), - adc::Transaction::read(1, 0xabba), - adc::Transaction::read(2, 0xbaab), - ]; - - let mut device = adc::Mock::new(&expectations); - assert_eq!(0xabcd, device.read(&mut adc::MockChan0).unwrap()); - assert_eq!(0xabba, device.read(&mut adc::MockChan1).unwrap()); - assert_eq!(0xbaab, device.read(&mut adc::MockChan2).unwrap()); - device.done() -} - -#[test] -fn adc_manager_simple() { - let expectations = [ - adc::Transaction::read(0, 0xabcd), - adc::Transaction::read(1, 0xabba), - adc::Transaction::read(2, 0xbaab), - ]; - - let mut device = adc::Mock::new(&expectations); - let manager = shared_bus::BusManagerSimple::new(device.clone()); - let mut proxy = manager.acquire_adc(); - - assert_eq!(0xabcd, proxy.read(&mut adc::MockChan0).unwrap()); - assert_eq!(0xabba, proxy.read(&mut adc::MockChan1).unwrap()); - assert_eq!(0xbaab, proxy.read(&mut adc::MockChan2).unwrap()); - device.done() -} - -#[test] -fn adc_manager_std() { - let expectations = [ - adc::Transaction::read(0, 0xabcd), - adc::Transaction::read(1, 0xabba), - adc::Transaction::read(2, 0xbaab), - ]; - - let mut device = adc::Mock::new(&expectations); - let manager: &'static shared_bus::BusManagerStd<_> = - shared_bus::new_std!(adc::Mock = device.clone()).unwrap(); - let mut proxy = manager.acquire_adc(); - - assert_eq!(0xabcd, proxy.read(&mut adc::MockChan0).unwrap()); - assert_eq!(0xabba, proxy.read(&mut adc::MockChan1).unwrap()); - assert_eq!(0xbaab, proxy.read(&mut adc::MockChan2).unwrap()); - device.done() -} - -#[test] -fn adc_proxy_multi() { - let expectations = [ - adc::Transaction::read(0, 0xabcd), - adc::Transaction::read(1, 0xabba), - adc::Transaction::read(2, 0xbaab), - ]; - - let mut device = adc::Mock::new(&expectations); - let manager = shared_bus::BusManagerSimple::new(device.clone()); - let mut proxy1 = manager.acquire_adc(); - let mut proxy2 = manager.acquire_adc(); - let mut proxy3 = manager.acquire_adc(); - - assert_eq!(0xabcd, proxy1.read(&mut adc::MockChan0).unwrap()); - assert_eq!(0xabba, proxy2.read(&mut adc::MockChan1).unwrap()); - assert_eq!(0xbaab, proxy3.read(&mut adc::MockChan2).unwrap()); - device.done() -} - -#[test] -fn adc_proxy_concurrent() { - let expectations = [ - adc::Transaction::read(0, 0xabcd), - adc::Transaction::read(1, 0xabba), - adc::Transaction::read(2, 0xbaab), - ]; - - let mut device = adc::Mock::new(&expectations); - let manager: &'static shared_bus::BusManagerStd<_> = - shared_bus::new_std!(adc::Mock = device.clone()).unwrap(); - let mut proxy1 = manager.acquire_adc(); - let mut proxy2 = manager.acquire_adc(); - let mut proxy3 = manager.acquire_adc(); - - thread::spawn(move || { - assert_eq!(0xabcd, proxy1.read(&mut adc::MockChan0).unwrap()); - }) - .join() - .unwrap(); - - thread::spawn(move || { - assert_eq!(0xabba, proxy2.read(&mut adc::MockChan1).unwrap()); - }) - .join() - .unwrap(); - - assert_eq!(0xbaab, proxy3.read(&mut adc::MockChan2).unwrap()); - - device.done() -} diff --git a/tests/i2c.rs b/tests/i2c.rs index 0903779..8dbe200 100644 --- a/tests/i2c.rs +++ b/tests/i2c.rs @@ -1,18 +1,18 @@ -use embedded_hal::prelude::*; -use embedded_hal_mock::i2c; +use embedded_hal::i2c::I2c; +use embedded_hal_mock::eh1::i2c; use std::thread; #[test] fn fake_i2c_device() { - let expect = vec![i2c::Transaction::write(0xc0, vec![0xff, 0xee])]; + let expect = vec![i2c::Transaction::write(0xc0, vec![0xffu8, 0xeeu8])]; let mut device = i2c::Mock::new(&expect); - device.write(0xc0, &[0xff, 0xee]).unwrap(); + device.write(0xc0, &[0xffu8, 0xeeu8]).unwrap(); device.done() } #[test] fn i2c_manager_manual() { - let expect = vec![i2c::Transaction::write(0xde, vec![0xad, 0xbe, 0xef])]; + let expect = vec![i2c::Transaction::write(0xde, vec![0xadu8, 0xbeu8, 0xefu8])]; let mut device = i2c::Mock::new(&expect); let manager = shared_bus::BusManagerSimple::new(device.clone()); let mut proxy = manager.acquire_i2c(); @@ -24,7 +24,7 @@ fn i2c_manager_manual() { #[test] fn i2c_manager_macro() { - let expect = vec![i2c::Transaction::write(0xde, vec![0xad, 0xbe, 0xef])]; + let expect = vec![i2c::Transaction::write(0xde, vec![0xadu8, 0xbeu8, 0xefu8])]; let mut device = i2c::Mock::new(&expect); let manager: &'static shared_bus::BusManagerStd<_> = shared_bus::new_std!(i2c::Mock = device.clone()).unwrap(); diff --git a/tests/spi.rs b/tests/spi.rs index bfce077..cc964a4 100644 --- a/tests/spi.rs +++ b/tests/spi.rs @@ -1,22 +1,22 @@ -use embedded_hal::prelude::*; -use embedded_hal_mock::spi; +use embedded_hal::spi::SpiDevice; +use embedded_hal_mock::eh1::spi; #[test] fn fake_spi_device() { - let expect = vec![spi::Transaction::write(vec![0xff, 0xee])]; + let expect = vec![spi::Transaction::write(&[0xffu8, 0xee])]; let mut device = spi::Mock::new(&expect); - device.write(&[0xff, 0xee]).unwrap(); + device.write(&[0xffu8, 0xee]).unwrap(); device.done() } #[test] fn spi_manager_manual() { - let expect = vec![spi::Transaction::write(vec![0xab, 0xcd, 0xef])]; + let expect = vec![spi::Transaction::write(&[0xabu8, 0xcd, 0xef])]; let mut device = spi::Mock::new(&expect); let manager = shared_bus::BusManagerSimple::new(device.clone()); let mut proxy = manager.acquire_spi(); - proxy.write(&[0xab, 0xcd, 0xef]).unwrap(); + proxy.write(&[0xabu8, 0xcd, 0xef]).unwrap(); device.done(); } @@ -24,8 +24,8 @@ fn spi_manager_manual() { #[test] fn spi_proxy() { let expect = vec![ - spi::Transaction::write(vec![0xab, 0xcd, 0xef]), - spi::Transaction::transfer(vec![0x01, 0x02], vec![0x03, 0x04]), + spi::Transaction::write(&[0xabu8, 0xcd, 0xef]), + spi::Transaction::transfer(&[0x01u8, 0x02], &[0x03u8, 0x04]), ]; let mut device = spi::Mock::new(&expect); let manager = shared_bus::BusManagerSimple::new(device.clone());