Skip to content

Commit ea00afc

Browse files
committed
PinMode::new
1 parent cafed6d commit ea00afc

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

examples/blinky_timer_irq.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use panic_halt as _;
1515
use stm32f1xx_hal as hal;
1616

1717
use crate::hal::{
18-
gpio::{gpioc, Output, PushPull},
18+
gpio::{gpioc, Output, PinState, PushPull},
1919
pac::{interrupt, Interrupt, Peripherals, TIM2},
2020
prelude::*,
2121
timer::{CounterMs, Event},
@@ -79,8 +79,9 @@ fn main() -> ! {
7979

8080
// Configure PC13 pin to blink LED
8181
let mut gpioc = dp.GPIOC.split();
82-
let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
83-
let _ = led.set_high(); // Turn off
82+
let led = Output::new(gpioc.pc13, &mut gpioc.crh, PinState::High);
83+
//or
84+
//let led = gpioc.pc13.into_push_pull_output_with_state(&mut gpioc.crh, PinState::High);
8485

8586
// Move the pin into our global storage
8687
cortex_m::interrupt::free(|cs| *G_LED.borrow(cs).borrow_mut() = Some(led));

src/gpio.rs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ pub trait Active {}
150150
#[derive(Default)]
151151
pub struct Input<PULL = Floating>(PhantomData<PULL>);
152152

153-
impl<MODE> Active for Input<MODE> {}
153+
impl<PULL> Active for Input<PULL> {}
154154

155155
/// Used by the debugger (type state)
156156
#[derive(Default)]
@@ -986,6 +986,61 @@ where
986986
}
987987
}
988988

989+
impl Analog {
990+
pub fn new<const P: char, const N: u8, MODE>(
991+
pin: Pin<P, N, MODE>,
992+
cr: &mut <Pin<P, N, MODE> as HL>::Cr,
993+
) -> Pin<P, N, Self>
994+
where
995+
Pin<P, N, MODE>: HL,
996+
Self: PinMode,
997+
{
998+
pin.into_mode(cr)
999+
}
1000+
}
1001+
1002+
impl<PULL> Input<PULL> {
1003+
pub fn new<const P: char, const N: u8, MODE>(
1004+
pin: Pin<P, N, MODE>,
1005+
cr: &mut <Pin<P, N, MODE> as HL>::Cr,
1006+
_pull: PULL,
1007+
) -> Pin<P, N, Self>
1008+
where
1009+
Pin<P, N, MODE>: HL,
1010+
Self: PinMode,
1011+
{
1012+
pin.into_mode(cr)
1013+
}
1014+
}
1015+
1016+
impl<Otype> Output<Otype> {
1017+
pub fn new<const P: char, const N: u8, MODE>(
1018+
mut pin: Pin<P, N, MODE>,
1019+
cr: &mut <Pin<P, N, MODE> as HL>::Cr,
1020+
state: PinState,
1021+
) -> Pin<P, N, Self>
1022+
where
1023+
Pin<P, N, MODE>: HL,
1024+
Self: PinMode,
1025+
{
1026+
pin._set_state(state);
1027+
pin.into_mode(cr)
1028+
}
1029+
}
1030+
1031+
impl<Otype> Alternate<Otype> {
1032+
pub fn new<const P: char, const N: u8, MODE>(
1033+
pin: Pin<P, N, MODE>,
1034+
cr: &mut <Pin<P, N, MODE> as HL>::Cr,
1035+
) -> Pin<P, N, Self>
1036+
where
1037+
Pin<P, N, MODE>: HL,
1038+
Self: PinMode,
1039+
{
1040+
pin.into_mode(cr)
1041+
}
1042+
}
1043+
9891044
gpio!(GPIOA, gpioa, PAx, 'A', [
9901045
PA0: (pa0, 0),
9911046
PA1: (pa1, 1),

0 commit comments

Comments
 (0)