From d54eb1107ee45c5030449a0de0c259da7236ca05 Mon Sep 17 00:00:00 2001 From: Rasmus Melchior Jacobsen Date: Mon, 22 May 2023 15:57:20 +0200 Subject: [PATCH 1/8] Yield between BlockingAsync NorFlash write and erase operations --- embassy-embedded-hal/Cargo.toml | 8 ++- embassy-embedded-hal/src/adapter.rs | 79 ++++++++++++++++++++++++++++- 2 files changed, 84 insertions(+), 3 deletions(-) diff --git a/embassy-embedded-hal/Cargo.toml b/embassy-embedded-hal/Cargo.toml index 19d512585..81cece686 100644 --- a/embassy-embedded-hal/Cargo.toml +++ b/embassy-embedded-hal/Cargo.toml @@ -17,8 +17,11 @@ std = [] nightly = ["embedded-hal-async", "embedded-storage-async"] [dependencies] +embassy-futures = { version = "0.1.0", path = "../embassy-futures" } embassy-sync = { version = "0.2.0", path = "../embassy-sync" } -embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = ["unproven"] } +embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = [ + "unproven", +] } embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.10" } embedded-hal-async = { version = "=0.2.0-alpha.1", optional = true } embedded-storage = "0.3.0" @@ -26,3 +29,6 @@ embedded-storage-async = { version = "0.4.0", optional = true } nb = "1.0.0" defmt = { version = "0.3", optional = true } + +[dev-dependencies] +futures-test = "0.3.17" diff --git a/embassy-embedded-hal/src/adapter.rs b/embassy-embedded-hal/src/adapter.rs index 171ff6c9f..169aad5e3 100644 --- a/embassy-embedded-hal/src/adapter.rs +++ b/embassy-embedded-hal/src/adapter.rs @@ -1,5 +1,6 @@ //! Adapters between embedded-hal traits. +use embassy_futures::yield_now; use embedded_hal_02::{blocking, serial}; /// Wrapper that implements async traits using blocking implementations. @@ -150,11 +151,18 @@ where const ERASE_SIZE: usize = ::ERASE_SIZE; async fn write(&mut self, offset: u32, data: &[u8]) -> Result<(), Self::Error> { - self.wrapped.write(offset, data) + self.wrapped.write(offset, data)?; + yield_now().await; + Ok(()) } async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { - self.wrapped.erase(from, to) + for from in (from..to).step_by(T::ERASE_SIZE) { + let to = core::cmp::min(from + T::ERASE_SIZE as u32, to); + self.wrapped.erase(from, to)?; + yield_now().await; + } + Ok(()) } } @@ -171,3 +179,70 @@ where self.wrapped.capacity() } } + +#[cfg(test)] +mod tests { + use super::*; + + extern crate std; + + #[derive(Default)] + struct FakeFlash(Vec<(u32, u32)>); + + impl embedded_storage::nor_flash::ErrorType for FakeFlash { + type Error = std::convert::Infallible; + } + + impl embedded_storage::nor_flash::ReadNorFlash for FakeFlash { + const READ_SIZE: usize = 1; + + fn read(&mut self, _offset: u32, _bytes: &mut [u8]) -> Result<(), Self::Error> { + unimplemented!() + } + + fn capacity(&self) -> usize { + unimplemented!() + } + } + + impl embedded_storage::nor_flash::NorFlash for FakeFlash { + const WRITE_SIZE: usize = 4; + const ERASE_SIZE: usize = 128; + + fn write(&mut self, _offset: u32, _bytes: &[u8]) -> Result<(), Self::Error> { + unimplemented!() + } + + fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { + self.0.push((from, to)); + Ok(()) + } + } + + #[futures_test::test] + async fn can_erase() { + let fake = FakeFlash::default(); + let mut yielding = BlockingAsync::new(fake); + + yielding.erase(0, 256).await.unwrap(); + + let fake = yielding.wrapped; + assert_eq!(2, fake.0.len()); + assert_eq!((0, 128), fake.0[0]); + assert_eq!((128, 256), fake.0[1]); + } + + #[futures_test::test] + async fn can_erase_wrong_erase_size() { + let fake = FakeFlash::default(); + let mut yielding = BlockingAsync::new(fake); + + yielding.erase(0, 257).await.unwrap(); + + let fake = yielding.wrapped; + assert_eq!(3, fake.0.len()); + assert_eq!((0, 128), fake.0[0]); + assert_eq!((128, 256), fake.0[1]); + assert_eq!((256, 257), fake.0[2]); + } +} From cd1bf31fedbd33170507245eef1f7ae576aa3557 Mon Sep 17 00:00:00 2001 From: Rasmus Melchior Jacobsen Date: Mon, 22 May 2023 16:48:31 +0200 Subject: [PATCH 2/8] Add YieldingAsync adapter --- embassy-embedded-hal/Cargo.toml | 4 +- .../{adapter.rs => adapter/blocking_async.rs} | 79 +----- embassy-embedded-hal/src/adapter/mod.rs | 5 + .../src/adapter/yielding_async.rs | 232 ++++++++++++++++++ 4 files changed, 241 insertions(+), 79 deletions(-) rename embassy-embedded-hal/src/{adapter.rs => adapter/blocking_async.rs} (70%) create mode 100644 embassy-embedded-hal/src/adapter/mod.rs create mode 100644 embassy-embedded-hal/src/adapter/yielding_async.rs diff --git a/embassy-embedded-hal/Cargo.toml b/embassy-embedded-hal/Cargo.toml index 81cece686..ad2f14568 100644 --- a/embassy-embedded-hal/Cargo.toml +++ b/embassy-embedded-hal/Cargo.toml @@ -14,10 +14,10 @@ target = "x86_64-unknown-linux-gnu" [features] std = [] # Enable nightly-only features -nightly = ["embedded-hal-async", "embedded-storage-async"] +nightly = ["embassy-futures", "embedded-hal-async", "embedded-storage-async"] [dependencies] -embassy-futures = { version = "0.1.0", path = "../embassy-futures" } +embassy-futures = { version = "0.1.0", path = "../embassy-futures", optional = true } embassy-sync = { version = "0.2.0", path = "../embassy-sync" } embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = [ "unproven", diff --git a/embassy-embedded-hal/src/adapter.rs b/embassy-embedded-hal/src/adapter/blocking_async.rs similarity index 70% rename from embassy-embedded-hal/src/adapter.rs rename to embassy-embedded-hal/src/adapter/blocking_async.rs index 169aad5e3..171ff6c9f 100644 --- a/embassy-embedded-hal/src/adapter.rs +++ b/embassy-embedded-hal/src/adapter/blocking_async.rs @@ -1,6 +1,5 @@ //! Adapters between embedded-hal traits. -use embassy_futures::yield_now; use embedded_hal_02::{blocking, serial}; /// Wrapper that implements async traits using blocking implementations. @@ -151,18 +150,11 @@ where const ERASE_SIZE: usize = ::ERASE_SIZE; async fn write(&mut self, offset: u32, data: &[u8]) -> Result<(), Self::Error> { - self.wrapped.write(offset, data)?; - yield_now().await; - Ok(()) + self.wrapped.write(offset, data) } async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { - for from in (from..to).step_by(T::ERASE_SIZE) { - let to = core::cmp::min(from + T::ERASE_SIZE as u32, to); - self.wrapped.erase(from, to)?; - yield_now().await; - } - Ok(()) + self.wrapped.erase(from, to) } } @@ -179,70 +171,3 @@ where self.wrapped.capacity() } } - -#[cfg(test)] -mod tests { - use super::*; - - extern crate std; - - #[derive(Default)] - struct FakeFlash(Vec<(u32, u32)>); - - impl embedded_storage::nor_flash::ErrorType for FakeFlash { - type Error = std::convert::Infallible; - } - - impl embedded_storage::nor_flash::ReadNorFlash for FakeFlash { - const READ_SIZE: usize = 1; - - fn read(&mut self, _offset: u32, _bytes: &mut [u8]) -> Result<(), Self::Error> { - unimplemented!() - } - - fn capacity(&self) -> usize { - unimplemented!() - } - } - - impl embedded_storage::nor_flash::NorFlash for FakeFlash { - const WRITE_SIZE: usize = 4; - const ERASE_SIZE: usize = 128; - - fn write(&mut self, _offset: u32, _bytes: &[u8]) -> Result<(), Self::Error> { - unimplemented!() - } - - fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { - self.0.push((from, to)); - Ok(()) - } - } - - #[futures_test::test] - async fn can_erase() { - let fake = FakeFlash::default(); - let mut yielding = BlockingAsync::new(fake); - - yielding.erase(0, 256).await.unwrap(); - - let fake = yielding.wrapped; - assert_eq!(2, fake.0.len()); - assert_eq!((0, 128), fake.0[0]); - assert_eq!((128, 256), fake.0[1]); - } - - #[futures_test::test] - async fn can_erase_wrong_erase_size() { - let fake = FakeFlash::default(); - let mut yielding = BlockingAsync::new(fake); - - yielding.erase(0, 257).await.unwrap(); - - let fake = yielding.wrapped; - assert_eq!(3, fake.0.len()); - assert_eq!((0, 128), fake.0[0]); - assert_eq!((128, 256), fake.0[1]); - assert_eq!((256, 257), fake.0[2]); - } -} diff --git a/embassy-embedded-hal/src/adapter/mod.rs b/embassy-embedded-hal/src/adapter/mod.rs new file mode 100644 index 000000000..787ac2979 --- /dev/null +++ b/embassy-embedded-hal/src/adapter/mod.rs @@ -0,0 +1,5 @@ +mod blocking_async; +mod yielding_async; + +pub use blocking_async::BlockingAsync; +pub use yielding_async::YieldingAsync; diff --git a/embassy-embedded-hal/src/adapter/yielding_async.rs b/embassy-embedded-hal/src/adapter/yielding_async.rs new file mode 100644 index 000000000..96d5cca8e --- /dev/null +++ b/embassy-embedded-hal/src/adapter/yielding_async.rs @@ -0,0 +1,232 @@ +use embassy_futures::yield_now; + +/// Wrapper that yields for each operation to the wrapped instance +/// +/// This can be used in combination with BlockingAsync to enforce yields +/// between long running blocking operations. +pub struct YieldingAsync { + wrapped: T, +} + +impl YieldingAsync { + /// Create a new instance of a wrapper that yields after each operation. + pub fn new(wrapped: T) -> Self { + Self { wrapped } + } +} + +// +// I2C implementations +// +impl embedded_hal_1::i2c::ErrorType for YieldingAsync +where + T: embedded_hal_1::i2c::ErrorType, +{ + type Error = T::Error; +} + +impl embedded_hal_async::i2c::I2c for YieldingAsync +where + T: embedded_hal_async::i2c::I2c, +{ + async fn read(&mut self, address: u8, read: &mut [u8]) -> Result<(), Self::Error> { + self.wrapped.read(address, read).await?; + yield_now().await; + Ok(()) + } + + async fn write(&mut self, address: u8, write: &[u8]) -> Result<(), Self::Error> { + self.wrapped.write(address, write).await?; + yield_now().await; + Ok(()) + } + + async fn write_read(&mut self, address: u8, write: &[u8], read: &mut [u8]) -> Result<(), Self::Error> { + self.wrapped.write_read(address, write, read).await?; + yield_now().await; + Ok(()) + } + + async fn transaction( + &mut self, + address: u8, + operations: &mut [embedded_hal_1::i2c::Operation<'_>], + ) -> Result<(), Self::Error> { + self.wrapped.transaction(address, operations).await?; + yield_now().await; + Ok(()) + } +} + +// +// SPI implementations +// + +impl embedded_hal_async::spi::ErrorType for YieldingAsync +where + T: embedded_hal_async::spi::ErrorType, +{ + type Error = T::Error; +} + +impl embedded_hal_async::spi::SpiBus for YieldingAsync +where + T: embedded_hal_async::spi::SpiBus, +{ + async fn transfer<'a>(&'a mut self, read: &'a mut [u8], write: &'a [u8]) -> Result<(), Self::Error> { + self.wrapped.transfer(read, write).await?; + yield_now().await; + Ok(()) + } + + async fn transfer_in_place<'a>(&'a mut self, words: &'a mut [u8]) -> Result<(), Self::Error> { + self.wrapped.transfer_in_place(words).await?; + yield_now().await; + Ok(()) + } +} + +impl embedded_hal_async::spi::SpiBusFlush for YieldingAsync +where + T: embedded_hal_async::spi::SpiBusFlush, +{ + async fn flush(&mut self) -> Result<(), Self::Error> { + self.wrapped.flush().await?; + yield_now().await; + Ok(()) + } +} + +impl embedded_hal_async::spi::SpiBusWrite for YieldingAsync +where + T: embedded_hal_async::spi::SpiBusWrite, +{ + async fn write(&mut self, data: &[u8]) -> Result<(), Self::Error> { + self.wrapped.write(data).await?; + yield_now().await; + Ok(()) + } +} + +impl embedded_hal_async::spi::SpiBusRead for YieldingAsync +where + T: embedded_hal_async::spi::SpiBusRead, +{ + async fn read(&mut self, data: &mut [u8]) -> Result<(), Self::Error> { + self.wrapped.read(data).await?; + yield_now().await; + Ok(()) + } +} + +/// +/// NOR flash implementations +/// +impl embedded_storage::nor_flash::ErrorType for YieldingAsync { + type Error = T::Error; +} + +impl embedded_storage_async::nor_flash::ReadNorFlash + for YieldingAsync +{ + const READ_SIZE: usize = T::READ_SIZE; + + async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { + self.wrapped.read(offset, bytes).await?; + Ok(()) + } + + fn capacity(&self) -> usize { + self.wrapped.capacity() + } +} + +impl embedded_storage_async::nor_flash::NorFlash for YieldingAsync { + const WRITE_SIZE: usize = T::WRITE_SIZE; + const ERASE_SIZE: usize = T::ERASE_SIZE; + + async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> { + self.wrapped.write(offset, bytes).await?; + yield_now().await; + Ok(()) + } + + async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { + // Yield between each actual erase + for from in (from..to).step_by(T::ERASE_SIZE) { + let to = core::cmp::min(from + T::ERASE_SIZE as u32, to); + self.wrapped.erase(from, to).await?; + yield_now().await; + } + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use embedded_storage_async::nor_flash::NorFlash; + + use super::*; + + extern crate std; + + #[derive(Default)] + struct FakeFlash(Vec<(u32, u32)>); + + impl embedded_storage::nor_flash::ErrorType for FakeFlash { + type Error = std::convert::Infallible; + } + + impl embedded_storage_async::nor_flash::ReadNorFlash for FakeFlash { + const READ_SIZE: usize = 1; + + async fn read(&mut self, _offset: u32, _bytes: &mut [u8]) -> Result<(), Self::Error> { + unimplemented!() + } + + fn capacity(&self) -> usize { + unimplemented!() + } + } + + impl embedded_storage_async::nor_flash::NorFlash for FakeFlash { + const WRITE_SIZE: usize = 4; + const ERASE_SIZE: usize = 128; + + async fn write(&mut self, _offset: u32, _bytes: &[u8]) -> Result<(), Self::Error> { + unimplemented!() + } + + async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { + self.0.push((from, to)); + Ok(()) + } + } + + #[futures_test::test] + async fn can_erase() { + let fake = FakeFlash::default(); + let mut yielding = YieldingAsync::new(fake); + + yielding.erase(0, 256).await.unwrap(); + + let fake = yielding.wrapped; + assert_eq!(2, fake.0.len()); + assert_eq!((0, 128), fake.0[0]); + assert_eq!((128, 256), fake.0[1]); + } + + #[futures_test::test] + async fn can_erase_wrong_erase_size() { + let fake = FakeFlash::default(); + let mut yielding = YieldingAsync::new(fake); + + yielding.erase(0, 257).await.unwrap(); + + let fake = yielding.wrapped; + assert_eq!(3, fake.0.len()); + assert_eq!((0, 128), fake.0[0]); + assert_eq!((128, 256), fake.0[1]); + assert_eq!((256, 257), fake.0[2]); + } +} From 187551f914aba22c001eb11a48e5fd15ea439b13 Mon Sep 17 00:00:00 2001 From: Rasmus Melchior Jacobsen Date: Mon, 22 May 2023 16:55:18 +0200 Subject: [PATCH 3/8] Move module documentation --- embassy-embedded-hal/src/adapter/blocking_async.rs | 2 -- embassy-embedded-hal/src/adapter/mod.rs | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/embassy-embedded-hal/src/adapter/blocking_async.rs b/embassy-embedded-hal/src/adapter/blocking_async.rs index 171ff6c9f..b996d6a75 100644 --- a/embassy-embedded-hal/src/adapter/blocking_async.rs +++ b/embassy-embedded-hal/src/adapter/blocking_async.rs @@ -1,5 +1,3 @@ -//! Adapters between embedded-hal traits. - use embedded_hal_02::{blocking, serial}; /// Wrapper that implements async traits using blocking implementations. diff --git a/embassy-embedded-hal/src/adapter/mod.rs b/embassy-embedded-hal/src/adapter/mod.rs index 787ac2979..28da56137 100644 --- a/embassy-embedded-hal/src/adapter/mod.rs +++ b/embassy-embedded-hal/src/adapter/mod.rs @@ -1,3 +1,5 @@ +//! Adapters between embedded-hal traits. + mod blocking_async; mod yielding_async; From 879c621394b4b7cf7f96242c4ae347812c17bf8e Mon Sep 17 00:00:00 2001 From: Rasmus Melchior Jacobsen Date: Tue, 23 May 2023 22:49:27 +0200 Subject: [PATCH 4/8] Ensure FlashRegion can only be created within this crate --- embassy-stm32/build.rs | 1 + embassy-stm32/src/flash/mod.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/embassy-stm32/build.rs b/embassy-stm32/build.rs index a00c6c416..ca0c36c26 100644 --- a/embassy-stm32/build.rs +++ b/embassy-stm32/build.rs @@ -206,6 +206,7 @@ fn main() { erase_size: #erase_size, write_size: #write_size, erase_value: #erase_value, + _ensure_internal: (), }; }); diff --git a/embassy-stm32/src/flash/mod.rs b/embassy-stm32/src/flash/mod.rs index f6efa7753..b93270ae1 100644 --- a/embassy-stm32/src/flash/mod.rs +++ b/embassy-stm32/src/flash/mod.rs @@ -19,6 +19,7 @@ pub struct FlashRegion { pub erase_size: u32, pub write_size: u32, pub erase_value: u8, + pub(crate) _ensure_internal: (), } #[derive(Debug, PartialEq)] From faf506b300291a04c534a22bec771a3106c72d16 Mon Sep 17 00:00:00 2001 From: Rasmus Melchior Jacobsen Date: Tue, 23 May 2023 22:50:41 +0200 Subject: [PATCH 5/8] Remove Drop for AltFlashLayout --- embassy-stm32/src/flash/common.rs | 1 + embassy-stm32/src/flash/f0.rs | 2 ++ embassy-stm32/src/flash/f3.rs | 2 ++ embassy-stm32/src/flash/f4.rs | 15 ++++++++------- embassy-stm32/src/flash/f7.rs | 2 ++ embassy-stm32/src/flash/h7.rs | 2 ++ embassy-stm32/src/flash/l.rs | 2 ++ 7 files changed, 19 insertions(+), 7 deletions(-) diff --git a/embassy-stm32/src/flash/common.rs b/embassy-stm32/src/flash/common.rs index 6d7f55974..a9bf3c3cd 100644 --- a/embassy-stm32/src/flash/common.rs +++ b/embassy-stm32/src/flash/common.rs @@ -17,6 +17,7 @@ impl<'d> Flash<'d> { } pub fn into_regions(self) -> FlashLayout<'d> { + family::set_default_layout(); FlashLayout::new(self.release()) } diff --git a/embassy-stm32/src/flash/f0.rs b/embassy-stm32/src/flash/f0.rs index 033afecd5..c6441ef16 100644 --- a/embassy-stm32/src/flash/f0.rs +++ b/embassy-stm32/src/flash/f0.rs @@ -7,6 +7,8 @@ use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE}; use crate::flash::Error; use crate::pac; +pub const fn set_default_layout() {} + pub const fn get_flash_regions() -> &'static [&'static FlashRegion] { &FLASH_REGIONS } diff --git a/embassy-stm32/src/flash/f3.rs b/embassy-stm32/src/flash/f3.rs index 10a09c42c..4c8172203 100644 --- a/embassy-stm32/src/flash/f3.rs +++ b/embassy-stm32/src/flash/f3.rs @@ -7,6 +7,8 @@ use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE}; use crate::flash::Error; use crate::pac; +pub const fn set_default_layout() {} + pub const fn get_flash_regions() -> &'static [&'static FlashRegion] { &FLASH_REGIONS } diff --git a/embassy-stm32/src/flash/f4.rs b/embassy-stm32/src/flash/f4.rs index 60ac62c17..63293c052 100644 --- a/embassy-stm32/src/flash/f4.rs +++ b/embassy-stm32/src/flash/f4.rs @@ -79,19 +79,20 @@ mod alt_regions { } } - impl Drop for AltFlashLayout<'_> { - fn drop(&mut self) { - unsafe { - super::lock(); - crate::pac::FLASH.optcr().modify(|r| r.set_db1m(false)) - }; - } } } #[cfg(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479))] pub use alt_regions::{AltFlashLayout, ALT_FLASH_REGIONS}; +#[cfg(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479))] +pub fn set_default_layout() { + unsafe { crate::pac::FLASH.optcr().modify(|r| r.set_db1m(false)) }; +} + +#[cfg(not(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479)))] +pub const fn set_default_layout() {} + #[cfg(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479))] pub fn get_flash_regions() -> &'static [&'static FlashRegion] { if unsafe { pac::FLASH.optcr().read().db1m() } { diff --git a/embassy-stm32/src/flash/f7.rs b/embassy-stm32/src/flash/f7.rs index 6427d5a09..ac2834a84 100644 --- a/embassy-stm32/src/flash/f7.rs +++ b/embassy-stm32/src/flash/f7.rs @@ -6,6 +6,8 @@ use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE}; use crate::flash::Error; use crate::pac; +pub const fn set_default_layout() {} + pub const fn get_flash_regions() -> &'static [&'static FlashRegion] { &FLASH_REGIONS } diff --git a/embassy-stm32/src/flash/h7.rs b/embassy-stm32/src/flash/h7.rs index 4f38d50c0..1b1631068 100644 --- a/embassy-stm32/src/flash/h7.rs +++ b/embassy-stm32/src/flash/h7.rs @@ -7,6 +7,8 @@ use super::{FlashRegion, FlashSector, BANK1_REGION, FLASH_REGIONS, WRITE_SIZE}; use crate::flash::Error; use crate::pac; +pub const fn set_default_layout() {} + const fn is_dual_bank() -> bool { FLASH_REGIONS.len() == 2 } diff --git a/embassy-stm32/src/flash/l.rs b/embassy-stm32/src/flash/l.rs index 7d9cc6ea3..c94f61900 100644 --- a/embassy-stm32/src/flash/l.rs +++ b/embassy-stm32/src/flash/l.rs @@ -6,6 +6,8 @@ use super::{FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE}; use crate::flash::Error; use crate::pac; +pub const fn set_default_layout() {} + pub const fn get_flash_regions() -> &'static [&'static FlashRegion] { &FLASH_REGIONS } From 14e3e72b0f54fe17245027be468fed30aba39266 Mon Sep 17 00:00:00 2001 From: Rasmus Melchior Jacobsen Date: Tue, 23 May 2023 22:51:26 +0200 Subject: [PATCH 6/8] Add missing implementations for f4 alternate regions --- embassy-stm32/src/flash/common.rs | 18 ++++---- embassy-stm32/src/flash/f4.rs | 69 +++++++++++++++++++++++++++---- 2 files changed, 70 insertions(+), 17 deletions(-) diff --git a/embassy-stm32/src/flash/common.rs b/embassy-stm32/src/flash/common.rs index a9bf3c3cd..432c8a43b 100644 --- a/embassy-stm32/src/flash/common.rs +++ b/embassy-stm32/src/flash/common.rs @@ -26,11 +26,11 @@ impl<'d> Flash<'d> { } pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> { - unsafe { blocking_write(FLASH_BASE as u32, FLASH_SIZE as u32, offset, bytes) } + unsafe { blocking_write_chunked(FLASH_BASE as u32, FLASH_SIZE as u32, offset, bytes) } } pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> { - unsafe { blocking_erase(FLASH_BASE as u32, from, to) } + unsafe { blocking_erase_sectored(FLASH_BASE as u32, from, to) } } pub(crate) fn release(self) -> PeripheralRef<'d, crate::peripherals::FLASH> { @@ -38,7 +38,7 @@ impl<'d> Flash<'d> { } } -fn blocking_read(base: u32, size: u32, offset: u32, bytes: &mut [u8]) -> Result<(), Error> { +pub(super) fn blocking_read(base: u32, size: u32, offset: u32, bytes: &mut [u8]) -> Result<(), Error> { if offset + bytes.len() as u32 > size { return Err(Error::Size); } @@ -49,7 +49,7 @@ fn blocking_read(base: u32, size: u32, offset: u32, bytes: &mut [u8]) -> Result< Ok(()) } -unsafe fn blocking_write(base: u32, size: u32, offset: u32, bytes: &[u8]) -> Result<(), Error> { +pub(super) unsafe fn blocking_write_chunked(base: u32, size: u32, offset: u32, bytes: &[u8]) -> Result<(), Error> { if offset + bytes.len() as u32 > size { return Err(Error::Size); } @@ -82,7 +82,7 @@ unsafe fn blocking_write(base: u32, size: u32, offset: u32, bytes: &[u8]) -> Res Ok(()) } -unsafe fn blocking_erase(base: u32, from: u32, to: u32) -> Result<(), Error> { +pub(super) unsafe fn blocking_erase_sectored(base: u32, from: u32, to: u32) -> Result<(), Error> { let start_address = base + from; let end_address = base + to; let regions = family::get_flash_regions(); @@ -155,11 +155,11 @@ impl FlashRegion { } pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> { - unsafe { blocking_write(self.base, self.size, offset, bytes) } + unsafe { blocking_write_chunked(self.base, self.size, offset, bytes) } } pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> { - unsafe { blocking_erase(self.base, from, to) } + unsafe { blocking_erase_sectored(self.base, from, to) } } } @@ -200,11 +200,11 @@ foreach_flash_region! { } pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> { - unsafe { blocking_write(self.0.base, self.0.size, offset, bytes) } + unsafe { blocking_write_chunked(self.0.base, self.0.size, offset, bytes) } } pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> { - unsafe { blocking_erase(self.0.base, from, to) } + unsafe { blocking_erase_sectored(self.0.base, from, to) } } } diff --git a/embassy-stm32/src/flash/f4.rs b/embassy-stm32/src/flash/f4.rs index 63293c052..7f1c5f671 100644 --- a/embassy-stm32/src/flash/f4.rs +++ b/embassy-stm32/src/flash/f4.rs @@ -11,8 +11,11 @@ mod alt_regions { use embassy_hal_common::PeripheralRef; use stm32_metapac::FLASH_SIZE; - use crate::_generated::flash_regions::{BANK1_REGION1, BANK1_REGION2, BANK1_REGION3}; - use crate::flash::{Bank1Region1, Bank1Region2, Flash, FlashBank, FlashRegion}; + use crate::_generated::flash_regions::{OTPRegion, BANK1_REGION1, BANK1_REGION2, BANK1_REGION3, OTP_REGION}; + use crate::flash::{ + blocking_erase_sectored, blocking_read, blocking_write_chunked, Bank1Region1, Bank1Region2, Error, Flash, + FlashBank, FlashRegion, + }; use crate::peripherals::FLASH; pub const ALT_BANK1_REGION3: FlashRegion = FlashRegion { @@ -45,20 +48,19 @@ mod alt_regions { &ALT_BANK2_REGION3, ]; - pub type AltBank1Region1<'d> = Bank1Region1<'d>; - pub type AltBank1Region2<'d> = Bank1Region2<'d>; pub struct AltBank1Region3<'d>(pub &'static FlashRegion, PeripheralRef<'d, FLASH>); pub struct AltBank2Region1<'d>(pub &'static FlashRegion, PeripheralRef<'d, FLASH>); pub struct AltBank2Region2<'d>(pub &'static FlashRegion, PeripheralRef<'d, FLASH>); pub struct AltBank2Region3<'d>(pub &'static FlashRegion, PeripheralRef<'d, FLASH>); pub struct AltFlashLayout<'d> { - pub bank1_region1: AltBank1Region1<'d>, - pub bank1_region2: AltBank1Region2<'d>, + pub bank1_region1: Bank1Region1<'d>, + pub bank1_region2: Bank1Region2<'d>, pub bank1_region3: AltBank1Region3<'d>, pub bank2_region1: AltBank2Region1<'d>, pub bank2_region2: AltBank2Region2<'d>, pub bank2_region3: AltBank2Region3<'d>, + pub otp_region: OTPRegion<'d>, } impl<'d> Flash<'d> { @@ -66,7 +68,7 @@ mod alt_regions { unsafe { crate::pac::FLASH.optcr().modify(|r| r.set_db1m(true)) }; // SAFETY: We never expose the cloned peripheral references, and their instance is not public. - // Also, all flash region operations are protected with a cs. + // Also, all blocking flash region operations are protected with a cs. let p = self.release(); AltFlashLayout { bank1_region1: Bank1Region1(&BANK1_REGION1, unsafe { p.clone_unchecked() }), @@ -75,15 +77,66 @@ mod alt_regions { bank2_region1: AltBank2Region1(&ALT_BANK2_REGION1, unsafe { p.clone_unchecked() }), bank2_region2: AltBank2Region2(&ALT_BANK2_REGION2, unsafe { p.clone_unchecked() }), bank2_region3: AltBank2Region3(&ALT_BANK2_REGION3, unsafe { p.clone_unchecked() }), + otp_region: OTPRegion(&OTP_REGION, unsafe { p.clone_unchecked() }), } } } + macro_rules! foreach_altflash_region { + ($type_name:ident, $region:ident) => { + impl $type_name<'_> { + pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> { + blocking_read(self.0.base, self.0.size, offset, bytes) + } + + pub fn blocking_write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Error> { + unsafe { blocking_write_chunked(self.0.base, self.0.size, offset, bytes) } + } + + pub fn blocking_erase(&mut self, from: u32, to: u32) -> Result<(), Error> { + unsafe { blocking_erase_sectored(self.0.base, from, to) } + } + } + + impl embedded_storage::nor_flash::ErrorType for $type_name<'_> { + type Error = Error; + } + + impl embedded_storage::nor_flash::ReadNorFlash for $type_name<'_> { + const READ_SIZE: usize = 1; + + fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { + self.blocking_read(offset, bytes) + } + + fn capacity(&self) -> usize { + self.0.size as usize + } + } + + impl embedded_storage::nor_flash::NorFlash for $type_name<'_> { + const WRITE_SIZE: usize = $region.write_size as usize; + const ERASE_SIZE: usize = $region.erase_size as usize; + + fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> { + self.blocking_write(offset, bytes) + } + + fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { + self.blocking_erase(from, to) + } + } + }; } + + foreach_altflash_region!(AltBank1Region3, ALT_BANK1_REGION3); + foreach_altflash_region!(AltBank2Region1, ALT_BANK2_REGION1); + foreach_altflash_region!(AltBank2Region2, ALT_BANK2_REGION2); + foreach_altflash_region!(AltBank2Region3, ALT_BANK2_REGION3); } #[cfg(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479))] -pub use alt_regions::{AltFlashLayout, ALT_FLASH_REGIONS}; +pub use alt_regions::*; #[cfg(any(stm32f427, stm32f429, stm32f437, stm32f439, stm32f469, stm32f479))] pub fn set_default_layout() { From 87acf5f50f2a976cae7546f691fb14ba7b81c714 Mon Sep 17 00:00:00 2001 From: Rasmus Melchior Jacobsen Date: Tue, 23 May 2023 23:01:55 +0200 Subject: [PATCH 7/8] Add missing set_default_layout() in "other" family --- embassy-stm32/src/flash/other.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/embassy-stm32/src/flash/other.rs b/embassy-stm32/src/flash/other.rs index c151cb828..556034654 100644 --- a/embassy-stm32/src/flash/other.rs +++ b/embassy-stm32/src/flash/other.rs @@ -2,6 +2,8 @@ use super::{Error, FlashRegion, FlashSector, FLASH_REGIONS, WRITE_SIZE}; +pub const fn set_default_layout() {} + pub const fn get_flash_regions() -> &'static [&'static FlashRegion] { &FLASH_REGIONS } From e785e1bc22fde8f203048d143ceafdd45ec4d4b6 Mon Sep 17 00:00:00 2001 From: Rasmus Melchior Jacobsen Date: Wed, 24 May 2023 14:40:34 +0200 Subject: [PATCH 8/8] Add ConcatFlash utility --- embassy-embedded-hal/src/flash.rs | 286 ++++++++++++++++++++++++++++++ embassy-embedded-hal/src/lib.rs | 2 + 2 files changed, 288 insertions(+) create mode 100644 embassy-embedded-hal/src/flash.rs diff --git a/embassy-embedded-hal/src/flash.rs b/embassy-embedded-hal/src/flash.rs new file mode 100644 index 000000000..9a6e4bd92 --- /dev/null +++ b/embassy-embedded-hal/src/flash.rs @@ -0,0 +1,286 @@ +//! Utilities related to flash. + +use embedded_storage::nor_flash::{ErrorType, NorFlash, NorFlashError, ReadNorFlash}; +#[cfg(feature = "nightly")] +use embedded_storage_async::nor_flash::{NorFlash as AsyncNorFlash, ReadNorFlash as AsyncReadNorFlash}; + +/// Convenience helper for concatenating two consecutive flashes into one. +/// This is especially useful if used with "flash regions", where one may +/// want to concatenate multiple regions into one larger region. +pub struct ConcatFlash(First, Second); + +impl ConcatFlash { + /// Create a new flash that concatenates two consecutive flashes. + pub fn new(first: First, second: Second) -> Self { + Self(first, second) + } +} + +const fn get_read_size(first_read_size: usize, second_read_size: usize) -> usize { + if first_read_size != second_read_size { + panic!("The read size for the concatenated flashes must be the same"); + } + first_read_size +} + +const fn get_write_size(first_write_size: usize, second_write_size: usize) -> usize { + if first_write_size != second_write_size { + panic!("The write size for the concatenated flashes must be the same"); + } + first_write_size +} + +const fn get_max_erase_size(first_erase_size: usize, second_erase_size: usize) -> usize { + let max_erase_size = if first_erase_size > second_erase_size { + first_erase_size + } else { + second_erase_size + }; + if max_erase_size % first_erase_size != 0 || max_erase_size % second_erase_size != 0 { + panic!("The erase sizes for the concatenated flashes must have have a gcd equal to the max erase size"); + } + max_erase_size +} + +impl ErrorType for ConcatFlash +where + First: ErrorType, + Second: ErrorType, + E: NorFlashError, +{ + type Error = E; +} + +impl ReadNorFlash for ConcatFlash +where + First: ReadNorFlash, + Second: ReadNorFlash, + E: NorFlashError, +{ + const READ_SIZE: usize = get_read_size(First::READ_SIZE, Second::READ_SIZE); + + fn read(&mut self, mut offset: u32, mut bytes: &mut [u8]) -> Result<(), E> { + if offset < self.0.capacity() as u32 { + let len = core::cmp::min(self.0.capacity() - offset as usize, bytes.len()); + self.0.read(offset, &mut bytes[..len])?; + offset += len as u32; + bytes = &mut bytes[len..]; + } + + if !bytes.is_empty() { + self.1.read(offset - self.0.capacity() as u32, bytes)?; + } + + Ok(()) + } + + fn capacity(&self) -> usize { + self.0.capacity() + self.1.capacity() + } +} + +impl NorFlash for ConcatFlash +where + First: NorFlash, + Second: NorFlash, + E: NorFlashError, +{ + const WRITE_SIZE: usize = get_write_size(First::WRITE_SIZE, Second::WRITE_SIZE); + const ERASE_SIZE: usize = get_max_erase_size(First::ERASE_SIZE, Second::ERASE_SIZE); + + fn write(&mut self, mut offset: u32, mut bytes: &[u8]) -> Result<(), E> { + if offset < self.0.capacity() as u32 { + let len = core::cmp::min(self.0.capacity() - offset as usize, bytes.len()); + self.0.write(offset, &bytes[..len])?; + offset += len as u32; + bytes = &bytes[len..]; + } + + if !bytes.is_empty() { + self.1.write(offset - self.0.capacity() as u32, bytes)?; + } + + Ok(()) + } + + fn erase(&mut self, mut from: u32, to: u32) -> Result<(), E> { + if from < self.0.capacity() as u32 { + let to = core::cmp::min(self.0.capacity() as u32, to); + self.0.erase(from, to)?; + from = self.0.capacity() as u32; + } + + if from < to { + self.1 + .erase(from - self.0.capacity() as u32, to - self.0.capacity() as u32)?; + } + + Ok(()) + } +} + +#[cfg(feature = "nightly")] +impl AsyncReadNorFlash for ConcatFlash +where + First: AsyncReadNorFlash, + Second: AsyncReadNorFlash, + E: NorFlashError, +{ + const READ_SIZE: usize = get_read_size(First::READ_SIZE, Second::READ_SIZE); + + async fn read(&mut self, mut offset: u32, mut bytes: &mut [u8]) -> Result<(), E> { + if offset < self.0.capacity() as u32 { + let len = core::cmp::min(self.0.capacity() - offset as usize, bytes.len()); + self.0.read(offset, &mut bytes[..len]).await?; + offset += len as u32; + bytes = &mut bytes[len..]; + } + + if !bytes.is_empty() { + self.1.read(offset - self.0.capacity() as u32, bytes).await?; + } + + Ok(()) + } + + fn capacity(&self) -> usize { + self.0.capacity() + self.1.capacity() + } +} + +#[cfg(feature = "nightly")] +impl AsyncNorFlash for ConcatFlash +where + First: AsyncNorFlash, + Second: AsyncNorFlash, + E: NorFlashError, +{ + const WRITE_SIZE: usize = get_write_size(First::WRITE_SIZE, Second::WRITE_SIZE); + const ERASE_SIZE: usize = get_max_erase_size(First::ERASE_SIZE, Second::ERASE_SIZE); + + async fn write(&mut self, mut offset: u32, mut bytes: &[u8]) -> Result<(), E> { + if offset < self.0.capacity() as u32 { + let len = core::cmp::min(self.0.capacity() - offset as usize, bytes.len()); + self.0.write(offset, &bytes[..len]).await?; + offset += len as u32; + bytes = &bytes[len..]; + } + + if !bytes.is_empty() { + self.1.write(offset - self.0.capacity() as u32, bytes).await?; + } + + Ok(()) + } + + async fn erase(&mut self, mut from: u32, to: u32) -> Result<(), E> { + if from < self.0.capacity() as u32 { + let to = core::cmp::min(self.0.capacity() as u32, to); + self.0.erase(from, to).await?; + from = self.0.capacity() as u32; + } + + if from < to { + self.1 + .erase(from - self.0.capacity() as u32, to - self.0.capacity() as u32) + .await?; + } + + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn can_write_and_read_across_flashes() { + let first = MemFlash::<64, 16, 4>::new(); + let second = MemFlash::<64, 64, 4>::new(); + let mut f = ConcatFlash::new(first, second); + + f.write(60, &[0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88]).unwrap(); + + assert_eq!(&[0x11, 0x22, 0x33, 0x44], &f.0 .0[60..]); + assert_eq!(&[0x55, 0x66, 0x77, 0x88], &f.1 .0[0..4]); + + let mut read_buf = [0; 8]; + f.read(60, &mut read_buf).unwrap(); + + assert_eq!(&[0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88], &read_buf); + } + + #[test] + fn can_erase_across_flashes() { + let mut first = MemFlash::<128, 16, 4>::new(); + let mut second = MemFlash::<128, 64, 4>::new(); + first.0.fill(0x00); + second.0.fill(0x00); + + let mut f = ConcatFlash::new(first, second); + + f.erase(64, 192).unwrap(); + + assert_eq!(&[0x00; 64], &f.0 .0[0..64]); + assert_eq!(&[0xff; 64], &f.0 .0[64..128]); + assert_eq!(&[0xff; 64], &f.1 .0[0..64]); + assert_eq!(&[0x00; 64], &f.1 .0[64..128]); + } + + pub struct MemFlash([u8; SIZE]); + + impl MemFlash { + pub const fn new() -> Self { + Self([0xff; SIZE]) + } + } + + impl ErrorType + for MemFlash + { + type Error = core::convert::Infallible; + } + + impl ReadNorFlash + for MemFlash + { + const READ_SIZE: usize = 1; + + fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { + let len = bytes.len(); + bytes.copy_from_slice(&self.0[offset as usize..offset as usize + len]); + Ok(()) + } + + fn capacity(&self) -> usize { + SIZE + } + } + + impl NorFlash + for MemFlash + { + const WRITE_SIZE: usize = WRITE_SIZE; + const ERASE_SIZE: usize = ERASE_SIZE; + + fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { + let from = from as usize; + let to = to as usize; + assert_eq!(0, from % ERASE_SIZE); + assert_eq!(0, to % ERASE_SIZE); + self.0[from..to].fill(0xff); + Ok(()) + } + + fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> { + let offset = offset as usize; + assert_eq!(0, bytes.len() % WRITE_SIZE); + assert_eq!(0, offset % WRITE_SIZE); + assert!(offset + bytes.len() <= SIZE); + + self.0[offset..offset + bytes.len()].copy_from_slice(bytes); + Ok(()) + } + } +} diff --git a/embassy-embedded-hal/src/lib.rs b/embassy-embedded-hal/src/lib.rs index 73c81b465..3aad838bd 100644 --- a/embassy-embedded-hal/src/lib.rs +++ b/embassy-embedded-hal/src/lib.rs @@ -7,6 +7,8 @@ #[cfg(feature = "nightly")] pub mod adapter; +pub mod flash; + pub mod shared_bus; /// Set the configuration of a peripheral driver.