2024-01-09 16:53:34 +08:00
|
|
|
use core::marker::PhantomData;
|
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-09 16:53:34 +08:00
|
|
|
use crate::laser_diode::current_sources::{*};
|
|
|
|
use crate::laser_diode::ld_drive::{self, LdDrive};
|
|
|
|
use crate::thermostat::max1968::MAX1968;
|
2024-01-04 17:13:46 +08:00
|
|
|
use crate::thermostat::thermostat::Thermostat;
|
2022-10-22 15:49:01 +08:00
|
|
|
use fugit::ExtU32;
|
|
|
|
use log::info;
|
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
|
|
|
};
|
|
|
|
|
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-16 17:36:16 +08:00
|
|
|
let (_eth_pins, usb, current_source_phy, _ad7172_phy, max1968_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-01-09 16:20:13 +08:00
|
|
|
let current_source = CurrentSource::new(current_source_phy, perif.ADC2);
|
|
|
|
|
2024-01-09 16:53:34 +08:00
|
|
|
let mut laser = LdDrive::new(current_source);
|
2024-01-09 16:20:13 +08:00
|
|
|
laser.setup();
|
|
|
|
laser.ld_open();
|
|
|
|
laser.power_up();
|
2024-01-11 12:51:08 +08:00
|
|
|
laser.set_ld_drive_current_limit(ElectricCurrent::new::<ampere>(0.2));
|
2024-01-09 16:20:13 +08:00
|
|
|
laser.ld_set_i(ElectricCurrent::new::<ampere>(0.15));
|
|
|
|
|
2024-01-09 16:53:34 +08:00
|
|
|
// 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});
|
2024-01-09 16:20:13 +08:00
|
|
|
|
2024-01-04 17:13:46 +08:00
|
|
|
let tec_driver = MAX1968::new(max1968_phy, perif.ADC1);
|
2023-12-21 13:13:06 +08:00
|
|
|
|
2024-01-04 17:13:46 +08:00
|
|
|
let mut thermostat = Thermostat::new(tec_driver);
|
|
|
|
thermostat.setup();
|
2024-01-09 17:03:06 +08:00
|
|
|
|
2024-01-04 17:13:46 +08:00
|
|
|
thermostat.power_up();
|
2024-01-09 17:03:06 +08:00
|
|
|
thermostat.calibrate_dac_value();
|
|
|
|
thermostat.set_i(ElectricCurrent::new::<ampere>(1.0));
|
2023-12-20 12:08:48 +08:00
|
|
|
|
2024-01-15 17:06:27 +08:00
|
|
|
let flash_store = flash_store::store(perif.FLASH);
|
|
|
|
|
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
|
|
|
}
|