126 lines
3.9 KiB
Rust
126 lines
3.9 KiB
Rust
use fugit::ExtU32;
|
|
use log::{debug, info};
|
|
use stm32f4xx_hal::{pac::{CorePeripherals, Peripherals},
|
|
rcc::RccExt,
|
|
time::MegaHertz,
|
|
timer::TimerExt,
|
|
watchdog::IndependentWatchdog};
|
|
use uom::si::{electric_current::{ampere, milliampere},
|
|
f32::ElectricCurrent};
|
|
|
|
use super::{gpio, sys_timer, usb};
|
|
use crate::{device::flash_store::{self, FlashStore},
|
|
laser_diode::{laser_diode::LdDrive, ld_ctrl::*},
|
|
net::net::{IpSettings, ServerHandle},
|
|
thermostat::{max1968::MAX1968, thermostat::Thermostat},
|
|
DeviceSettings};
|
|
|
|
#[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(42).convert())
|
|
.pclk2(MegaHertz::from_raw(84).convert())
|
|
.freeze();
|
|
|
|
sys_timer::setup(core_perif.SYST, clocks);
|
|
|
|
let (mut hw_rev, 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.GPIOE,
|
|
perif.SPI1,
|
|
perif.SPI2,
|
|
perif.SPI3,
|
|
perif.OTG_FS_GLOBAL,
|
|
perif.OTG_FS_DEVICE,
|
|
perif.OTG_FS_PWRCLK,
|
|
);
|
|
|
|
usb::State::setup(usb);
|
|
|
|
debug!("Setting up TEC");
|
|
let tec_driver = MAX1968::new(max1968_phy, perif.ADC1, perif.ADC2, perif.DMA2);
|
|
let mut thermostat = Thermostat::new(tec_driver, ad7172_phy);
|
|
thermostat.setup();
|
|
thermostat.power_up();
|
|
thermostat.calibrate_dac_value();
|
|
thermostat.set_i(ElectricCurrent::new::<ampere>(0.0));
|
|
|
|
debug!("Setting up Laser Driver");
|
|
let current_source = LdCtrl::new(current_source_phy);
|
|
let mut laser = LdDrive::new(current_source, perif.ADC3, perif.TIM2.counter(&clocks), pd_mon_phy);
|
|
laser.setup();
|
|
laser.ld_open();
|
|
laser.ld_set_i(ElectricCurrent::new::<ampere>(0.0));
|
|
laser.set_pd_i_limit(ElectricCurrent::new::<milliampere>(2.5));
|
|
laser.set_pd_mon_calibrated_vdda(thermostat.get_calibrated_vdda());
|
|
laser.power_up();
|
|
|
|
debug!("Setting up Internal Flash Driver");
|
|
let flash_store = flash_store::store(perif.FLASH);
|
|
|
|
let mut ip_settings: IpSettings = IpSettings::default();
|
|
let device_settings: DeviceSettings;
|
|
match flash_store.read_value("Device") {
|
|
Ok(Some(config)) => {
|
|
device_settings = config;
|
|
ip_settings = device_settings.ip_settings;
|
|
debug!("Found Device Settings");
|
|
}
|
|
Ok(None) => {
|
|
debug!("Flash does not have IP Settings");
|
|
}
|
|
Err(e) => {
|
|
debug!("Cannot Store Flash: {:?}", e);
|
|
}
|
|
}
|
|
|
|
debug!("Setting up ETH");
|
|
let mac_addr = hw_rev.get_mac_address();
|
|
let ethernet_parts_in = stm32_eth::PartsIn {
|
|
dma: perif.ETHERNET_DMA,
|
|
mac: perif.ETHERNET_MAC,
|
|
mmc: perif.ETHERNET_MMC,
|
|
ptp: perif.ETHERNET_PTP,
|
|
};
|
|
ServerHandle::new(
|
|
eth_pins,
|
|
eth_mgmt_pins,
|
|
ethernet_parts_in,
|
|
clocks,
|
|
perif.TIM5.counter(&clocks),
|
|
mac_addr,
|
|
ip_settings,
|
|
);
|
|
|
|
debug!("Setting Watchdog");
|
|
let mut wd = IndependentWatchdog::new(perif.IWDG);
|
|
wd.start(WATCHDOG_PERIOD.millis());
|
|
wd.feed();
|
|
|
|
info!("Kirdy setup complete");
|
|
|
|
(wd, flash_store, laser, thermostat)
|
|
}
|