diff --git a/embassy-stm32/build.rs b/embassy-stm32/build.rs index 53f209780..f5bdadf5b 100644 --- a/embassy-stm32/build.rs +++ b/embassy-stm32/build.rs @@ -162,13 +162,15 @@ fn main() { let regions_len = flash_memory_regions.len(); flash_regions.extend(quote! { - pub struct FlashRegions { + pub struct FlashRegions<'d> { + _inner: embassy_hal_common::PeripheralRef<'d, crate::peripherals::FLASH>, #(#fields),* } - impl FlashRegions { - pub(crate) const fn take() -> Self { + impl<'d> FlashRegions<'d> { + pub(crate) const fn new(p: embassy_hal_common::PeripheralRef<'d, crate::peripherals::FLASH>) -> Self { Self { + _inner: p, #(#inits),* } } diff --git a/embassy-stm32/src/flash/mod.rs b/embassy-stm32/src/flash/mod.rs index 29db2d132..1d1f034aa 100644 --- a/embassy-stm32/src/flash/mod.rs +++ b/embassy-stm32/src/flash/mod.rs @@ -16,7 +16,7 @@ use crate::Peripheral; mod family; pub struct Flash<'d> { - _inner: PeripheralRef<'d, FLASH>, + inner: PeripheralRef<'d, FLASH>, } pub struct FlashRegionSettings { @@ -39,11 +39,13 @@ static REGION_LOCK: Mutex = Mutex::new(()); impl<'d> Flash<'d> { pub fn new(p: impl Peripheral

+ 'd) -> Self { into_ref!(p); - Self { _inner: p } + Self { inner: p } } - pub fn into_regions(self) -> FlashRegions { - FlashRegions::take() + pub fn into_regions(self) -> FlashRegions<'d> { + let mut flash = self; + let p = unsafe { flash.inner.clone_unchecked() }; + FlashRegions::new(p) } pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> { @@ -123,7 +125,7 @@ impl Drop for Flash<'_> { } } -impl Drop for FlashRegions { +impl Drop for FlashRegions<'_> { fn drop(&mut self) { unsafe { family::lock() }; }