1
0
forked from M-Labs/kirdy

cleanup boot.rs, thermostat.rs

This commit is contained in:
linuswck 2024-02-01 15:49:41 +08:00
parent ee0ed8ebe7
commit 172c3e1dfc
2 changed files with 26 additions and 34 deletions

View File

@ -1,6 +1,5 @@
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;
@ -8,7 +7,7 @@ use crate::thermostat::thermostat::Thermostat;
use crate::net::net::ServerHandle;
use stm32_eth;
use fugit::ExtU32;
use log::info;
use log::{info, debug};
use stm32f4xx_hal::{
pac::{CorePeripherals, Peripherals},
rcc::RccExt,
@ -62,43 +61,38 @@ pub fn bootup(
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");
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();
thermostat.set_i(ElectricCurrent::new::<ampere>(1.0));
debug!("Setting up Laser Driver");
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::<ampere>(0.2));
laser.ld_set_i(ElectricCurrent::new::<ampere>(0.15));
laser.set_pd_i_limit(ElectricCurrent::new::<milliampere>(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::<ampere>(1.0));
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);
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");
let mut wd = IndependentWatchdog::new(perif.IWDG);
wd.start(WATCHDOG_PERIOD.millis());
wd.feed();

View File

@ -6,7 +6,7 @@ use crate::thermostat::max1968::{MAX1968, AdcReadTarget, PwmPinsEnum};
use crate::thermostat::ad7172;
use crate::thermostat::pid_state::PidState;
use crate::thermostat::steinhart_hart;
use log::info;
use log::debug;
use uom::si::{
electric_current::ampere,
electric_potential::volt,
@ -167,7 +167,6 @@ impl Thermostat{
}
fn set_center_pt(&mut self, value: ElectricPotential){
info!("set center pt: {:?}", value);
self.tec_setting.center_pt = value;
}
@ -233,14 +232,14 @@ impl Thermostat{
/// VREF measurement can introduce significant noise at the current output, degrading the stabilily performance of the
/// thermostat.
pub fn calibrate_dac_value(&mut self) {
const STEPS: i32 = 18;
let target_voltage = self.max1968.adc_read(AdcReadTarget::VREF, 64);
let mut start_value = 1;
let mut best_error = ElectricPotential::new::<volt>(100.0);
for step in (0..18).rev() {
info!("Step: {} Calibrating", step);
for step in (0..STEPS).rev() {
debug!("TEC VREF Calibrating: Step {}/{}", step, STEPS);
let mut prev_value = start_value;
for value in (start_value..=ad5680::MAX_VALUE).step_by(1 << step) {
info!("Calibrating: Value: {:?}", value);
self.max1968.phy.dac.set(value).unwrap();
sys_timer::sleep(5);
@ -258,7 +257,6 @@ impl Thermostat{
prev_value = value;
}
}
info!("Best Error: {:?}", best_error);
}
pub fn pid_engaged(&mut self) -> bool {