2022-10-22 21:00:17 +08:00
|
|
|
use super::{gpio, sys_timer, usb};
|
2024-01-15 17:06:27 +08:00
|
|
|
use crate::device::flash_store::{self, FlashStore};
|
2024-01-18 16:13:08 +08:00
|
|
|
use crate::laser_diode::ld_ctrl::{*};
|
2024-01-24 12:14:11 +08:00
|
|
|
use crate::laser_diode::laser_diode::LdDrive;
|
2024-01-09 16:53:34 +08:00
|
|
|
use crate::thermostat::max1968::MAX1968;
|
2024-01-04 17:13:46 +08:00
|
|
|
use crate::thermostat::thermostat::Thermostat;
|
2024-01-31 17:16:10 +08:00
|
|
|
use crate::net::net::ServerHandle;
|
|
|
|
use stm32_eth;
|
2022-10-22 15:49:01 +08:00
|
|
|
use fugit::ExtU32;
|
2024-02-01 15:49:41 +08:00
|
|
|
use log::{info, debug};
|
2022-10-20 21:21:01 +08:00
|
|
|
use stm32f4xx_hal::{
|
|
|
|
pac::{CorePeripherals, Peripherals},
|
2022-10-22 15:49:01 +08:00
|
|
|
rcc::RccExt,
|
|
|
|
time::MegaHertz,
|
|
|
|
watchdog::IndependentWatchdog,
|
2022-10-20 21:21:01 +08:00
|
|
|
};
|
|
|
|
|
2024-01-24 17:03:06 +08:00
|
|
|
use uom::si::electric_current::milliampere;
|
2023-12-21 13:13:06 +08:00
|
|
|
use uom::si::{electric_current::ampere, f64::ElectricCurrent};
|
2023-12-20 14:51:34 +08:00
|
|
|
|
2022-10-20 21:21:01 +08:00
|
|
|
#[cfg(not(feature = "semihosting"))]
|
2024-01-15 17:06:27 +08:00
|
|
|
const WATCHDOG_PERIOD: u32 = 4000;
|
2022-10-20 21:21:01 +08:00
|
|
|
#[cfg(feature = "semihosting")]
|
|
|
|
const WATCHDOG_PERIOD: u32 = 30000;
|
|
|
|
|
2023-12-21 13:13:06 +08:00
|
|
|
pub fn bootup(
|
|
|
|
mut core_perif: CorePeripherals,
|
|
|
|
perif: Peripherals,
|
2024-01-15 17:06:27 +08:00
|
|
|
) -> (IndependentWatchdog, FlashStore, LdDrive, Thermostat) {
|
2022-10-20 21:21:01 +08:00
|
|
|
core_perif.SCB.enable_icache();
|
|
|
|
core_perif.SCB.enable_dcache(&mut core_perif.CPUID);
|
|
|
|
|
2022-10-22 15:49:01 +08:00
|
|
|
let clocks = perif
|
|
|
|
.RCC
|
|
|
|
.constrain()
|
2022-10-20 21:21:01 +08:00
|
|
|
.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();
|
|
|
|
|
2022-10-22 01:58:18 +08:00
|
|
|
sys_timer::setup(core_perif.SYST, clocks);
|
|
|
|
|
2024-01-31 17:16:10 +08:00
|
|
|
let (eth_pins, eth_mgmt_pins, usb, current_source_phy, ad7172_phy, max1968_phy, pd_mon_phy) = gpio::setup(
|
2022-10-22 15:49:01 +08:00
|
|
|
clocks,
|
2023-12-20 12:08:48 +08:00
|
|
|
perif.TIM4,
|
2022-10-22 15:49:01 +08:00
|
|
|
perif.GPIOA,
|
|
|
|
perif.GPIOB,
|
|
|
|
perif.GPIOC,
|
|
|
|
perif.GPIOD,
|
|
|
|
perif.GPIOG,
|
2023-12-20 12:08:48 +08:00
|
|
|
perif.SPI1,
|
2022-10-22 15:49:01 +08:00
|
|
|
perif.SPI2,
|
2024-01-16 17:36:16 +08:00
|
|
|
perif.SPI3,
|
2022-10-22 15:49:01 +08:00
|
|
|
perif.OTG_FS_GLOBAL,
|
|
|
|
perif.OTG_FS_DEVICE,
|
|
|
|
perif.OTG_FS_PWRCLK,
|
2022-10-21 12:05:55 +08:00
|
|
|
);
|
2022-10-22 01:58:18 +08:00
|
|
|
|
|
|
|
usb::State::setup(usb);
|
|
|
|
|
2024-02-01 15:49:41 +08:00
|
|
|
debug!("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();
|
2024-02-02 17:22:57 +08:00
|
|
|
thermostat.set_i(ElectricCurrent::new::<ampere>(0.0));
|
2024-01-31 17:16:10 +08:00
|
|
|
|
2024-02-01 15:49:41 +08:00
|
|
|
debug!("Setting up Laser Driver");
|
2024-01-24 12:14:11 +08:00
|
|
|
let current_source = LdCtrl::new(current_source_phy);
|
|
|
|
let mut laser = LdDrive::new(current_source, perif.ADC2, pd_mon_phy);
|
2024-01-09 16:20:13 +08:00
|
|
|
laser.setup();
|
|
|
|
laser.ld_open();
|
2024-01-11 12:51:08 +08:00
|
|
|
laser.set_ld_drive_current_limit(ElectricCurrent::new::<ampere>(0.2));
|
2024-02-02 17:22:57 +08:00
|
|
|
laser.ld_set_i(ElectricCurrent::new::<ampere>(0.0));
|
2024-01-24 17:03:06 +08:00
|
|
|
laser.set_pd_i_limit(ElectricCurrent::new::<milliampere>(2.5));
|
|
|
|
laser.set_pd_mon_calibrated_vdda(thermostat.get_calibrated_vdda());
|
2024-02-01 15:49:41 +08:00
|
|
|
laser.power_up();
|
|
|
|
|
|
|
|
debug!("Setting up Internal Flash Driver");
|
2024-01-15 17:06:27 +08:00
|
|
|
let flash_store = flash_store::store(perif.FLASH);
|
|
|
|
|
2024-02-01 15:49:41 +08:00
|
|
|
debug!("Setting up ETH");
|
|
|
|
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);
|
|
|
|
|
|
|
|
debug!("Setting Watchdog");
|
2022-10-20 21:21:01 +08:00
|
|
|
let mut wd = IndependentWatchdog::new(perif.IWDG);
|
|
|
|
wd.start(WATCHDOG_PERIOD.millis());
|
|
|
|
wd.feed();
|
|
|
|
|
2022-10-21 12:05:55 +08:00
|
|
|
info!("Kirdy setup complete");
|
|
|
|
|
2024-01-15 17:06:27 +08:00
|
|
|
(wd, flash_store, laser, thermostat)
|
2022-10-22 15:49:01 +08:00
|
|
|
}
|