diff options
| -rw-r--r-- | embassy-nrf/src/gpio.rs | 97 | ||||
| -rw-r--r-- | embassy-nrf/src/gpiote.rs | 83 | ||||
| -rw-r--r-- | examples/nrf/src/bin/gpiote_port.rs | 11 | ||||
| -rw-r--r-- | examples/nrf/src/bin/wdt.rs | 3 |
4 files changed, 107 insertions, 87 deletions
diff --git a/embassy-nrf/src/gpio.rs b/embassy-nrf/src/gpio.rs index 91b2e585a..190d8470f 100644 --- a/embassy-nrf/src/gpio.rs +++ b/embassy-nrf/src/gpio.rs | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | #![macro_use] | 1 | #![macro_use] |
| 2 | 2 | ||
| 3 | use core::convert::Infallible; | 3 | use core::convert::Infallible; |
| 4 | use core::future::Future; | ||
| 4 | use core::hint::unreachable_unchecked; | 5 | use core::hint::unreachable_unchecked; |
| 5 | use core::marker::PhantomData; | 6 | use core::marker::PhantomData; |
| 6 | 7 | ||
| @@ -72,6 +73,54 @@ impl<'d, T: Pin> InputPin for Input<'d, T> { | |||
| 72 | } | 73 | } |
| 73 | } | 74 | } |
| 74 | 75 | ||
| 76 | #[cfg(feature = "gpiote")] | ||
| 77 | impl<'d, T: Pin> embassy::traits::gpio::WaitForHigh for Input<'d, T> { | ||
| 78 | #[rustfmt::skip] | ||
| 79 | type Future<'a> where Self: 'a = impl Future<Output=()> + Unpin + 'a; | ||
| 80 | |||
| 81 | fn wait_for_high<'a>(&'a mut self) -> Self::Future<'a> { | ||
| 82 | self.pin.conf().modify(|_, w| w.sense().high()); | ||
| 83 | |||
| 84 | crate::gpiote::PortInputFuture { | ||
| 85 | pin_port: self.pin.pin_port(), | ||
| 86 | phantom: PhantomData, | ||
| 87 | } | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | #[cfg(feature = "gpiote")] | ||
| 92 | impl<'d, T: Pin> embassy::traits::gpio::WaitForLow for Input<'d, T> { | ||
| 93 | #[rustfmt::skip] | ||
| 94 | type Future<'a> where Self: 'a = impl Future<Output=()> + Unpin + 'a; | ||
| 95 | |||
| 96 | fn wait_for_low<'a>(&'a mut self) -> Self::Future<'a> { | ||
| 97 | self.pin.conf().modify(|_, w| w.sense().low()); | ||
| 98 | |||
| 99 | crate::gpiote::PortInputFuture { | ||
| 100 | pin_port: self.pin.pin_port(), | ||
| 101 | phantom: PhantomData, | ||
| 102 | } | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | #[cfg(feature = "gpiote")] | ||
| 107 | impl<'d, T: Pin> embassy::traits::gpio::WaitForAnyEdge for Input<'d, T> { | ||
| 108 | #[rustfmt::skip] | ||
| 109 | type Future<'a> where Self: 'a = impl Future<Output=()> + Unpin + 'a; | ||
| 110 | |||
| 111 | fn wait_for_any_edge<'a>(&'a mut self) -> Self::Future<'a> { | ||
| 112 | if self.is_high().ok().unwrap() { | ||
| 113 | self.pin.conf().modify(|_, w| w.sense().low()); | ||
| 114 | } else { | ||
| 115 | self.pin.conf().modify(|_, w| w.sense().high()); | ||
| 116 | } | ||
| 117 | crate::gpiote::PortInputFuture { | ||
| 118 | pin_port: self.pin.pin_port(), | ||
| 119 | phantom: PhantomData, | ||
| 120 | } | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 75 | /// Digital input or output level. | 124 | /// Digital input or output level. |
| 76 | #[derive(Debug, Eq, PartialEq)] | 125 | #[derive(Debug, Eq, PartialEq)] |
| 77 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 126 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| @@ -277,6 +326,54 @@ impl<'d, T: Pin> StatefulOutputPin for FlexPin<'d, T> { | |||
| 277 | } | 326 | } |
| 278 | } | 327 | } |
| 279 | 328 | ||
| 329 | #[cfg(feature = "gpiote")] | ||
| 330 | impl<'d, T: Pin> embassy::traits::gpio::WaitForHigh for FlexPin<'d, T> { | ||
| 331 | #[rustfmt::skip] | ||
| 332 | type Future<'a> where Self: 'a = impl Future<Output=()> + Unpin + 'a; | ||
| 333 | |||
| 334 | fn wait_for_high<'a>(&'a mut self) -> Self::Future<'a> { | ||
| 335 | self.pin.conf().modify(|_, w| w.sense().high()); | ||
| 336 | |||
| 337 | crate::gpiote::PortInputFuture { | ||
| 338 | pin_port: self.pin.pin_port(), | ||
| 339 | phantom: PhantomData, | ||
| 340 | } | ||
| 341 | } | ||
| 342 | } | ||
| 343 | |||
| 344 | #[cfg(feature = "gpiote")] | ||
| 345 | impl<'d, T: Pin> embassy::traits::gpio::WaitForLow for FlexPin<'d, T> { | ||
| 346 | #[rustfmt::skip] | ||
| 347 | type Future<'a> where Self: 'a = impl Future<Output=()> + Unpin + 'a; | ||
| 348 | |||
| 349 | fn wait_for_low<'a>(&'a mut self) -> Self::Future<'a> { | ||
| 350 | self.pin.conf().modify(|_, w| w.sense().low()); | ||
| 351 | |||
| 352 | crate::gpiote::PortInputFuture { | ||
| 353 | pin_port: self.pin.pin_port(), | ||
| 354 | phantom: PhantomData, | ||
| 355 | } | ||
| 356 | } | ||
| 357 | } | ||
| 358 | |||
| 359 | #[cfg(feature = "gpiote")] | ||
| 360 | impl<'d, T: Pin> embassy::traits::gpio::WaitForAnyEdge for FlexPin<'d, T> { | ||
| 361 | #[rustfmt::skip] | ||
| 362 | type Future<'a> where Self: 'a = impl Future<Output=()> + Unpin + 'a; | ||
| 363 | |||
| 364 | fn wait_for_any_edge<'a>(&'a mut self) -> Self::Future<'a> { | ||
| 365 | if self.is_high().ok().unwrap() { | ||
| 366 | self.pin.conf().modify(|_, w| w.sense().low()); | ||
| 367 | } else { | ||
| 368 | self.pin.conf().modify(|_, w| w.sense().high()); | ||
| 369 | } | ||
| 370 | crate::gpiote::PortInputFuture { | ||
| 371 | pin_port: self.pin.pin_port(), | ||
| 372 | phantom: PhantomData, | ||
| 373 | } | ||
| 374 | } | ||
| 375 | } | ||
| 376 | |||
| 280 | pub(crate) mod sealed { | 377 | pub(crate) mod sealed { |
| 281 | use super::*; | 378 | use super::*; |
| 282 | 379 | ||
diff --git a/embassy-nrf/src/gpiote.rs b/embassy-nrf/src/gpiote.rs index 2e9a73564..4b595c589 100644 --- a/embassy-nrf/src/gpiote.rs +++ b/embassy-nrf/src/gpiote.rs | |||
| @@ -3,7 +3,6 @@ use core::future::Future; | |||
| 3 | use core::marker::PhantomData; | 3 | use core::marker::PhantomData; |
| 4 | use core::task::{Context, Poll}; | 4 | use core::task::{Context, Poll}; |
| 5 | use embassy::interrupt::{Interrupt, InterruptExt}; | 5 | use embassy::interrupt::{Interrupt, InterruptExt}; |
| 6 | use embassy::traits::gpio::{WaitForAnyEdge, WaitForHigh, WaitForLow}; | ||
| 7 | use embassy::waitqueue::AtomicWaker; | 6 | use embassy::waitqueue::AtomicWaker; |
| 8 | use embassy_hal_common::unsafe_impl_unborrow; | 7 | use embassy_hal_common::unsafe_impl_unborrow; |
| 9 | use embedded_hal::digital::v2::{InputPin, StatefulOutputPin}; | 8 | use embedded_hal::digital::v2::{InputPin, StatefulOutputPin}; |
| @@ -312,86 +311,12 @@ impl<'d, C: Channel, T: GpioPin> OutputChannel<'d, C, T> { | |||
| 312 | } | 311 | } |
| 313 | } | 312 | } |
| 314 | 313 | ||
| 315 | /// GPIOTE port input driver | 314 | pub(crate) struct PortInputFuture<'a> { |
| 316 | pub struct PortInput<'d, T: GpioPin> { | 315 | pub(crate) pin_port: u8, |
| 317 | pin: Input<'d, T>, | 316 | pub(crate) phantom: PhantomData<&'a mut AnyPin>, |
| 318 | } | ||
| 319 | |||
| 320 | impl<'d, T: GpioPin> Unpin for PortInput<'d, T> {} | ||
| 321 | |||
| 322 | impl<'d, T: GpioPin> PortInput<'d, T> { | ||
| 323 | pub fn new(pin: Input<'d, T>) -> Self { | ||
| 324 | Self { pin } | ||
| 325 | } | ||
| 326 | } | ||
| 327 | |||
| 328 | impl<'d, T: GpioPin> InputPin for PortInput<'d, T> { | ||
| 329 | type Error = Infallible; | ||
| 330 | |||
| 331 | fn is_high(&self) -> Result<bool, Self::Error> { | ||
| 332 | self.pin.is_high() | ||
| 333 | } | ||
| 334 | |||
| 335 | fn is_low(&self) -> Result<bool, Self::Error> { | ||
| 336 | self.pin.is_low() | ||
| 337 | } | ||
| 338 | } | ||
| 339 | |||
| 340 | impl<'d, T: GpioPin> WaitForHigh for PortInput<'d, T> { | ||
| 341 | type Future<'a> | ||
| 342 | where | ||
| 343 | Self: 'a, | ||
| 344 | = PortInputFuture<'a>; | ||
| 345 | |||
| 346 | fn wait_for_high<'a>(&'a mut self) -> Self::Future<'a> { | ||
| 347 | self.pin.pin.conf().modify(|_, w| w.sense().high()); | ||
| 348 | |||
| 349 | PortInputFuture { | ||
| 350 | pin_port: self.pin.pin.pin_port(), | ||
| 351 | phantom: PhantomData, | ||
| 352 | } | ||
| 353 | } | ||
| 354 | } | ||
| 355 | |||
| 356 | impl<'d, T: GpioPin> WaitForLow for PortInput<'d, T> { | ||
| 357 | type Future<'a> | ||
| 358 | where | ||
| 359 | Self: 'a, | ||
| 360 | = PortInputFuture<'a>; | ||
| 361 | |||
| 362 | fn wait_for_low<'a>(&'a mut self) -> Self::Future<'a> { | ||
| 363 | self.pin.pin.conf().modify(|_, w| w.sense().low()); | ||
| 364 | |||
| 365 | PortInputFuture { | ||
| 366 | pin_port: self.pin.pin.pin_port(), | ||
| 367 | phantom: PhantomData, | ||
| 368 | } | ||
| 369 | } | ||
| 370 | } | 317 | } |
| 371 | 318 | ||
| 372 | impl<'d, T: GpioPin> WaitForAnyEdge for PortInput<'d, T> { | 319 | impl<'a> Unpin for PortInputFuture<'a> {} |
| 373 | type Future<'a> | ||
| 374 | where | ||
| 375 | Self: 'a, | ||
| 376 | = PortInputFuture<'a>; | ||
| 377 | |||
| 378 | fn wait_for_any_edge<'a>(&'a mut self) -> Self::Future<'a> { | ||
| 379 | if self.is_high().ok().unwrap() { | ||
| 380 | self.pin.pin.conf().modify(|_, w| w.sense().low()); | ||
| 381 | } else { | ||
| 382 | self.pin.pin.conf().modify(|_, w| w.sense().high()); | ||
| 383 | } | ||
| 384 | PortInputFuture { | ||
| 385 | pin_port: self.pin.pin.pin_port(), | ||
| 386 | phantom: PhantomData, | ||
| 387 | } | ||
| 388 | } | ||
| 389 | } | ||
| 390 | |||
| 391 | pub struct PortInputFuture<'a> { | ||
| 392 | pin_port: u8, | ||
| 393 | phantom: PhantomData<&'a mut AnyPin>, | ||
| 394 | } | ||
| 395 | 320 | ||
| 396 | impl<'a> Drop for PortInputFuture<'a> { | 321 | impl<'a> Drop for PortInputFuture<'a> { |
| 397 | fn drop(&mut self) { | 322 | fn drop(&mut self) { |
diff --git a/examples/nrf/src/bin/gpiote_port.rs b/examples/nrf/src/bin/gpiote_port.rs index ba9436aca..76c861d95 100644 --- a/examples/nrf/src/bin/gpiote_port.rs +++ b/examples/nrf/src/bin/gpiote_port.rs | |||
| @@ -8,12 +8,11 @@ mod example_common; | |||
| 8 | use embassy::executor::Spawner; | 8 | use embassy::executor::Spawner; |
| 9 | use embassy::traits::gpio::{WaitForHigh, WaitForLow}; | 9 | use embassy::traits::gpio::{WaitForHigh, WaitForLow}; |
| 10 | use embassy_nrf::gpio::{AnyPin, Input, Pin as _, Pull}; | 10 | use embassy_nrf::gpio::{AnyPin, Input, Pin as _, Pull}; |
| 11 | use embassy_nrf::gpiote::PortInput; | ||
| 12 | use embassy_nrf::Peripherals; | 11 | use embassy_nrf::Peripherals; |
| 13 | use example_common::*; | 12 | use example_common::*; |
| 14 | 13 | ||
| 15 | #[embassy::task(pool_size = 4)] | 14 | #[embassy::task(pool_size = 4)] |
| 16 | async fn button_task(n: usize, mut pin: PortInput<'static, AnyPin>) { | 15 | async fn button_task(n: usize, mut pin: Input<'static, AnyPin>) { |
| 17 | loop { | 16 | loop { |
| 18 | pin.wait_for_low().await; | 17 | pin.wait_for_low().await; |
| 19 | info!("Button {:?} pressed!", n); | 18 | info!("Button {:?} pressed!", n); |
| @@ -26,10 +25,10 @@ async fn button_task(n: usize, mut pin: PortInput<'static, AnyPin>) { | |||
| 26 | async fn main(spawner: Spawner, p: Peripherals) { | 25 | async fn main(spawner: Spawner, p: Peripherals) { |
| 27 | info!("Starting!"); | 26 | info!("Starting!"); |
| 28 | 27 | ||
| 29 | let btn1 = PortInput::new(Input::new(p.P0_11.degrade(), Pull::Up)); | 28 | let btn1 = Input::new(p.P0_11.degrade(), Pull::Up); |
| 30 | let btn2 = PortInput::new(Input::new(p.P0_12.degrade(), Pull::Up)); | 29 | let btn2 = Input::new(p.P0_12.degrade(), Pull::Up); |
| 31 | let btn3 = PortInput::new(Input::new(p.P0_24.degrade(), Pull::Up)); | 30 | let btn3 = Input::new(p.P0_24.degrade(), Pull::Up); |
| 32 | let btn4 = PortInput::new(Input::new(p.P0_25.degrade(), Pull::Up)); | 31 | let btn4 = Input::new(p.P0_25.degrade(), Pull::Up); |
| 33 | 32 | ||
| 34 | unwrap!(spawner.spawn(button_task(1, btn1))); | 33 | unwrap!(spawner.spawn(button_task(1, btn1))); |
| 35 | unwrap!(spawner.spawn(button_task(2, btn2))); | 34 | unwrap!(spawner.spawn(button_task(2, btn2))); |
diff --git a/examples/nrf/src/bin/wdt.rs b/examples/nrf/src/bin/wdt.rs index 76f171cd3..78c2205d9 100644 --- a/examples/nrf/src/bin/wdt.rs +++ b/examples/nrf/src/bin/wdt.rs | |||
| @@ -8,7 +8,6 @@ mod example_common; | |||
| 8 | use defmt::*; | 8 | use defmt::*; |
| 9 | use embassy::executor::Spawner; | 9 | use embassy::executor::Spawner; |
| 10 | use embassy_nrf::gpio::{Input, Pull}; | 10 | use embassy_nrf::gpio::{Input, Pull}; |
| 11 | use embassy_nrf::gpiote::PortInput; | ||
| 12 | use embassy_nrf::wdt::{Config, Watchdog}; | 11 | use embassy_nrf::wdt::{Config, Watchdog}; |
| 13 | use embassy_nrf::Peripherals; | 12 | use embassy_nrf::Peripherals; |
| 14 | use embassy_traits::gpio::{WaitForHigh, WaitForLow}; | 13 | use embassy_traits::gpio::{WaitForHigh, WaitForLow}; |
| @@ -32,7 +31,7 @@ async fn main(_spawner: Spawner, p: Peripherals) { | |||
| 32 | } | 31 | } |
| 33 | }; | 32 | }; |
| 34 | 33 | ||
| 35 | let mut button = PortInput::new(Input::new(p.P0_11, Pull::Up)); | 34 | let mut button = Input::new(p.P0_11, Pull::Up); |
| 36 | 35 | ||
| 37 | info!("Watchdog started, press button 1 to pet it or I'll reset in 3 seconds!"); | 36 | info!("Watchdog started, press button 1 to pet it or I'll reset in 3 seconds!"); |
| 38 | 37 | ||
