From f53b9190ef68ec2ba49d7845ef9e71757935b243 Mon Sep 17 00:00:00 2001 From: LongHairedHacker Date: Sun, 11 Apr 2021 17:23:17 +0200 Subject: [PATCH] Added blink application --- src/application/mod.rs | 34 ++++++++++++++++++++++++++++++++++ src/application/setup.rs | 31 +++++++++++++++++++++++++++++++ src/lib.rs | 2 ++ src/main.rs | 12 ++++++++++-- 4 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 src/application/mod.rs create mode 100644 src/application/setup.rs diff --git a/src/application/mod.rs b/src/application/mod.rs new file mode 100644 index 0000000..3bc78e3 --- /dev/null +++ b/src/application/mod.rs @@ -0,0 +1,34 @@ +use cortex_m::prelude::*; +use embedded_hal::digital::v2::OutputPin; +use stm32f1xx_hal::{ + delay::Delay, + gpio::{gpioc, Output, PushPull}, +}; + +mod setup; + +//use crate::exit; +pub use setup::setup; + +pub struct App { + delay: Delay, + board_led: gpioc::PC13>, +} + +impl App { + pub fn run(mut self) -> ! { + defmt::info!("Application Startup!"); + + loop { + defmt::info!("Blink!"); + self.board_led.set_high().unwrap(); + self.delay.delay_ms(500u32); + + defmt::info!("Blonk!"); + self.board_led.set_low().unwrap(); + self.delay.delay_ms(500u32); + } + + //exit(); + } +} diff --git a/src/application/setup.rs b/src/application/setup.rs new file mode 100644 index 0000000..bd12a64 --- /dev/null +++ b/src/application/setup.rs @@ -0,0 +1,31 @@ +use stm32f1xx_hal::{delay::Delay, prelude::*, stm32}; + +use crate::application::App; + +pub fn setup(cp: cortex_m::peripheral::Peripherals, dp: stm32::Peripherals) -> App { + // Take ownership over the raw flash and rcc devices and convert them into the corresponding + // HAL structs + let mut flash = dp.FLASH.constrain(); + + let mut rcc = dp.RCC.constrain(); + + // Freeze the configuration of all the clocks in the system and store the frozen frequencies in + // `clocks` + let clocks = rcc + .cfgr + .use_hse(8.mhz()) + .sysclk(72.mhz()) + .pclk1(36.mhz()) + .freeze(&mut flash.acr); + + defmt::info!("Clock Setup done"); + + // Acquire the GPIOC peripheral + let mut gpioc = dp.GPIOC.split(&mut rcc.apb2); + + let board_led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh); + + let delay = Delay::new(cp.SYST, clocks); + + App { delay, board_led } +} diff --git a/src/lib.rs b/src/lib.rs index 97aff03..cc1e69a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,8 @@ use defmt_rtt as _; // global logger use panic_probe as _; use stm32f1xx_hal as _; +pub mod application; + // same panicking *behavior* as `panic-probe` but doesn't print a panic message // this prevents the panic message being printed *twice* when `defmt::panic` is invoked #[defmt::panic_handler] diff --git a/src/main.rs b/src/main.rs index 32352df..b2733d7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,19 @@ #![no_std] #![no_main] -use wspr_beacon::exit; +use stm32f1xx_hal::pac; + +use wspr_beacon::application; #[cortex_m_rt::entry] fn main() -> ! { defmt::info!("Hello, world!"); - exit() + // Get access to the core peripherals from the cortex-m crate + let cp = cortex_m::Peripherals::take().unwrap(); + // Get access to the device specific peripherals from the peripheral access crate + let dp = pac::Peripherals::take().unwrap(); + + let app = application::setup(cp, dp); + app.run() }