Keep peripheral lifetime when calling into_regions()

This commit is contained in:
Rasmus Melchior Jacobsen 2023-03-29 12:10:24 +02:00
parent 6806bb9692
commit 4ee3d15519
2 changed files with 12 additions and 8 deletions

View File

@ -162,13 +162,15 @@ fn main() {
let regions_len = flash_memory_regions.len(); let regions_len = flash_memory_regions.len();
flash_regions.extend(quote! { flash_regions.extend(quote! {
pub struct FlashRegions { pub struct FlashRegions<'d> {
_inner: embassy_hal_common::PeripheralRef<'d, crate::peripherals::FLASH>,
#(#fields),* #(#fields),*
} }
impl FlashRegions { impl<'d> FlashRegions<'d> {
pub(crate) const fn take() -> Self { pub(crate) const fn new(p: embassy_hal_common::PeripheralRef<'d, crate::peripherals::FLASH>) -> Self {
Self { Self {
_inner: p,
#(#inits),* #(#inits),*
} }
} }

View File

@ -16,7 +16,7 @@ use crate::Peripheral;
mod family; mod family;
pub struct Flash<'d> { pub struct Flash<'d> {
_inner: PeripheralRef<'d, FLASH>, inner: PeripheralRef<'d, FLASH>,
} }
pub struct FlashRegionSettings { pub struct FlashRegionSettings {
@ -39,11 +39,13 @@ static REGION_LOCK: Mutex<CriticalSectionRawMutex, ()> = Mutex::new(());
impl<'d> Flash<'d> { impl<'d> Flash<'d> {
pub fn new(p: impl Peripheral<P = FLASH> + 'd) -> Self { pub fn new(p: impl Peripheral<P = FLASH> + 'd) -> Self {
into_ref!(p); into_ref!(p);
Self { _inner: p } Self { inner: p }
} }
pub fn into_regions(self) -> FlashRegions { pub fn into_regions(self) -> FlashRegions<'d> {
FlashRegions::take() 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> { 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) { fn drop(&mut self) {
unsafe { family::lock() }; unsafe { family::lock() };
} }