aboutsummaryrefslogtreecommitdiff
path: root/embassy-nrf
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2020-11-09 01:04:34 +0100
committerDario Nieuwenhuis <[email protected]>2020-11-09 01:04:34 +0100
commit2e062f562773f4f4ff978e7976c2d4b08b968a6c (patch)
tree7a671295164f2c2dfd3b2432a12d7038bec6676a /embassy-nrf
parent61b1d4e18889fa0c6238fe2d5758e2276a62c590 (diff)
gpiote: change port api to directly return futures.
Diffstat (limited to 'embassy-nrf')
-rw-r--r--embassy-nrf/src/gpiote.rs35
1 files changed, 22 insertions, 13 deletions
diff --git a/embassy-nrf/src/gpiote.rs b/embassy-nrf/src/gpiote.rs
index 2b1ca40b1..9626d3299 100644
--- a/embassy-nrf/src/gpiote.rs
+++ b/embassy-nrf/src/gpiote.rs
@@ -1,6 +1,8 @@
1use anyfmt::{panic, *}; 1use anyfmt::{panic, *};
2use core::cell::Cell; 2use core::cell::Cell;
3use core::future::Future;
3use core::ptr; 4use core::ptr;
5use core::task::{Context, Poll};
4use embassy::util::Signal; 6use embassy::util::Signal;
5 7
6use crate::hal::gpio::{Input, Level, Output, Pin, Port}; 8use crate::hal::gpio::{Input, Level, Output, Pin, Port};
@@ -201,10 +203,18 @@ impl Gpiote {
201 }) 203 })
202 } 204 }
203 205
204 pub fn new_port_input<'a, T>(&'a self, pin: Pin<Input<T>>) -> PortInput<'a, T> { 206 pub fn wait_port_input<'a, T>(
207 &'a self,
208 pin: &'a Pin<Input<T>>,
209 polarity: PortInputPolarity,
210 ) -> PortInputFuture<'a, T> {
205 interrupt::free(|_| { 211 interrupt::free(|_| {
206 unsafe { INSTANCE = self }; 212 unsafe { INSTANCE = self };
207 PortInput { gpiote: self, pin } 213 PortInputFuture {
214 gpiote: self,
215 pin,
216 polarity,
217 }
208 }) 218 })
209 } 219 }
210 220
@@ -285,29 +295,28 @@ impl Gpiote {
285 } 295 }
286} 296}
287 297
288pub struct PortInput<'a, T> { 298pub struct PortInputFuture<'a, T> {
289 gpiote: &'a Gpiote, 299 gpiote: &'a Gpiote,
290 pin: Pin<Input<T>>, 300 pin: &'a Pin<Input<T>>,
301 polarity: PortInputPolarity,
291} 302}
292 303
293impl<'a, T> Drop for PortInput<'a, T> { 304impl<'a, T> Drop for PortInputFuture<'a, T> {
294 fn drop(&mut self) { 305 fn drop(&mut self) {
295 pin_conf(&self.pin).modify(|_, w| w.sense().disabled()); 306 pin_conf(&self.pin).modify(|_, w| w.sense().disabled());
296 self.gpiote.port_signals[pin_num(&self.pin)].reset(); 307 self.gpiote.port_signals[pin_num(&self.pin)].reset();
297 } 308 }
298} 309}
299 310
300impl<'a, T> PortInput<'a, T> { 311impl<'a, T> Future for PortInputFuture<'a, T> {
301 pub async fn wait(&self, polarity: PortInputPolarity) { 312 type Output = ();
302 pin_conf(&self.pin).modify(|_, w| match polarity { 313
314 fn poll(self: core::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
315 pin_conf(&self.pin).modify(|_, w| match self.polarity {
303 PortInputPolarity::Low => w.sense().low(), 316 PortInputPolarity::Low => w.sense().low(),
304 PortInputPolarity::High => w.sense().high(), 317 PortInputPolarity::High => w.sense().high(),
305 }); 318 });
306 self.gpiote.port_signals[pin_num(&self.pin)].wait().await; 319 self.gpiote.port_signals[pin_num(&self.pin)].poll_wait(cx)
307 }
308
309 pub fn pin(&self) -> &Pin<Input<T>> {
310 &self.pin
311 } 320 }
312} 321}
313 322