init basic examples and setup
This commit is contained in:
14
examples/button-input/Cargo.toml
Normal file
14
examples/button-input/Cargo.toml
Normal file
@@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "button-input"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
publish = false
|
||||
build = "build.rs"
|
||||
|
||||
[dependencies]
|
||||
cortex-m.workspace = true
|
||||
cortex-m-rt.workspace = true
|
||||
defmt.workspace = true
|
||||
defmt-rtt.workspace = true
|
||||
panic-probe.workspace = true
|
||||
stm32f1xx-hal = { workspace = true, features = ["medium", "stm32f103"] }
|
||||
9
examples/button-input/build.rs
Normal file
9
examples/button-input/build.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
fn main() {
|
||||
println!("cargo:rerun-if-changed=../../memory.x");
|
||||
println!(
|
||||
"cargo:rustc-link-search={}",
|
||||
std::path::PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap())
|
||||
.join("../..")
|
||||
.display()
|
||||
);
|
||||
}
|
||||
47
examples/button-input/src/main.rs
Normal file
47
examples/button-input/src/main.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
#![deny(unsafe_code)]
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use cortex_m_rt::entry;
|
||||
use defmt::info;
|
||||
use defmt_rtt as _;
|
||||
use panic_probe as _;
|
||||
use stm32f1xx_hal::{pac, prelude::*};
|
||||
|
||||
const POLL_MS: u8 = 25;
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
let dp = pac::Peripherals::take().unwrap();
|
||||
let cp = cortex_m::Peripherals::take().unwrap();
|
||||
|
||||
let mut rcc = dp.RCC.constrain();
|
||||
let mut gpioa = dp.GPIOA.split(&mut rcc);
|
||||
let mut gpioc = dp.GPIOC.split(&mut rcc);
|
||||
|
||||
let button = gpioa.pa0.into_pull_down_input(&mut gpioa.crl);
|
||||
let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
|
||||
let mut delay = cp.SYST.delay(&rcc.clocks);
|
||||
let mut was_pressed = button.is_high();
|
||||
|
||||
led.set_high();
|
||||
info!("button-input: PA0 button to 3V3 with internal pull-down");
|
||||
|
||||
loop {
|
||||
let pressed = button.is_high();
|
||||
|
||||
if pressed != was_pressed {
|
||||
was_pressed = pressed;
|
||||
|
||||
if pressed {
|
||||
led.set_low();
|
||||
info!("button pressed");
|
||||
} else {
|
||||
led.set_high();
|
||||
info!("button released");
|
||||
}
|
||||
}
|
||||
|
||||
delay.delay_ms(POLL_MS);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user