@@ -42,6 +42,7 @@ pub struct Output<MODE> {
42
42
pub struct PushPull ;
43
43
44
44
/// GPIO Pin speed selection
45
+ #[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
45
46
pub enum Speed {
46
47
Low = 0 ,
47
48
Medium = 1 ,
@@ -62,14 +63,14 @@ pub(crate) enum AltMode {
62
63
}
63
64
64
65
#[ cfg( feature = "stm32l0x1" ) ]
65
- #[ derive( Copy , Clone ) ]
66
+ #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
66
67
pub enum Port {
67
68
PA ,
68
69
PB ,
69
70
}
70
71
71
72
#[ cfg( any( feature = "stm32l0x2" , feature = "stm32l0x3" ) ) ]
72
- #[ derive( Copy , Clone ) ]
73
+ #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
73
74
pub enum Port {
74
75
PA ,
75
76
PB ,
@@ -79,11 +80,6 @@ pub enum Port {
79
80
PH ,
80
81
}
81
82
82
- #[ derive( Debug ) ]
83
- pub enum Error {
84
- Foo ,
85
- }
86
-
87
83
macro_rules! gpio {
88
84
( $GPIOX: ident, $gpiox: ident, $iopxenr: ident, $PXx: ident, [
89
85
$( $PXi: ident: ( $pxi: ident, $i: expr, $MODE: ty) , ) +
@@ -117,9 +113,7 @@ macro_rules! gpio {
117
113
Parts {
118
114
$(
119
115
$pxi: $PXi {
120
- i: $i,
121
- port: Port :: $PXx,
122
- _mode: PhantomData
116
+ _mode: PhantomData ,
123
117
} ,
124
118
) +
125
119
}
@@ -128,34 +122,45 @@ macro_rules! gpio {
128
122
129
123
/// Partially erased pin
130
124
pub struct $PXx<MODE > {
131
- pub i: u8 ,
132
- pub port: Port ,
125
+ i: u8 ,
133
126
_mode: PhantomData <MODE >,
134
127
}
135
128
129
+ impl <MODE > $PXx<MODE > {
130
+ /// Returns the port this pin is part of.
131
+ pub fn port( & self ) -> Port {
132
+ Port :: $PXx
133
+ }
134
+
135
+ /// Returns this pin's number inside its port.
136
+ pub fn pin_number( & self ) -> u8 {
137
+ self . i
138
+ }
139
+ }
140
+
136
141
impl <MODE > OutputPin for $PXx<Output <MODE >> {
137
- type Error = ( ) ;
142
+ type Error = void :: Void ;
138
143
139
- fn set_high( & mut self ) -> Result <( ) , ( ) > {
144
+ fn set_high( & mut self ) -> Result <( ) , Self :: Error > {
140
145
// NOTE(unsafe) atomic write to a stateless register
141
146
unsafe { ( * $GPIOX:: ptr( ) ) . bsrr. write( |w| w. bits( 1 << self . i) ) } ;
142
147
Ok ( ( ) )
143
148
}
144
149
145
- fn set_low( & mut self ) -> Result <( ) , ( ) > {
150
+ fn set_low( & mut self ) -> Result <( ) , Self :: Error > {
146
151
// NOTE(unsafe) atomic write to a stateless register
147
152
unsafe { ( * $GPIOX:: ptr( ) ) . bsrr. write( |w| w. bits( 1 << ( self . i + 16 ) ) ) } ;
148
153
Ok ( ( ) )
149
154
}
150
155
}
151
156
152
157
impl <MODE > StatefulOutputPin for $PXx<Output <MODE >> {
153
- fn is_set_high( & self ) -> Result <bool , ( ) > {
158
+ fn is_set_high( & self ) -> Result <bool , Self :: Error > {
154
159
let is_high = self . is_set_low( ) ?;
155
160
Ok ( is_high)
156
161
}
157
162
158
- fn is_set_low( & self ) -> Result <bool , ( ) > {
163
+ fn is_set_low( & self ) -> Result <bool , Self :: Error > {
159
164
// NOTE(unsafe) atomic read with no side effects
160
165
let is_low = unsafe { ( * $GPIOX:: ptr( ) ) . odr. read( ) . bits( ) & ( 1 << self . i) == 0 } ;
161
166
Ok ( is_low)
@@ -165,29 +170,29 @@ macro_rules! gpio {
165
170
impl <MODE > toggleable:: Default for $PXx<Output <MODE >> { }
166
171
167
172
impl <MODE > InputPin for $PXx<Output <MODE >> {
168
- type Error = ( ) ;
173
+ type Error = void :: Void ;
169
174
170
- fn is_high( & self ) -> Result <bool , ( ) > {
175
+ fn is_high( & self ) -> Result <bool , Self :: Error > {
171
176
let is_high = !self . is_low( ) ?;
172
177
Ok ( is_high)
173
178
}
174
179
175
- fn is_low( & self ) -> Result <bool , ( ) > {
180
+ fn is_low( & self ) -> Result <bool , Self :: Error > {
176
181
// NOTE(unsafe) atomic read with no side effects
177
182
let is_low = unsafe { ( * $GPIOX:: ptr( ) ) . idr. read( ) . bits( ) & ( 1 << self . i) == 0 } ;
178
183
Ok ( is_low)
179
184
}
180
185
}
181
186
182
187
impl <MODE > InputPin for $PXx<Input <MODE >> {
183
- type Error = ( ) ;
188
+ type Error = void :: Void ;
184
189
185
- fn is_high( & self ) -> Result <bool , ( ) > {
190
+ fn is_high( & self ) -> Result <bool , Self :: Error > {
186
191
let is_high = !self . is_low( ) ?;
187
192
Ok ( is_high)
188
193
}
189
194
190
- fn is_low( & self ) -> Result <bool , ( ) > {
195
+ fn is_low( & self ) -> Result <bool , Self :: Error > {
191
196
// NOTE(unsafe) atomic read with no side effects
192
197
let is_low = unsafe { ( * $GPIOX:: ptr( ) ) . idr. read( ) . bits( ) & ( 1 << self . i) == 0 } ;
193
198
Ok ( is_low)
@@ -197,11 +202,21 @@ macro_rules! gpio {
197
202
$(
198
203
/// Pin
199
204
pub struct $PXi<MODE > {
200
- pub i: u8 ,
201
- pub port: Port ,
202
205
_mode: PhantomData <MODE >,
203
206
}
204
207
208
+ impl <MODE > $PXi<MODE > {
209
+ /// Returns the port this pin is part of.
210
+ pub fn port( & self ) -> Port {
211
+ Port :: $PXx
212
+ }
213
+
214
+ /// Returns this pin's number inside its port.
215
+ pub fn pin_number( & self ) -> u8 {
216
+ $i
217
+ }
218
+ }
219
+
205
220
impl <MODE > $PXi<MODE > {
206
221
/// Configures the pin to operate as a floating input pin
207
222
pub fn into_floating_input(
@@ -217,8 +232,6 @@ macro_rules! gpio {
217
232
} )
218
233
} ;
219
234
$PXi {
220
- i: $i,
221
- port: Port :: $PXx,
222
235
_mode: PhantomData
223
236
}
224
237
}
@@ -237,8 +250,6 @@ macro_rules! gpio {
237
250
} )
238
251
} ;
239
252
$PXi {
240
- i: $i,
241
- port: Port :: $PXx,
242
253
_mode: PhantomData
243
254
}
244
255
}
@@ -257,8 +268,6 @@ macro_rules! gpio {
257
268
} )
258
269
} ;
259
270
$PXi {
260
- i: $i,
261
- port: Port :: $PXx,
262
271
_mode: PhantomData
263
272
}
264
273
}
@@ -277,8 +286,6 @@ macro_rules! gpio {
277
286
} ) ;
278
287
}
279
288
$PXi {
280
- i: $i,
281
- port: Port :: $PXx,
282
289
_mode: PhantomData
283
290
}
284
291
}
@@ -300,8 +307,6 @@ macro_rules! gpio {
300
307
} )
301
308
} ;
302
309
$PXi {
303
- i: $i,
304
- port: Port :: $PXx,
305
310
_mode: PhantomData
306
311
}
307
312
}
@@ -323,8 +328,6 @@ macro_rules! gpio {
323
328
} )
324
329
} ;
325
330
$PXi {
326
- i: $i,
327
- port: Port :: $PXx,
328
331
_mode: PhantomData
329
332
}
330
333
}
@@ -371,22 +374,21 @@ macro_rules! gpio {
371
374
pub fn downgrade( self ) -> $PXx<Output <MODE >> {
372
375
$PXx {
373
376
i: $i,
374
- port: Port :: $PXx,
375
377
_mode: self . _mode,
376
378
}
377
379
}
378
380
}
379
381
380
382
impl <MODE > OutputPin for $PXi<Output <MODE >> {
381
- type Error = ( ) ;
383
+ type Error = void :: Void ;
382
384
383
- fn set_high( & mut self ) -> Result <( ) , ( ) > {
385
+ fn set_high( & mut self ) -> Result <( ) , Self :: Error > {
384
386
// NOTE(unsafe) atomic write to a stateless register
385
387
unsafe { ( * $GPIOX:: ptr( ) ) . bsrr. write( |w| w. bits( 1 << $i) ) } ;
386
388
Ok ( ( ) )
387
389
}
388
390
389
- fn set_low( & mut self ) -> Result <( ) , ( ) > {
391
+ fn set_low( & mut self ) -> Result <( ) , Self :: Error > {
390
392
// NOTE(unsafe) atomic write to a stateless register
391
393
unsafe { ( * $GPIOX:: ptr( ) ) . bsrr. write( |w| w. bits( 1 << ( $i + 16 ) ) ) } ;
392
394
Ok ( ( ) )
@@ -395,12 +397,12 @@ macro_rules! gpio {
395
397
396
398
impl <MODE > StatefulOutputPin for $PXi<Output <MODE >> {
397
399
398
- fn is_set_high( & self ) -> Result <bool , ( ) > {
400
+ fn is_set_high( & self ) -> Result <bool , Self :: Error > {
399
401
let is_set_high = !self . is_set_low( ) ?;
400
402
Ok ( is_set_high)
401
403
}
402
404
403
- fn is_set_low( & self ) -> Result <bool , ( ) > {
405
+ fn is_set_low( & self ) -> Result <bool , Self :: Error > {
404
406
// NOTE(unsafe) atomic read with no side effects
405
407
let is_set_low = unsafe { ( * $GPIOX:: ptr( ) ) . odr. read( ) . bits( ) & ( 1 << $i) == 0 } ;
406
408
Ok ( is_set_low)
@@ -410,14 +412,14 @@ macro_rules! gpio {
410
412
impl <MODE > toggleable:: Default for $PXi<Output <MODE >> { }
411
413
412
414
impl <MODE > InputPin for $PXi<Output <MODE >> {
413
- type Error = ( ) ;
415
+ type Error = void :: Void ;
414
416
415
- fn is_high( & self ) -> Result <bool , ( ) > {
417
+ fn is_high( & self ) -> Result <bool , Self :: Error > {
416
418
let is_high = !self . is_low( ) ?;
417
419
Ok ( is_high)
418
420
}
419
421
420
- fn is_low( & self ) -> Result <bool , ( ) > {
422
+ fn is_low( & self ) -> Result <bool , Self :: Error > {
421
423
// NOTE(unsafe) atomic read with no side effects
422
424
let is_low = unsafe { ( * $GPIOX:: ptr( ) ) . idr. read( ) . bits( ) & ( 1 << $i) == 0 } ;
423
425
Ok ( is_low)
@@ -432,22 +434,20 @@ macro_rules! gpio {
432
434
pub fn downgrade( self ) -> $PXx<Input <MODE >> {
433
435
$PXx {
434
436
i: $i,
435
- port: Port :: $PXx,
436
437
_mode: self . _mode,
437
438
}
438
439
}
439
440
}
440
441
441
442
impl <MODE > InputPin for $PXi<Input <MODE >> {
443
+ type Error = void:: Void ;
442
444
443
- type Error = ( ) ;
444
-
445
- fn is_high( & self ) -> Result <bool , ( ) > {
445
+ fn is_high( & self ) -> Result <bool , Self :: Error > {
446
446
let is_high = !self . is_low( ) ?;
447
447
Ok ( is_high)
448
448
}
449
449
450
- fn is_low( & self ) -> Result <bool , ( ) > {
450
+ fn is_low( & self ) -> Result <bool , Self :: Error > {
451
451
// NOTE(unsafe) atomic read with no side effects
452
452
let is_low = unsafe { ( * $GPIOX:: ptr( ) ) . idr. read( ) . bits( ) & ( 1 << $i) == 0 } ;
453
453
Ok ( is_low)
@@ -517,7 +517,7 @@ gpio!(GPIOC, gpioc, iopcen, PC, [
517
517
] ) ;
518
518
519
519
#[ cfg( any( feature = "stm32l0x2" , feature = "stm32l0x3" ) ) ]
520
- gpio ! ( GPIOD , gpiod, iopcen , PC , [
520
+ gpio ! ( GPIOD , gpiod, iopden , PD , [
521
521
PD0 : ( pd0, 0 , Input <Floating >) ,
522
522
PD1 : ( pd1, 1 , Input <Floating >) ,
523
523
PD2 : ( pd2, 2 , Input <Floating >) ,
0 commit comments