1
1
//! General Purpose Input / Output
2
2
3
3
use core:: marker:: PhantomData ;
4
+ use core:: convert:: Infallible ;
4
5
5
6
use crate :: rcc:: Rcc ;
6
7
@@ -61,7 +62,7 @@ pub struct Output<MODE> {
61
62
/// Push pull output (type state)
62
63
pub struct PushPull ;
63
64
64
- use embedded_hal:: digital:: { toggleable, InputPin , OutputPin , StatefulOutputPin } ;
65
+ use embedded_hal:: digital:: v2 :: { toggleable, InputPin , OutputPin , StatefulOutputPin } ;
65
66
66
67
/// Fully erased pin
67
68
pub struct Pin < MODE > {
@@ -78,51 +79,57 @@ unsafe impl<MODE> Send for Pin<MODE> {}
78
79
79
80
impl < MODE > StatefulOutputPin for Pin < Output < MODE > > {
80
81
#[ inline( always) ]
81
- fn is_set_high ( & self ) -> bool {
82
- ! self . is_set_low ( )
82
+ fn is_set_high ( & self ) -> Result < bool , Self :: Error > {
83
+ self . is_set_low ( ) . map ( |v| !v )
83
84
}
84
85
85
86
#[ inline( always) ]
86
- fn is_set_low ( & self ) -> bool {
87
- unsafe { ( * self . port ) . is_set_low ( self . i ) }
87
+ fn is_set_low ( & self ) -> Result < bool , Self :: Error > {
88
+ Ok ( unsafe { ( * self . port ) . is_set_low ( self . i ) } )
88
89
}
89
90
}
90
91
91
92
impl < MODE > OutputPin for Pin < Output < MODE > > {
93
+ type Error = Infallible ;
94
+
92
95
#[ inline( always) ]
93
- fn set_high ( & mut self ) {
94
- unsafe { ( * self . port ) . set_high ( self . i ) }
96
+ fn set_high ( & mut self ) -> Result < ( ) , Self :: Error > {
97
+ Ok ( unsafe { ( * self . port ) . set_high ( self . i ) } )
95
98
}
96
99
97
100
#[ inline( always) ]
98
- fn set_low ( & mut self ) {
99
- unsafe { ( * self . port ) . set_low ( self . i ) }
101
+ fn set_low ( & mut self ) -> Result < ( ) , Self :: Error > {
102
+ Ok ( unsafe { ( * self . port ) . set_low ( self . i ) } )
100
103
}
101
104
}
102
105
103
106
impl < MODE > toggleable:: Default for Pin < Output < MODE > > { }
104
107
105
108
impl InputPin for Pin < Output < OpenDrain > > {
109
+ type Error = Infallible ;
110
+
106
111
#[ inline( always) ]
107
- fn is_high ( & self ) -> bool {
108
- ! self . is_low ( )
112
+ fn is_high ( & self ) -> Result < bool , Self :: Error > {
113
+ self . is_low ( ) . map ( |v| !v )
109
114
}
110
115
111
116
#[ inline( always) ]
112
- fn is_low ( & self ) -> bool {
113
- unsafe { ( * self . port ) . is_low ( self . i ) }
117
+ fn is_low ( & self ) -> Result < bool , Self :: Error > {
118
+ Ok ( unsafe { ( * self . port ) . is_low ( self . i ) } )
114
119
}
115
120
}
116
121
117
122
impl < MODE > InputPin for Pin < Input < MODE > > {
123
+ type Error = Infallible ;
124
+
118
125
#[ inline( always) ]
119
- fn is_high ( & self ) -> bool {
120
- ! self . is_low ( )
126
+ fn is_high ( & self ) -> Result < bool , Self :: Error > {
127
+ self . is_low ( ) . map ( |v| !v )
121
128
}
122
129
123
130
#[ inline( always) ]
124
- fn is_low ( & self ) -> bool {
125
- unsafe { ( * self . port ) . is_low ( self . i ) }
131
+ fn is_low ( & self ) -> Result < bool , Self :: Error > {
132
+ Ok ( unsafe { ( * self . port ) . is_low ( self . i ) } )
126
133
}
127
134
}
128
135
@@ -162,8 +169,9 @@ macro_rules! gpio {
162
169
/// GPIO
163
170
pub mod $gpiox {
164
171
use core:: marker:: PhantomData ;
172
+ use core:: convert:: Infallible ;
165
173
166
- use embedded_hal:: digital:: { InputPin , OutputPin , StatefulOutputPin , toggleable} ;
174
+ use embedded_hal:: digital:: v2 :: { InputPin , OutputPin , StatefulOutputPin , toggleable} ;
167
175
use crate :: {
168
176
rcc:: Rcc ,
169
177
stm32:: $GPIOX
@@ -483,34 +491,38 @@ macro_rules! gpio {
483
491
}
484
492
485
493
impl <MODE > StatefulOutputPin for $PXi<Output <MODE >> {
486
- fn is_set_high( & self ) -> bool {
487
- ! self . is_set_low( )
494
+ fn is_set_high( & self ) -> Result < bool , Self :: Error > {
495
+ self . is_set_low( ) . map ( |v| !v )
488
496
}
489
497
490
- fn is_set_low( & self ) -> bool {
491
- unsafe { ( * $GPIOX:: ptr( ) ) . is_set_low( $i) }
498
+ fn is_set_low( & self ) -> Result < bool , Self :: Error > {
499
+ Ok ( unsafe { ( * $GPIOX:: ptr( ) ) . is_set_low( $i) } )
492
500
}
493
501
}
494
502
495
503
impl <MODE > OutputPin for $PXi<Output <MODE >> {
496
- fn set_high( & mut self ) {
497
- unsafe { ( * $GPIOX:: ptr( ) ) . set_high( $i) }
504
+ type Error = Infallible ;
505
+
506
+ fn set_high( & mut self ) -> Result <( ) , Self :: Error > {
507
+ Ok ( unsafe { ( * $GPIOX:: ptr( ) ) . set_high( $i) } )
498
508
}
499
509
500
- fn set_low( & mut self ) {
501
- unsafe { ( * $GPIOX:: ptr( ) ) . set_low( $i) }
510
+ fn set_low( & mut self ) -> Result < ( ) , Self :: Error > {
511
+ Ok ( unsafe { ( * $GPIOX:: ptr( ) ) . set_low( $i) } )
502
512
}
503
513
}
504
514
505
515
impl <MODE > toggleable:: Default for $PXi<Output <MODE >> { }
506
516
507
517
impl InputPin for $PXi<Output <OpenDrain >> {
508
- fn is_high( & self ) -> bool {
509
- !self . is_low( )
518
+ type Error = Infallible ;
519
+
520
+ fn is_high( & self ) -> Result <bool , Self :: Error > {
521
+ self . is_low( ) . map( |v| !v)
510
522
}
511
523
512
- fn is_low( & self ) -> bool {
513
- unsafe { ( * $GPIOX:: ptr( ) ) . is_low( $i) }
524
+ fn is_low( & self ) -> Result < bool , Self :: Error > {
525
+ Ok ( unsafe { ( * $GPIOX:: ptr( ) ) . is_low( $i) } )
514
526
}
515
527
}
516
528
@@ -529,12 +541,14 @@ macro_rules! gpio {
529
541
}
530
542
531
543
impl <MODE > InputPin for $PXi<Input <MODE >> {
532
- fn is_high( & self ) -> bool {
533
- !self . is_low( )
544
+ type Error = Infallible ;
545
+
546
+ fn is_high( & self ) -> Result <bool , Self :: Error > {
547
+ self . is_low( ) . map( |v| !v)
534
548
}
535
549
536
- fn is_low( & self ) -> bool {
537
- unsafe { ( * $GPIOX:: ptr( ) ) . is_low( $i) }
550
+ fn is_low( & self ) -> Result < bool , Self :: Error > {
551
+ Ok ( unsafe { ( * $GPIOX:: ptr( ) ) . is_low( $i) } )
538
552
}
539
553
}
540
554
) +
0 commit comments