1
- use core:: marker:: PhantomData ;
1
+ use core:: { convert :: Infallible , marker:: PhantomData } ;
2
2
3
3
/// Disconnected pin in input mode (type state, reset value).
4
4
pub struct Disconnected ;
@@ -85,7 +85,7 @@ use crate::pac::P1;
85
85
#[ cfg( feature = "5340-net" ) ]
86
86
use crate :: pac:: P1_NS as P1 ;
87
87
88
- use crate :: hal :: digital:: v2 :: { InputPin , OutputPin , StatefulOutputPin } ;
88
+ use embedded_hal :: digital:: { ErrorType , InputPin , OutputPin , StatefulOutputPin } ;
89
89
use void:: Void ;
90
90
91
91
impl < MODE > Pin < MODE > {
@@ -348,7 +348,63 @@ impl<MODE> Pin<MODE> {
348
348
}
349
349
}
350
350
351
+ impl < MODE > ErrorType for Pin < MODE > {
352
+ type Error = Infallible ;
353
+ }
354
+
351
355
impl < MODE > InputPin for Pin < Input < MODE > > {
356
+ fn is_high ( & mut self ) -> Result < bool , Self :: Error > {
357
+ self . is_low ( ) . map ( |v| !v)
358
+ }
359
+
360
+ fn is_low ( & mut self ) -> Result < bool , Self :: Error > {
361
+ Ok ( self . block ( ) . in_ . read ( ) . bits ( ) & ( 1 << self . pin ( ) ) == 0 )
362
+ }
363
+ }
364
+
365
+ impl InputPin for Pin < Output < OpenDrainIO > > {
366
+ fn is_high ( & mut self ) -> Result < bool , Self :: Error > {
367
+ self . is_low ( ) . map ( |v| !v)
368
+ }
369
+
370
+ fn is_low ( & mut self ) -> Result < bool , Self :: Error > {
371
+ Ok ( self . block ( ) . in_ . read ( ) . bits ( ) & ( 1 << self . pin ( ) ) == 0 )
372
+ }
373
+ }
374
+
375
+ impl < MODE > OutputPin for Pin < Output < MODE > > {
376
+ fn set_high ( & mut self ) -> Result < ( ) , Self :: Error > {
377
+ // NOTE(unsafe) atomic write to a stateless register - TODO(AJM) verify?
378
+ // TODO - I wish I could do something like `.pins$i()`...
379
+ unsafe {
380
+ self . block ( ) . outset . write ( |w| w. bits ( 1u32 << self . pin ( ) ) ) ;
381
+ }
382
+ Ok ( ( ) )
383
+ }
384
+
385
+ fn set_low ( & mut self ) -> Result < ( ) , Self :: Error > {
386
+ // NOTE(unsafe) atomic write to a stateless register - TODO(AJM) verify?
387
+ // TODO - I wish I could do something like `.pins$i()`...
388
+ unsafe {
389
+ self . block ( ) . outclr . write ( |w| w. bits ( 1u32 << self . pin ( ) ) ) ;
390
+ }
391
+ Ok ( ( ) )
392
+ }
393
+ }
394
+
395
+ impl < MODE > StatefulOutputPin for Pin < Output < MODE > > {
396
+ fn is_set_high ( & mut self ) -> Result < bool , Self :: Error > {
397
+ self . is_set_low ( ) . map ( |v| !v)
398
+ }
399
+
400
+ fn is_set_low ( & mut self ) -> Result < bool , Self :: Error > {
401
+ // NOTE(unsafe) atomic read with no side effects - TODO(AJM) verify?
402
+ // TODO - I wish I could do something like `.pins$i()`...
403
+ Ok ( self . block ( ) . out . read ( ) . bits ( ) & ( 1 << self . pin ( ) ) == 0 )
404
+ }
405
+ }
406
+
407
+ impl < MODE > embedded_hal_02:: digital:: v2:: InputPin for Pin < Input < MODE > > {
352
408
type Error = Void ;
353
409
354
410
fn is_high ( & self ) -> Result < bool , Self :: Error > {
@@ -360,7 +416,7 @@ impl<MODE> InputPin for Pin<Input<MODE>> {
360
416
}
361
417
}
362
418
363
- impl InputPin for Pin < Output < OpenDrainIO > > {
419
+ impl embedded_hal_02 :: digital :: v2 :: InputPin for Pin < Output < OpenDrainIO > > {
364
420
type Error = Void ;
365
421
366
422
fn is_high ( & self ) -> Result < bool , Self :: Error > {
@@ -372,7 +428,7 @@ impl InputPin for Pin<Output<OpenDrainIO>> {
372
428
}
373
429
}
374
430
375
- impl < MODE > OutputPin for Pin < Output < MODE > > {
431
+ impl < MODE > embedded_hal_02 :: digital :: v2 :: OutputPin for Pin < Output < MODE > > {
376
432
type Error = Void ;
377
433
378
434
/// Set the output as high.
@@ -396,7 +452,7 @@ impl<MODE> OutputPin for Pin<Output<MODE>> {
396
452
}
397
453
}
398
454
399
- impl < MODE > StatefulOutputPin for Pin < Output < MODE > > {
455
+ impl < MODE > embedded_hal_02 :: digital :: v2 :: StatefulOutputPin for Pin < Output < MODE > > {
400
456
/// Is the output pin set as high?
401
457
fn is_set_high ( & self ) -> Result < bool , Self :: Error > {
402
458
self . is_set_low ( ) . map ( |v| !v)
@@ -482,10 +538,10 @@ macro_rules! gpio {
482
538
$PX
483
539
} ;
484
540
485
- use crate :: hal:: digital:: v2:: { OutputPin , StatefulOutputPin , InputPin } ;
541
+ use core:: convert:: Infallible ;
542
+ use embedded_hal:: digital:: { ErrorType , InputPin , OutputPin , StatefulOutputPin } ;
486
543
use void:: Void ;
487
544
488
-
489
545
// ===============================================================
490
546
// This chunk allows you to obtain an nrf-hal gpio from the
491
547
// upstream nrf52 gpio definitions by defining a trait
@@ -695,7 +751,58 @@ macro_rules! gpio {
695
751
}
696
752
}
697
753
754
+ impl <MODE > ErrorType for $PXi<MODE > {
755
+ type Error = Infallible ;
756
+ }
757
+
698
758
impl <MODE > InputPin for $PXi<Input <MODE >> {
759
+ fn is_high( & mut self ) -> Result <bool , Self :: Error > {
760
+ self . is_low( ) . map( |v| !v)
761
+ }
762
+
763
+ fn is_low( & mut self ) -> Result <bool , Self :: Error > {
764
+ Ok ( unsafe { ( ( * $PX:: ptr( ) ) . in_. read( ) . bits( ) & ( 1 << $i) ) == 0 } )
765
+ }
766
+ }
767
+
768
+ impl InputPin for $PXi<Output <OpenDrainIO >> {
769
+ fn is_high( & mut self ) -> Result <bool , Self :: Error > {
770
+ self . is_low( ) . map( |v| !v)
771
+ }
772
+
773
+ fn is_low( & mut self ) -> Result <bool , Self :: Error > {
774
+ Ok ( unsafe { ( ( * $PX:: ptr( ) ) . in_. read( ) . bits( ) & ( 1 << $i) ) == 0 } )
775
+ }
776
+ }
777
+ impl <MODE > OutputPin for $PXi<Output <MODE >> {
778
+ fn set_high( & mut self ) -> Result <( ) , Self :: Error > {
779
+ // NOTE(unsafe) atomic write to a stateless register - TODO(AJM) verify?
780
+ // TODO - I wish I could do something like `.pins$i()`...
781
+ unsafe { ( * $PX:: ptr( ) ) . outset. write( |w| w. bits( 1u32 << $i) ) ; }
782
+ Ok ( ( ) )
783
+ }
784
+
785
+ fn set_low( & mut self ) -> Result <( ) , Self :: Error > {
786
+ // NOTE(unsafe) atomic write to a stateless register - TODO(AJM) verify?
787
+ // TODO - I wish I could do something like `.pins$i()`...
788
+ unsafe { ( * $PX:: ptr( ) ) . outclr. write( |w| w. bits( 1u32 << $i) ) ; }
789
+ Ok ( ( ) )
790
+ }
791
+ }
792
+
793
+ impl <MODE > StatefulOutputPin for $PXi<Output <MODE >> {
794
+ fn is_set_high( & mut self ) -> Result <bool , Self :: Error > {
795
+ self . is_set_low( ) . map( |v| !v)
796
+ }
797
+
798
+ fn is_set_low( & mut self ) -> Result <bool , Self :: Error > {
799
+ // NOTE(unsafe) atomic read with no side effects - TODO(AJM) verify?
800
+ // TODO - I wish I could do something like `.pins$i()`...
801
+ Ok ( unsafe { ( ( * $PX:: ptr( ) ) . out. read( ) . bits( ) & ( 1 << $i) ) == 0 } )
802
+ }
803
+ }
804
+
805
+ impl <MODE > embedded_hal_02:: digital:: v2:: InputPin for $PXi<Input <MODE >> {
699
806
type Error = Void ;
700
807
701
808
fn is_high( & self ) -> Result <bool , Self :: Error > {
@@ -707,7 +814,7 @@ macro_rules! gpio {
707
814
}
708
815
}
709
816
710
- impl InputPin for $PXi<Output <OpenDrainIO >> {
817
+ impl embedded_hal_02 :: digital :: v2 :: InputPin for $PXi<Output <OpenDrainIO >> {
711
818
type Error = Void ;
712
819
713
820
fn is_high( & self ) -> Result <bool , Self :: Error > {
@@ -725,7 +832,7 @@ macro_rules! gpio {
725
832
}
726
833
}
727
834
728
- impl <MODE > OutputPin for $PXi<Output <MODE >> {
835
+ impl <MODE > embedded_hal_02 :: digital :: v2 :: OutputPin for $PXi<Output <MODE >> {
729
836
type Error = Void ;
730
837
731
838
/// Set the output as high
@@ -745,7 +852,7 @@ macro_rules! gpio {
745
852
}
746
853
}
747
854
748
- impl <MODE > StatefulOutputPin for $PXi<Output <MODE >> {
855
+ impl <MODE > embedded_hal_02 :: digital :: v2 :: StatefulOutputPin for $PXi<Output <MODE >> {
749
856
/// Is the output pin set as high?
750
857
fn is_set_high( & self ) -> Result <bool , Self :: Error > {
751
858
self . is_set_low( ) . map( |v| !v)
0 commit comments