ld: Fix unable to save & load ld_i flash settings

This commit is contained in:
linuswck 2024-03-19 12:39:50 +08:00
parent 3ad7c417f6
commit dbbd438e92
3 changed files with 19 additions and 13 deletions

View File

@ -118,7 +118,7 @@ impl LdDrive{
} }
pub fn power_up(&mut self){ pub fn power_up(&mut self){
let prev_i_set = LdCurrentOutCtrlTimer::get_target_i(); let prev_i_set = self.settings.ld_drive_current;
LdCurrentOutCtrlTimer::reset(); LdCurrentOutCtrlTimer::reset();
let _ = self.ctrl.set_i(ElectricCurrent::new::<milliampere>(0.0), Settings::LD_DRIVE_TRANSIMPEDANCE, Settings::DAC_OUT_V_MAX); let _ = self.ctrl.set_i(ElectricCurrent::new::<milliampere>(0.0), Settings::LD_DRIVE_TRANSIMPEDANCE, Settings::DAC_OUT_V_MAX);
LdPwrExcProtector::pwr_on_and_arm_protection(); LdPwrExcProtector::pwr_on_and_arm_protection();
@ -142,14 +142,14 @@ impl LdDrive{
} }
pub fn ld_set_i(&mut self, i: ElectricCurrent){ pub fn ld_set_i(&mut self, i: ElectricCurrent){
LdCurrentOutCtrlTimer::set_target_i_and_listen_irq(i, self.settings.ld_drive_current); self.settings.ld_drive_current = i;
LdCurrentOutCtrlTimer::set_target_i_and_listen_irq(i, self.ctrl.get_i_set());
} }
pub fn poll_and_update_output_current(&mut self) -> ElectricCurrent { pub fn poll_and_update_output_current(&mut self) -> ElectricCurrent {
match LdCurrentOutCtrlTimer::get_irq_status() { match LdCurrentOutCtrlTimer::get_irq_status() {
Some(i_set) => { Some(i_set) => {
let i_set = self.ctrl.set_i(i_set, Settings::LD_DRIVE_TRANSIMPEDANCE, Settings::DAC_OUT_V_MAX); let i_set = self.ctrl.set_i(i_set, Settings::LD_DRIVE_TRANSIMPEDANCE, Settings::DAC_OUT_V_MAX);
self.settings.ld_drive_current = i_set;
LdCurrentOutCtrlTimer::clear_alarm_and_resume_listening(); LdCurrentOutCtrlTimer::clear_alarm_and_resume_listening();
i_set i_set
} }
@ -195,10 +195,15 @@ impl LdDrive{
} }
pub fn get_status_report(&mut self) -> StatusReport { pub fn get_status_report(&mut self) -> StatusReport {
let ld_i_set = if LdPwrExcProtector::get_status().pwr_engaged {
self.ctrl.get_i_set()
} else {
ElectricCurrent::new::<ampere>(0.0)
};
StatusReport { StatusReport {
pwr_on: LdPwrExcProtector::get_status().pwr_engaged, pwr_on: LdPwrExcProtector::get_status().pwr_engaged,
pwr_excursion: LdPwrExcProtector::get_status().pwr_excursion, pwr_excursion: LdPwrExcProtector::get_status().pwr_excursion,
ld_i_set: self.settings.ld_drive_current, ld_i_set: ld_i_set,
pd_i: self.get_pd_i(), pd_i: self.get_pd_i(),
pd_pwr: self.get_pd_pwr(), pd_pwr: self.get_pd_pwr(),
term_status: self.get_term_status(), term_status: self.get_term_status(),

View File

@ -8,7 +8,8 @@ use stm32f4xx_hal::{
use uom::si::{ use uom::si::{
ratio::ratio, ratio::ratio,
f32::{ElectricPotential, ElectricCurrent}, f32::{ElectricPotential, ElectricCurrent},
electric_current::ampere,
}; };
use crate::laser_diode::max5719::{self, Dac}; use crate::laser_diode::max5719::{self, Dac};
@ -49,12 +50,14 @@ type DacLoad = PB14<Output<PushPull>>;
pub struct LdCtrl{ pub struct LdCtrl{
pub phy: LdCtrlPhy<Channel0>, pub phy: LdCtrlPhy<Channel0>,
i_set: ElectricCurrent,
} }
impl LdCtrl { impl LdCtrl {
pub fn new(phy_ch0: LdCtrlPhy<Channel0>) -> Self { pub fn new(phy_ch0: LdCtrlPhy<Channel0>) -> Self {
LdCtrl { LdCtrl {
phy: phy_ch0, phy: phy_ch0,
i_set: ElectricCurrent::new::<ampere>(0.0)
} }
} }
@ -85,6 +88,11 @@ impl LdCtrl {
} }
pub fn set_i(&mut self, current: ElectricCurrent, transimpedance: TransimpedanceUnit, dac_out_v_max: ElectricPotential) -> ElectricCurrent { pub fn set_i(&mut self, current: ElectricCurrent, transimpedance: TransimpedanceUnit, dac_out_v_max: ElectricPotential) -> ElectricCurrent {
self.set_dac(current * transimpedance, dac_out_v_max) / transimpedance self.i_set = self.set_dac(current * transimpedance, dac_out_v_max) / transimpedance;
self.i_set
}
pub fn get_i_set(&mut self) -> ElectricCurrent{
self.i_set
} }
} }

View File

@ -63,13 +63,6 @@ impl LdCurrentOutCtrlTimer {
} }
} }
pub fn get_target_i() -> ElectricCurrent {
if let Some(ref mut ld_current_out_ctrl_timer ) = LdCurrentOutCtrlTimer::get() {
return ld_current_out_ctrl_timer.target_i
}
ElectricCurrent::new::<ampere>(0.0)
}
pub fn set_alarm() { pub fn set_alarm() {
if let Some(ref mut ld_current_out_ctrl_timer ) = LdCurrentOutCtrlTimer::get() { if let Some(ref mut ld_current_out_ctrl_timer ) = LdCurrentOutCtrlTimer::get() {
ld_current_out_ctrl_timer.timeout = true; ld_current_out_ctrl_timer.timeout = true;