Skip to content

Commit 2fc0ecf

Browse files
authored
Merge pull request #257 from Piroro-hs/reexport-pins
Define pin type aliases directly under gpio module
2 parents 3a9d480 + fb7ca26 commit 2fc0ecf

File tree

5 files changed

+120
-124
lines changed

5 files changed

+120
-124
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1616
### Changed
1717

1818
- `PXx` struct (representing a generic GPIO pin) implements `Send` and `Sync` [#251]
19+
- Each pin aliases (`PA0`, `PA1`, ..) are defined under `gpio` module directly.
20+
Re-export from gpio port sub-modules are provided for compatibility. [#257]
1921

2022
### Breaking Changes
2123

@@ -351,6 +353,7 @@ let clocks = rcc
351353
[defmt]: https://github.com/knurling-rs/defmt
352354
[filter]: https://defmt.ferrous-systems.com/filtering.html
353355

356+
[#257]: https://github.com/stm32-rs/stm32f3xx-hal/pull/257
354357
[#255]: https://github.com/stm32-rs/stm32f3xx-hal/pull/255
355358
[#252]: https://github.com/stm32-rs/stm32f3xx-hal/pull/252
356359
[#247]: https://github.com/stm32-rs/stm32f3xx-hal/pull/247

examples/gpio_interrupts.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
#![no_main]
22
#![no_std]
33

4-
use panic_semihosting as _;
4+
use core::cell::RefCell;
55

6-
use stm32f3xx_hal as hal;
6+
use panic_semihosting as _;
77

8-
use core::cell::RefCell;
9-
use cortex_m::asm;
10-
use cortex_m::interrupt::Mutex;
11-
use cortex_m::peripheral::NVIC;
8+
use cortex_m::{asm, interrupt::Mutex, peripheral::NVIC};
129
use cortex_m_rt::entry;
13-
use hal::gpio::{gpioa, gpioe, Edge, Input, Output, PushPull};
14-
use hal::interrupt;
15-
use hal::pac;
16-
use hal::prelude::*;
1710

18-
type LedPin = gpioe::PE9<Output<PushPull>>;
11+
use stm32f3xx_hal::{
12+
gpio::{self, Edge, Input, Output, PushPull},
13+
interrupt, pac,
14+
prelude::*,
15+
};
16+
17+
type LedPin = gpio::PE9<Output<PushPull>>;
1918
static LED: Mutex<RefCell<Option<LedPin>>> = Mutex::new(RefCell::new(None));
2019

21-
type ButtonPin = gpioa::PA0<Input>;
20+
type ButtonPin = gpio::PA0<Input>;
2221
static BUTTON: Mutex<RefCell<Option<ButtonPin>>> = Mutex::new(RefCell::new(None));
2322

2423
// When the user button is pressed. The north LED will toggle.

examples/serial_echo_rtic.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,16 @@ mod app {
99
use rtt_target::{rprintln, rtt_init_print};
1010
use stm32f3xx_hal::{
1111
gpio::{self, Output, PushPull, AF7},
12+
pac,
1213
prelude::*,
1314
serial::{Event, Serial},
1415
};
1516

1617
#[monotonic(binds = SysTick, default = true)]
1718
type DwtMono = DwtSystick<48_000_000>;
1819

19-
type SerialType = Serial<
20-
stm32f3xx_hal::pac::USART1,
21-
(
22-
gpio::gpioa::PA9<AF7<PushPull>>,
23-
gpio::gpioa::PA10<AF7<PushPull>>,
24-
),
25-
>;
26-
type DirType = stm32f3xx_hal::gpio::gpioe::PE13<Output<PushPull>>;
20+
type SerialType = Serial<pac::USART1, (gpio::PA9<AF7<PushPull>>, gpio::PA10<AF7<PushPull>>)>;
21+
type DirType = gpio::PE13<Output<PushPull>>;
2722

2823
#[shared]
2924
struct Shared {}

src/adc.rs

Lines changed: 62 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,24 @@
99
//!
1010
//! [examples/adc.rs]: https://github.com/stm32-rs/stm32f3xx-hal/blob/v0.7.0/examples/adc.rs
1111
12-
use crate::{
13-
gpio::Analog,
14-
rcc::{Clocks, AHB},
15-
};
1612
use cortex_m::asm;
1713
use embedded_hal::adc::{Channel, OneShot};
1814

1915
use crate::{
20-
gpio::{gpioa, gpiob, gpioc},
21-
pac::{ADC1, ADC1_2, ADC2},
16+
gpio::{self, Analog},
17+
pac::{adc1::cfgr::ALIGN_A, adc1_2::ccr::CKMODE_A, ADC1, ADC1_2, ADC2},
18+
rcc::{Clocks, AHB},
2219
};
23-
use stm32f3::stm32f303::{adc1::cfgr::ALIGN_A, adc1_2::ccr::CKMODE_A};
24-
const MAX_ADVREGEN_STARTUP_US: u32 = 10;
2520

2621
#[cfg(any(
2722
feature = "stm32f303xb",
2823
feature = "stm32f303xc",
2924
feature = "stm32f303xd",
3025
feature = "stm32f303xe",
3126
))]
32-
use crate::{
33-
gpio::{gpiod, gpioe, gpiof},
34-
pac::{ADC3, ADC3_4, ADC4},
35-
};
27+
use crate::pac::{ADC3, ADC3_4, ADC4};
28+
29+
const MAX_ADVREGEN_STARTUP_US: u32 = 10;
3630

3731
/// Analog Digital Converter Peripheral
3832
// TODO: Remove `pub` from the register block once all functionalities are implemented.
@@ -174,21 +168,21 @@ macro_rules! adc_pins {
174168

175169
#[cfg(feature = "stm32f303")]
176170
adc_pins!(ADC1,
177-
gpioa::PA0<Analog> => 1,
178-
gpioa::PA1<Analog> => 2,
179-
gpioa::PA2<Analog> => 3,
180-
gpioa::PA3<Analog> => 4,
181-
gpioc::PC0<Analog> => 6,
182-
gpioc::PC1<Analog> => 7,
183-
gpioc::PC2<Analog> => 8,
184-
gpioc::PC3<Analog> => 9,
171+
gpio::PA0<Analog> => 1,
172+
gpio::PA1<Analog> => 2,
173+
gpio::PA2<Analog> => 3,
174+
gpio::PA3<Analog> => 4,
175+
gpio::PC0<Analog> => 6,
176+
gpio::PC1<Analog> => 7,
177+
gpio::PC2<Analog> => 8,
178+
gpio::PC3<Analog> => 9,
185179
);
186180

187181
#[cfg(any(feature = "stm32f303x6", feature = "stm32f303x8"))]
188182
adc_pins!(ADC1,
189-
gpiob::PB0<Analog> => 11,
190-
gpiob::PB1<Analog> => 12,
191-
gpiob::PB13<Analog> => 13,
183+
gpio::PB0<Analog> => 11,
184+
gpio::PB1<Analog> => 12,
185+
gpio::PB13<Analog> => 13,
192186
);
193187

194188
#[cfg(any(
@@ -198,33 +192,33 @@ adc_pins!(ADC1,
198192
feature = "stm32f303xe",
199193
))]
200194
adc_pins!(ADC1,
201-
gpiof::PF4<Analog> => 5,
202-
gpiof::PF2<Analog> => 10,
195+
gpio::PF4<Analog> => 5,
196+
gpio::PF2<Analog> => 10,
203197
);
204198

205199
// # ADC2 Pin/Channel mapping
206200
// ## f303
207201

208202
#[cfg(feature = "stm32f303")]
209203
adc_pins!(ADC2,
210-
gpioa::PA4<Analog> => 1,
211-
gpioa::PA5<Analog> => 2,
212-
gpioa::PA6<Analog> => 3,
213-
gpioa::PA7<Analog> => 4,
214-
gpioc::PC4<Analog> => 5,
215-
gpioc::PC0<Analog> => 6,
216-
gpioc::PC1<Analog> => 7,
217-
gpioc::PC2<Analog> => 8,
218-
gpioc::PC3<Analog> => 9,
219-
gpioc::PC5<Analog> => 11,
220-
gpiob::PB2<Analog> => 12,
204+
gpio::PA4<Analog> => 1,
205+
gpio::PA5<Analog> => 2,
206+
gpio::PA6<Analog> => 3,
207+
gpio::PA7<Analog> => 4,
208+
gpio::PC4<Analog> => 5,
209+
gpio::PC0<Analog> => 6,
210+
gpio::PC1<Analog> => 7,
211+
gpio::PC2<Analog> => 8,
212+
gpio::PC3<Analog> => 9,
213+
gpio::PC5<Analog> => 11,
214+
gpio::PB2<Analog> => 12,
221215
);
222216

223217
#[cfg(any(feature = "stm32f303x6", feature = "stm32f303x8"))]
224218
adc_pins!(ADC2,
225-
gpiob::PB12<Analog> => 13,
226-
gpiob::PB14<Analog> => 14,
227-
gpiob::PB15<Analog> => 15,
219+
gpio::PB12<Analog> => 13,
220+
gpio::PB14<Analog> => 14,
221+
gpio::PB15<Analog> => 15,
228222
);
229223

230224
#[cfg(any(
@@ -234,7 +228,7 @@ adc_pins!(ADC2,
234228
feature = "stm32f303xe",
235229
))]
236230
adc_pins!(ADC2,
237-
gpiof::PF2<Analog> => 10,
231+
gpio::PF2<Analog> => 10,
238232
);
239233

240234
// # ADC3 Pin/Channel mapping
@@ -247,22 +241,22 @@ adc_pins!(ADC2,
247241
feature = "stm32f303xe",
248242
))]
249243
adc_pins!(ADC3,
250-
gpiob::PB1<Analog> => 1,
251-
gpioe::PE9<Analog> => 2,
252-
gpioe::PE13<Analog> => 3,
244+
gpio::PB1<Analog> => 1,
245+
gpio::PE9<Analog> => 2,
246+
gpio::PE13<Analog> => 3,
253247
// There is no ADC3 Channel #4
254-
gpiob::PB13<Analog> => 5,
255-
gpioe::PE8<Analog> => 6,
256-
gpiod::PD10<Analog> => 7,
257-
gpiod::PD11<Analog> => 8,
258-
gpiod::PD12<Analog> => 9,
259-
gpiod::PD13<Analog> => 10,
260-
gpiod::PD14<Analog> => 11,
261-
gpiob::PB0<Analog> => 12,
262-
gpioe::PE7<Analog> => 13,
263-
gpioe::PE10<Analog> => 14,
264-
gpioe::PE11<Analog> => 15,
265-
gpioe::PE12<Analog> => 16,
248+
gpio::PB13<Analog> => 5,
249+
gpio::PE8<Analog> => 6,
250+
gpio::PD10<Analog> => 7,
251+
gpio::PD11<Analog> => 8,
252+
gpio::PD12<Analog> => 9,
253+
gpio::PD13<Analog> => 10,
254+
gpio::PD14<Analog> => 11,
255+
gpio::PB0<Analog> => 12,
256+
gpio::PE7<Analog> => 13,
257+
gpio::PE10<Analog> => 14,
258+
gpio::PE11<Analog> => 15,
259+
gpio::PE12<Analog> => 16,
266260
);
267261

268262
// # ADC4 Pin/Channel mapping
@@ -275,19 +269,19 @@ adc_pins!(ADC3,
275269
feature = "stm32f303xe",
276270
))]
277271
adc_pins!(ADC4,
278-
gpioe::PE14<Analog> => 1,
279-
gpioe::PE15<Analog> => 2,
280-
gpiob::PB12<Analog> => 3,
281-
gpiob::PB14<Analog> => 4,
282-
gpiob::PB15<Analog> => 5,
283-
gpioe::PE8<Analog> => 6,
284-
gpiod::PD10<Analog> => 7,
285-
gpiod::PD11<Analog> => 8,
286-
gpiod::PD12<Analog> => 9,
287-
gpiod::PD13<Analog> => 10,
288-
gpiod::PD14<Analog> => 11,
289-
gpiod::PD8<Analog> => 12,
290-
gpiod::PD9<Analog> => 13,
272+
gpio::PE14<Analog> => 1,
273+
gpio::PE15<Analog> => 2,
274+
gpio::PB12<Analog> => 3,
275+
gpio::PB14<Analog> => 4,
276+
gpio::PB15<Analog> => 5,
277+
gpio::PE8<Analog> => 6,
278+
gpio::PD10<Analog> => 7,
279+
gpio::PD11<Analog> => 8,
280+
gpio::PD12<Analog> => 9,
281+
gpio::PD13<Analog> => 10,
282+
gpio::PD14<Analog> => 11,
283+
gpio::PD8<Analog> => 12,
284+
gpio::PD9<Analog> => 13,
291285
);
292286

293287
// Abstract implementation of ADC functionality

src/gpio.rs

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -755,42 +755,54 @@ macro_rules! gpio {
755755
gpio_mapped: $gpioy:ident,
756756
iopen: $iopxen:ident,
757757
ioprst: $iopxrst:ident,
758-
partially_erased_pin: $PXx:ty,
758+
partially_erased_pin: $PXx:ident,
759759
pins: [$(
760760
$i:literal => (
761-
$PXi:ty, $pxi:ident, $MODE:ty, $AFR:ident, [$($IntoAfi:ident),*],
761+
$PXi:ident, $pxi:ident, $MODE:ty, $AFR:ident, [$($IntoAfi:ident),*],
762762
),
763763
)+],
764764
}) => {
765-
paste::paste!{
765+
paste::paste! {
766766
#[doc = "GPIO port " $GPIOX " (type state)"]
767767
pub struct $Gpiox;
768-
}
769768

770-
impl private::Gpio for $Gpiox {
771-
type Reg = crate::pac::$gpioy::RegisterBlock;
769+
impl private::Gpio for $Gpiox {
770+
type Reg = crate::pac::$gpioy::RegisterBlock;
772771

773-
#[inline(always)]
774-
fn ptr(&self) -> *const Self::Reg {
775-
crate::pac::$GPIOX::ptr()
772+
#[inline(always)]
773+
fn ptr(&self) -> *const Self::Reg {
774+
crate::pac::$GPIOX::ptr()
775+
}
776+
777+
#[inline(always)]
778+
fn port_index(&self) -> u8 {
779+
$port_index
780+
}
776781
}
777782

778-
#[inline(always)]
779-
fn port_index(&self) -> u8 {
780-
$port_index
783+
impl marker::Gpio for $Gpiox {}
784+
785+
impl marker::GpioStatic for $Gpiox {
786+
type MODER = $gpiox::MODER;
787+
type OTYPER = $gpiox::OTYPER;
788+
type OSPEEDR = $gpiox::OSPEEDR;
789+
type PUPDR = $gpiox::PUPDR;
781790
}
782-
}
783791

784-
impl marker::Gpio for $Gpiox {}
792+
$(
793+
#[doc = "Pin " $PXi]
794+
pub type $PXi<Mode> = Pin<$Gpiox, U<$i>, Mode>;
785795

786-
impl marker::GpioStatic for $Gpiox {
787-
type MODER = $gpiox::MODER;
788-
type OTYPER = $gpiox::OTYPER;
789-
type OSPEEDR = $gpiox::OSPEEDR;
790-
type PUPDR = $gpiox::PUPDR;
791-
}
796+
$(
797+
impl<Mode> marker::$IntoAfi for $PXi<Mode> {
798+
type AFR = $gpiox::$AFR;
799+
}
800+
)*
801+
)+
802+
803+
#[doc = "Partially erased pin for " $GPIOX]
804+
pub type $PXx<Mode> = Pin<$Gpiox, Ux, Mode>;
792805

793-
paste::paste!{
794806
#[doc = "All Pins and associated registers for GPIO port " $GPIOX]
795807
pub mod $gpiox {
796808
use core::marker::PhantomData;
@@ -800,14 +812,21 @@ macro_rules! gpio {
800812
rcc::AHB,
801813
};
802814

803-
use super::{marker, Afr, $Gpiox, GpioExt, Moder, Ospeedr, Otyper, Pin, Pupdr, U, Ux};
815+
use super::{Afr, $Gpiox, GpioExt, Moder, Ospeedr, Otyper, Pupdr, U};
804816

805817
#[allow(unused_imports)]
806818
use super::{
807819
Input, Output, Analog, PushPull, OpenDrain,
808820
AF0, AF1, AF2, AF3, AF4, AF5, AF6, AF7, AF8, AF9, AF10, AF11, AF12, AF13, AF14, AF15,
809821
};
810822

823+
pub use super::{
824+
$PXx,
825+
$(
826+
$PXi,
827+
)+
828+
};
829+
811830
/// GPIO parts
812831
pub struct Parts {
813832
/// Opaque AFRH register
@@ -923,20 +942,6 @@ macro_rules! gpio {
923942
fn pull_down { PULLDOWN }
924943
}
925944
}
926-
927-
/// Partially erased pin
928-
pub type $PXx<Mode> = Pin<$Gpiox, Ux, Mode>;
929-
930-
$(
931-
#[doc = "Pin " $PXi]
932-
pub type $PXi<Mode> = Pin<$Gpiox, U<$i>, Mode>;
933-
934-
$(
935-
impl<Mode> marker::$IntoAfi for $PXi<Mode> {
936-
type AFR = $AFR;
937-
}
938-
)*
939-
)+
940945
}
941946
}
942947
};

0 commit comments

Comments
 (0)