Skip to content

Commit 1b25dc5

Browse files
authored
Merge pull request #58 from jonas-schievink/gpio-fixes
GPIO cleanup and fixes
2 parents 3d08dbd + 8b72915 commit 1b25dc5

File tree

4 files changed

+57
-57
lines changed

4 files changed

+57
-57
lines changed

examples/button_irq.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn main() -> ! {
4343

4444
// Configure the external interrupt on the falling edge for the pin 0.
4545
let exti = dp.EXTI;
46-
exti.listen(&mut syscfg, button.port, button.i, TriggerEdge::Falling);
46+
exti.listen(&mut syscfg, button.port(), button.pin_number(), TriggerEdge::Falling);
4747

4848
// Store the external interrupt and LED in mutex reffcells to make them
4949
// available from the interrupt.

examples/button_irq_rtfm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const APP: () = {
3232

3333
// Configure the external interrupt on the falling edge for the pin 0.
3434
let exti = device.EXTI;
35-
exti.listen(&mut syscfg, button.port, button.i, TriggerEdge::Falling);
35+
exti.listen(&mut syscfg, button.port(), button.pin_number(), TriggerEdge::Falling);
3636

3737
// Return the initialised resources.
3838
init::LateResources {

examples/exti_wakeup.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ fn main() -> ! {
3333

3434
exti.listen(
3535
&mut syscfg,
36-
button.port,
37-
button.i,
36+
button.port(),
37+
button.pin_number(),
3838
exti::TriggerEdge::Falling,
3939
);
4040

4141
loop {
4242
exti.wait_for_irq(
43-
button.i,
43+
button.pin_number(),
4444
pwr.stop_mode(
4545
&mut scb,
4646
&mut rcc,

src/gpio.rs

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub struct Output<MODE> {
4242
pub struct PushPull;
4343

4444
/// GPIO Pin speed selection
45+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
4546
pub enum Speed {
4647
Low = 0,
4748
Medium = 1,
@@ -62,14 +63,14 @@ pub(crate) enum AltMode {
6263
}
6364

6465
#[cfg(feature = "stm32l0x1")]
65-
#[derive(Copy, Clone)]
66+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
6667
pub enum Port {
6768
PA,
6869
PB,
6970
}
7071

7172
#[cfg(any(feature = "stm32l0x2", feature = "stm32l0x3"))]
72-
#[derive(Copy, Clone)]
73+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
7374
pub enum Port {
7475
PA,
7576
PB,
@@ -79,11 +80,6 @@ pub enum Port {
7980
PH,
8081
}
8182

82-
#[derive(Debug)]
83-
pub enum Error {
84-
Foo,
85-
}
86-
8783
macro_rules! gpio {
8884
($GPIOX:ident, $gpiox:ident, $iopxenr:ident, $PXx:ident, [
8985
$($PXi:ident: ($pxi:ident, $i:expr, $MODE:ty),)+
@@ -117,9 +113,7 @@ macro_rules! gpio {
117113
Parts {
118114
$(
119115
$pxi: $PXi {
120-
i: $i,
121-
port: Port::$PXx,
122-
_mode: PhantomData
116+
_mode: PhantomData,
123117
},
124118
)+
125119
}
@@ -128,34 +122,45 @@ macro_rules! gpio {
128122

129123
/// Partially erased pin
130124
pub struct $PXx<MODE> {
131-
pub i: u8,
132-
pub port: Port,
125+
i: u8,
133126
_mode: PhantomData<MODE>,
134127
}
135128

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+
136141
impl<MODE> OutputPin for $PXx<Output<MODE>> {
137-
type Error = ();
142+
type Error = void::Void;
138143

139-
fn set_high(&mut self) -> Result<(), ()> {
144+
fn set_high(&mut self) -> Result<(), Self::Error> {
140145
// NOTE(unsafe) atomic write to a stateless register
141146
unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << self.i)) };
142147
Ok(())
143148
}
144149

145-
fn set_low(&mut self) -> Result<(), ()> {
150+
fn set_low(&mut self) -> Result<(), Self::Error> {
146151
// NOTE(unsafe) atomic write to a stateless register
147152
unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << (self.i + 16))) };
148153
Ok(())
149154
}
150155
}
151156

152157
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> {
154159
let is_high = self.is_set_low()?;
155160
Ok(is_high)
156161
}
157162

158-
fn is_set_low(&self) -> Result<bool, ()> {
163+
fn is_set_low(&self) -> Result<bool, Self::Error> {
159164
// NOTE(unsafe) atomic read with no side effects
160165
let is_low = unsafe { (*$GPIOX::ptr()).odr.read().bits() & (1 << self.i) == 0 };
161166
Ok(is_low)
@@ -165,29 +170,29 @@ macro_rules! gpio {
165170
impl<MODE> toggleable::Default for $PXx<Output<MODE>> {}
166171

167172
impl<MODE> InputPin for $PXx<Output<MODE>> {
168-
type Error = ();
173+
type Error = void::Void;
169174

170-
fn is_high(&self) -> Result<bool, ()> {
175+
fn is_high(&self) -> Result<bool, Self::Error> {
171176
let is_high = !self.is_low()?;
172177
Ok(is_high)
173178
}
174179

175-
fn is_low(&self) -> Result<bool, ()> {
180+
fn is_low(&self) -> Result<bool, Self::Error> {
176181
// NOTE(unsafe) atomic read with no side effects
177182
let is_low = unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << self.i) == 0 };
178183
Ok(is_low)
179184
}
180185
}
181186

182187
impl<MODE> InputPin for $PXx<Input<MODE>> {
183-
type Error = ();
188+
type Error = void::Void;
184189

185-
fn is_high(&self) -> Result<bool, ()> {
190+
fn is_high(&self) -> Result<bool, Self::Error> {
186191
let is_high = !self.is_low()?;
187192
Ok(is_high)
188193
}
189194

190-
fn is_low(&self) -> Result<bool, ()> {
195+
fn is_low(&self) -> Result<bool, Self::Error> {
191196
// NOTE(unsafe) atomic read with no side effects
192197
let is_low = unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << self.i) == 0 };
193198
Ok(is_low)
@@ -197,11 +202,21 @@ macro_rules! gpio {
197202
$(
198203
/// Pin
199204
pub struct $PXi<MODE> {
200-
pub i: u8,
201-
pub port: Port,
202205
_mode: PhantomData<MODE>,
203206
}
204207

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+
205220
impl<MODE> $PXi<MODE> {
206221
/// Configures the pin to operate as a floating input pin
207222
pub fn into_floating_input(
@@ -217,8 +232,6 @@ macro_rules! gpio {
217232
})
218233
};
219234
$PXi {
220-
i: $i,
221-
port: Port::$PXx,
222235
_mode: PhantomData
223236
}
224237
}
@@ -237,8 +250,6 @@ macro_rules! gpio {
237250
})
238251
};
239252
$PXi {
240-
i: $i,
241-
port: Port::$PXx,
242253
_mode: PhantomData
243254
}
244255
}
@@ -257,8 +268,6 @@ macro_rules! gpio {
257268
})
258269
};
259270
$PXi {
260-
i: $i,
261-
port: Port::$PXx,
262271
_mode: PhantomData
263272
}
264273
}
@@ -277,8 +286,6 @@ macro_rules! gpio {
277286
});
278287
}
279288
$PXi {
280-
i: $i,
281-
port: Port::$PXx,
282289
_mode: PhantomData
283290
}
284291
}
@@ -300,8 +307,6 @@ macro_rules! gpio {
300307
})
301308
};
302309
$PXi {
303-
i: $i,
304-
port: Port::$PXx,
305310
_mode: PhantomData
306311
}
307312
}
@@ -323,8 +328,6 @@ macro_rules! gpio {
323328
})
324329
};
325330
$PXi {
326-
i: $i,
327-
port: Port::$PXx,
328331
_mode: PhantomData
329332
}
330333
}
@@ -371,22 +374,21 @@ macro_rules! gpio {
371374
pub fn downgrade(self) -> $PXx<Output<MODE>> {
372375
$PXx {
373376
i: $i,
374-
port: Port::$PXx,
375377
_mode: self._mode,
376378
}
377379
}
378380
}
379381

380382
impl<MODE> OutputPin for $PXi<Output<MODE>> {
381-
type Error = ();
383+
type Error = void::Void;
382384

383-
fn set_high(&mut self) -> Result<(), ()> {
385+
fn set_high(&mut self) -> Result<(), Self::Error> {
384386
// NOTE(unsafe) atomic write to a stateless register
385387
unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << $i)) };
386388
Ok(())
387389
}
388390

389-
fn set_low(&mut self) -> Result<(), ()> {
391+
fn set_low(&mut self) -> Result<(), Self::Error> {
390392
// NOTE(unsafe) atomic write to a stateless register
391393
unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << ($i + 16))) };
392394
Ok(())
@@ -395,12 +397,12 @@ macro_rules! gpio {
395397

396398
impl<MODE> StatefulOutputPin for $PXi<Output<MODE>> {
397399

398-
fn is_set_high(&self) -> Result<bool, ()> {
400+
fn is_set_high(&self) -> Result<bool, Self::Error> {
399401
let is_set_high = !self.is_set_low()?;
400402
Ok(is_set_high)
401403
}
402404

403-
fn is_set_low(&self) -> Result<bool, ()> {
405+
fn is_set_low(&self) -> Result<bool, Self::Error> {
404406
// NOTE(unsafe) atomic read with no side effects
405407
let is_set_low = unsafe { (*$GPIOX::ptr()).odr.read().bits() & (1 << $i) == 0 };
406408
Ok(is_set_low)
@@ -410,14 +412,14 @@ macro_rules! gpio {
410412
impl<MODE> toggleable::Default for $PXi<Output<MODE>> {}
411413

412414
impl<MODE> InputPin for $PXi<Output<MODE>> {
413-
type Error = ();
415+
type Error = void::Void;
414416

415-
fn is_high(&self) -> Result<bool, ()> {
417+
fn is_high(&self) -> Result<bool, Self::Error> {
416418
let is_high = !self.is_low()?;
417419
Ok(is_high)
418420
}
419421

420-
fn is_low(&self) -> Result<bool, ()> {
422+
fn is_low(&self) -> Result<bool, Self::Error> {
421423
// NOTE(unsafe) atomic read with no side effects
422424
let is_low = unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << $i) == 0 };
423425
Ok(is_low)
@@ -432,22 +434,20 @@ macro_rules! gpio {
432434
pub fn downgrade(self) -> $PXx<Input<MODE>> {
433435
$PXx {
434436
i: $i,
435-
port: Port::$PXx,
436437
_mode: self._mode,
437438
}
438439
}
439440
}
440441

441442
impl<MODE> InputPin for $PXi<Input<MODE>> {
443+
type Error = void::Void;
442444

443-
type Error = ();
444-
445-
fn is_high(&self) -> Result<bool, ()> {
445+
fn is_high(&self) -> Result<bool, Self::Error> {
446446
let is_high = !self.is_low()?;
447447
Ok(is_high)
448448
}
449449

450-
fn is_low(&self) -> Result<bool, ()> {
450+
fn is_low(&self) -> Result<bool, Self::Error> {
451451
// NOTE(unsafe) atomic read with no side effects
452452
let is_low = unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << $i) == 0 };
453453
Ok(is_low)
@@ -517,7 +517,7 @@ gpio!(GPIOC, gpioc, iopcen, PC, [
517517
]);
518518

519519
#[cfg(any(feature = "stm32l0x2", feature = "stm32l0x3"))]
520-
gpio!(GPIOD, gpiod, iopcen, PC, [
520+
gpio!(GPIOD, gpiod, iopden, PD, [
521521
PD0: (pd0, 0, Input<Floating>),
522522
PD1: (pd1, 1, Input<Floating>),
523523
PD2: (pd2, 2, Input<Floating>),

0 commit comments

Comments
 (0)