Skip to content

Commit 3046bd4

Browse files
committed
Add a pin_probe example
1 parent 44fe194 commit 3046bd4

File tree

1 file changed

+193
-0
lines changed

1 file changed

+193
-0
lines changed

examples/pin_probe.rs

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
#![no_main]
2+
#![no_std]
3+
4+
pub fn init() {}
5+
6+
use cortex_m_rt::entry;
7+
use stm32h7xx_hal::gpio::{
8+
erased::EPin, Floating, Input, Output, PushPull,
9+
};
10+
use stm32h7xx_hal::hal::digital::v2::{InputPin, OutputPin};
11+
use stm32h7xx_hal::{delay::Delay, pac, prelude::*};
12+
13+
#[macro_use]
14+
mod utilities;
15+
16+
use log::info;
17+
18+
#[entry]
19+
fn main() -> ! {
20+
utilities::logger::init();
21+
let cp = cortex_m::Peripherals::take().unwrap();
22+
let dp = pac::Peripherals::take().unwrap();
23+
24+
// Constrain and Freeze power
25+
let pwr = dp.PWR.constrain();
26+
let pwrcfg = example_power!(pwr).freeze();
27+
28+
// Constrain and Freeze clock
29+
let rcc = dp.RCC.constrain();
30+
let ccdr = rcc.sys_ck(200.mhz()).freeze(pwrcfg, &dp.SYSCFG);
31+
32+
let mut delay = Delay::new(cp.SYST, ccdr.clocks);
33+
34+
let gpioa = dp.GPIOA.split(ccdr.peripheral.GPIOA);
35+
let gpiob = dp.GPIOB.split(ccdr.peripheral.GPIOB);
36+
let gpioc = dp.GPIOC.split(ccdr.peripheral.GPIOC);
37+
let gpiod = dp.GPIOD.split(ccdr.peripheral.GPIOD);
38+
let gpioe = dp.GPIOE.split(ccdr.peripheral.GPIOE);
39+
let gpiof = dp.GPIOF.split(ccdr.peripheral.GPIOF);
40+
let gpiog = dp.GPIOG.split(ccdr.peripheral.GPIOG);
41+
42+
let mut input_pin = gpioa.pa0.into_floating_input().erase();
43+
44+
let mut most_pins = [
45+
//gpioa.pa0.into_push_pull_output().erase(), // Used as probe pin
46+
gpioa.pa1.into_push_pull_output().erase(),
47+
gpioa.pa2.into_push_pull_output().erase(),
48+
gpioa.pa3.into_push_pull_output().erase(),
49+
gpioa.pa4.into_push_pull_output().erase(),
50+
gpioa.pa5.into_push_pull_output().erase(),
51+
gpioa.pa6.into_push_pull_output().erase(),
52+
gpioa.pa7.into_push_pull_output().erase(),
53+
gpioa.pa8.into_push_pull_output().erase(),
54+
gpioa.pa9.into_push_pull_output().erase(),
55+
gpioa.pa10.into_push_pull_output().erase(),
56+
gpioa.pa11.into_push_pull_output().erase(),
57+
gpioa.pa12.into_push_pull_output().erase(),
58+
gpioa.pa13.into_push_pull_output().erase(),
59+
gpioa.pa14.into_push_pull_output().erase(),
60+
gpioa.pa15.into_push_pull_output().erase(),
61+
gpiob.pb0.into_push_pull_output().erase(),
62+
gpiob.pb1.into_push_pull_output().erase(),
63+
gpiob.pb2.into_push_pull_output().erase(),
64+
gpiob.pb3.into_push_pull_output().erase(),
65+
gpiob.pb4.into_push_pull_output().erase(),
66+
gpiob.pb5.into_push_pull_output().erase(),
67+
gpiob.pb6.into_push_pull_output().erase(),
68+
gpiob.pb7.into_push_pull_output().erase(),
69+
gpiob.pb8.into_push_pull_output().erase(),
70+
gpiob.pb9.into_push_pull_output().erase(),
71+
gpiob.pb10.into_push_pull_output().erase(),
72+
gpiob.pb11.into_push_pull_output().erase(),
73+
gpiob.pb12.into_push_pull_output().erase(),
74+
gpiob.pb13.into_push_pull_output().erase(),
75+
gpiob.pb14.into_push_pull_output().erase(),
76+
gpiob.pb15.into_push_pull_output().erase(),
77+
gpioc.pc0.into_push_pull_output().erase(),
78+
gpioc.pc1.into_push_pull_output().erase(),
79+
gpioc.pc2.into_push_pull_output().erase(),
80+
gpioc.pc3.into_push_pull_output().erase(),
81+
gpioc.pc4.into_push_pull_output().erase(),
82+
gpioc.pc5.into_push_pull_output().erase(),
83+
gpioc.pc6.into_push_pull_output().erase(),
84+
gpioc.pc7.into_push_pull_output().erase(),
85+
gpioc.pc8.into_push_pull_output().erase(),
86+
gpioc.pc9.into_push_pull_output().erase(),
87+
gpioc.pc10.into_push_pull_output().erase(),
88+
gpioc.pc11.into_push_pull_output().erase(),
89+
gpioc.pc12.into_push_pull_output().erase(),
90+
gpioc.pc13.into_push_pull_output().erase(),
91+
gpioc.pc14.into_push_pull_output().erase(),
92+
gpioc.pc15.into_push_pull_output().erase(),
93+
gpiod.pd0.into_push_pull_output().erase(),
94+
gpiod.pd1.into_push_pull_output().erase(),
95+
gpiod.pd2.into_push_pull_output().erase(),
96+
gpiod.pd3.into_push_pull_output().erase(),
97+
gpiod.pd4.into_push_pull_output().erase(),
98+
gpiod.pd5.into_push_pull_output().erase(),
99+
gpiod.pd6.into_push_pull_output().erase(),
100+
gpiod.pd7.into_push_pull_output().erase(),
101+
gpiod.pd8.into_push_pull_output().erase(),
102+
gpiod.pd9.into_push_pull_output().erase(),
103+
gpiod.pd10.into_push_pull_output().erase(),
104+
gpiod.pd11.into_push_pull_output().erase(),
105+
gpiod.pd12.into_push_pull_output().erase(),
106+
gpiod.pd13.into_push_pull_output().erase(),
107+
gpiod.pd14.into_push_pull_output().erase(),
108+
gpiod.pd15.into_push_pull_output().erase(),
109+
gpioe.pe0.into_push_pull_output().erase(),
110+
gpioe.pe1.into_push_pull_output().erase(),
111+
gpioe.pe2.into_push_pull_output().erase(),
112+
gpioe.pe3.into_push_pull_output().erase(),
113+
gpioe.pe4.into_push_pull_output().erase(),
114+
gpioe.pe5.into_push_pull_output().erase(),
115+
gpioe.pe6.into_push_pull_output().erase(),
116+
gpioe.pe7.into_push_pull_output().erase(),
117+
gpioe.pe8.into_push_pull_output().erase(),
118+
gpioe.pe9.into_push_pull_output().erase(),
119+
gpioe.pe10.into_push_pull_output().erase(),
120+
gpioe.pe11.into_push_pull_output().erase(),
121+
gpioe.pe12.into_push_pull_output().erase(),
122+
gpioe.pe13.into_push_pull_output().erase(),
123+
gpioe.pe14.into_push_pull_output().erase(),
124+
gpioe.pe15.into_push_pull_output().erase(),
125+
gpiof.pf0.into_push_pull_output().erase(),
126+
gpiof.pf1.into_push_pull_output().erase(),
127+
gpiof.pf2.into_push_pull_output().erase(),
128+
gpiof.pf3.into_push_pull_output().erase(),
129+
gpiof.pf4.into_push_pull_output().erase(),
130+
gpiof.pf5.into_push_pull_output().erase(),
131+
gpiof.pf6.into_push_pull_output().erase(),
132+
gpiof.pf7.into_push_pull_output().erase(),
133+
gpiof.pf8.into_push_pull_output().erase(),
134+
gpiof.pf9.into_push_pull_output().erase(),
135+
gpiof.pf10.into_push_pull_output().erase(),
136+
gpiof.pf11.into_push_pull_output().erase(),
137+
gpiof.pf12.into_push_pull_output().erase(),
138+
gpiof.pf13.into_push_pull_output().erase(),
139+
gpiof.pf14.into_push_pull_output().erase(),
140+
gpiof.pf15.into_push_pull_output().erase(),
141+
gpiog.pg0.into_push_pull_output().erase(),
142+
gpiog.pg1.into_push_pull_output().erase(),
143+
gpiog.pg2.into_push_pull_output().erase(),
144+
gpiog.pg3.into_push_pull_output().erase(),
145+
gpiog.pg4.into_push_pull_output().erase(),
146+
gpiog.pg5.into_push_pull_output().erase(),
147+
gpiog.pg6.into_push_pull_output().erase(),
148+
gpiog.pg7.into_push_pull_output().erase(),
149+
gpiog.pg8.into_push_pull_output().erase(),
150+
gpiog.pg9.into_push_pull_output().erase(),
151+
gpiog.pg10.into_push_pull_output().erase(),
152+
gpiog.pg11.into_push_pull_output().erase(),
153+
gpiog.pg12.into_push_pull_output().erase(),
154+
gpiog.pg13.into_push_pull_output().erase(),
155+
gpiog.pg14.into_push_pull_output().erase(),
156+
gpiog.pg15.into_push_pull_output().erase(),
157+
];
158+
159+
160+
loop {
161+
let found_index = probe_pins(&mut input_pin, &mut most_pins, &mut delay);
162+
info!("Pin: {:?}", found_index.map(|index| &most_pins[index]));
163+
delay.delay_ms(1_u16);
164+
}
165+
166+
}
167+
168+
169+
fn probe_pins<'a>(
170+
input: &mut EPin<Input<Floating>>,
171+
pins: &'a mut [EPin<Output<PushPull>>],
172+
delay: &mut Delay,
173+
)
174+
-> Option<usize> {
175+
176+
if input.is_high().unwrap() {
177+
return None;
178+
}
179+
180+
for (i,pin) in pins.iter_mut().enumerate() {
181+
pin.set_high().unwrap();
182+
delay.delay_ms(1_u16);
183+
184+
if input.is_high().unwrap() {
185+
pin.set_low().unwrap();
186+
return Some(i);
187+
}
188+
189+
pin.set_low().unwrap();
190+
}
191+
192+
None
193+
}

0 commit comments

Comments
 (0)