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)]
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);
}
}

View file

@ -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 {

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 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!");
}
}

View file

@ -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<T: Instance + digital::InputPin> digital::InputPin for 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 {
let s = unsafe { self.get_unchecked_mut() };
s.pin.clear_pending_bit();
fn wait_for_state<'a>(&'a mut self, state: bool) -> impl Future<Output = ()> + '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<T: Instance + digital::InputPin + 'static> ExtiPin<T> {
});
});
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<T: Instance + 'static> ExtiPin<T> {
fn wait_for_edge<'a>(
self: Pin<&'a mut Self>,
state: EdgeOption,
) -> impl Future<Output = ()> + '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<Output = ()> + '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<T: Instance + 'static> ExtiPin<T> {
impl<T: Instance + digital::InputPin + 'static> WaitForHigh for ExtiPin<T> {
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)
}
}
@ -157,7 +148,7 @@ impl<T: Instance + digital::InputPin + 'static> WaitForHigh for ExtiPin<T> {
impl<T: Instance + digital::InputPin + 'static> WaitForLow for ExtiPin<T> {
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)
}
}
@ -176,7 +167,7 @@ impl<T: Instance + digital::InputPin + 'static> WaitForLow for ExtiPin<T> {
impl<T: Instance + 'static> WaitForRisingEdge for ExtiPin<T> {
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)
}
}
@ -184,7 +175,7 @@ impl<T: Instance + 'static> WaitForRisingEdge for ExtiPin<T> {
impl<T: Instance + 'static> WaitForFallingEdge for ExtiPin<T> {
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)
}
}
@ -192,7 +183,7 @@ impl<T: Instance + 'static> WaitForFallingEdge for ExtiPin<T> {
impl<T: Instance + 'static> WaitForAnyEdge for ExtiPin<T> {
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)
}
}

View file

@ -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<Output = ()> + '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<Output = ()> + '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<Output = ()> + '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>;
}