From 806a5ac871a5ba942b37180aa94ca6edf73aee0b Mon Sep 17 00:00:00 2001 From: cscherr Date: Thu, 24 Apr 2025 14:31:51 +0200 Subject: [PATCH] feat: add more example programs --- Cargo.toml | 2 +- examples/blinky-press.rs | 36 +++++++++++++++++++++++++++++++ examples/blinky-toggle.rs | 45 +++++++++++++++++++++++++++++++++++++++ examples/blinky.rs | 2 +- 4 files changed, 83 insertions(+), 2 deletions(-) create mode 100755 examples/blinky-press.rs create mode 100755 examples/blinky-toggle.rs diff --git a/Cargo.toml b/Cargo.toml index 6fdfb5f..3b2c1e0 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ edition = "2024" [dependencies] hal = { package = "stm32l0xx-hal", version = "0.10.0", features = [ - "mcu-STM32L053R8Hx", # mcu-STM32L053R8Hx or mcu-STM32L053R8Tx not sure + "mcu-STM32L053R8Tx", "rt", ] } cortex-m-rt = "0.7.5" diff --git a/examples/blinky-press.rs b/examples/blinky-press.rs new file mode 100755 index 0000000..7ef948d --- /dev/null +++ b/examples/blinky-press.rs @@ -0,0 +1,36 @@ +#![no_main] +#![no_std] + +extern crate panic_halt; + +use cortex_m_rt::entry; +use hal::{pac, prelude::*, rcc::Config}; + +#[entry] +fn main() -> ! { + let dp = pac::Peripherals::take().unwrap(); + let cp = cortex_m::Peripherals::take().unwrap(); + + // Configure the clock. + let mut rcc = dp.RCC.freeze(Config::hsi16()); + + let gpioa = dp.GPIOA.split(&mut rcc); + let gpioc = dp.GPIOC.split(&mut rcc); + + // Configure PA5 as output. + let mut led = gpioa.pa5.into_push_pull_output(); + let mut button = gpioc.pc13.into_pull_down_input(); + + // Get the delay provider. + let mut delay = cp.SYST.delay(rcc.clocks); + + loop { + if button.is_low().expect("button.is_low failed") { + led.set_high().unwrap(); + } else { + led.set_low().unwrap(); + } + + delay.delay_ms(10_u16); + } +} diff --git a/examples/blinky-toggle.rs b/examples/blinky-toggle.rs new file mode 100755 index 0000000..3dc25ae --- /dev/null +++ b/examples/blinky-toggle.rs @@ -0,0 +1,45 @@ +#![no_main] +#![no_std] + +extern crate panic_halt; + +use cortex_m_rt::entry; +use hal::{pac, prelude::*, rcc::Config}; + +#[entry] +fn main() -> ! { + let dp = pac::Peripherals::take().unwrap(); + let cp = cortex_m::Peripherals::take().unwrap(); + + // Configure the clock. + let mut rcc = dp.RCC.freeze(Config::hsi16()); + + let gpioa = dp.GPIOA.split(&mut rcc); + let gpioc = dp.GPIOC.split(&mut rcc); + + // Configure PA5 as output. + let mut led = gpioa.pa5.into_push_pull_output(); + let button = gpioc.pc13.into_pull_down_input(); + + // Get the delay provider. + let mut _delay = cp.SYST.delay(rcc.clocks); + + let mut is_on: bool; + let mut was_on: bool = false; + #[allow(unused_variables)] // it is used later?? + let mut enable_led: bool = false; + + loop { + is_on = button.is_low().unwrap(); + if is_on != was_on { + enable_led ^= true; + } + if is_on { + led.set_high().unwrap(); + } else { + led.set_low().unwrap(); + } + + was_on = is_on; + } +} diff --git a/examples/blinky.rs b/examples/blinky.rs index 054a19b..18472e5 100755 --- a/examples/blinky.rs +++ b/examples/blinky.rs @@ -18,7 +18,7 @@ fn main() -> ! { // the RCC register. let gpioa = dp.GPIOA.split(&mut rcc); - // Configure PA1 as output. + // Configure PA5 as output. let mut led = gpioa.pa5.into_push_pull_output(); // Get the delay provider.