Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example of GPIO input mode #316

Merged
merged 5 commits into from
Jul 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,7 @@ required-features = ["has-can"]
[[example]]
name = "can-rtic"
required-features = ["has-can", "rt"]

[[example]]
name = "gpio_input"
required-features = ["stm32f103"]
73 changes: 73 additions & 0 deletions examples/gpio_input.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//! 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 embedded_hal::digital::v2::InputPin;
use panic_halt as _;
use stm32f1xx_hal::{delay::Delay, 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 mut rcc = dp.RCC.constrain();

let clock = rcc.cfgr.freeze(&mut flash.acr);

let mut gpioa = dp.GPIOA.split(&mut rcc.apb2);
let mut _gpiob = dp.GPIOB.split(&mut rcc.apb2);
let mut gpioc = dp.GPIOC.split(&mut rcc.apb2);
let mut gpiod = dp.GPIOD.split(&mut rcc.apb2);

// red_led and green_led
let mut red_led = gpioa
.pa8
.into_push_pull_output_with_state(&mut gpioa.crh, stm32f1xx_hal::gpio::State::High);
let mut green_led = gpiod
.pd2
.into_push_pull_output_with_state(&mut gpiod.crl, stm32f1xx_hal::gpio::State::High);

let mut afio = dp.AFIO.constrain(&mut rcc.apb2);
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 = Delay::new(cp.SYST, clock);
loop {
let key_result = (key_0.is_low().unwrap(), key_1.is_low().unwrap());
if key_up && (key_result.0 || key_result.1) {
key_up = false;
delay.delay_ms(10u8);
match key_result {
(x, _) if x == true => red_led.toggle().unwrap(),
(_, y) if y == true => green_led.toggle().unwrap(),
(_, _) => (),
}
} else if !key_result.0 && !key_result.1 {
key_up = true;
delay.delay_ms(10u8);
}
}
}