nrf: add unborrow! macro

This commit is contained in:
Dario Nieuwenhuis 2021-03-21 22:09:06 +01:00
parent a134fce122
commit 7b6086d19e
3 changed files with 17 additions and 17 deletions

View File

@ -73,3 +73,12 @@ macro_rules! peripherals {
}; };
} }
#[macro_export]
macro_rules! unborrow {
($($name:ident),*) => {
$(
let $name = unsafe { $name.unborrow() };
)*
}
}

View File

@ -5,6 +5,7 @@ use core::task::Poll;
use embassy::interrupt::Interrupt; use embassy::interrupt::Interrupt;
use embassy_extras::peripheral::{PeripheralMutex, PeripheralState}; use embassy_extras::peripheral::{PeripheralMutex, PeripheralState};
use embassy_extras::unborrow;
use crate::fmt::{assert, assert_eq, *}; use crate::fmt::{assert, assert_eq, *};
use crate::gpio::Pin as GpioPin; use crate::gpio::Pin as GpioPin;
@ -61,14 +62,7 @@ impl<'d, T: Instance> Qspi<'d, T> {
io3: impl PeripheralBorrow<Target = impl GpioPin> + 'd, io3: impl PeripheralBorrow<Target = impl GpioPin> + 'd,
config: Config, config: Config,
) -> Self { ) -> Self {
let mut qspi = unsafe { qspi.unborrow() }; unborrow!(qspi, irq, sck, csn, io0, io1, io2, io3);
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() };
let r = qspi.regs(); let r = qspi.regs();
@ -361,7 +355,7 @@ mod sealed {
use super::*; use super::*;
pub trait Instance { 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 { macro_rules! make_impl {
($type:ident, $irq:ident) => { ($type:ident, $irq:ident) => {
impl sealed::Instance for peripherals::$type { impl sealed::Instance for peripherals::$type {
fn regs(&mut self) -> &pac::qspi::RegisterBlock { fn regs(&self) -> &pac::qspi::RegisterBlock {
unsafe { &*pac::$type::ptr() } unsafe { &*pac::$type::ptr() }
} }
} }

View File

@ -5,6 +5,7 @@ use core::sync::atomic::{compiler_fence, Ordering};
use core::task::Poll; use core::task::Poll;
use embassy::traits; use embassy::traits;
use embassy::util::{wake_on_interrupt, PeripheralBorrow}; use embassy::util::{wake_on_interrupt, PeripheralBorrow};
use embassy_extras::unborrow;
use futures::future::poll_fn; use futures::future::poll_fn;
use traits::spi::FullDuplex; use traits::spi::FullDuplex;
@ -46,11 +47,7 @@ impl<'d, T: Instance> Spim<'d, T> {
mosi: impl PeripheralBorrow<Target = impl GpioPin> + 'd, mosi: impl PeripheralBorrow<Target = impl GpioPin> + 'd,
config: Config, config: Config,
) -> Self { ) -> Self {
let mut spim = unsafe { spim.unborrow() }; unborrow!(spim, irq, sck, miso, mosi);
let irq = unsafe { irq.unborrow() };
let sck = unsafe { sck.unborrow() };
let miso = unsafe { miso.unborrow() };
let mosi = unsafe { mosi.unborrow() };
let r = spim.regs(); let r = spim.regs();
@ -209,7 +206,7 @@ mod sealed {
use super::*; use super::*;
pub trait Instance { 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 { macro_rules! make_impl {
($type:ident, $irq:ident) => { ($type:ident, $irq:ident) => {
impl sealed::Instance for peripherals::$type { impl sealed::Instance for peripherals::$type {
fn regs(&mut self) -> &pac::spim0::RegisterBlock { fn regs(&self) -> &pac::spim0::RegisterBlock {
unsafe { &*pac::$type::ptr() } unsafe { &*pac::$type::ptr() }
} }
} }