From f30ff9cadcf575100a4e08c972d2f161172a3fc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Als=C3=A9r?= Date: Fri, 8 Jul 2022 15:47:47 +0200 Subject: [PATCH 01/11] Shared buses with SetConfig --- embassy-embedded-hal/src/lib.rs | 4 + .../src/shared_bus/blocking/i2c.rs | 84 +++++++++++++++++++ .../src/shared_bus/blocking/spi.rs | 50 +++++++++++ embassy-embedded-hal/src/shared_bus/i2c.rs | 78 +++++++++++++++++ embassy-embedded-hal/src/shared_bus/spi.rs | 61 ++++++++++++++ embassy-nrf/Cargo.toml | 1 + embassy-nrf/src/spim.rs | 43 ++++++++++ embassy-nrf/src/twim.rs | 10 +++ examples/nrf/src/bin/shared_bus.rs | 21 +++++ 9 files changed, 352 insertions(+) create mode 100644 examples/nrf/src/bin/shared_bus.rs diff --git a/embassy-embedded-hal/src/lib.rs b/embassy-embedded-hal/src/lib.rs index 27ffa742..aae71992 100644 --- a/embassy-embedded-hal/src/lib.rs +++ b/embassy-embedded-hal/src/lib.rs @@ -4,3 +4,7 @@ pub mod adapter; pub mod shared_bus; + +pub trait SetConfig { + fn set_config(&mut self, config: &C); +} diff --git a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs index 2c762fe1..6f5f0705 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs @@ -7,6 +7,7 @@ use embedded_hal_1::i2c::blocking::{I2c, Operation}; use embedded_hal_1::i2c::ErrorType; use crate::shared_bus::i2c::I2cBusDeviceError; +use crate::SetConfig; pub struct I2cBusDevice<'a, M: RawMutex, BUS> { bus: &'a Mutex>, @@ -82,3 +83,86 @@ where todo!() } } + +pub struct I2cBusDeviceWithConfig<'a, M: RawMutex, BUS, C> { + bus: &'a Mutex>, + config: C, +} + +impl<'a, M: RawMutex, BUS, C> I2cBusDeviceWithConfig<'a, M, BUS, C> { + pub fn new(bus: &'a Mutex>, config: C) -> Self { + Self { bus, config } + } +} + +impl<'a, M: RawMutex, BUS, C> ErrorType for I2cBusDeviceWithConfig<'a, M, BUS, C> +where + BUS: ErrorType, +{ + type Error = I2cBusDeviceError; +} + +impl I2c for I2cBusDeviceWithConfig<'_, M, BUS, C> +where + M: RawMutex, + BUS: I2c + SetConfig, +{ + fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { + self.bus.lock(|bus| { + let mut bus = bus.borrow_mut(); + bus.set_config(&self.config); + bus.read(address, buffer).map_err(I2cBusDeviceError::I2c) + }) + } + + fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> { + self.bus.lock(|bus| { + let mut bus = bus.borrow_mut(); + bus.set_config(&self.config); + bus.write(address, bytes).map_err(I2cBusDeviceError::I2c) + }) + } + + fn write_read(&mut self, address: u8, wr_buffer: &[u8], rd_buffer: &mut [u8]) -> Result<(), Self::Error> { + self.bus.lock(|bus| { + let mut bus = bus.borrow_mut(); + bus.set_config(&self.config); + bus.write_read(address, wr_buffer, rd_buffer) + .map_err(I2cBusDeviceError::I2c) + }) + } + + fn transaction<'a>(&mut self, address: u8, operations: &mut [Operation<'a>]) -> Result<(), Self::Error> { + let _ = address; + let _ = operations; + todo!() + } + + fn write_iter>(&mut self, addr: u8, bytes: B) -> Result<(), Self::Error> { + let _ = addr; + let _ = bytes; + todo!() + } + + fn write_iter_read>( + &mut self, + addr: u8, + bytes: B, + buffer: &mut [u8], + ) -> Result<(), Self::Error> { + let _ = addr; + let _ = bytes; + let _ = buffer; + todo!() + } + + fn transaction_iter<'a, O: IntoIterator>>( + &mut self, + address: u8, + operations: O, + ) -> Result<(), Self::Error> { + let _ = address; + let _ = operations; + todo!() + } +} diff --git a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs index c08bcbf6..d54ca6bf 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs @@ -8,6 +8,7 @@ use embedded_hal_1::spi; use embedded_hal_1::spi::blocking::{SpiBusFlush, SpiDevice}; use crate::shared_bus::spi::SpiBusDeviceError; +use crate::SetConfig; pub struct SpiBusDevice<'a, M: RawMutex, BUS, CS> { bus: &'a Mutex>, @@ -55,3 +56,52 @@ where }) } } + +pub struct SpiBusDeviceWithConfig<'a, M: RawMutex, BUS, CS, C> { + bus: &'a Mutex>, + cs: CS, + config: C, +} + +impl<'a, M: RawMutex, BUS, CS, C> SpiBusDeviceWithConfig<'a, M, BUS, CS, C> { + pub fn new(bus: &'a Mutex>, cs: CS, config: C) -> Self { + Self { bus, cs, config } + } +} + +impl<'a, M: RawMutex, BUS, CS, C> spi::ErrorType for SpiBusDeviceWithConfig<'a, M, BUS, CS, C> +where + BUS: spi::ErrorType, + CS: OutputPin, +{ + type Error = SpiBusDeviceError; +} + +impl SpiDevice for SpiBusDeviceWithConfig<'_, M, BUS, CS, C> +where + M: RawMutex, + BUS: SpiBusFlush + SetConfig, + CS: OutputPin, +{ + type Bus = BUS; + + fn transaction(&mut self, f: impl FnOnce(&mut Self::Bus) -> Result) -> Result { + self.bus.lock(|bus| { + let mut bus = bus.borrow_mut(); + bus.set_config(&self.config); + self.cs.set_low().map_err(SpiBusDeviceError::Cs)?; + + let f_res = f(&mut bus); + + // On failure, it's important to still flush and deassert CS. + let flush_res = bus.flush(); + let cs_res = self.cs.set_high(); + + let f_res = f_res.map_err(SpiBusDeviceError::Spi)?; + flush_res.map_err(SpiBusDeviceError::Spi)?; + cs_res.map_err(SpiBusDeviceError::Cs)?; + + Ok(f_res) + }) + } +} diff --git a/embassy-embedded-hal/src/shared_bus/i2c.rs b/embassy-embedded-hal/src/shared_bus/i2c.rs index e8131288..0e964773 100644 --- a/embassy-embedded-hal/src/shared_bus/i2c.rs +++ b/embassy-embedded-hal/src/shared_bus/i2c.rs @@ -29,6 +29,8 @@ use embassy::blocking_mutex::raw::RawMutex; use embassy::mutex::Mutex; use embedded_hal_async::i2c; +use crate::SetConfig; + #[derive(Copy, Clone, Eq, PartialEq, Debug)] pub enum I2cBusDeviceError { I2c(BUS), @@ -116,3 +118,79 @@ where async move { todo!() } } } + +pub struct I2cBusDeviceWithConfig<'a, M: RawMutex, BUS, C> { + bus: &'a Mutex, + config: C, +} + +impl<'a, M: RawMutex, BUS, C> I2cBusDeviceWithConfig<'a, M, BUS, C> { + pub fn new(bus: &'a Mutex, config: C) -> Self { + Self { bus, config } + } +} + +impl<'a, M: RawMutex, BUS, C> i2c::ErrorType for I2cBusDeviceWithConfig<'a, M, BUS, C> +where + BUS: i2c::ErrorType, +{ + type Error = I2cBusDeviceError; +} + +impl i2c::I2c for I2cBusDeviceWithConfig<'_, M, BUS, C> +where + M: RawMutex + 'static, + BUS: i2c::I2c + SetConfig + 'static, +{ + type ReadFuture<'a> = impl Future> + 'a where Self: 'a; + + fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Self::ReadFuture<'a> { + async move { + let mut bus = self.bus.lock().await; + bus.set_config(&self.config); + bus.read(address, buffer).await.map_err(I2cBusDeviceError::I2c)?; + Ok(()) + } + } + + type WriteFuture<'a> = impl Future> + 'a where Self: 'a; + + fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Self::WriteFuture<'a> { + async move { + let mut bus = self.bus.lock().await; + bus.set_config(&self.config); + bus.write(address, bytes).await.map_err(I2cBusDeviceError::I2c)?; + Ok(()) + } + } + + type WriteReadFuture<'a> = impl Future> + 'a where Self: 'a; + + fn write_read<'a>( + &'a mut self, + address: u8, + wr_buffer: &'a [u8], + rd_buffer: &'a mut [u8], + ) -> Self::WriteReadFuture<'a> { + async move { + let mut bus = self.bus.lock().await; + bus.set_config(&self.config); + bus.write_read(address, wr_buffer, rd_buffer) + .await + .map_err(I2cBusDeviceError::I2c)?; + Ok(()) + } + } + + type TransactionFuture<'a, 'b> = impl Future> + 'a where Self: 'a, 'b: 'a; + + fn transaction<'a, 'b>( + &'a mut self, + address: u8, + operations: &'a mut [embedded_hal_async::i2c::Operation<'b>], + ) -> Self::TransactionFuture<'a, 'b> { + let _ = address; + let _ = operations; + async move { todo!() } + } +} diff --git a/embassy-embedded-hal/src/shared_bus/spi.rs b/embassy-embedded-hal/src/shared_bus/spi.rs index fd4b6d56..04378c33 100644 --- a/embassy-embedded-hal/src/shared_bus/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/spi.rs @@ -34,6 +34,8 @@ use embedded_hal_1::digital::blocking::OutputPin; use embedded_hal_1::spi::ErrorType; use embedded_hal_async::spi; +use crate::SetConfig; + #[derive(Copy, Clone, Eq, PartialEq, Debug)] pub enum SpiBusDeviceError { Spi(BUS), @@ -109,3 +111,62 @@ where } } } + +pub struct SpiBusDeviceWithConfig<'a, M: RawMutex, BUS, CS, C> { + bus: &'a Mutex, + cs: CS, + config: C, +} + +impl<'a, M: RawMutex, BUS, CS, C> SpiBusDeviceWithConfig<'a, M, BUS, CS, C> { + pub fn new(bus: &'a Mutex, cs: CS, config: C) -> Self { + Self { bus, cs, config } + } +} + +impl<'a, M: RawMutex, BUS, CS, C> spi::ErrorType for SpiBusDeviceWithConfig<'a, M, BUS, CS, C> +where + BUS: spi::ErrorType, + CS: OutputPin, +{ + type Error = SpiBusDeviceError; +} + +impl spi::SpiDevice for SpiBusDeviceWithConfig<'_, M, BUS, CS, C> +where + M: RawMutex + 'static, + BUS: spi::SpiBusFlush + SetConfig + 'static, + CS: OutputPin, +{ + type Bus = BUS; + + type TransactionFuture<'a, R, F, Fut> = impl Future> + 'a + where + Self: 'a, R: 'a, F: FnOnce(*mut Self::Bus) -> Fut + 'a, + Fut: Future::Error>> + 'a; + + fn transaction<'a, R, F, Fut>(&'a mut self, f: F) -> Self::TransactionFuture<'a, R, F, Fut> + where + R: 'a, + F: FnOnce(*mut Self::Bus) -> Fut + 'a, + Fut: Future::Error>> + 'a, + { + async move { + let mut bus = self.bus.lock().await; + bus.set_config(&self.config); + self.cs.set_low().map_err(SpiBusDeviceError::Cs)?; + + let f_res = f(&mut *bus).await; + + // On failure, it's important to still flush and deassert CS. + let flush_res = bus.flush().await; + let cs_res = self.cs.set_high(); + + let f_res = f_res.map_err(SpiBusDeviceError::Spi)?; + flush_res.map_err(SpiBusDeviceError::Spi)?; + cs_res.map_err(SpiBusDeviceError::Cs)?; + + Ok(f_res) + } + } +} diff --git a/embassy-nrf/Cargo.toml b/embassy-nrf/Cargo.toml index 887ea1bd..3f9dabbb 100644 --- a/embassy-nrf/Cargo.toml +++ b/embassy-nrf/Cargo.toml @@ -68,6 +68,7 @@ embassy = { version = "0.1.0", path = "../embassy" } embassy-cortex-m = { version = "0.1.0", path = "../embassy-cortex-m", features = ["prio-bits-3"]} embassy-macros = { version = "0.1.0", path = "../embassy-macros", features = ["nrf"]} embassy-hal-common = {version = "0.1.0", path = "../embassy-hal-common" } +embassy-embedded-hal = {version = "0.1.0", path = "../embassy-embedded-hal" } embassy-usb = {version = "0.1.0", path = "../embassy-usb", optional=true } embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = ["unproven"] } diff --git a/embassy-nrf/src/spim.rs b/embassy-nrf/src/spim.rs index efccfeca..882bf4b4 100644 --- a/embassy-nrf/src/spim.rs +++ b/embassy-nrf/src/spim.rs @@ -4,6 +4,7 @@ use core::marker::PhantomData; use core::sync::atomic::{compiler_fence, Ordering}; use core::task::Poll; +use embassy_embedded_hal::SetConfig; use embassy_hal_common::unborrow; pub use embedded_hal_02::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3}; use futures::future::poll_fn; @@ -521,3 +522,45 @@ cfg_if::cfg_if! { } } } + +impl<'d, T: Instance> SetConfig for Spim<'d, T> { + fn set_config(&mut self, config: &Config) { + let r = T::regs(); + // Configure mode. + let mode = config.mode; + r.config.write(|w| { + match mode { + MODE_0 => { + w.order().msb_first(); + w.cpol().active_high(); + w.cpha().leading(); + } + MODE_1 => { + w.order().msb_first(); + w.cpol().active_high(); + w.cpha().trailing(); + } + MODE_2 => { + w.order().msb_first(); + w.cpol().active_low(); + w.cpha().leading(); + } + MODE_3 => { + w.order().msb_first(); + w.cpol().active_low(); + w.cpha().trailing(); + } + } + + w + }); + + // Configure frequency. + let frequency = config.frequency; + r.frequency.write(|w| w.frequency().variant(frequency)); + + // Set over-read character + let orc = config.orc; + r.orc.write(|w| unsafe { w.orc().bits(orc) }); + } +} diff --git a/embassy-nrf/src/twim.rs b/embassy-nrf/src/twim.rs index c3921104..8189d3e7 100644 --- a/embassy-nrf/src/twim.rs +++ b/embassy-nrf/src/twim.rs @@ -15,6 +15,7 @@ use core::task::Poll; #[cfg(feature = "time")] use embassy::time::{Duration, Instant}; use embassy::waitqueue::AtomicWaker; +use embassy_embedded_hal::SetConfig; use embassy_hal_common::unborrow; use futures::future::poll_fn; @@ -24,6 +25,7 @@ use crate::interrupt::{Interrupt, InterruptExt}; use crate::util::{slice_in_ram, slice_in_ram_or}; use crate::{gpio, pac, Unborrow}; +#[derive(Clone, Copy)] pub enum Frequency { #[doc = "26738688: 100 kbps"] K100 = 26738688, @@ -877,3 +879,11 @@ cfg_if::cfg_if! { } } } + +impl<'d, T: Instance> SetConfig for Twim<'d, T> { + fn set_config(&mut self, config: &Config) { + let r = T::regs(); + r.frequency + .write(|w| unsafe { w.frequency().bits(config.frequency as u32) }); + } +} diff --git a/examples/nrf/src/bin/shared_bus.rs b/examples/nrf/src/bin/shared_bus.rs new file mode 100644 index 00000000..23d16f79 --- /dev/null +++ b/examples/nrf/src/bin/shared_bus.rs @@ -0,0 +1,21 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +use embassy::executor::Spawner; +use embassy::time::{Duration, Timer}; +use embassy_nrf::gpio::{Level, Output, OutputDrive}; +use embassy_nrf::Peripherals; +use {defmt_rtt as _, panic_probe as _}; + +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + let mut led = Output::new(p.P0_13, Level::Low, OutputDrive::Standard); + + loop { + led.set_high(); + Timer::after(Duration::from_millis(300)).await; + led.set_low(); + Timer::after(Duration::from_millis(300)).await; + } +} From 15384d27bb181bf48e580ca4a1c4fd848ecb1720 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Als=C3=A9r?= Date: Fri, 8 Jul 2022 23:42:19 +0200 Subject: [PATCH 02/11] Merge upstream --- embassy-embedded-hal/src/shared_bus/blocking/i2c.rs | 5 ++--- embassy-embedded-hal/src/shared_bus/blocking/spi.rs | 7 ++++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs index 12c2a1f4..e8dd0f24 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs @@ -101,7 +101,6 @@ where } } -<<<<<<< HEAD pub struct I2cBusDeviceWithConfig<'a, M: RawMutex, BUS, C> { bus: &'a Mutex>, config: C, @@ -183,7 +182,8 @@ where let _ = operations; todo!() } -======= +} + impl<'a, M, BUS, E> embedded_hal_02::blocking::i2c::Write for I2cBusDevice<'_, M, BUS> where M: RawMutex, @@ -224,5 +224,4 @@ where .map_err(I2cBusDeviceError::I2c) }) } ->>>>>>> master } diff --git a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs index b31efd64..2bcf47cf 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs @@ -76,7 +76,6 @@ where } } -<<<<<<< HEAD pub struct SpiBusDeviceWithConfig<'a, M: RawMutex, BUS, CS, C> { bus: &'a Mutex>, cs: CS, @@ -120,8 +119,11 @@ where let f_res = f_res.map_err(SpiBusDeviceError::Spi)?; flush_res.map_err(SpiBusDeviceError::Spi)?; cs_res.map_err(SpiBusDeviceError::Cs)?; + Ok(f_res) + }) + } +} -======= impl<'d, M, BUS, CS, BusErr, CsErr> embedded_hal_02::blocking::spi::Transfer for SpiBusDevice<'_, M, BUS, CS> where M: RawMutex, @@ -158,7 +160,6 @@ where let cs_res = self.cs.set_high(); let f_res = f_res.map_err(SpiBusDeviceError::Spi)?; cs_res.map_err(SpiBusDeviceError::Cs)?; ->>>>>>> master Ok(f_res) }) } From d637510b44ec8e58581f3e22f8398b53145503dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Als=C3=A9r?= Date: Sat, 9 Jul 2022 00:00:55 +0200 Subject: [PATCH 03/11] Associated type --- embassy-embedded-hal/src/lib.rs | 5 +- .../src/shared_bus/blocking/i2c.rs | 103 +++++++++--------- .../src/shared_bus/blocking/spi.rs | 97 +++++++++-------- embassy-embedded-hal/src/shared_bus/i2c.rs | 16 +-- embassy-embedded-hal/src/shared_bus/spi.rs | 17 +-- embassy-nrf/src/spim.rs | 5 +- embassy-nrf/src/twim.rs | 5 +- examples/nrf/src/bin/shared_bus.rs | 21 ---- 8 files changed, 128 insertions(+), 141 deletions(-) delete mode 100644 examples/nrf/src/bin/shared_bus.rs diff --git a/embassy-embedded-hal/src/lib.rs b/embassy-embedded-hal/src/lib.rs index aae71992..688d0b48 100644 --- a/embassy-embedded-hal/src/lib.rs +++ b/embassy-embedded-hal/src/lib.rs @@ -5,6 +5,7 @@ pub mod adapter; pub mod shared_bus; -pub trait SetConfig { - fn set_config(&mut self, config: &C); +pub trait SetConfig { + type Config; + fn set_config(&mut self, config: &Self::Config); } diff --git a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs index e8dd0f24..0c8338c7 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs @@ -101,28 +101,71 @@ where } } -pub struct I2cBusDeviceWithConfig<'a, M: RawMutex, BUS, C> { - bus: &'a Mutex>, - config: C, +impl<'a, M, BUS, E> embedded_hal_02::blocking::i2c::Write for I2cBusDevice<'_, M, BUS> +where + M: RawMutex, + BUS: embedded_hal_02::blocking::i2c::Write, +{ + type Error = I2cBusDeviceError; + + fn write<'w>(&mut self, addr: u8, bytes: &'w [u8]) -> Result<(), Self::Error> { + self.bus + .lock(|bus| bus.borrow_mut().write(addr, bytes).map_err(I2cBusDeviceError::I2c)) + } } -impl<'a, M: RawMutex, BUS, C> I2cBusDeviceWithConfig<'a, M, BUS, C> { - pub fn new(bus: &'a Mutex>, config: C) -> Self { +impl<'a, M, BUS, E> embedded_hal_02::blocking::i2c::Read for I2cBusDevice<'_, M, BUS> +where + M: RawMutex, + BUS: embedded_hal_02::blocking::i2c::Read, +{ + type Error = I2cBusDeviceError; + + fn read<'w>(&mut self, addr: u8, bytes: &'w mut [u8]) -> Result<(), Self::Error> { + self.bus + .lock(|bus| bus.borrow_mut().read(addr, bytes).map_err(I2cBusDeviceError::I2c)) + } +} + +impl<'a, M, BUS, E> embedded_hal_02::blocking::i2c::WriteRead for I2cBusDevice<'_, M, BUS> +where + M: RawMutex, + BUS: embedded_hal_02::blocking::i2c::WriteRead, +{ + type Error = I2cBusDeviceError; + + fn write_read<'w>(&mut self, addr: u8, bytes: &'w [u8], buffer: &'w mut [u8]) -> Result<(), Self::Error> { + self.bus.lock(|bus| { + bus.borrow_mut() + .write_read(addr, bytes, buffer) + .map_err(I2cBusDeviceError::I2c) + }) + } +} + +pub struct I2cBusDeviceWithConfig<'a, M: RawMutex, BUS: SetConfig> { + bus: &'a Mutex>, + config: BUS::Config, +} + +impl<'a, M: RawMutex, BUS: SetConfig> I2cBusDeviceWithConfig<'a, M, BUS> { + pub fn new(bus: &'a Mutex>, config: BUS::Config) -> Self { Self { bus, config } } } -impl<'a, M: RawMutex, BUS, C> ErrorType for I2cBusDeviceWithConfig<'a, M, BUS, C> +impl<'a, M, BUS> ErrorType for I2cBusDeviceWithConfig<'a, M, BUS> where - BUS: ErrorType, + M: RawMutex, + BUS: ErrorType + SetConfig, { type Error = I2cBusDeviceError; } -impl I2c for I2cBusDeviceWithConfig<'_, M, BUS, C> +impl I2c for I2cBusDeviceWithConfig<'_, M, BUS> where M: RawMutex, - BUS: I2c + SetConfig, + BUS: I2c + SetConfig, { fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { self.bus.lock(|bus| { @@ -183,45 +226,3 @@ where todo!() } } - -impl<'a, M, BUS, E> embedded_hal_02::blocking::i2c::Write for I2cBusDevice<'_, M, BUS> -where - M: RawMutex, - BUS: embedded_hal_02::blocking::i2c::Write, -{ - type Error = I2cBusDeviceError; - - fn write<'w>(&mut self, addr: u8, bytes: &'w [u8]) -> Result<(), Self::Error> { - self.bus - .lock(|bus| bus.borrow_mut().write(addr, bytes).map_err(I2cBusDeviceError::I2c)) - } -} - -impl<'a, M, BUS, E> embedded_hal_02::blocking::i2c::Read for I2cBusDevice<'_, M, BUS> -where - M: RawMutex, - BUS: embedded_hal_02::blocking::i2c::Read, -{ - type Error = I2cBusDeviceError; - - fn read<'w>(&mut self, addr: u8, bytes: &'w mut [u8]) -> Result<(), Self::Error> { - self.bus - .lock(|bus| bus.borrow_mut().read(addr, bytes).map_err(I2cBusDeviceError::I2c)) - } -} - -impl<'a, M, BUS, E> embedded_hal_02::blocking::i2c::WriteRead for I2cBusDevice<'_, M, BUS> -where - M: RawMutex, - BUS: embedded_hal_02::blocking::i2c::WriteRead, -{ - type Error = I2cBusDeviceError; - - fn write_read<'w>(&mut self, addr: u8, bytes: &'w [u8], buffer: &'w mut [u8]) -> Result<(), Self::Error> { - self.bus.lock(|bus| { - bus.borrow_mut() - .write_read(addr, bytes, buffer) - .map_err(I2cBusDeviceError::I2c) - }) - } -} diff --git a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs index 2bcf47cf..456da885 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs @@ -76,54 +76,6 @@ where } } -pub struct SpiBusDeviceWithConfig<'a, M: RawMutex, BUS, CS, C> { - bus: &'a Mutex>, - cs: CS, - config: C, -} - -impl<'a, M: RawMutex, BUS, CS, C> SpiBusDeviceWithConfig<'a, M, BUS, CS, C> { - pub fn new(bus: &'a Mutex>, cs: CS, config: C) -> Self { - Self { bus, cs, config } - } -} - -impl<'a, M: RawMutex, BUS, CS, C> spi::ErrorType for SpiBusDeviceWithConfig<'a, M, BUS, CS, C> -where - BUS: spi::ErrorType, - CS: OutputPin, -{ - type Error = SpiBusDeviceError; -} - -impl SpiDevice for SpiBusDeviceWithConfig<'_, M, BUS, CS, C> -where - M: RawMutex, - BUS: SpiBusFlush + SetConfig, - CS: OutputPin, -{ - type Bus = BUS; - - fn transaction(&mut self, f: impl FnOnce(&mut Self::Bus) -> Result) -> Result { - self.bus.lock(|bus| { - let mut bus = bus.borrow_mut(); - bus.set_config(&self.config); - self.cs.set_low().map_err(SpiBusDeviceError::Cs)?; - - let f_res = f(&mut bus); - - // On failure, it's important to still flush and deassert CS. - let flush_res = bus.flush(); - let cs_res = self.cs.set_high(); - - let f_res = f_res.map_err(SpiBusDeviceError::Spi)?; - flush_res.map_err(SpiBusDeviceError::Spi)?; - cs_res.map_err(SpiBusDeviceError::Cs)?; - Ok(f_res) - }) - } -} - impl<'d, M, BUS, CS, BusErr, CsErr> embedded_hal_02::blocking::spi::Transfer for SpiBusDevice<'_, M, BUS, CS> where M: RawMutex, @@ -164,3 +116,52 @@ where }) } } + +pub struct SpiBusDeviceWithConfig<'a, M: RawMutex, BUS: SetConfig, CS> { + bus: &'a Mutex>, + cs: CS, + config: BUS::Config, +} + +impl<'a, M: RawMutex, BUS: SetConfig, CS> SpiBusDeviceWithConfig<'a, M, BUS, CS> { + pub fn new(bus: &'a Mutex>, cs: CS, config: BUS::Config) -> Self { + Self { bus, cs, config } + } +} + +impl<'a, M, BUS, CS> spi::ErrorType for SpiBusDeviceWithConfig<'a, M, BUS, CS> +where + M: RawMutex, + BUS: spi::ErrorType + SetConfig, + CS: OutputPin, +{ + type Error = SpiBusDeviceError; +} + +impl SpiDevice for SpiBusDeviceWithConfig<'_, M, BUS, CS> +where + M: RawMutex, + BUS: SpiBusFlush + SetConfig, + CS: OutputPin, +{ + type Bus = BUS; + + fn transaction(&mut self, f: impl FnOnce(&mut Self::Bus) -> Result) -> Result { + self.bus.lock(|bus| { + let mut bus = bus.borrow_mut(); + bus.set_config(&self.config); + self.cs.set_low().map_err(SpiBusDeviceError::Cs)?; + + let f_res = f(&mut bus); + + // On failure, it's important to still flush and deassert CS. + let flush_res = bus.flush(); + let cs_res = self.cs.set_high(); + + let f_res = f_res.map_err(SpiBusDeviceError::Spi)?; + flush_res.map_err(SpiBusDeviceError::Spi)?; + cs_res.map_err(SpiBusDeviceError::Cs)?; + Ok(f_res) + }) + } +} diff --git a/embassy-embedded-hal/src/shared_bus/i2c.rs b/embassy-embedded-hal/src/shared_bus/i2c.rs index 0e964773..18f14453 100644 --- a/embassy-embedded-hal/src/shared_bus/i2c.rs +++ b/embassy-embedded-hal/src/shared_bus/i2c.rs @@ -119,28 +119,30 @@ where } } -pub struct I2cBusDeviceWithConfig<'a, M: RawMutex, BUS, C> { +pub struct I2cBusDeviceWithConfig<'a, M: RawMutex, BUS: SetConfig> { bus: &'a Mutex, - config: C, + config: BUS::Config, } -impl<'a, M: RawMutex, BUS, C> I2cBusDeviceWithConfig<'a, M, BUS, C> { - pub fn new(bus: &'a Mutex, config: C) -> Self { +impl<'a, M: RawMutex, BUS: SetConfig> I2cBusDeviceWithConfig<'a, M, BUS> { + pub fn new(bus: &'a Mutex, config: BUS::Config) -> Self { Self { bus, config } } } -impl<'a, M: RawMutex, BUS, C> i2c::ErrorType for I2cBusDeviceWithConfig<'a, M, BUS, C> +impl<'a, M, BUS> i2c::ErrorType for I2cBusDeviceWithConfig<'a, M, BUS> where BUS: i2c::ErrorType, + M: RawMutex, + BUS: SetConfig, { type Error = I2cBusDeviceError; } -impl i2c::I2c for I2cBusDeviceWithConfig<'_, M, BUS, C> +impl i2c::I2c for I2cBusDeviceWithConfig<'_, M, BUS> where M: RawMutex + 'static, - BUS: i2c::I2c + SetConfig + 'static, + BUS: i2c::I2c + SetConfig + 'static, { type ReadFuture<'a> = impl Future> + 'a where Self: 'a; diff --git a/embassy-embedded-hal/src/shared_bus/spi.rs b/embassy-embedded-hal/src/shared_bus/spi.rs index 04378c33..8e3762e6 100644 --- a/embassy-embedded-hal/src/shared_bus/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/spi.rs @@ -112,30 +112,31 @@ where } } -pub struct SpiBusDeviceWithConfig<'a, M: RawMutex, BUS, CS, C> { +pub struct SpiBusDeviceWithConfig<'a, M: RawMutex, BUS: SetConfig, CS> { bus: &'a Mutex, cs: CS, - config: C, + config: BUS::Config, } -impl<'a, M: RawMutex, BUS, CS, C> SpiBusDeviceWithConfig<'a, M, BUS, CS, C> { - pub fn new(bus: &'a Mutex, cs: CS, config: C) -> Self { +impl<'a, M: RawMutex, BUS: SetConfig, CS> SpiBusDeviceWithConfig<'a, M, BUS, CS> { + pub fn new(bus: &'a Mutex, cs: CS, config: BUS::Config) -> Self { Self { bus, cs, config } } } -impl<'a, M: RawMutex, BUS, CS, C> spi::ErrorType for SpiBusDeviceWithConfig<'a, M, BUS, CS, C> +impl<'a, M, BUS, CS> spi::ErrorType for SpiBusDeviceWithConfig<'a, M, BUS, CS> where - BUS: spi::ErrorType, + BUS: spi::ErrorType + SetConfig, CS: OutputPin, + M: RawMutex, { type Error = SpiBusDeviceError; } -impl spi::SpiDevice for SpiBusDeviceWithConfig<'_, M, BUS, CS, C> +impl spi::SpiDevice for SpiBusDeviceWithConfig<'_, M, BUS, CS> where M: RawMutex + 'static, - BUS: spi::SpiBusFlush + SetConfig + 'static, + BUS: spi::SpiBusFlush + SetConfig + 'static, CS: OutputPin, { type Bus = BUS; diff --git a/embassy-nrf/src/spim.rs b/embassy-nrf/src/spim.rs index 882bf4b4..d34d9a0c 100644 --- a/embassy-nrf/src/spim.rs +++ b/embassy-nrf/src/spim.rs @@ -523,8 +523,9 @@ cfg_if::cfg_if! { } } -impl<'d, T: Instance> SetConfig for Spim<'d, T> { - fn set_config(&mut self, config: &Config) { +impl<'d, T: Instance> SetConfig for Spim<'d, T> { + type Config = Config; + fn set_config(&mut self, config: &Self::Config) { let r = T::regs(); // Configure mode. let mode = config.mode; diff --git a/embassy-nrf/src/twim.rs b/embassy-nrf/src/twim.rs index 8189d3e7..2088691b 100644 --- a/embassy-nrf/src/twim.rs +++ b/embassy-nrf/src/twim.rs @@ -880,8 +880,9 @@ cfg_if::cfg_if! { } } -impl<'d, T: Instance> SetConfig for Twim<'d, T> { - fn set_config(&mut self, config: &Config) { +impl<'d, T: Instance> SetConfig for Twim<'d, T> { + type Config = Config; + fn set_config(&mut self, config: &Self::Config) { let r = T::regs(); r.frequency .write(|w| unsafe { w.frequency().bits(config.frequency as u32) }); diff --git a/examples/nrf/src/bin/shared_bus.rs b/examples/nrf/src/bin/shared_bus.rs deleted file mode 100644 index 23d16f79..00000000 --- a/examples/nrf/src/bin/shared_bus.rs +++ /dev/null @@ -1,21 +0,0 @@ -#![no_std] -#![no_main] -#![feature(type_alias_impl_trait)] - -use embassy::executor::Spawner; -use embassy::time::{Duration, Timer}; -use embassy_nrf::gpio::{Level, Output, OutputDrive}; -use embassy_nrf::Peripherals; -use {defmt_rtt as _, panic_probe as _}; - -#[embassy::main] -async fn main(_spawner: Spawner, p: Peripherals) { - let mut led = Output::new(p.P0_13, Level::Low, OutputDrive::Standard); - - loop { - led.set_high(); - Timer::after(Duration::from_millis(300)).await; - led.set_low(); - Timer::after(Duration::from_millis(300)).await; - } -} From 85e67d94ad1a2ce35afcb64b4f9a527b863e8a50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Als=C3=A9r?= Date: Sat, 9 Jul 2022 00:32:55 +0200 Subject: [PATCH 04/11] impl SetConfig for rp2040 SPI --- embassy-rp/Cargo.toml | 1 + embassy-rp/src/spi.rs | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/embassy-rp/Cargo.toml b/embassy-rp/Cargo.toml index 217221b0..0a278ab0 100644 --- a/embassy-rp/Cargo.toml +++ b/embassy-rp/Cargo.toml @@ -30,6 +30,7 @@ unstable-traits = ["embedded-hal-1"] embassy = { version = "0.1.0", path = "../embassy", features = [ "time-tick-1mhz", "nightly"] } embassy-cortex-m = { version = "0.1.0", path = "../embassy-cortex-m", features = ["prio-bits-3"]} embassy-hal-common = {version = "0.1.0", path = "../embassy-hal-common" } +embassy-embedded-hal = {version = "0.1.0", path = "../embassy-embedded-hal" } embassy-macros = { version = "0.1.0", path = "../embassy-macros", features = ["rp"]} atomic-polyfill = "0.1.5" defmt = { version = "0.3", optional = true } diff --git a/embassy-rp/src/spi.rs b/embassy-rp/src/spi.rs index e988e41d..6b3f2238 100644 --- a/embassy-rp/src/spi.rs +++ b/embassy-rp/src/spi.rs @@ -1,5 +1,6 @@ use core::marker::PhantomData; +use embassy_embedded_hal::SetConfig; use embassy_hal_common::unborrow; pub use embedded_hal_02::spi::{Phase, Polarity}; @@ -350,3 +351,20 @@ mod eh1 { } } } + +impl<'d, T: Instance> SetConfig for Spi<'d, T> { + type Config = Config; + fn set_config(&mut self, config: &Self::Config) { + let p = self.inner.regs(); + let (presc, postdiv) = calc_prescs(config.frequency); + unsafe { + p.cpsr().write(|w| w.set_cpsdvsr(presc)); + p.cr0().write(|w| { + w.set_dss(0b0111); // 8bit + w.set_spo(config.polarity == Polarity::IdleHigh); + w.set_sph(config.phase == Phase::CaptureOnSecondTransition); + w.set_scr(postdiv); + }); + } + } +} From 880b71a1e8246011109d64794ec98477722bde0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Als=C3=A9r?= Date: Sat, 9 Jul 2022 02:28:05 +0200 Subject: [PATCH 05/11] impl SetConfig for stm32 i2c and SPI --- embassy-stm32/Cargo.toml | 1 + embassy-stm32/src/i2c/v1.rs | 21 +++++++++++++++++++++ embassy-stm32/src/i2c/v2.rs | 17 +++++++++++++++++ embassy-stm32/src/spi/mod.rs | 8 ++++++++ 4 files changed, 47 insertions(+) diff --git a/embassy-stm32/Cargo.toml b/embassy-stm32/Cargo.toml index a21384be..547aad02 100644 --- a/embassy-stm32/Cargo.toml +++ b/embassy-stm32/Cargo.toml @@ -35,6 +35,7 @@ embassy = { version = "0.1.0", path = "../embassy" } embassy-cortex-m = { version = "0.1.0", path = "../embassy-cortex-m", features = ["prio-bits-4"]} embassy-macros = { version = "0.1.0", path = "../embassy-macros", features = ["stm32"] } embassy-hal-common = {version = "0.1.0", path = "../embassy-hal-common" } +embassy-embedded-hal = {version = "0.1.0", path = "../embassy-embedded-hal" } embassy-net = { version = "0.1.0", path = "../embassy-net", optional = true } embassy-usb = {version = "0.1.0", path = "../embassy-usb", optional = true } diff --git a/embassy-stm32/src/i2c/v1.rs b/embassy-stm32/src/i2c/v1.rs index 87a4f969..4bff90af 100644 --- a/embassy-stm32/src/i2c/v1.rs +++ b/embassy-stm32/src/i2c/v1.rs @@ -1,5 +1,6 @@ use core::marker::PhantomData; +use embassy_embedded_hal::SetConfig; use embassy_hal_common::unborrow; use crate::gpio::sealed::AFType; @@ -381,3 +382,23 @@ impl Timings { } } } + +impl<'d, T: Instance> SetConfig for I2c<'d, T> { + type Config = Hertz; + fn set_config(&mut self, config: &Self::Config) { + let timings = Timings::new(T::frequency(), *config); + unsafe { + T::regs().cr2().modify(|reg| { + reg.set_freq(timings.freq); + }); + T::regs().ccr().modify(|reg| { + reg.set_f_s(timings.mode.f_s()); + reg.set_duty(timings.duty.duty()); + reg.set_ccr(timings.ccr); + }); + T::regs().trise().modify(|reg| { + reg.set_trise(timings.trise); + }); + } + } +} diff --git a/embassy-stm32/src/i2c/v2.rs b/embassy-stm32/src/i2c/v2.rs index 1a085e78..8778da91 100644 --- a/embassy-stm32/src/i2c/v2.rs +++ b/embassy-stm32/src/i2c/v2.rs @@ -4,6 +4,7 @@ use core::task::Poll; use atomic_polyfill::{AtomicUsize, Ordering}; use embassy::waitqueue::AtomicWaker; +use embassy_embedded_hal::SetConfig; use embassy_hal_common::drop::OnDrop; use embassy_hal_common::unborrow; use futures::future::poll_fn; @@ -899,3 +900,19 @@ cfg_if::cfg_if! { } } } + +impl<'d, T: Instance> SetConfig for I2c<'d, T> { + type Config = Hertz; + fn set_config(&mut self, config: &Self::Config) { + let timings = Timings::new(T::frequency(), *config); + unsafe { + T::regs().timingr().write(|reg| { + reg.set_presc(timings.prescale); + reg.set_scll(timings.scll); + reg.set_sclh(timings.sclh); + reg.set_sdadel(timings.sdadel); + reg.set_scldel(timings.scldel); + }); + } + } +} diff --git a/embassy-stm32/src/spi/mod.rs b/embassy-stm32/src/spi/mod.rs index 7c142e50..a839bb41 100644 --- a/embassy-stm32/src/spi/mod.rs +++ b/embassy-stm32/src/spi/mod.rs @@ -3,6 +3,7 @@ use core::marker::PhantomData; use core::ptr; +use embassy_embedded_hal::SetConfig; use embassy_hal_common::unborrow; pub use embedded_hal_02::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3}; use futures::future::join; @@ -1022,3 +1023,10 @@ foreach_peripheral!( impl Instance for peripherals::$inst {} }; ); + +impl<'d, T: Instance, Tx, Rx> SetConfig for Spi<'d, T, Tx, Rx> { + type Config = Config; + fn set_config(&mut self, config: &Self::Config) { + self.reconfigure(*config); + } +} From baae64d911d6f2bd2560b64b3856471b390f41cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Als=C3=A9r?= Date: Sat, 9 Jul 2022 23:41:02 +0200 Subject: [PATCH 06/11] Add embassy-embedded-hal nightly feature --- embassy-embedded-hal/Cargo.toml | 2 ++ embassy-embedded-hal/src/shared_bus/i2c.rs | 3 +++ embassy-embedded-hal/src/shared_bus/spi.rs | 3 +++ embassy-nrf/Cargo.toml | 2 +- embassy-rp/Cargo.toml | 2 +- embassy-stm32/Cargo.toml | 2 +- 6 files changed, 11 insertions(+), 3 deletions(-) diff --git a/embassy-embedded-hal/Cargo.toml b/embassy-embedded-hal/Cargo.toml index d1b9f3ea..15d3beb2 100644 --- a/embassy-embedded-hal/Cargo.toml +++ b/embassy-embedded-hal/Cargo.toml @@ -5,6 +5,8 @@ edition = "2021" [features] std = [] +# Enable nightly-only features +nightly = ["embedded-hal-async"] [dependencies] embassy = { version = "0.1.0", path = "../embassy" } diff --git a/embassy-embedded-hal/src/shared_bus/i2c.rs b/embassy-embedded-hal/src/shared_bus/i2c.rs index 18f14453..f63190e6 100644 --- a/embassy-embedded-hal/src/shared_bus/i2c.rs +++ b/embassy-embedded-hal/src/shared_bus/i2c.rs @@ -27,6 +27,7 @@ use core::future::Future; use embassy::blocking_mutex::raw::RawMutex; use embassy::mutex::Mutex; +#[cfg(feature = "nightly")] use embedded_hal_async::i2c; use crate::SetConfig; @@ -64,6 +65,7 @@ where type Error = I2cBusDeviceError; } +#[cfg(feature = "nightly")] impl i2c::I2c for I2cBusDevice<'_, M, BUS> where M: RawMutex + 'static, @@ -139,6 +141,7 @@ where type Error = I2cBusDeviceError; } +#[cfg(feature = "nightly")] impl i2c::I2c for I2cBusDeviceWithConfig<'_, M, BUS> where M: RawMutex + 'static, diff --git a/embassy-embedded-hal/src/shared_bus/spi.rs b/embassy-embedded-hal/src/shared_bus/spi.rs index 8e3762e6..136352e0 100644 --- a/embassy-embedded-hal/src/shared_bus/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/spi.rs @@ -32,6 +32,7 @@ use embassy::blocking_mutex::raw::RawMutex; use embassy::mutex::Mutex; use embedded_hal_1::digital::blocking::OutputPin; use embedded_hal_1::spi::ErrorType; +#[cfg(feature = "nightly")] use embedded_hal_async::spi; use crate::SetConfig; @@ -74,6 +75,7 @@ where type Error = SpiBusDeviceError; } +#[cfg(feature = "nightly")] impl spi::SpiDevice for SpiBusDevice<'_, M, BUS, CS> where M: RawMutex + 'static, @@ -133,6 +135,7 @@ where type Error = SpiBusDeviceError; } +#[cfg(feature = "nightly")] impl spi::SpiDevice for SpiBusDeviceWithConfig<'_, M, BUS, CS> where M: RawMutex + 'static, diff --git a/embassy-nrf/Cargo.toml b/embassy-nrf/Cargo.toml index 3f9dabbb..3569a70c 100644 --- a/embassy-nrf/Cargo.toml +++ b/embassy-nrf/Cargo.toml @@ -21,7 +21,7 @@ time = ["embassy/time"] defmt = ["dep:defmt", "embassy/defmt", "embassy-usb?/defmt", "embedded-io?/defmt"] # Enable nightly-only features -nightly = ["embassy/nightly", "embedded-hal-1", "embedded-hal-async", "embassy-usb", "embedded-storage-async", "dep:embedded-io"] +nightly = ["embassy/nightly", "embedded-hal-1", "embedded-hal-async", "embassy-usb", "embedded-storage-async", "dep:embedded-io", "embassy-embedded-hal/nightly"] # Reexport the PAC for the currently enabled chip at `embassy_nrf::pac`. # This is unstable because semver-minor (non-breaking) releases of embassy-nrf may major-bump (breaking) the PAC version. diff --git a/embassy-rp/Cargo.toml b/embassy-rp/Cargo.toml index 0a278ab0..95ae76cd 100644 --- a/embassy-rp/Cargo.toml +++ b/embassy-rp/Cargo.toml @@ -20,7 +20,7 @@ flavors = [ unstable-pac = [] # Enable nightly-only features -nightly = ["embassy/nightly", "embedded-hal-1", "embedded-hal-async"] +nightly = ["embassy/nightly", "embedded-hal-1", "embedded-hal-async", "embassy-embedded-hal/nightly"] # Implement embedded-hal 1.0 alpha traits. # Implement embedded-hal-async traits if `nightly` is set as well. diff --git a/embassy-stm32/Cargo.toml b/embassy-stm32/Cargo.toml index 547aad02..a5c444fb 100644 --- a/embassy-stm32/Cargo.toml +++ b/embassy-stm32/Cargo.toml @@ -91,7 +91,7 @@ time-driver-tim12 = ["_time-driver"] time-driver-tim15 = ["_time-driver"] # Enable nightly-only features -nightly = ["embassy/nightly", "embedded-hal-1", "embedded-hal-async", "embedded-storage-async", "dep:embedded-io", "dep:embassy-usb"] +nightly = ["embassy/nightly", "embedded-hal-1", "embedded-hal-async", "embedded-storage-async", "dep:embedded-io", "dep:embassy-usb", "embassy-embedded-hal/nightly"] # Reexport stm32-metapac at `embassy_stm32::pac`. # This is unstable because semver-minor (non-breaking) releases of embassy-stm32 may major-bump (breaking) the stm32-metapac version. From 20f56b856feb3cefde300b76ceab85b6bd29c5a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Als=C3=A9r?= Date: Sat, 9 Jul 2022 23:44:08 +0200 Subject: [PATCH 07/11] Add embassy-embedded-hal nightly feature --- embassy-embedded-hal/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/embassy-embedded-hal/Cargo.toml b/embassy-embedded-hal/Cargo.toml index 15d3beb2..94034a66 100644 --- a/embassy-embedded-hal/Cargo.toml +++ b/embassy-embedded-hal/Cargo.toml @@ -12,7 +12,7 @@ nightly = ["embedded-hal-async"] embassy = { version = "0.1.0", path = "../embassy" } embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = ["unproven"] } embedded-hal-1 = { package = "embedded-hal", version = "1.0.0-alpha.8" } -embedded-hal-async = { version = "0.1.0-alpha.1" } +embedded-hal-async = { version = "0.1.0-alpha.1", optional = true } embedded-storage = "0.3.0" embedded-storage-async = "0.3.0" nb = "1.0.0" \ No newline at end of file From ef24faf2df594d0eca1542ac02834af6aafa3853 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Als=C3=A9r?= Date: Sun, 10 Jul 2022 00:05:57 +0200 Subject: [PATCH 08/11] Add asynch mod to shared_bus --- embassy-embedded-hal/src/lib.rs | 2 ++ .../src/shared_bus/{ => asynch}/i2c.rs | 25 +++++-------- .../src/shared_bus/asynch/mod.rs | 3 ++ .../src/shared_bus/{ => asynch}/spi.rs | 36 ++++++++----------- .../src/shared_bus/blocking/i2c.rs | 2 +- .../src/shared_bus/blocking/spi.rs | 2 +- embassy-embedded-hal/src/shared_bus/mod.rs | 18 +++++++--- 7 files changed, 44 insertions(+), 44 deletions(-) rename embassy-embedded-hal/src/shared_bus/{ => asynch}/i2c.rs (97%) create mode 100644 embassy-embedded-hal/src/shared_bus/asynch/mod.rs rename embassy-embedded-hal/src/shared_bus/{ => asynch}/spi.rs (96%) diff --git a/embassy-embedded-hal/src/lib.rs b/embassy-embedded-hal/src/lib.rs index 688d0b48..f57d83b5 100644 --- a/embassy-embedded-hal/src/lib.rs +++ b/embassy-embedded-hal/src/lib.rs @@ -2,7 +2,9 @@ #![feature(generic_associated_types)] #![feature(type_alias_impl_trait)] +#[cfg(feature = "nightly")] pub mod adapter; + pub mod shared_bus; pub trait SetConfig { diff --git a/embassy-embedded-hal/src/shared_bus/i2c.rs b/embassy-embedded-hal/src/shared_bus/asynch/i2c.rs similarity index 97% rename from embassy-embedded-hal/src/shared_bus/i2c.rs rename to embassy-embedded-hal/src/shared_bus/asynch/i2c.rs index f63190e6..40831749 100644 --- a/embassy-embedded-hal/src/shared_bus/i2c.rs +++ b/embassy-embedded-hal/src/shared_bus/asynch/i2c.rs @@ -27,14 +27,19 @@ use core::future::Future; use embassy::blocking_mutex::raw::RawMutex; use embassy::mutex::Mutex; -#[cfg(feature = "nightly")] use embedded_hal_async::i2c; +use crate::shared_bus::I2cBusDeviceError; use crate::SetConfig; -#[derive(Copy, Clone, Eq, PartialEq, Debug)] -pub enum I2cBusDeviceError { - I2c(BUS), +pub struct I2cBusDevice<'a, M: RawMutex, BUS> { + bus: &'a Mutex, +} + +impl<'a, M: RawMutex, BUS> I2cBusDevice<'a, M, BUS> { + pub fn new(bus: &'a Mutex) -> Self { + Self { bus } + } } impl i2c::Error for I2cBusDeviceError @@ -48,16 +53,6 @@ where } } -pub struct I2cBusDevice<'a, M: RawMutex, BUS> { - bus: &'a Mutex, -} - -impl<'a, M: RawMutex, BUS> I2cBusDevice<'a, M, BUS> { - pub fn new(bus: &'a Mutex) -> Self { - Self { bus } - } -} - impl<'a, M: RawMutex, BUS> i2c::ErrorType for I2cBusDevice<'a, M, BUS> where BUS: i2c::ErrorType, @@ -65,7 +60,6 @@ where type Error = I2cBusDeviceError; } -#[cfg(feature = "nightly")] impl i2c::I2c for I2cBusDevice<'_, M, BUS> where M: RawMutex + 'static, @@ -141,7 +135,6 @@ where type Error = I2cBusDeviceError; } -#[cfg(feature = "nightly")] impl i2c::I2c for I2cBusDeviceWithConfig<'_, M, BUS> where M: RawMutex + 'static, diff --git a/embassy-embedded-hal/src/shared_bus/asynch/mod.rs b/embassy-embedded-hal/src/shared_bus/asynch/mod.rs new file mode 100644 index 00000000..2e660b72 --- /dev/null +++ b/embassy-embedded-hal/src/shared_bus/asynch/mod.rs @@ -0,0 +1,3 @@ +//! Asynchronous shared bus implementations for embedded-hal-async +pub mod i2c; +pub mod spi; diff --git a/embassy-embedded-hal/src/shared_bus/spi.rs b/embassy-embedded-hal/src/shared_bus/asynch/spi.rs similarity index 96% rename from embassy-embedded-hal/src/shared_bus/spi.rs rename to embassy-embedded-hal/src/shared_bus/asynch/spi.rs index 136352e0..f3795bb1 100644 --- a/embassy-embedded-hal/src/shared_bus/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/asynch/spi.rs @@ -32,30 +32,11 @@ use embassy::blocking_mutex::raw::RawMutex; use embassy::mutex::Mutex; use embedded_hal_1::digital::blocking::OutputPin; use embedded_hal_1::spi::ErrorType; -#[cfg(feature = "nightly")] use embedded_hal_async::spi; +use crate::shared_bus::SpiBusDeviceError; use crate::SetConfig; -#[derive(Copy, Clone, Eq, PartialEq, Debug)] -pub enum SpiBusDeviceError { - Spi(BUS), - Cs(CS), -} - -impl spi::Error for SpiBusDeviceError -where - BUS: spi::Error + Debug, - CS: Debug, -{ - fn kind(&self) -> spi::ErrorKind { - match self { - Self::Spi(e) => e.kind(), - Self::Cs(_) => spi::ErrorKind::Other, - } - } -} - pub struct SpiBusDevice<'a, M: RawMutex, BUS, CS> { bus: &'a Mutex, cs: CS, @@ -75,7 +56,19 @@ where type Error = SpiBusDeviceError; } -#[cfg(feature = "nightly")] +impl spi::Error for SpiBusDeviceError +where + BUS: spi::Error + Debug, + CS: Debug, +{ + fn kind(&self) -> spi::ErrorKind { + match self { + Self::Spi(e) => e.kind(), + Self::Cs(_) => spi::ErrorKind::Other, + } + } +} + impl spi::SpiDevice for SpiBusDevice<'_, M, BUS, CS> where M: RawMutex + 'static, @@ -135,7 +128,6 @@ where type Error = SpiBusDeviceError; } -#[cfg(feature = "nightly")] impl spi::SpiDevice for SpiBusDeviceWithConfig<'_, M, BUS, CS> where M: RawMutex + 'static, diff --git a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs index 0c8338c7..ac361a78 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs @@ -23,7 +23,7 @@ use embassy::blocking_mutex::Mutex; use embedded_hal_1::i2c::blocking::{I2c, Operation}; use embedded_hal_1::i2c::ErrorType; -use crate::shared_bus::i2c::I2cBusDeviceError; +use crate::shared_bus::I2cBusDeviceError; use crate::SetConfig; pub struct I2cBusDevice<'a, M: RawMutex, BUS> { diff --git a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs index 456da885..704858cd 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs @@ -26,7 +26,7 @@ use embedded_hal_1::digital::blocking::OutputPin; use embedded_hal_1::spi; use embedded_hal_1::spi::blocking::{SpiBusFlush, SpiDevice}; -use crate::shared_bus::spi::SpiBusDeviceError; +use crate::shared_bus::SpiBusDeviceError; use crate::SetConfig; pub struct SpiBusDevice<'a, M: RawMutex, BUS, CS> { diff --git a/embassy-embedded-hal/src/shared_bus/mod.rs b/embassy-embedded-hal/src/shared_bus/mod.rs index cd748cac..2309a6c3 100644 --- a/embassy-embedded-hal/src/shared_bus/mod.rs +++ b/embassy-embedded-hal/src/shared_bus/mod.rs @@ -1,6 +1,16 @@ //! Shared bus implementations +#[cfg(feature = "nightly")] +pub mod asynch; + pub mod blocking; -/// Shared i2c bus implementation for embedded-hal-async -pub mod i2c; -/// Shared SPI bus implementation for embedded-hal-async -pub mod spi; + +#[derive(Copy, Clone, Eq, PartialEq, Debug)] +pub enum I2cBusDeviceError { + I2c(BUS), +} + +#[derive(Copy, Clone, Eq, PartialEq, Debug)] +pub enum SpiBusDeviceError { + Spi(BUS), + Cs(CS), +} From ce7bc32755166670d814de7ebef32a0747b2779f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Als=C3=A9r?= Date: Sun, 10 Jul 2022 00:15:20 +0200 Subject: [PATCH 09/11] Nightly feature gate for embedded-storage-async --- embassy-embedded-hal/Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/embassy-embedded-hal/Cargo.toml b/embassy-embedded-hal/Cargo.toml index 94034a66..d2606d26 100644 --- a/embassy-embedded-hal/Cargo.toml +++ b/embassy-embedded-hal/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" [features] std = [] # Enable nightly-only features -nightly = ["embedded-hal-async"] +nightly = ["embedded-hal-async", "embedded-storage-async"] [dependencies] embassy = { version = "0.1.0", path = "../embassy" } @@ -14,5 +14,5 @@ embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = ["un embedded-hal-1 = { package = "embedded-hal", version = "1.0.0-alpha.8" } embedded-hal-async = { version = "0.1.0-alpha.1", optional = true } embedded-storage = "0.3.0" -embedded-storage-async = "0.3.0" +embedded-storage-async = { version = "0.3.0", optional = true } nb = "1.0.0" \ No newline at end of file From c9ceec879750287caeda406aa475d3b179119302 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Als=C3=A9r?= Date: Sun, 10 Jul 2022 00:49:46 +0200 Subject: [PATCH 10/11] Cleanup --- .../src/shared_bus/asynch/i2c.rs | 12 -------- .../src/shared_bus/asynch/spi.rs | 14 ---------- embassy-embedded-hal/src/shared_bus/mod.rs | 28 +++++++++++++++++++ 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/embassy-embedded-hal/src/shared_bus/asynch/i2c.rs b/embassy-embedded-hal/src/shared_bus/asynch/i2c.rs index 40831749..13611792 100644 --- a/embassy-embedded-hal/src/shared_bus/asynch/i2c.rs +++ b/embassy-embedded-hal/src/shared_bus/asynch/i2c.rs @@ -22,7 +22,6 @@ //! let i2c_dev2 = I2cBusDevice::new(i2c_bus); //! let mpu = Mpu6050::new(i2c_dev2); //! ``` -use core::fmt::Debug; use core::future::Future; use embassy::blocking_mutex::raw::RawMutex; @@ -42,17 +41,6 @@ impl<'a, M: RawMutex, BUS> I2cBusDevice<'a, M, BUS> { } } -impl i2c::Error for I2cBusDeviceError -where - BUS: i2c::Error + Debug, -{ - fn kind(&self) -> i2c::ErrorKind { - match self { - Self::I2c(e) => e.kind(), - } - } -} - impl<'a, M: RawMutex, BUS> i2c::ErrorType for I2cBusDevice<'a, M, BUS> where BUS: i2c::ErrorType, diff --git a/embassy-embedded-hal/src/shared_bus/asynch/spi.rs b/embassy-embedded-hal/src/shared_bus/asynch/spi.rs index f3795bb1..8034c90b 100644 --- a/embassy-embedded-hal/src/shared_bus/asynch/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/asynch/spi.rs @@ -25,7 +25,6 @@ //! let spi_dev2 = SpiBusDevice::new(spi_bus, cs_pin2); //! let display2 = ST7735::new(spi_dev2, dc2, rst2, Default::default(), 160, 128); //! ``` -use core::fmt::Debug; use core::future::Future; use embassy::blocking_mutex::raw::RawMutex; @@ -56,19 +55,6 @@ where type Error = SpiBusDeviceError; } -impl spi::Error for SpiBusDeviceError -where - BUS: spi::Error + Debug, - CS: Debug, -{ - fn kind(&self) -> spi::ErrorKind { - match self { - Self::Spi(e) => e.kind(), - Self::Cs(_) => spi::ErrorKind::Other, - } - } -} - impl spi::SpiDevice for SpiBusDevice<'_, M, BUS, CS> where M: RawMutex + 'static, diff --git a/embassy-embedded-hal/src/shared_bus/mod.rs b/embassy-embedded-hal/src/shared_bus/mod.rs index 2309a6c3..61200443 100644 --- a/embassy-embedded-hal/src/shared_bus/mod.rs +++ b/embassy-embedded-hal/src/shared_bus/mod.rs @@ -1,4 +1,8 @@ //! Shared bus implementations +use core::fmt::Debug; + +use embedded_hal_1::{i2c, spi}; + #[cfg(feature = "nightly")] pub mod asynch; @@ -9,8 +13,32 @@ pub enum I2cBusDeviceError { I2c(BUS), } +impl i2c::Error for I2cBusDeviceError +where + BUS: i2c::Error + Debug, +{ + fn kind(&self) -> i2c::ErrorKind { + match self { + Self::I2c(e) => e.kind(), + } + } +} + #[derive(Copy, Clone, Eq, PartialEq, Debug)] pub enum SpiBusDeviceError { Spi(BUS), Cs(CS), } + +impl spi::Error for SpiBusDeviceError +where + BUS: spi::Error + Debug, + CS: Debug, +{ + fn kind(&self) -> spi::ErrorKind { + match self { + Self::Spi(e) => e.kind(), + Self::Cs(_) => spi::ErrorKind::Other, + } + } +} From c9b58561538b5fb3e5702744c9f5653d039f8c98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Als=C3=A9r?= Date: Sun, 10 Jul 2022 00:58:05 +0200 Subject: [PATCH 11/11] Features only on nightly --- embassy-embedded-hal/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/embassy-embedded-hal/src/lib.rs b/embassy-embedded-hal/src/lib.rs index f57d83b5..d77c2d63 100644 --- a/embassy-embedded-hal/src/lib.rs +++ b/embassy-embedded-hal/src/lib.rs @@ -1,6 +1,5 @@ #![cfg_attr(not(feature = "std"), no_std)] -#![feature(generic_associated_types)] -#![feature(type_alias_impl_trait)] +#![cfg_attr(feature = "nightly", feature(generic_associated_types, type_alias_impl_trait))] #[cfg(feature = "nightly")] pub mod adapter;