aboutsummaryrefslogtreecommitdiff
path: root/embassy-nrf/src
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2021-12-14 13:23:40 +0100
committerDario Nieuwenhuis <[email protected]>2021-12-14 13:23:40 +0100
commit153b1bbdbfc38b7973b27c05589514bee993e690 (patch)
tree4c289c0da1dede60214a85ce6b7262c06cfc75d9 /embassy-nrf/src
parentff82c76935d86b7444e6abc7296c4c3e09261484 (diff)
nrf/gpiote: remove PortInput, move impls to Input.
Diffstat (limited to 'embassy-nrf/src')
-rw-r--r--embassy-nrf/src/gpio.rs97
-rw-r--r--embassy-nrf/src/gpiote.rs83
2 files changed, 101 insertions, 79 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
3use core::convert::Infallible; 3use core::convert::Infallible;
4use core::future::Future;
4use core::hint::unreachable_unchecked; 5use core::hint::unreachable_unchecked;
5use core::marker::PhantomData; 6use 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")]
77impl<'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")]
92impl<'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")]
107impl<'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")]
330impl<'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")]
345impl<'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")]
360impl<'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
280pub(crate) mod sealed { 377pub(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;
3use core::marker::PhantomData; 3use core::marker::PhantomData;
4use core::task::{Context, Poll}; 4use core::task::{Context, Poll};
5use embassy::interrupt::{Interrupt, InterruptExt}; 5use embassy::interrupt::{Interrupt, InterruptExt};
6use embassy::traits::gpio::{WaitForAnyEdge, WaitForHigh, WaitForLow};
7use embassy::waitqueue::AtomicWaker; 6use embassy::waitqueue::AtomicWaker;
8use embassy_hal_common::unsafe_impl_unborrow; 7use embassy_hal_common::unsafe_impl_unborrow;
9use embedded_hal::digital::v2::{InputPin, StatefulOutputPin}; 8use 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 314pub(crate) struct PortInputFuture<'a> {
316pub 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
320impl<'d, T: GpioPin> Unpin for PortInput<'d, T> {}
321
322impl<'d, T: GpioPin> PortInput<'d, T> {
323 pub fn new(pin: Input<'d, T>) -> Self {
324 Self { pin }
325 }
326}
327
328impl<'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
340impl<'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
356impl<'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
372impl<'d, T: GpioPin> WaitForAnyEdge for PortInput<'d, T> { 319impl<'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
391pub struct PortInputFuture<'a> {
392 pin_port: u8,
393 phantom: PhantomData<&'a mut AnyPin>,
394}
395 320
396impl<'a> Drop for PortInputFuture<'a> { 321impl<'a> Drop for PortInputFuture<'a> {
397 fn drop(&mut self) { 322 fn drop(&mut self) {