From 18a603ab5f3bf8add34384b522dee3afbe0c63aa Mon Sep 17 00:00:00 2001 From: topquark12 Date: Thu, 20 Oct 2022 21:21:01 +0800 Subject: [PATCH] refactor, shorten main --- src/device/boot.rs | 52 +++++++++++++++++++++++++++ src/device/{log.rs => log_setup.rs} | 0 src/device/mod.rs | 5 +-- src/device/{timer.rs => sys_timer.rs} | 0 src/main.rs | 38 +++----------------- 5 files changed, 60 insertions(+), 35 deletions(-) create mode 100644 src/device/boot.rs rename src/device/{log.rs => log_setup.rs} (100%) rename src/device/{timer.rs => sys_timer.rs} (100%) diff --git a/src/device/boot.rs b/src/device/boot.rs new file mode 100644 index 0000000..193a6d1 --- /dev/null +++ b/src/device/boot.rs @@ -0,0 +1,52 @@ +use stm32f4xx_hal::{ + watchdog::IndependentWatchdog, + rcc::RccExt, + pac::{CorePeripherals, Peripherals}, + time::MegaHertz, prelude::_stm32f4xx_hal_gpio_GpioExt, +}; +use fugit::ExtU32; +use log::info; +use super::{log_setup, sys_timer, usb}; + +#[cfg(not(feature = "semihosting"))] +const WATCHDOG_PERIOD: u32 = 1000; +#[cfg(feature = "semihosting")] +const WATCHDOG_PERIOD: u32 = 30000; + +pub fn bootup (mut core_perif : CorePeripherals, perif : Peripherals) -> ( + IndependentWatchdog +) { + + log_setup::init_log(); + info!("Kirdy init"); + + core_perif.SCB.enable_icache(); + core_perif.SCB.enable_dcache(&mut core_perif.CPUID); + + + let clocks = perif.RCC.constrain() + .cfgr + .use_hse(MegaHertz::from_raw(8).convert()) + .sysclk(MegaHertz::from_raw(168).convert()) + .hclk(MegaHertz::from_raw(168).convert()) + .pclk1(MegaHertz::from_raw(32).convert()) + .pclk2(MegaHertz::from_raw(64).convert()) + .freeze(); + + // let (pins, eth_pins, usb) = Pins::setup( + // clocks, perif.TIM4, + // perif.GPIOA, perif.GPIOB, perif.GPIOC, perif.GPIOD, perif.GPIOE, perif.GPIOF, perif.GPIOG, + // perif.SPI1, perif.SPI2, perif.SPI3, + // perif.ADC1, + // perif.OTG_FS_GLOBAL, perif.OTG_FS_DEVICE, perif.OTG_FS_PWRCLK, + // ); + + let mut wd = IndependentWatchdog::new(perif.IWDG); + wd.start(WATCHDOG_PERIOD.millis()); + wd.feed(); + + sys_timer::setup(core_perif.SYST, clocks); + + (wd) + +} \ No newline at end of file diff --git a/src/device/log.rs b/src/device/log_setup.rs similarity index 100% rename from src/device/log.rs rename to src/device/log_setup.rs diff --git a/src/device/mod.rs b/src/device/mod.rs index 37d3a02..094bf6f 100644 --- a/src/device/mod.rs +++ b/src/device/mod.rs @@ -1,4 +1,5 @@ -pub mod log; +pub mod log_setup; pub mod usb; -pub mod timer; +pub mod sys_timer; +pub mod boot; // pub mod init_gpio; \ No newline at end of file diff --git a/src/device/timer.rs b/src/device/sys_timer.rs similarity index 100% rename from src/device/timer.rs rename to src/device/sys_timer.rs diff --git a/src/main.rs b/src/main.rs index 628f5e4..9807aa7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,50 +10,22 @@ use stm32f4xx_hal::{ time::MegaHertz, prelude::_stm32f4xx_hal_gpio_GpioExt, }; use log::info; -use fugit::ExtU32; mod device; -use device::log::init_log; -// use setup::init_gpio::Pins; - -use device::timer; - -#[cfg(not(feature = "semihosting"))] -const WATCHDOG_PERIOD: u32 = 1000; -#[cfg(feature = "semihosting")] -const WATCHDOG_PERIOD: u32 = 30000; +use device::boot::bootup; +use device::sys_timer; #[entry] fn main() -> ! { - init_log(); - info!("Kirdy init"); let mut core_perif = CorePeripherals::take().unwrap(); - core_perif.SCB.enable_icache(); - core_perif.SCB.enable_dcache(&mut core_perif.CPUID); - let perif = Peripherals::take().unwrap(); - let clocks = perif.RCC.constrain() - .cfgr - .use_hse(MegaHertz::from_raw(8).convert()) - .sysclk(MegaHertz::from_raw(168).convert()) - .hclk(MegaHertz::from_raw(168).convert()) - .pclk1(MegaHertz::from_raw(32).convert()) - .pclk2(MegaHertz::from_raw(64).convert()) - .freeze(); - - let mut wd = IndependentWatchdog::new(perif.IWDG); - wd.start(WATCHDOG_PERIOD.millis()); - wd.feed(); - - timer::setup(core_perif.SYST, clocks); - let gpioc = perif.GPIOC.split(); - let mut pc9 = gpioc.pc9.into_push_pull_output(); + let (mut wd) = bootup(core_perif, perif); loop { - pc9.toggle(); - timer::sleep(1); wd.feed(); + info!("looping"); + sys_timer::sleep(10); } } \ No newline at end of file