Skip to content

Commit 63a2284

Browse files
authored
Merge pull request #211 from TheZoq2/improved_gpio
* Add temporary pin modes * Add example for changing pin mode * Add stateful versions of the output pin temp conversions * Add dynamic gpio pin mode * Add documentation for GPIO pins * Fix gpio changelog appearing in the wrong place * Fix some doc issues
2 parents 494b996 + 0b46f3d commit 63a2284

File tree

4 files changed

+633
-128
lines changed

4 files changed

+633
-128
lines changed

CHANGELOG.md

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

88
## [Unreleased]
99

10+
### Added
11+
12+
- Add runtime-reconfigurable GPIO pins
13+
1014
### Fixed
1115

1216
- Fix period retrieval for timers

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::{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: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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_floating_input(&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+
block!(timer.wait()).unwrap();
40+
hprintln!("{}", pin.is_high().unwrap()).unwrap();
41+
pin.as_push_pull_output(&mut gpioc.crh, |out| {
42+
out.set_high().unwrap();
43+
block!(timer.wait()).unwrap();
44+
out.set_low().unwrap();
45+
block!(timer.wait()).unwrap();
46+
});
47+
pin.as_push_pull_output_with_state(&mut gpioc.crh, State::High, |out| {
48+
block!(timer.wait()).unwrap();
49+
out.set_low().unwrap();
50+
block!(timer.wait()).unwrap();
51+
});
52+
}
53+
}

0 commit comments

Comments
 (0)