use super::{gpio, sys_timer, usb}; use crate::device::flash_store::{self, FlashStore}; use crate::device::sys_timer::sleep; use crate::laser_diode::ld_ctrl::{*}; use crate::laser_diode::laser_diode::LdDrive; use crate::thermostat::max1968::MAX1968; use crate::thermostat::thermostat::Thermostat; use crate::net::net::ServerHandle; use stm32_eth; use fugit::ExtU32; use log::info; use stm32f4xx_hal::{ pac::{CorePeripherals, Peripherals}, rcc::RccExt, time::MegaHertz, watchdog::IndependentWatchdog, }; use uom::si::electric_current::milliampere; 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, eth_mgmt_pins, usb, current_source_phy, ad7172_phy, max1968_phy, pd_mon_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 ethernet_parts_in = stm32_eth::PartsIn { dma: perif.ETHERNET_DMA, mac: perif.ETHERNET_MAC, mmc: perif.ETHERNET_MMC, ptp: perif.ETHERNET_PTP, }; sleep(1000); info!("Setting up ETH"); ServerHandle::new(eth_pins, eth_mgmt_pins, ethernet_parts_in, clocks); info!("Setting finish setting up ETH"); let current_source = LdCtrl::new(current_source_phy); info!("Setting up Laser"); let mut laser = LdDrive::new(current_source, perif.ADC2, pd_mon_phy); laser.setup(); laser.ld_open(); laser.set_ld_drive_current_limit(ElectricCurrent::new::(0.2)); laser.ld_set_i(ElectricCurrent::new::(0.15)); laser.set_pd_i_limit(ElectricCurrent::new::(2.5)); laser.power_up(); info!("Setting up TEC"); let tec_driver = MAX1968::new(max1968_phy, perif.ADC1); let mut thermostat = Thermostat::new(tec_driver, ad7172_phy); thermostat.setup(); thermostat.power_up(); thermostat.calibrate_dac_value(); thermostat.set_i(ElectricCurrent::new::(1.0)); laser.set_pd_mon_calibrated_vdda(thermostat.get_calibrated_vdda()); 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) }