use core::marker::PhantomData; use super::{gpio, sys_timer, usb}; use crate::device::flash_store::{self, FlashStore}; use crate::laser_diode::current_sources::{*}; use crate::laser_diode::ld_drive::{self, LdDrive}; use crate::thermostat::max1968::MAX1968; use crate::thermostat::thermostat::Thermostat; 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 = 4000; #[cfg(feature = "semihosting")] const WATCHDOG_PERIOD: u32 = 30000; pub fn bootup( mut core_perif: CorePeripherals, perif: Peripherals, ) -> (IndependentWatchdog, FlashStore, LdDrive, Thermostat) { 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, _ad7172_phy, max1968_phy) = gpio::setup( clocks, perif.TIM4, perif.GPIOA, perif.GPIOB, perif.GPIOC, perif.GPIOD, perif.GPIOG, perif.SPI1, perif.SPI2, perif.SPI3, perif.OTG_FS_GLOBAL, perif.OTG_FS_DEVICE, perif.OTG_FS_PWRCLK, ); usb::State::setup(usb); let current_source = CurrentSource::new(current_source_phy, perif.ADC2); let mut laser = LdDrive::new(current_source); laser.setup(); laser.ld_open(); laser.power_up(); laser.set_ld_drive_current_limit(ElectricCurrent::new::(0.2)); laser.ld_set_i(ElectricCurrent::new::(0.15)); // Set a non-zero value so that there is a non NAN power reading laser.set_pd_i_to_out_pwr(ld_drive::IToPowerUnit {dimension: PhantomData, units: PhantomData, value: 0.001}); let tec_driver = MAX1968::new(max1968_phy, perif.ADC1); let mut thermostat = Thermostat::new(tec_driver); thermostat.setup(); thermostat.power_up(); thermostat.calibrate_dac_value(); thermostat.set_i(ElectricCurrent::new::(1.0)); let flash_store = flash_store::store(perif.FLASH); let mut wd = IndependentWatchdog::new(perif.IWDG); wd.start(WATCHDOG_PERIOD.millis()); wd.feed(); info!("Kirdy setup complete"); (wd, flash_store, laser, thermostat) }