Remove Pin from GPIO traits

This commit is contained in:
Dario Nieuwenhuis 2021-04-14 16:25:54 +02:00
parent 59ccc45f28
commit 8b1ffb2cb7
5 changed files with 27 additions and 38 deletions

View File

@ -21,9 +21,9 @@ use example_common::*;
#[embassy::task(pool_size = 4)] #[embassy::task(pool_size = 4)]
async fn button_task(n: usize, mut pin: PortInput<'static, AnyPin>) { async fn button_task(n: usize, mut pin: PortInput<'static, AnyPin>) {
loop { loop {
Pin::new(&mut pin).wait_for_low().await; pin.wait_for_low().await;
info!("Button {:?} pressed!", n); info!("Button {:?} pressed!", n);
Pin::new(&mut pin).wait_for_high().await; pin.wait_for_high().await;
info!("Button {:?} released!", n); info!("Button {:?} released!", n);
} }
} }

View File

@ -1,7 +1,6 @@
use core::convert::Infallible; use core::convert::Infallible;
use core::future::Future; use core::future::Future;
use core::marker::PhantomData; use core::marker::PhantomData;
use core::pin::Pin;
use core::task::{Context, Poll}; use core::task::{Context, Poll};
use embassy::interrupt::InterruptExt; use embassy::interrupt::InterruptExt;
use embassy::traits::gpio::{WaitForHigh, WaitForLow}; 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> { impl<'d, T: GpioPin> WaitForHigh for PortInput<'d, T> {
type Future<'a> = PortInputFuture<'a>; 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()); self.pin.pin.conf().modify(|_, w| w.sense().high());
PortInputFuture { PortInputFuture {
@ -331,7 +330,7 @@ impl<'d, T: GpioPin> WaitForHigh for PortInput<'d, T> {
impl<'d, T: GpioPin> WaitForLow for PortInput<'d, T> { impl<'d, T: GpioPin> WaitForLow for PortInput<'d, T> {
type Future<'a> = PortInputFuture<'a>; 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()); self.pin.pin.conf().modify(|_, w| w.sense().low());
PortInputFuture { PortInputFuture {

View File

@ -26,13 +26,12 @@ async fn run(dp: stm32::Peripherals, _cp: cortex_m::Peripherals) {
let button = gpioa.pa0.into_pull_up_input(); let button = gpioa.pa0.into_pull_up_input();
let mut syscfg = dp.SYSCFG.constrain(); let mut syscfg = dp.SYSCFG.constrain();
let pin = ExtiPin::new(button, interrupt::take!(EXTI0), &mut syscfg); let mut pin = ExtiPin::new(button, interrupt::take!(EXTI0), &mut syscfg);
pin_mut!(pin);
info!("Starting loop"); info!("Starting loop");
loop { loop {
pin.as_mut().wait_for_rising_edge().await; pin.wait_for_rising_edge().await;
info!("edge detected!"); info!("edge detected!");
} }
} }

View File

@ -1,6 +1,5 @@
use core::future::Future; use core::future::Future;
use core::mem; use core::mem;
use core::pin::Pin;
use cortex_m; use cortex_m;
use crate::hal::gpio; use crate::hal::gpio;
@ -96,13 +95,10 @@ impl<T: Instance + digital::InputPin> digital::InputPin for ExtiPin<T> {
} }
impl<T: Instance + digital::InputPin + 'static> ExtiPin<T> { impl<T: Instance + digital::InputPin + 'static> ExtiPin<T> {
fn wait_for_state<'a>(self: Pin<&'a mut Self>, state: bool) -> impl Future<Output = ()> + 'a { fn wait_for_state<'a>(&'a mut self, state: bool) -> impl Future<Output = ()> + 'a {
let s = unsafe { self.get_unchecked_mut() };
s.pin.clear_pending_bit();
async move { async move {
let fut = InterruptFuture::new(&mut s.interrupt); let fut = InterruptFuture::new(&mut self.interrupt);
let pin = &mut s.pin; let pin = &mut self.pin;
cortex_m::interrupt::free(|_| { cortex_m::interrupt::free(|_| {
pin.trigger_edge(if state { pin.trigger_edge(if state {
EdgeOption::Rising EdgeOption::Rising
@ -111,37 +107,32 @@ impl<T: Instance + digital::InputPin + 'static> ExtiPin<T> {
}); });
}); });
if (state && s.pin.is_high().unwrap_or(false)) if (state && self.pin.is_high().unwrap_or(false))
|| (!state && s.pin.is_low().unwrap_or(false)) || (!state && self.pin.is_low().unwrap_or(false))
{ {
return; return;
} }
fut.await; fut.await;
s.pin.clear_pending_bit(); self.pin.clear_pending_bit();
} }
} }
} }
impl<T: Instance + 'static> ExtiPin<T> { impl<T: Instance + 'static> ExtiPin<T> {
fn wait_for_edge<'a>( fn wait_for_edge<'a>(&'a mut self, state: EdgeOption) -> impl Future<Output = ()> + 'a {
self: Pin<&'a mut Self>, self.pin.clear_pending_bit();
state: EdgeOption,
) -> impl Future<Output = ()> + 'a {
let s = unsafe { self.get_unchecked_mut() };
s.pin.clear_pending_bit();
async move { async move {
let fut = InterruptFuture::new(&mut s.interrupt); let fut = InterruptFuture::new(&mut self.interrupt);
let pin = &mut s.pin; let pin = &mut self.pin;
cortex_m::interrupt::free(|_| { cortex_m::interrupt::free(|_| {
pin.trigger_edge(state); pin.trigger_edge(state);
}); });
fut.await; fut.await;
s.pin.clear_pending_bit(); self.pin.clear_pending_bit();
} }
} }
} }
@ -149,7 +140,7 @@ impl<T: Instance + 'static> ExtiPin<T> {
impl<T: Instance + digital::InputPin + 'static> WaitForHigh for ExtiPin<T> { impl<T: Instance + digital::InputPin + 'static> WaitForHigh for ExtiPin<T> {
type Future<'a> = impl Future<Output = ()> + 'a; type Future<'a> = impl Future<Output = ()> + '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) self.wait_for_state(true)
} }
} }
@ -157,7 +148,7 @@ impl<T: Instance + digital::InputPin + 'static> WaitForHigh for ExtiPin<T> {
impl<T: Instance + digital::InputPin + 'static> WaitForLow for ExtiPin<T> { impl<T: Instance + digital::InputPin + 'static> WaitForLow for ExtiPin<T> {
type Future<'a> = impl Future<Output = ()> + 'a; type Future<'a> = impl Future<Output = ()> + '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) self.wait_for_state(false)
} }
} }
@ -176,7 +167,7 @@ impl<T: Instance + digital::InputPin + 'static> WaitForLow for ExtiPin<T> {
impl<T: Instance + 'static> WaitForRisingEdge for ExtiPin<T> { impl<T: Instance + 'static> WaitForRisingEdge for ExtiPin<T> {
type Future<'a> = impl Future<Output = ()> + 'a; type Future<'a> = impl Future<Output = ()> + '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) self.wait_for_edge(EdgeOption::Rising)
} }
} }
@ -184,7 +175,7 @@ impl<T: Instance + 'static> WaitForRisingEdge for ExtiPin<T> {
impl<T: Instance + 'static> WaitForFallingEdge for ExtiPin<T> { impl<T: Instance + 'static> WaitForFallingEdge for ExtiPin<T> {
type Future<'a> = impl Future<Output = ()> + 'a; type Future<'a> = impl Future<Output = ()> + '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) self.wait_for_edge(EdgeOption::Falling)
} }
} }
@ -192,7 +183,7 @@ impl<T: Instance + 'static> WaitForFallingEdge for ExtiPin<T> {
impl<T: Instance + 'static> WaitForAnyEdge for ExtiPin<T> { impl<T: Instance + 'static> WaitForAnyEdge for ExtiPin<T> {
type Future<'a> = impl Future<Output = ()> + 'a; type Future<'a> = impl Future<Output = ()> + '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) self.wait_for_edge(EdgeOption::RisingFalling)
} }
} }

View File

@ -9,7 +9,7 @@ pub trait WaitForHigh {
/// ///
/// If the pin is already high, the future completes immediately. /// If the pin is already high, the future completes immediately.
/// Otherwise, it completes when it becomes high. /// 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. /// Wait for a pin to become low.
@ -20,7 +20,7 @@ pub trait WaitForLow {
/// ///
/// If the pin is already low, the future completes immediately. /// If the pin is already low, the future completes immediately.
/// Otherwise, it completes when it becomes low. /// 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) /// Wait for a rising edge (transition from low to high)
@ -28,7 +28,7 @@ pub trait WaitForRisingEdge {
type Future<'a>: Future<Output = ()> + 'a; type Future<'a>: Future<Output = ()> + 'a;
/// Wait for a rising edge (transition from low to high) /// 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) /// Wait for a falling edge (transition from high to low)
@ -36,7 +36,7 @@ pub trait WaitForFallingEdge {
type Future<'a>: Future<Output = ()> + 'a; type Future<'a>: Future<Output = ()> + 'a;
/// Wait for a falling edge (transition from high to low) /// 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) /// Wait for any edge (any transition, high to low or low to high)
@ -44,5 +44,5 @@ pub trait WaitForAnyEdge {
type Future<'a>: Future<Output = ()> + 'a; type Future<'a>: Future<Output = ()> + 'a;
/// Wait for any edge (any transition, high to low or low to high) /// 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>;
} }