diff --git a/embassy-embedded-hal/Cargo.toml b/embassy-embedded-hal/Cargo.toml index d1b9f3ea..d2606d26 100644 --- a/embassy-embedded-hal/Cargo.toml +++ b/embassy-embedded-hal/Cargo.toml @@ -5,12 +5,14 @@ edition = "2021" [features] std = [] +# Enable nightly-only features +nightly = ["embedded-hal-async", "embedded-storage-async"] [dependencies] 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" +embedded-storage-async = { version = "0.3.0", optional = true } nb = "1.0.0" \ No newline at end of file diff --git a/embassy-embedded-hal/src/lib.rs b/embassy-embedded-hal/src/lib.rs index 27ffa742..d77c2d63 100644 --- a/embassy-embedded-hal/src/lib.rs +++ b/embassy-embedded-hal/src/lib.rs @@ -1,6 +1,12 @@ #![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; + pub mod shared_bus; + +pub trait SetConfig { + type Config; + fn set_config(&mut self, config: &Self::Config); +} diff --git a/embassy-embedded-hal/src/shared_bus/i2c.rs b/embassy-embedded-hal/src/shared_bus/asynch/i2c.rs similarity index 57% rename from embassy-embedded-hal/src/shared_bus/i2c.rs rename to embassy-embedded-hal/src/shared_bus/asynch/i2c.rs index e8131288..13611792 100644 --- a/embassy-embedded-hal/src/shared_bus/i2c.rs +++ b/embassy-embedded-hal/src/shared_bus/asynch/i2c.rs @@ -22,28 +22,14 @@ //! 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; use embassy::mutex::Mutex; use embedded_hal_async::i2c; -#[derive(Copy, Clone, Eq, PartialEq, Debug)] -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(), - } - } -} +use crate::shared_bus::I2cBusDeviceError; +use crate::SetConfig; pub struct I2cBusDevice<'a, M: RawMutex, BUS> { bus: &'a Mutex, @@ -116,3 +102,81 @@ where async move { todo!() } } } + +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, 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> +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/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 61% rename from embassy-embedded-hal/src/shared_bus/spi.rs rename to embassy-embedded-hal/src/shared_bus/asynch/spi.rs index fd4b6d56..8034c90b 100644 --- a/embassy-embedded-hal/src/shared_bus/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; @@ -34,24 +33,8 @@ use embedded_hal_1::digital::blocking::OutputPin; use embedded_hal_1::spi::ErrorType; use embedded_hal_async::spi; -#[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, - } - } -} +use crate::shared_bus::SpiBusDeviceError; +use crate::SetConfig; pub struct SpiBusDevice<'a, M: RawMutex, BUS, CS> { bus: &'a Mutex, @@ -109,3 +92,63 @@ 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 + BUS: spi::ErrorType + SetConfig, + CS: OutputPin, + M: RawMutex, +{ + type Error = SpiBusDeviceError; +} + +impl spi::SpiDevice for SpiBusDeviceWithConfig<'_, M, BUS, CS> +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-embedded-hal/src/shared_bus/blocking/i2c.rs b/embassy-embedded-hal/src/shared_bus/blocking/i2c.rs index bfbcb6c2..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,8 @@ 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> { bus: &'a Mutex>, @@ -141,3 +142,87 @@ where }) } } + +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, BUS> ErrorType for I2cBusDeviceWithConfig<'a, M, BUS> +where + M: RawMutex, + BUS: ErrorType + SetConfig, +{ + type Error = I2cBusDeviceError; +} + +impl I2c for I2cBusDeviceWithConfig<'_, M, BUS> +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 81cf9745..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,8 @@ 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> { bus: &'a Mutex>, @@ -115,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/mod.rs b/embassy-embedded-hal/src/shared_bus/mod.rs index cd748cac..61200443 100644 --- a/embassy-embedded-hal/src/shared_bus/mod.rs +++ b/embassy-embedded-hal/src/shared_bus/mod.rs @@ -1,6 +1,44 @@ //! Shared bus implementations +use core::fmt::Debug; + +use embedded_hal_1::{i2c, spi}; + +#[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), +} + +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, + } + } +} diff --git a/embassy-nrf/Cargo.toml b/embassy-nrf/Cargo.toml index 887ea1bd..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. @@ -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..d34d9a0c 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,46 @@ cfg_if::cfg_if! { } } } + +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; + 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..2088691b 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,12 @@ cfg_if::cfg_if! { } } } + +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/embassy-rp/Cargo.toml b/embassy-rp/Cargo.toml index 217221b0..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. @@ -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); + }); + } + } +} diff --git a/embassy-stm32/Cargo.toml b/embassy-stm32/Cargo.toml index a21384be..a5c444fb 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 } @@ -90,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. diff --git a/embassy-stm32/src/i2c/v1.rs b/embassy-stm32/src/i2c/v1.rs index f56e50f9..c328224f 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; @@ -449,3 +450,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 08b43beb..672f0939 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; @@ -941,3 +942,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); + } +}