Skip to content

Commit 81eab2a

Browse files
committed
Add gpio erased example
1 parent aa2e809 commit 81eab2a

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,7 @@ required-features = ["stm32f303"]
139139
[[example]]
140140
name = "i2c_scanner"
141141
required-features = ["stm32f303xc"]
142+
143+
[[example]]
144+
name = "gpio_erased"
145+
required-features = ["rt", "stm32f303xc"]

examples/gpio_erased.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// TOOD Implement:
2+
// https://github.com/dfrankland/proton-c/commit/0289f1cfa15000d6b1b2a8175420c16ffdff7451
3+
#![no_main]
4+
#![no_std]
5+
6+
use panic_semihosting as _;
7+
8+
use cortex_m::asm;
9+
use cortex_m_rt::entry;
10+
use cortex_m_semihosting::hprintln;
11+
12+
use hal::gpio::{self, Floating, Input};
13+
use hal::pac;
14+
use hal::prelude::*;
15+
use stm32f3xx_hal as hal;
16+
17+
#[entry]
18+
fn main() -> ! {
19+
let dp = pac::Peripherals::take().unwrap();
20+
21+
let mut rcc = dp.RCC.constrain();
22+
let mut gpiob = dp.GPIOB.split(&mut rcc.ahb);
23+
let mut gpioc = dp.GPIOC.split(&mut rcc.ahb);
24+
let mut gpiod = dp.GPIOD.split(&mut rcc.ahb);
25+
26+
let mut pin_array: [gpio::PXx<Input<Floating>>; 4] = [
27+
gpiob
28+
.pb11
29+
.into_floating_input(&mut gpiob.moder, &mut gpiob.pupdr)
30+
.downgrade()
31+
.downgrade(),
32+
gpioc
33+
.pc4
34+
.into_floating_input(&mut gpioc.moder, &mut gpioc.pupdr)
35+
.downgrade()
36+
.downgrade(),
37+
gpiod
38+
.pd3
39+
.into_floating_input(&mut gpiod.moder, &mut gpiod.pupdr)
40+
.downgrade()
41+
.downgrade(),
42+
gpiod
43+
.pd2
44+
.into_floating_input(&mut gpiod.moder, &mut gpiod.pupdr)
45+
.downgrade()
46+
.downgrade(),
47+
];
48+
49+
hprintln!("Start scanning pin array").unwrap();
50+
loop {
51+
for pin in pin_array.iter_mut() {
52+
hprintln!("Value is {}", pin.is_high().unwrap()).unwrap();
53+
asm::delay(1_000);
54+
}
55+
}
56+
}

src/gpio.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ macro_rules! gpio {
156156
}
157157

158158
/// Fully erased pin
159+
///
160+
/// This moves the pin type information to be known
161+
/// at runtime, and erases the specific compile time type of the GPIO.
162+
/// It does only matter, that it is a GPIO pin with a specific MODE.
163+
///
164+
/// See [examples/gpio_erased.rs] as an example.
165+
///
166+
/// [examples/gpio_erased.rs]: https://github.com/stm32-rs/stm32f3xx-hal/blob/v0.5.0/examples/gpio_erased.rs
159167
pub struct PXx<MODE> {
160168
i: u8,
161169
gpio: Gpio,

0 commit comments

Comments
 (0)