-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy pathgpio_input.rs
72 lines (63 loc) · 2.38 KB
/
gpio_input.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//! Through buttons to control the LED.
//!
//! This assumes that has two LEDs, the red light is connected to pa8 and the green light is connected to pd2.
//!
//! Meanwhile, it has two buttons, we can call them key_0 and key_1.
//! The key_0 is connected to pc5, and the key_1 is connected to pa15.
//!
//! We need to set into_pull_up_input for pc5 and pa15, for the reason that the key_0 and key_1 were connected to GND.
//!
//! Use key_0 to control the red light, key_1 to control the green light.
//! Only press a button after releasing the button to turns on the led, again turns down the led.
//!
//! And the long press was a nullity.
#![deny(unsafe_code)]
#![no_std]
#![no_main]
use cortex_m_rt::entry;
use panic_halt as _;
use stm32f1xx_hal::{gpio::PinState, pac, prelude::*};
#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().unwrap();
let mut flash = dp.FLASH.constrain();
let rcc = dp.RCC.constrain();
let clock = rcc.cfgr.freeze(&mut flash.acr);
let mut gpioa = dp.GPIOA.split();
let mut _gpiob = dp.GPIOB.split();
let mut gpioc = dp.GPIOC.split();
let mut gpiod = dp.GPIOD.split();
// red_led and green_led
let mut red_led = gpioa
.pa8
.into_push_pull_output_with_state(&mut gpioa.crh, PinState::High);
let mut green_led = gpiod
.pd2
.into_push_pull_output_with_state(&mut gpiod.crl, PinState::High);
let mut afio = dp.AFIO.constrain();
let (gpioa_pa15, _gpiob_pb3, _gpiob_pb4) =
afio.mapr.disable_jtag(gpioa.pa15, _gpiob.pb3, _gpiob.pb4);
// key_0 and key_1
let key_0 = gpioc.pc5.into_pull_up_input(&mut gpioc.crl);
let key_1 = gpioa_pa15.into_pull_up_input(&mut gpioa.crh);
// The key_up for check buttons if long press.
// if key_up is true, and buttons were not long press.
let mut key_up: bool = true;
let mut delay = cp.SYST.delay(&clock);
loop {
let key_result = (key_0.is_low(), key_1.is_low());
if key_up && (key_result.0 || key_result.1) {
key_up = false;
delay.delay_ms(10u8);
match key_result {
(true, _) => red_led.toggle(),
(_, true) => green_led.toggle(),
(_, _) => (),
}
} else if !key_result.0 && !key_result.1 {
key_up = true;
delay.delay_ms(10u8);
}
}
}