diff options
| author | Rasmus Melchior Jacobsen <[email protected]> | 2023-03-29 12:10:24 +0200 |
|---|---|---|
| committer | Rasmus Melchior Jacobsen <[email protected]> | 2023-03-29 12:10:24 +0200 |
| commit | 4ee3d15519aaf3a290fd78063b88d182ff3aab53 (patch) | |
| tree | 9f3c8ad811b2d8c63cd50815f14788f6348895c3 | |
| parent | 6806bb969278acc9d3cde34897453b29807157c1 (diff) | |
Keep peripheral lifetime when calling into_regions()
| -rw-r--r-- | embassy-stm32/build.rs | 8 | ||||
| -rw-r--r-- | embassy-stm32/src/flash/mod.rs | 12 |
2 files changed, 12 insertions, 8 deletions
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() { | |||
| 162 | 162 | ||
| 163 | let regions_len = flash_memory_regions.len(); | 163 | let regions_len = flash_memory_regions.len(); |
| 164 | flash_regions.extend(quote! { | 164 | flash_regions.extend(quote! { |
| 165 | pub struct FlashRegions { | 165 | pub struct FlashRegions<'d> { |
| 166 | _inner: embassy_hal_common::PeripheralRef<'d, crate::peripherals::FLASH>, | ||
| 166 | #(#fields),* | 167 | #(#fields),* |
| 167 | } | 168 | } |
| 168 | 169 | ||
| 169 | impl FlashRegions { | 170 | impl<'d> FlashRegions<'d> { |
| 170 | pub(crate) const fn take() -> Self { | 171 | pub(crate) const fn new(p: embassy_hal_common::PeripheralRef<'d, crate::peripherals::FLASH>) -> Self { |
| 171 | Self { | 172 | Self { |
| 173 | _inner: p, | ||
| 172 | #(#inits),* | 174 | #(#inits),* |
| 173 | } | 175 | } |
| 174 | } | 176 | } |
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; | |||
| 16 | mod family; | 16 | mod family; |
| 17 | 17 | ||
| 18 | pub struct Flash<'d> { | 18 | pub struct Flash<'d> { |
| 19 | _inner: PeripheralRef<'d, FLASH>, | 19 | inner: PeripheralRef<'d, FLASH>, |
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | pub struct FlashRegionSettings { | 22 | pub struct FlashRegionSettings { |
| @@ -39,11 +39,13 @@ static REGION_LOCK: Mutex<CriticalSectionRawMutex, ()> = Mutex::new(()); | |||
| 39 | impl<'d> Flash<'d> { | 39 | impl<'d> Flash<'d> { |
| 40 | pub fn new(p: impl Peripheral<P = FLASH> + 'd) -> Self { | 40 | pub fn new(p: impl Peripheral<P = FLASH> + 'd) -> Self { |
| 41 | into_ref!(p); | 41 | into_ref!(p); |
| 42 | Self { _inner: p } | 42 | Self { inner: p } |
| 43 | } | 43 | } |
| 44 | 44 | ||
| 45 | pub fn into_regions(self) -> FlashRegions { | 45 | pub fn into_regions(self) -> FlashRegions<'d> { |
| 46 | FlashRegions::take() | 46 | let mut flash = self; |
| 47 | let p = unsafe { flash.inner.clone_unchecked() }; | ||
| 48 | FlashRegions::new(p) | ||
| 47 | } | 49 | } |
| 48 | 50 | ||
| 49 | pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> { | 51 | pub fn blocking_read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Error> { |
| @@ -123,7 +125,7 @@ impl Drop for Flash<'_> { | |||
| 123 | } | 125 | } |
| 124 | } | 126 | } |
| 125 | 127 | ||
| 126 | impl Drop for FlashRegions { | 128 | impl Drop for FlashRegions<'_> { |
| 127 | fn drop(&mut self) { | 129 | fn drop(&mut self) { |
| 128 | unsafe { family::lock() }; | 130 | unsafe { family::lock() }; |
| 129 | } | 131 | } |
