1- use core:: marker:: PhantomData ;
1+ use core:: { convert :: Infallible , marker:: PhantomData } ;
22
33/// Disconnected pin in input mode (type state, reset value).
44pub struct Disconnected ;
@@ -85,7 +85,7 @@ use crate::pac::P1;
8585#[ cfg( feature = "5340-net" ) ]
8686use crate :: pac:: P1_NS as P1 ;
8787
88- use crate :: hal :: digital:: v2 :: { InputPin , OutputPin , StatefulOutputPin } ;
88+ use embedded_hal :: digital:: { ErrorType , InputPin , OutputPin , StatefulOutputPin } ;
8989use void:: Void ;
9090
9191impl < MODE > Pin < MODE > {
@@ -348,7 +348,63 @@ impl<MODE> Pin<MODE> {
348348 }
349349}
350350
351+ impl < MODE > ErrorType for Pin < MODE > {
352+ type Error = Infallible ;
353+ }
354+
351355impl < 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 > > {
352408 type Error = Void ;
353409
354410 fn is_high ( & self ) -> Result < bool , Self :: Error > {
@@ -360,7 +416,7 @@ impl<MODE> InputPin for Pin<Input<MODE>> {
360416 }
361417}
362418
363- impl InputPin for Pin < Output < OpenDrainIO > > {
419+ impl embedded_hal_02 :: digital :: v2 :: InputPin for Pin < Output < OpenDrainIO > > {
364420 type Error = Void ;
365421
366422 fn is_high ( & self ) -> Result < bool , Self :: Error > {
@@ -372,7 +428,7 @@ impl InputPin for Pin<Output<OpenDrainIO>> {
372428 }
373429}
374430
375- impl < MODE > OutputPin for Pin < Output < MODE > > {
431+ impl < MODE > embedded_hal_02 :: digital :: v2 :: OutputPin for Pin < Output < MODE > > {
376432 type Error = Void ;
377433
378434 /// Set the output as high.
@@ -396,7 +452,7 @@ impl<MODE> OutputPin for Pin<Output<MODE>> {
396452 }
397453}
398454
399- impl < MODE > StatefulOutputPin for Pin < Output < MODE > > {
455+ impl < MODE > embedded_hal_02 :: digital :: v2 :: StatefulOutputPin for Pin < Output < MODE > > {
400456 /// Is the output pin set as high?
401457 fn is_set_high ( & self ) -> Result < bool , Self :: Error > {
402458 self . is_set_low ( ) . map ( |v| !v)
@@ -482,10 +538,10 @@ macro_rules! gpio {
482538 $PX
483539 } ;
484540
485- use crate :: hal:: digital:: v2:: { OutputPin , StatefulOutputPin , InputPin } ;
541+ use core:: convert:: Infallible ;
542+ use embedded_hal:: digital:: { ErrorType , InputPin , OutputPin , StatefulOutputPin } ;
486543 use void:: Void ;
487544
488-
489545 // ===============================================================
490546 // This chunk allows you to obtain an nrf-hal gpio from the
491547 // upstream nrf52 gpio definitions by defining a trait
@@ -695,7 +751,58 @@ macro_rules! gpio {
695751 }
696752 }
697753
754+ impl <MODE > ErrorType for $PXi<MODE > {
755+ type Error = Infallible ;
756+ }
757+
698758 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 >> {
699806 type Error = Void ;
700807
701808 fn is_high( & self ) -> Result <bool , Self :: Error > {
@@ -707,7 +814,7 @@ macro_rules! gpio {
707814 }
708815 }
709816
710- impl InputPin for $PXi<Output <OpenDrainIO >> {
817+ impl embedded_hal_02 :: digital :: v2 :: InputPin for $PXi<Output <OpenDrainIO >> {
711818 type Error = Void ;
712819
713820 fn is_high( & self ) -> Result <bool , Self :: Error > {
@@ -725,7 +832,7 @@ macro_rules! gpio {
725832 }
726833 }
727834
728- impl <MODE > OutputPin for $PXi<Output <MODE >> {
835+ impl <MODE > embedded_hal_02 :: digital :: v2 :: OutputPin for $PXi<Output <MODE >> {
729836 type Error = Void ;
730837
731838 /// Set the output as high
@@ -745,7 +852,7 @@ macro_rules! gpio {
745852 }
746853 }
747854
748- impl <MODE > StatefulOutputPin for $PXi<Output <MODE >> {
855+ impl <MODE > embedded_hal_02 :: digital :: v2 :: StatefulOutputPin for $PXi<Output <MODE >> {
749856 /// Is the output pin set as high?
750857 fn is_set_high( & self ) -> Result <bool , Self :: Error > {
751858 self . is_set_low( ) . map( |v| !v)
0 commit comments