Skip to content

Commit 993eed1

Browse files
authored
Merge pull request #802 from stm32-rs/mode-new
PinMode::new constructors
2 parents 388b0a0 + 4c43bf1 commit 993eed1

File tree

5 files changed

+40
-8
lines changed

5 files changed

+40
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Added
1111

12+
- `AnyPin` alias for `ErasedPin`
13+
- `new` constructors for `Input`, `Output`, `Analog`
1214
- Add `f469disc-lcd-test` with color/BER test pattern LCD output [#789]
1315
- Port `dsihost` implementation from stm32h7xx-hal [#786]
1416
- I2C 10-bit address support for I2c [#772] [#783]

examples/rtic-button.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use panic_halt as _;
88
#[rtic::app(device = stm32f4xx_hal::pac, peripherals = true)]
99
mod app {
1010
use stm32f4xx_hal::{
11-
gpio::{gpioa::PA0, gpioc::PC13, Edge, Input, Output, PushPull},
11+
gpio::{gpioa::PA0, gpioc::PC13, Edge, Input, Output, PinState, Pull},
1212
prelude::*,
1313
};
1414
const SYSFREQ: u32 = 100_000_000;
@@ -20,7 +20,7 @@ mod app {
2020
#[local]
2121
struct Local {
2222
button: PA0<Input>,
23-
led: PC13<Output<PushPull>>,
23+
led: PC13<Output>,
2424
}
2525

2626
#[init]
@@ -34,12 +34,14 @@ mod app {
3434
let gpioa = ctx.device.GPIOA.split();
3535
let gpioc = ctx.device.GPIOC.split();
3636
// button
37-
let mut button = gpioa.pa0.into_pull_up_input();
37+
let mut button = Input::new(gpioa.pa0, Pull::Up);
38+
// or
39+
//let mut button = gpioa.pa0.into_pull_up_input();
3840
button.make_interrupt_source(&mut syscfg);
3941
button.enable_interrupt(&mut ctx.device.EXTI);
4042
button.trigger_on_edge(&mut ctx.device.EXTI, Edge::Falling);
4143
// led
42-
let led = gpioc.pc13.into_push_pull_output();
44+
let led = Output::new(gpioc.pc13, PinState::Low);
4345

4446
(
4547
Shared {

src/gpio.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub use convert::PinMode;
6363
mod partially_erased;
6464
pub use partially_erased::{PEPin, PartiallyErasedPin};
6565
mod erased;
66-
pub use erased::{EPin, ErasedPin};
66+
pub use erased::{AnyPin, ErasedPin};
6767
mod exti;
6868
pub use exti::ExtiPin;
6969
mod dynamic;
@@ -145,8 +145,8 @@ pub struct OpenDrain;
145145
/// Output mode (type state)
146146
#[derive(Debug, Default)]
147147
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
148-
pub struct Output<MODE = PushPull> {
149-
_mode: PhantomData<MODE>,
148+
pub struct Output<Otype = PushPull> {
149+
_mode: PhantomData<Otype>,
150150
}
151151

152152
/// Push pull output (type state)

src/gpio/convert.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,34 @@
11
use super::*;
22
use crate::pac::gpioa::{moder::MODER0 as Mode, otyper::OT0 as OutputType};
33

4+
impl Input {
5+
pub fn new<const P: char, const N: u8, MODE: PinMode>(
6+
pin: Pin<P, N, MODE>,
7+
pull: Pull,
8+
) -> Pin<P, N, Self> {
9+
pin.into_mode().internal_resistor(pull)
10+
}
11+
}
12+
13+
impl<Otype> Output<Otype> {
14+
pub fn new<const P: char, const N: u8, MODE: PinMode>(
15+
mut pin: Pin<P, N, MODE>,
16+
state: PinState,
17+
) -> Pin<P, N, Self>
18+
where
19+
Self: PinMode,
20+
{
21+
pin._set_state(state);
22+
pin.into_mode()
23+
}
24+
}
25+
26+
impl Analog {
27+
pub fn new<const P: char, const N: u8, MODE: PinMode>(pin: Pin<P, N, MODE>) -> Pin<P, N, Self> {
28+
pin.into_mode()
29+
}
30+
}
31+
432
impl<const P: char, const N: u8, const A: u8> Pin<P, N, Alternate<A, PushPull>> {
533
/// Turns pin alternate configuration pin into open drain
634
pub fn set_open_drain(self) -> Pin<P, N, Alternate<A, OpenDrain>> {

src/gpio/erased.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::*;
22

3-
pub use ErasedPin as EPin;
3+
pub use ErasedPin as AnyPin;
44

55
/// Fully erased pin
66
///

0 commit comments

Comments
 (0)