Skip to content

Commit f7204fc

Browse files
committed
Add dynamic gpio pin mode
1 parent f3ff26b commit f7204fc

File tree

3 files changed

+343
-146
lines changed

3 files changed

+343
-146
lines changed

examples/dynamic_gpio.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#![deny(unsafe_code)]
2+
#![no_std]
3+
#![no_main]
4+
5+
use panic_halt as _;
6+
7+
use nb::block;
8+
9+
use cortex_m_rt::entry;
10+
use cortex_m_semihosting::hprintln;
11+
use embedded_hal::digital::v2::{InputPin, OutputPin};
12+
use stm32f1xx_hal::{gpio::State, pac, prelude::*, timer::Timer};
13+
14+
#[entry]
15+
fn main() -> ! {
16+
// Get access to the core peripherals from the cortex-m crate
17+
let cp = cortex_m::Peripherals::take().unwrap();
18+
// Get access to the device specific peripherals from the peripheral access crate
19+
let dp = pac::Peripherals::take().unwrap();
20+
21+
// Take ownership over the raw flash and rcc devices and convert them into the corresponding
22+
// HAL structs
23+
let mut flash = dp.FLASH.constrain();
24+
let mut rcc = dp.RCC.constrain();
25+
26+
// Freeze the configuration of all the clocks in the system and store the frozen frequencies in
27+
// `clocks`
28+
let clocks = rcc.cfgr.freeze(&mut flash.acr);
29+
30+
// Acquire the GPIOC peripheral
31+
let mut gpioc = dp.GPIOC.split(&mut rcc.apb2);
32+
33+
let mut pin = gpioc.pc13.into_dynamic(&mut gpioc.crh);
34+
// Configure the syst timer to trigger an update every second
35+
let mut timer = Timer::syst(cp.SYST, &clocks).start_count_down(1.hz());
36+
37+
// Wait for the timer to trigger an update and change the state of the LED
38+
loop {
39+
pin.make_floating_input(&mut gpioc.crh);
40+
block!(timer.wait()).unwrap();
41+
hprintln!("{}", pin.is_high().unwrap()).unwrap();
42+
43+
pin.make_push_pull_output(&mut gpioc.crh);
44+
pin.set_high().unwrap();
45+
block!(timer.wait()).unwrap();
46+
pin.set_low().unwrap();
47+
block!(timer.wait()).unwrap();
48+
}
49+
}

examples/multi_mode_gpio.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use nb::block;
99
use cortex_m_rt::entry;
1010
use cortex_m_semihosting::hprintln;
1111
use embedded_hal::digital::v2::{InputPin, OutputPin};
12-
use stm32f1xx_hal::{pac, prelude::*, timer::Timer, gpio::State};
12+
use stm32f1xx_hal::{gpio::State, pac, prelude::*, timer::Timer};
1313

1414
#[entry]
1515
fn main() -> ! {

0 commit comments

Comments
 (0)