From bff0ad9286ea219d0e19855b8ed8ba3e1a0f1bdf Mon Sep 17 00:00:00 2001 From: Grant Miller Date: Sun, 3 Jul 2022 16:54:01 -0500 Subject: [PATCH] Update embassy-rp --- embassy-nrf/src/gpio.rs | 2 +- embassy-rp/src/gpio.rs | 42 +++++++++++++++++++------------- embassy-rp/src/spi.rs | 53 ++++++++++++++++------------------------- embassy-rp/src/uart.rs | 12 +++------- 4 files changed, 50 insertions(+), 59 deletions(-) diff --git a/embassy-nrf/src/gpio.rs b/embassy-nrf/src/gpio.rs index e5764f8a0..53ca15d3d 100644 --- a/embassy-nrf/src/gpio.rs +++ b/embassy-nrf/src/gpio.rs @@ -417,7 +417,7 @@ impl AnyPin { Self { pin_port } } - pub fn unborrow_and_degrade<'a>(pin: impl Unborrow + 'a) -> Unborrowed<'a, Self> { + pub(crate) fn unborrow_and_degrade<'a>(pin: impl Unborrow + 'a) -> Unborrowed<'a, Self> { Unborrowed::new(AnyPin { pin_port: pin.unborrow().pin_port(), }) diff --git a/embassy-rp/src/gpio.rs b/embassy-rp/src/gpio.rs index 53c26de1c..94d0f25c7 100644 --- a/embassy-rp/src/gpio.rs +++ b/embassy-rp/src/gpio.rs @@ -1,11 +1,11 @@ +#![macro_use] use core::future::Future; -use core::marker::PhantomData; use core::pin::Pin as FuturePin; use core::task::{Context, Poll}; use embassy::waitqueue::AtomicWaker; use embassy_cortex_m::interrupt::{Interrupt, InterruptExt}; -use embassy_hal_common::{unborrow, unsafe_impl_unborrow}; +use embassy_hal_common::{unborrow, unsafe_impl_unborrow, Unborrowed}; use crate::pac::common::{Reg, RW}; use crate::pac::SIO; @@ -177,13 +177,13 @@ unsafe fn IO_IRQ_BANK0() { } struct InputFuture<'a, T: Pin> { - pin: &'a mut T, + pin: Unborrowed<'a, T>, level: InterruptTrigger, - phantom: PhantomData<&'a mut AnyPin>, } impl<'d, T: Pin> InputFuture<'d, T> { - pub fn new(pin: &'d mut T, level: InterruptTrigger) -> Self { + pub fn new(pin: impl Unborrow + 'd, level: InterruptTrigger) -> Self { + unborrow!(pin); unsafe { let irq = interrupt::IO_IRQ_BANK0::steal(); irq.disable(); @@ -215,11 +215,7 @@ impl<'d, T: Pin> InputFuture<'d, T> { irq.enable(); } - Self { - pin, - level, - phantom: PhantomData, - } + Self { pin, level } } } @@ -419,8 +415,7 @@ impl<'d, T: Pin> OutputOpenDrain<'d, T> { /// set while not in output mode, so the pin's level will be 'remembered' when it is not in output /// mode. pub struct Flex<'d, T: Pin> { - pin: T, - phantom: PhantomData<&'d mut T>, + pin: Unborrowed<'d, T>, } impl<'d, T: Pin> Flex<'d, T> { @@ -438,10 +433,7 @@ impl<'d, T: Pin> Flex<'d, T> { }); } - Self { - pin, - phantom: PhantomData, - } + Self { pin } } #[inline] @@ -667,7 +659,25 @@ pub trait Pin: Unborrow + sealed::Pin { pub struct AnyPin { pin_bank: u8, } + +impl AnyPin { + pub(crate) fn unborrow_and_degrade<'a>(pin: impl Unborrow + 'a) -> Unborrowed<'a, Self> { + Unborrowed::new(AnyPin { + pin_bank: pin.unborrow().pin_bank(), + }) + } +} + +macro_rules! unborrow_and_degrade { + ($($name:ident),*) => { + $( + let $name = $crate::gpio::AnyPin::unborrow_and_degrade($name); + )* + }; +} + unsafe_impl_unborrow!(AnyPin); + impl Pin for AnyPin {} impl sealed::Pin for AnyPin { fn pin_bank(&self) -> u8 { diff --git a/embassy-rp/src/spi.rs b/embassy-rp/src/spi.rs index 6b3f2238a..df030942e 100644 --- a/embassy-rp/src/spi.rs +++ b/embassy-rp/src/spi.rs @@ -1,7 +1,5 @@ -use core::marker::PhantomData; - use embassy_embedded_hal::SetConfig; -use embassy_hal_common::unborrow; +use embassy_hal_common::{unborrow, Unborrowed}; pub use embedded_hal_02::spi::{Phase, Polarity}; use crate::gpio::sealed::Pin as _; @@ -33,8 +31,7 @@ impl Default for Config { } pub struct Spi<'d, T: Instance> { - inner: T, - phantom: PhantomData<&'d mut T>, + inner: Unborrowed<'d, T>, } fn div_roundup(a: u32, b: u32) -> u32 { @@ -63,48 +60,41 @@ fn calc_prescs(freq: u32) -> (u8, u8) { impl<'d, T: Instance> Spi<'d, T> { pub fn new( inner: impl Unborrow + 'd, - clk: impl Unborrow> + 'd, - mosi: impl Unborrow> + 'd, - miso: impl Unborrow> + 'd, + clk: impl Unborrow + 'd> + 'd, + mosi: impl Unborrow + 'd> + 'd, + miso: impl Unborrow + 'd> + 'd, config: Config, ) -> Self { - unborrow!(clk, mosi, miso); - Self::new_inner( - inner, - Some(clk.degrade()), - Some(mosi.degrade()), - Some(miso.degrade()), - None, - config, - ) + unborrow_and_degrade!(clk, mosi, miso); + Self::new_inner(inner, Some(clk), Some(mosi), Some(miso), None, config) } pub fn new_txonly( inner: impl Unborrow + 'd, - clk: impl Unborrow> + 'd, - mosi: impl Unborrow> + 'd, + clk: impl Unborrow + 'd> + 'd, + mosi: impl Unborrow + 'd> + 'd, config: Config, ) -> Self { - unborrow!(clk, mosi); - Self::new_inner(inner, Some(clk.degrade()), Some(mosi.degrade()), None, None, config) + unborrow_and_degrade!(clk, mosi); + Self::new_inner(inner, Some(clk), Some(mosi), None, None, config) } pub fn new_rxonly( inner: impl Unborrow + 'd, - clk: impl Unborrow> + 'd, - miso: impl Unborrow> + 'd, + clk: impl Unborrow + 'd> + 'd, + miso: impl Unborrow + 'd> + 'd, config: Config, ) -> Self { - unborrow!(clk, miso); - Self::new_inner(inner, Some(clk.degrade()), None, Some(miso.degrade()), None, config) + unborrow_and_degrade!(clk, miso); + Self::new_inner(inner, Some(clk), None, Some(miso), None, config) } fn new_inner( inner: impl Unborrow + 'd, - clk: Option, - mosi: Option, - miso: Option, - cs: Option, + clk: Option>, + mosi: Option>, + miso: Option>, + cs: Option>, config: Config, ) -> Self { unborrow!(inner); @@ -137,10 +127,7 @@ impl<'d, T: Instance> Spi<'d, T> { pin.io().ctrl().write(|w| w.set_funcsel(1)); } } - Self { - inner, - phantom: PhantomData, - } + Self { inner } } pub fn blocking_write(&mut self, data: &[u8]) -> Result<(), Error> { diff --git a/embassy-rp/src/uart.rs b/embassy-rp/src/uart.rs index c95407b6f..33119af6a 100644 --- a/embassy-rp/src/uart.rs +++ b/embassy-rp/src/uart.rs @@ -1,6 +1,4 @@ -use core::marker::PhantomData; - -use embassy_hal_common::unborrow; +use embassy_hal_common::{unborrow, Unborrowed}; use gpio::Pin; use crate::{gpio, pac, peripherals, Unborrow}; @@ -23,8 +21,7 @@ impl Default for Config { } pub struct Uart<'d, T: Instance> { - inner: T, - phantom: PhantomData<&'d mut T>, + inner: Unborrowed<'d, T>, } impl<'d, T: Instance> Uart<'d, T> { @@ -78,10 +75,7 @@ impl<'d, T: Instance> Uart<'d, T> { cts.io().ctrl().write(|w| w.set_funcsel(2)); rts.io().ctrl().write(|w| w.set_funcsel(2)); } - Self { - inner, - phantom: PhantomData, - } + Self { inner } } pub fn send(&mut self, data: &[u8]) {