use super::{gpio, sys_timer, usb}; use crate::laser_diode::current_sources::*; use crate::thermostat::max1968::MAX1968; use fugit::ExtU32; use log::info; use stm32f4xx_hal::{ pac::{CorePeripherals, Peripherals}, rcc::RccExt, time::MegaHertz, watchdog::IndependentWatchdog, }; use uom::si::{electric_current::ampere, f64::ElectricCurrent}; #[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, MAX1968) { 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(); sys_timer::setup(core_perif.SYST, clocks); let (_eth_pins, usb, current_source_phy, max1968_phy) = gpio::setup( clocks, perif.TIM4, perif.GPIOA, perif.GPIOB, perif.GPIOC, perif.GPIOD, perif.GPIOG, perif.SPI1, perif.SPI2, perif.OTG_FS_GLOBAL, perif.OTG_FS_DEVICE, perif.OTG_FS_PWRCLK, ); usb::State::setup(usb); let mut laser = CurrentSource { phy: current_source_phy, setting: CurrentSourceSettings { output_current: 0.0, }, }; laser.setup(); laser.set_current(0.1).unwrap(); let mut tec_driver = MAX1968::new(max1968_phy, perif.ADC1); tec_driver.setup(); tec_driver.set_i(ElectricCurrent::new::(1.0)); tec_driver.power_up(); let mut wd = IndependentWatchdog::new(perif.IWDG); wd.start(WATCHDOG_PERIOD.millis()); wd.feed(); info!("Kirdy setup complete"); (wd, tec_driver) }