diff --git a/embassy-extras/src/macros.rs b/embassy-extras/src/macros.rs index 478549ac..5c50f5d9 100644 --- a/embassy-extras/src/macros.rs +++ b/embassy-extras/src/macros.rs @@ -73,3 +73,12 @@ macro_rules! peripherals { }; } + +#[macro_export] +macro_rules! unborrow { + ($($name:ident),*) => { + $( + let $name = unsafe { $name.unborrow() }; + )* + } +} diff --git a/embassy-nrf/src/qspi.rs b/embassy-nrf/src/qspi.rs index 39cefa0a..10564ba5 100644 --- a/embassy-nrf/src/qspi.rs +++ b/embassy-nrf/src/qspi.rs @@ -5,6 +5,7 @@ use core::task::Poll; use embassy::interrupt::Interrupt; use embassy_extras::peripheral::{PeripheralMutex, PeripheralState}; +use embassy_extras::unborrow; use crate::fmt::{assert, assert_eq, *}; use crate::gpio::Pin as GpioPin; @@ -61,14 +62,7 @@ impl<'d, T: Instance> Qspi<'d, T> { io3: impl PeripheralBorrow + 'd, config: Config, ) -> Self { - let mut qspi = unsafe { qspi.unborrow() }; - let irq = unsafe { irq.unborrow() }; - let sck = unsafe { sck.unborrow() }; - let csn = unsafe { csn.unborrow() }; - let io0 = unsafe { io0.unborrow() }; - let io1 = unsafe { io1.unborrow() }; - let io2 = unsafe { io2.unborrow() }; - let io3 = unsafe { io3.unborrow() }; + unborrow!(qspi, irq, sck, csn, io0, io1, io2, io3); let r = qspi.regs(); @@ -361,7 +355,7 @@ mod sealed { use super::*; pub trait Instance { - fn regs(&mut self) -> &pac::qspi::RegisterBlock; + fn regs(&self) -> &pac::qspi::RegisterBlock; } } @@ -372,7 +366,7 @@ pub trait Instance: sealed::Instance + 'static { macro_rules! make_impl { ($type:ident, $irq:ident) => { impl sealed::Instance for peripherals::$type { - fn regs(&mut self) -> &pac::qspi::RegisterBlock { + fn regs(&self) -> &pac::qspi::RegisterBlock { unsafe { &*pac::$type::ptr() } } } diff --git a/embassy-nrf/src/spim.rs b/embassy-nrf/src/spim.rs index 214868b6..c84861f3 100644 --- a/embassy-nrf/src/spim.rs +++ b/embassy-nrf/src/spim.rs @@ -5,6 +5,7 @@ use core::sync::atomic::{compiler_fence, Ordering}; use core::task::Poll; use embassy::traits; use embassy::util::{wake_on_interrupt, PeripheralBorrow}; +use embassy_extras::unborrow; use futures::future::poll_fn; use traits::spi::FullDuplex; @@ -46,11 +47,7 @@ impl<'d, T: Instance> Spim<'d, T> { mosi: impl PeripheralBorrow + 'd, config: Config, ) -> Self { - let mut spim = unsafe { spim.unborrow() }; - let irq = unsafe { irq.unborrow() }; - let sck = unsafe { sck.unborrow() }; - let miso = unsafe { miso.unborrow() }; - let mosi = unsafe { mosi.unborrow() }; + unborrow!(spim, irq, sck, miso, mosi); let r = spim.regs(); @@ -209,7 +206,7 @@ mod sealed { use super::*; pub trait Instance { - fn regs(&mut self) -> &pac::spim0::RegisterBlock; + fn regs(&self) -> &pac::spim0::RegisterBlock; } } @@ -220,7 +217,7 @@ pub trait Instance: sealed::Instance + 'static { macro_rules! make_impl { ($type:ident, $irq:ident) => { impl sealed::Instance for peripherals::$type { - fn regs(&mut self) -> &pac::spim0::RegisterBlock { + fn regs(&self) -> &pac::spim0::RegisterBlock { unsafe { &*pac::$type::ptr() } } }