Skip to content

Commit

Permalink
Merge pull request #211 from TheZoq2/improved_gpio
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
TheZoq2 committed Jun 24, 2020
2 parents 494b996 + 0b46f3d commit 63a2284
Show file tree
Hide file tree
Showing 4 changed files with 633 additions and 128 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added

- Add runtime-reconfigurable GPIO pins

### Fixed

- Fix period retrieval for timers
Expand Down
49 changes: 49 additions & 0 deletions examples/dynamic_gpio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#![deny(unsafe_code)]
#![no_std]
#![no_main]

use panic_halt as _;

use nb::block;

use cortex_m_rt::entry;
use cortex_m_semihosting::hprintln;
use embedded_hal::digital::v2::{InputPin, OutputPin};
use stm32f1xx_hal::{pac, prelude::*, timer::Timer};

#[entry]
fn main() -> ! {
// Get access to the core peripherals from the cortex-m crate
let cp = cortex_m::Peripherals::take().unwrap();
// Get access to the device specific peripherals from the peripheral access crate
let dp = pac::Peripherals::take().unwrap();

// Take ownership over the raw flash and rcc devices and convert them into the corresponding
// HAL structs
let mut flash = dp.FLASH.constrain();
let mut rcc = dp.RCC.constrain();

// Freeze the configuration of all the clocks in the system and store the frozen frequencies in
// `clocks`
let clocks = rcc.cfgr.freeze(&mut flash.acr);

// Acquire the GPIOC peripheral
let mut gpioc = dp.GPIOC.split(&mut rcc.apb2);

let mut pin = gpioc.pc13.into_dynamic(&mut gpioc.crh);
// Configure the syst timer to trigger an update every second
let mut timer = Timer::syst(cp.SYST, &clocks).start_count_down(1.hz());

// Wait for the timer to trigger an update and change the state of the LED
loop {
pin.make_floating_input(&mut gpioc.crh);
block!(timer.wait()).unwrap();
hprintln!("{}", pin.is_high().unwrap()).unwrap();

pin.make_push_pull_output(&mut gpioc.crh);
pin.set_high().unwrap();
block!(timer.wait()).unwrap();
pin.set_low().unwrap();
block!(timer.wait()).unwrap();
}
}
53 changes: 53 additions & 0 deletions examples/multi_mode_gpio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#![deny(unsafe_code)]
#![no_std]
#![no_main]

use panic_halt as _;

use nb::block;

use cortex_m_rt::entry;
use cortex_m_semihosting::hprintln;
use embedded_hal::digital::v2::{InputPin, OutputPin};
use stm32f1xx_hal::{gpio::State, pac, prelude::*, timer::Timer};

#[entry]
fn main() -> ! {
// Get access to the core peripherals from the cortex-m crate
let cp = cortex_m::Peripherals::take().unwrap();
// Get access to the device specific peripherals from the peripheral access crate
let dp = pac::Peripherals::take().unwrap();

// Take ownership over the raw flash and rcc devices and convert them into the corresponding
// HAL structs
let mut flash = dp.FLASH.constrain();
let mut rcc = dp.RCC.constrain();

// Freeze the configuration of all the clocks in the system and store the frozen frequencies in
// `clocks`
let clocks = rcc.cfgr.freeze(&mut flash.acr);

// Acquire the GPIOC peripheral
let mut gpioc = dp.GPIOC.split(&mut rcc.apb2);

let mut pin = gpioc.pc13.into_floating_input(&mut gpioc.crh);
// Configure the syst timer to trigger an update every second
let mut timer = Timer::syst(cp.SYST, &clocks).start_count_down(1.hz());

// Wait for the timer to trigger an update and change the state of the LED
loop {
block!(timer.wait()).unwrap();
hprintln!("{}", pin.is_high().unwrap()).unwrap();
pin.as_push_pull_output(&mut gpioc.crh, |out| {
out.set_high().unwrap();
block!(timer.wait()).unwrap();
out.set_low().unwrap();
block!(timer.wait()).unwrap();
});
pin.as_push_pull_output_with_state(&mut gpioc.crh, State::High, |out| {
block!(timer.wait()).unwrap();
out.set_low().unwrap();
block!(timer.wait()).unwrap();
});
}
}
Loading

0 comments on commit 63a2284

Please sign in to comment.