From 8b1ffb2cb7bd2e0c0f50eefb2391c15ae3050e73 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Wed, 14 Apr 2021 16:25:54 +0200 Subject: [PATCH] Remove Pin from GPIO traits --- embassy-nrf-examples/src/bin/gpiote_port.rs | 4 +- embassy-nrf/src/gpiote.rs | 5 +-- embassy-stm32-examples/src/bin/exti.rs | 5 +-- embassy-stm32/src/exti.rs | 41 ++++++++------------- embassy-traits/src/gpio.rs | 10 ++--- 5 files changed, 27 insertions(+), 38 deletions(-) diff --git a/embassy-nrf-examples/src/bin/gpiote_port.rs b/embassy-nrf-examples/src/bin/gpiote_port.rs index bbf5bc49..386806df 100644 --- a/embassy-nrf-examples/src/bin/gpiote_port.rs +++ b/embassy-nrf-examples/src/bin/gpiote_port.rs @@ -21,9 +21,9 @@ use example_common::*; #[embassy::task(pool_size = 4)] async fn button_task(n: usize, mut pin: PortInput<'static, AnyPin>) { loop { - Pin::new(&mut pin).wait_for_low().await; + pin.wait_for_low().await; info!("Button {:?} pressed!", n); - Pin::new(&mut pin).wait_for_high().await; + pin.wait_for_high().await; info!("Button {:?} released!", n); } } diff --git a/embassy-nrf/src/gpiote.rs b/embassy-nrf/src/gpiote.rs index 412eef1b..ead3c47d 100644 --- a/embassy-nrf/src/gpiote.rs +++ b/embassy-nrf/src/gpiote.rs @@ -1,7 +1,6 @@ use core::convert::Infallible; use core::future::Future; use core::marker::PhantomData; -use core::pin::Pin; use core::task::{Context, Poll}; use embassy::interrupt::InterruptExt; use embassy::traits::gpio::{WaitForHigh, WaitForLow}; @@ -318,7 +317,7 @@ impl<'d, T: GpioPin> InputPin for PortInput<'d, T> { impl<'d, T: GpioPin> WaitForHigh for PortInput<'d, T> { type Future<'a> = PortInputFuture<'a>; - fn wait_for_high<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a> { + fn wait_for_high<'a>(&'a mut self) -> Self::Future<'a> { self.pin.pin.conf().modify(|_, w| w.sense().high()); PortInputFuture { @@ -331,7 +330,7 @@ impl<'d, T: GpioPin> WaitForHigh for PortInput<'d, T> { impl<'d, T: GpioPin> WaitForLow for PortInput<'d, T> { type Future<'a> = PortInputFuture<'a>; - fn wait_for_low<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a> { + fn wait_for_low<'a>(&'a mut self) -> Self::Future<'a> { self.pin.pin.conf().modify(|_, w| w.sense().low()); PortInputFuture { diff --git a/embassy-stm32-examples/src/bin/exti.rs b/embassy-stm32-examples/src/bin/exti.rs index 27744c4c..e13b23ba 100644 --- a/embassy-stm32-examples/src/bin/exti.rs +++ b/embassy-stm32-examples/src/bin/exti.rs @@ -26,13 +26,12 @@ async fn run(dp: stm32::Peripherals, _cp: cortex_m::Peripherals) { let button = gpioa.pa0.into_pull_up_input(); let mut syscfg = dp.SYSCFG.constrain(); - let pin = ExtiPin::new(button, interrupt::take!(EXTI0), &mut syscfg); - pin_mut!(pin); + let mut pin = ExtiPin::new(button, interrupt::take!(EXTI0), &mut syscfg); info!("Starting loop"); loop { - pin.as_mut().wait_for_rising_edge().await; + pin.wait_for_rising_edge().await; info!("edge detected!"); } } diff --git a/embassy-stm32/src/exti.rs b/embassy-stm32/src/exti.rs index 8d70defe..9a12f811 100644 --- a/embassy-stm32/src/exti.rs +++ b/embassy-stm32/src/exti.rs @@ -1,6 +1,5 @@ use core::future::Future; use core::mem; -use core::pin::Pin; use cortex_m; use crate::hal::gpio; @@ -96,13 +95,10 @@ impl digital::InputPin for ExtiPin { } impl ExtiPin { - fn wait_for_state<'a>(self: Pin<&'a mut Self>, state: bool) -> impl Future + 'a { - let s = unsafe { self.get_unchecked_mut() }; - - s.pin.clear_pending_bit(); + fn wait_for_state<'a>(&'a mut self, state: bool) -> impl Future + 'a { async move { - let fut = InterruptFuture::new(&mut s.interrupt); - let pin = &mut s.pin; + let fut = InterruptFuture::new(&mut self.interrupt); + let pin = &mut self.pin; cortex_m::interrupt::free(|_| { pin.trigger_edge(if state { EdgeOption::Rising @@ -111,37 +107,32 @@ impl ExtiPin { }); }); - if (state && s.pin.is_high().unwrap_or(false)) - || (!state && s.pin.is_low().unwrap_or(false)) + if (state && self.pin.is_high().unwrap_or(false)) + || (!state && self.pin.is_low().unwrap_or(false)) { return; } fut.await; - s.pin.clear_pending_bit(); + self.pin.clear_pending_bit(); } } } impl ExtiPin { - fn wait_for_edge<'a>( - self: Pin<&'a mut Self>, - state: EdgeOption, - ) -> impl Future + 'a { - let s = unsafe { self.get_unchecked_mut() }; - - s.pin.clear_pending_bit(); + fn wait_for_edge<'a>(&'a mut self, state: EdgeOption) -> impl Future + 'a { + self.pin.clear_pending_bit(); async move { - let fut = InterruptFuture::new(&mut s.interrupt); - let pin = &mut s.pin; + let fut = InterruptFuture::new(&mut self.interrupt); + let pin = &mut self.pin; cortex_m::interrupt::free(|_| { pin.trigger_edge(state); }); fut.await; - s.pin.clear_pending_bit(); + self.pin.clear_pending_bit(); } } } @@ -149,7 +140,7 @@ impl ExtiPin { impl WaitForHigh for ExtiPin { type Future<'a> = impl Future + 'a; - fn wait_for_high<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a> { + fn wait_for_high<'a>(&'a mut self) -> Self::Future<'a> { self.wait_for_state(true) } } @@ -157,7 +148,7 @@ impl WaitForHigh for ExtiPin { impl WaitForLow for ExtiPin { type Future<'a> = impl Future + 'a; - fn wait_for_low<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a> { + fn wait_for_low<'a>(&'a mut self) -> Self::Future<'a> { self.wait_for_state(false) } } @@ -176,7 +167,7 @@ impl WaitForLow for ExtiPin { impl WaitForRisingEdge for ExtiPin { type Future<'a> = impl Future + 'a; - fn wait_for_rising_edge<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a> { + fn wait_for_rising_edge<'a>(&'a mut self) -> Self::Future<'a> { self.wait_for_edge(EdgeOption::Rising) } } @@ -184,7 +175,7 @@ impl WaitForRisingEdge for ExtiPin { impl WaitForFallingEdge for ExtiPin { type Future<'a> = impl Future + 'a; - fn wait_for_falling_edge<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a> { + fn wait_for_falling_edge<'a>(&'a mut self) -> Self::Future<'a> { self.wait_for_edge(EdgeOption::Falling) } } @@ -192,7 +183,7 @@ impl WaitForFallingEdge for ExtiPin { impl WaitForAnyEdge for ExtiPin { type Future<'a> = impl Future + 'a; - fn wait_for_any_edge<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a> { + fn wait_for_any_edge<'a>(&'a mut self) -> Self::Future<'a> { self.wait_for_edge(EdgeOption::RisingFalling) } } diff --git a/embassy-traits/src/gpio.rs b/embassy-traits/src/gpio.rs index 4c3feac2..c4ae206c 100644 --- a/embassy-traits/src/gpio.rs +++ b/embassy-traits/src/gpio.rs @@ -9,7 +9,7 @@ pub trait WaitForHigh { /// /// If the pin is already high, the future completes immediately. /// Otherwise, it completes when it becomes high. - fn wait_for_high<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a>; + fn wait_for_high<'a>(&'a mut self) -> Self::Future<'a>; } /// Wait for a pin to become low. @@ -20,7 +20,7 @@ pub trait WaitForLow { /// /// If the pin is already low, the future completes immediately. /// Otherwise, it completes when it becomes low. - fn wait_for_low<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a>; + fn wait_for_low<'a>(&'a mut self) -> Self::Future<'a>; } /// Wait for a rising edge (transition from low to high) @@ -28,7 +28,7 @@ pub trait WaitForRisingEdge { type Future<'a>: Future + 'a; /// Wait for a rising edge (transition from low to high) - fn wait_for_rising_edge<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a>; + fn wait_for_rising_edge<'a>(&'a mut self) -> Self::Future<'a>; } /// Wait for a falling edge (transition from high to low) @@ -36,7 +36,7 @@ pub trait WaitForFallingEdge { type Future<'a>: Future + 'a; /// Wait for a falling edge (transition from high to low) - fn wait_for_falling_edge<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a>; + fn wait_for_falling_edge<'a>(&'a mut self) -> Self::Future<'a>; } /// Wait for any edge (any transition, high to low or low to high) @@ -44,5 +44,5 @@ pub trait WaitForAnyEdge { type Future<'a>: Future + 'a; /// Wait for any edge (any transition, high to low or low to high) - fn wait_for_any_edge<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a>; + fn wait_for_any_edge<'a>(&'a mut self) -> Self::Future<'a>; }