aboutsummaryrefslogtreecommitdiff
path: root/embassy-nrf/src/gpio.rs
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2022-01-13 23:56:25 +0100
committerDario Nieuwenhuis <[email protected]>2022-01-13 23:56:39 +0100
commit7997687f3b4c8f679ae458ee28cd338ed9e44b2e (patch)
tree9d31efb45bd909a249f6ff61f066c4638b0832f9 /embassy-nrf/src/gpio.rs
parent6eec3d8acca1a4c6a853d0b65e43ec0a0f5c5c27 (diff)
nrf: impl embedded-hal 1.0 and embedded-hal-async traits.
Diffstat (limited to 'embassy-nrf/src/gpio.rs')
-rw-r--r--embassy-nrf/src/gpio.rs229
1 files changed, 157 insertions, 72 deletions
diff --git a/embassy-nrf/src/gpio.rs b/embassy-nrf/src/gpio.rs
index abda909f0..3f204d564 100644
--- a/embassy-nrf/src/gpio.rs
+++ b/embassy-nrf/src/gpio.rs
@@ -7,7 +7,6 @@ use core::marker::PhantomData;
7use cfg_if::cfg_if; 7use cfg_if::cfg_if;
8use embassy::util::Unborrow; 8use embassy::util::Unborrow;
9use embassy_hal_common::{unborrow, unsafe_impl_unborrow}; 9use embassy_hal_common::{unborrow, unsafe_impl_unborrow};
10use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin};
11use gpio::pin_cnf::DRIVE_A; 10use gpio::pin_cnf::DRIVE_A;
12 11
13use crate::pac; 12use crate::pac;
@@ -57,18 +56,6 @@ impl<'d, T: Pin> Input<'d, T> {
57 } 56 }
58} 57}
59 58
60impl<'d, T: Pin> InputPin for Input<'d, T> {
61 type Error = Infallible;
62
63 fn is_high(&self) -> Result<bool, Self::Error> {
64 Ok(self.is_high())
65 }
66
67 fn is_low(&self) -> Result<bool, Self::Error> {
68 Ok(self.is_low())
69 }
70}
71
72/// Digital input or output level. 59/// Digital input or output level.
73#[derive(Debug, Eq, PartialEq)] 60#[derive(Debug, Eq, PartialEq)]
74#[cfg_attr(feature = "defmt", derive(defmt::Format))] 61#[cfg_attr(feature = "defmt", derive(defmt::Format))]
@@ -142,28 +129,6 @@ impl<'d, T: Pin> Output<'d, T> {
142 } 129 }
143} 130}
144 131
145impl<'d, T: Pin> OutputPin for Output<'d, T> {
146 type Error = Infallible;
147
148 fn set_high(&mut self) -> Result<(), Self::Error> {
149 Ok(self.set_high())
150 }
151
152 fn set_low(&mut self) -> Result<(), Self::Error> {
153 Ok(self.set_low())
154 }
155}
156
157impl<'d, T: Pin> StatefulOutputPin for Output<'d, T> {
158 fn is_set_high(&self) -> Result<bool, Self::Error> {
159 Ok(self.is_set_high())
160 }
161
162 fn is_set_low(&self) -> Result<bool, Self::Error> {
163 Ok(self.is_set_low())
164 }
165}
166
167/// GPIO flexible pin. 132/// GPIO flexible pin.
168/// 133///
169/// This pin can either be a disconnected, input, or output pin. The level register bit will remain 134/// This pin can either be a disconnected, input, or output pin. The level register bit will remain
@@ -276,43 +241,6 @@ impl<'d, T: Pin> Drop for Flex<'d, T> {
276 } 241 }
277} 242}
278 243
279/// Implement [`InputPin`] for [`Flex`];
280///
281/// If the pin is not in input mode the result is unspecified.
282impl<'d, T: Pin> InputPin for Flex<'d, T> {
283 type Error = Infallible;
284
285 fn is_high(&self) -> Result<bool, Self::Error> {
286 Ok(self.is_high())
287 }
288
289 fn is_low(&self) -> Result<bool, Self::Error> {
290 Ok(self.is_low())
291 }
292}
293
294impl<'d, T: Pin> OutputPin for Flex<'d, T> {
295 type Error = Infallible;
296
297 fn set_high(&mut self) -> Result<(), Self::Error> {
298 Ok(self.set_high())
299 }
300
301 fn set_low(&mut self) -> Result<(), Self::Error> {
302 Ok(self.set_low())
303 }
304}
305
306impl<'d, T: Pin> StatefulOutputPin for Flex<'d, T> {
307 fn is_set_high(&self) -> Result<bool, Self::Error> {
308 Ok(self.is_set_high())
309 }
310
311 fn is_set_low(&self) -> Result<bool, Self::Error> {
312 Ok(self.is_set_low())
313 }
314}
315
316pub(crate) mod sealed { 244pub(crate) mod sealed {
317 use super::*; 245 use super::*;
318 246
@@ -491,3 +419,160 @@ macro_rules! impl_pin {
491 } 419 }
492 }; 420 };
493} 421}
422
423// ====================
424
425mod eh02 {
426 use super::*;
427
428 impl<'d, T: Pin> embedded_hal_02::digital::v2::InputPin for Input<'d, T> {
429 type Error = Infallible;
430
431 fn is_high(&self) -> Result<bool, Self::Error> {
432 Ok(self.is_high())
433 }
434
435 fn is_low(&self) -> Result<bool, Self::Error> {
436 Ok(self.is_low())
437 }
438 }
439
440 impl<'d, T: Pin> embedded_hal_02::digital::v2::OutputPin for Output<'d, T> {
441 type Error = Infallible;
442
443 fn set_high(&mut self) -> Result<(), Self::Error> {
444 Ok(self.set_high())
445 }
446
447 fn set_low(&mut self) -> Result<(), Self::Error> {
448 Ok(self.set_low())
449 }
450 }
451
452 impl<'d, T: Pin> embedded_hal_02::digital::v2::StatefulOutputPin for Output<'d, T> {
453 fn is_set_high(&self) -> Result<bool, Self::Error> {
454 Ok(self.is_set_high())
455 }
456
457 fn is_set_low(&self) -> Result<bool, Self::Error> {
458 Ok(self.is_set_low())
459 }
460 }
461
462 /// Implement [`InputPin`] for [`Flex`];
463 ///
464 /// If the pin is not in input mode the result is unspecified.
465 impl<'d, T: Pin> embedded_hal_02::digital::v2::InputPin for Flex<'d, T> {
466 type Error = Infallible;
467
468 fn is_high(&self) -> Result<bool, Self::Error> {
469 Ok(self.is_high())
470 }
471
472 fn is_low(&self) -> Result<bool, Self::Error> {
473 Ok(self.is_low())
474 }
475 }
476
477 impl<'d, T: Pin> embedded_hal_02::digital::v2::OutputPin for Flex<'d, T> {
478 type Error = Infallible;
479
480 fn set_high(&mut self) -> Result<(), Self::Error> {
481 Ok(self.set_high())
482 }
483
484 fn set_low(&mut self) -> Result<(), Self::Error> {
485 Ok(self.set_low())
486 }
487 }
488
489 impl<'d, T: Pin> embedded_hal_02::digital::v2::StatefulOutputPin for Flex<'d, T> {
490 fn is_set_high(&self) -> Result<bool, Self::Error> {
491 Ok(self.is_set_high())
492 }
493
494 fn is_set_low(&self) -> Result<bool, Self::Error> {
495 Ok(self.is_set_low())
496 }
497 }
498}
499
500#[cfg(feature = "unstable-traits")]
501mod eh1 {
502 use super::*;
503
504 impl<'d, T: Pin> embedded_hal_1::digital::ErrorType for Input<'d, T> {
505 type Error = Infallible;
506 }
507
508 impl<'d, T: Pin> embedded_hal_1::digital::blocking::InputPin for Input<'d, T> {
509 fn is_high(&self) -> Result<bool, Self::Error> {
510 Ok(self.is_high())
511 }
512
513 fn is_low(&self) -> Result<bool, Self::Error> {
514 Ok(self.is_low())
515 }
516 }
517
518 impl<'d, T: Pin> embedded_hal_1::digital::ErrorType for Output<'d, T> {
519 type Error = Infallible;
520 }
521
522 impl<'d, T: Pin> embedded_hal_1::digital::blocking::OutputPin for Output<'d, T> {
523 fn set_high(&mut self) -> Result<(), Self::Error> {
524 Ok(self.set_high())
525 }
526
527 fn set_low(&mut self) -> Result<(), Self::Error> {
528 Ok(self.set_low())
529 }
530 }
531
532 impl<'d, T: Pin> embedded_hal_1::digital::blocking::StatefulOutputPin for Output<'d, T> {
533 fn is_set_high(&self) -> Result<bool, Self::Error> {
534 Ok(self.is_set_high())
535 }
536
537 fn is_set_low(&self) -> Result<bool, Self::Error> {
538 Ok(self.is_set_low())
539 }
540 }
541
542 impl<'d, T: Pin> embedded_hal_1::digital::ErrorType for Flex<'d, T> {
543 type Error = Infallible;
544 }
545
546 /// Implement [`InputPin`] for [`Flex`];
547 ///
548 /// If the pin is not in input mode the result is unspecified.
549 impl<'d, T: Pin> embedded_hal_1::digital::blocking::InputPin for Flex<'d, T> {
550 fn is_high(&self) -> Result<bool, Self::Error> {
551 Ok(self.is_high())
552 }
553
554 fn is_low(&self) -> Result<bool, Self::Error> {
555 Ok(self.is_low())
556 }
557 }
558
559 impl<'d, T: Pin> embedded_hal_1::digital::blocking::OutputPin for Flex<'d, T> {
560 fn set_high(&mut self) -> Result<(), Self::Error> {
561 Ok(self.set_high())
562 }
563
564 fn set_low(&mut self) -> Result<(), Self::Error> {
565 Ok(self.set_low())
566 }
567 }
568
569 impl<'d, T: Pin> embedded_hal_1::digital::blocking::StatefulOutputPin for Flex<'d, T> {
570 fn is_set_high(&self) -> Result<bool, Self::Error> {
571 Ok(self.is_set_high())
572 }
573
574 fn is_set_low(&self) -> Result<bool, Self::Error> {
575 Ok(self.is_set_low())
576 }
577 }
578}