laser_diode: use analog wdg fns

This commit is contained in:
linuswck 2024-01-24 12:11:35 +08:00
parent c3022e9db1
commit af283b17ac
3 changed files with 44 additions and 56 deletions

View File

@ -1,5 +1,7 @@
use miniconf::Miniconf; use miniconf::Miniconf;
use stm32f4xx_hal::pac::ADC2;
use crate::laser_diode::ld_ctrl::LdCtrl; use crate::laser_diode::ld_ctrl::LdCtrl;
use crate::laser_diode::analog_wdg::{LdAnalogWdg, self};
use core::{marker::PhantomData, f64::NAN}; use core::{marker::PhantomData, f64::NAN};
use uom::si::{ use uom::si::{
@ -60,7 +62,9 @@ pub struct LdDrive{
} }
impl LdDrive{ impl LdDrive{
pub fn new(current_source: LdCtrl)-> Self{ pub fn new(current_source: LdCtrl, pins_adc: ADC2, phy: analog_wdg::LdAnalogWdgPhy)-> Self {
LdAnalogWdg::setup(pins_adc, phy);
LdDrive { LdDrive {
ctrl: current_source, ctrl: current_source,
settings: Settings::default() settings: Settings::default()
@ -68,19 +72,11 @@ impl LdDrive{
} }
pub fn setup(&mut self) { pub fn setup(&mut self) {
self.power_down(); LdAnalogWdg::pwr_disengage();
self.ld_set_i(ElectricCurrent::new::<milliampere>(0.0)); self.ld_set_i(ElectricCurrent::new::<milliampere>(0.0));
self.ld_short(); self.ld_short();
} }
pub fn power_up(&mut self){
self.ctrl.power_up();
}
pub fn power_down(&mut self){
self.ctrl.power_down();
}
pub fn get_ld_drive_current(&mut self) -> ElectricCurrent{ pub fn get_ld_drive_current(&mut self) -> ElectricCurrent{
self.settings.ld_drive_current self.settings.ld_drive_current
} }
@ -97,8 +93,16 @@ impl LdDrive{
self.ctrl.ld_short_disable(); self.ctrl.ld_short_disable();
} }
pub fn power_up(&mut self){
LdAnalogWdg::pwr_engage();
}
pub fn power_down(&mut self){
LdAnalogWdg::pwr_disengage();
}
pub fn get_pd_i(&mut self) -> ElectricCurrent { pub fn get_pd_i(&mut self) -> ElectricCurrent {
self.ctrl.get_pd_mon_v() * Settings::PD_MON_TRANSCONDUCTANCE LdAnalogWdg::get_pd_v() * Settings::PD_MON_TRANSCONDUCTANCE
} }
pub fn ld_set_i(&mut self, i: ElectricCurrent) -> ElectricCurrent { pub fn ld_set_i(&mut self, i: ElectricCurrent) -> ElectricCurrent {
@ -107,4 +111,30 @@ impl LdDrive{
self.settings.ld_drive_current = ld_i_set; self.settings.ld_drive_current = ld_i_set;
ld_i_set ld_i_set
} }
// Set the calibrated VDDA value obtained from ADC1 calibration
pub fn set_pd_mon_calibrated_vdda(val_cal: u32) {
LdAnalogWdg::set_calibrated_vdda(val_cal)
}
pub fn pd_mon_status() -> analog_wdg::AlarmStatus {
LdAnalogWdg::get_alarm_status()
}
pub fn pd_mon_clear_alarm(&mut self) {
LdAnalogWdg::clear_alarm_status();
}
pub fn pd_mon_engage(){
LdAnalogWdg::enable_watchdog_interrupt()
}
pub fn pd_mon_disengage(){
LdAnalogWdg::disable_watchdog_interrupt()
}
pub fn set_ld_power_limit(pwr_limit: Power){
// LdAnalogWdg::set_htr(convert pwr_limit to raw adc code)
unimplemented!()
}
} }

View File

@ -1,17 +1,12 @@
use stm32f4xx_hal::{ use stm32f4xx_hal::{
adc::{ gpio::{gpioa::*, gpiob::*, gpiod::*, Alternate, Output, PushPull},
config::{self, AdcConfig},
Adc,
},
gpio::{gpioa::*, gpiob::*, gpiod::*, Alternate, Output, PushPull, Analog, PD9},
hal::{blocking::spi::Transfer, digital::v2::OutputPin}, hal::{blocking::spi::Transfer, digital::v2::OutputPin},
pac::{SPI2, ADC2}, pac::SPI2,
spi::{NoMiso, Spi, TransferModeNormal}, spi::{NoMiso, Spi, TransferModeNormal},
}; };
use uom::si::{ use uom::si::{
ratio::ratio, ratio::ratio,
electric_potential::millivolt,
f64::{ElectricPotential, ElectricCurrent}, f64::{ElectricPotential, ElectricCurrent},
}; };
@ -19,8 +14,6 @@ use crate::laser_diode::max5719::{self, Dac};
use crate::laser_diode::laser_diode::TransimpedanceUnit; use crate::laser_diode::laser_diode::TransimpedanceUnit;
pub trait ChannelPins { pub trait ChannelPins {
type PdMonPin;
type CurrentSourceLdoEn: OutputPin;
type CurrentSourceShort: OutputPin; type CurrentSourceShort: OutputPin;
type Max5719Load: OutputPin; type Max5719Load: OutputPin;
type Max5719Cs: OutputPin; type Max5719Cs: OutputPin;
@ -29,15 +22,11 @@ pub trait ChannelPins {
pub struct LdCtrlPhy<C: ChannelPins> { pub struct LdCtrlPhy<C: ChannelPins> {
pub dac: Dac<C::Max5719Spi, C::Max5719Cs, C::Max5719Load>, pub dac: Dac<C::Max5719Spi, C::Max5719Cs, C::Max5719Load>,
pub current_source_ldo_en_pin: C::CurrentSourceLdoEn,
pub current_source_short_pin: C::CurrentSourceShort, pub current_source_short_pin: C::CurrentSourceShort,
pub pd_mon_pin : C::PdMonPin,
} }
pub struct Channel0; pub struct Channel0;
impl ChannelPins for Channel0 { impl ChannelPins for Channel0 {
type PdMonPin = PA3<Analog>;
type CurrentSourceLdoEn = PD9<Output<PushPull>>;
type CurrentSourceShort = PA4<Output<PushPull>>; type CurrentSourceShort = PA4<Output<PushPull>>;
type Max5719Load = DacLoad; type Max5719Load = DacLoad;
type Max5719Cs = DacCs; type Max5719Cs = DacCs;
@ -48,38 +37,17 @@ type DacSpi = Spi<SPI2, (PB10<Alternate<5>>, NoMiso, PB15<Alternate<5>>), Transf
type DacCs = PD8<Output<PushPull>>; type DacCs = PD8<Output<PushPull>>;
type DacLoad = PB14<Output<PushPull>>; type DacLoad = PB14<Output<PushPull>>;
pub struct LdCtrl{ pub struct LdCtrl{
pub phy: LdCtrlPhy<Channel0>, pub phy: LdCtrlPhy<Channel0>,
pub pins_adc: Adc<ADC2>
} }
impl LdCtrl { impl LdCtrl {
pub fn new(phy_ch0: LdCtrlPhy<Channel0>, adc2: ADC2) -> Self { pub fn new(phy_ch0: LdCtrlPhy<Channel0>) -> Self {
let config = AdcConfig::default()
.clock(config::Clock::Pclk2_div_2)
.default_sample_time(config::SampleTime::Cycles_480);
let pins_adc = Adc::adc2(adc2, true, config);
LdCtrl { LdCtrl {
phy: phy_ch0, phy: phy_ch0,
pins_adc: pins_adc,
} }
} }
pub fn power_up(&mut self) {
self.phy.current_source_ldo_en_pin.set_high();
}
#[deprecated(note=
"To be removed when rev0_3 has arrived
Rev0_2 has hardware connection bug for LD_EN.
LD_EN will always be enabled.")]
pub fn power_down(&mut self) {
self.phy.current_source_ldo_en_pin.set_low();
}
// LD Terminals are shorted together // LD Terminals are shorted together
pub fn ld_short_enable(&mut self) { pub fn ld_short_enable(&mut self) {
self.phy.current_source_short_pin.set_low(); self.phy.current_source_short_pin.set_low();
@ -90,15 +58,6 @@ impl LdCtrl {
self.phy.current_source_short_pin.set_high(); self.phy.current_source_short_pin.set_high();
} }
pub fn get_pd_mon_v(&mut self) -> ElectricPotential{
let sample = self.pins_adc.convert(
&self.phy.pd_mon_pin,
stm32f4xx_hal::adc::config::SampleTime::Cycles_480,
);
let mv = self.pins_adc.sample_to_millivolts(sample as u16);
ElectricPotential::new::<millivolt>(mv as f64)
}
pub fn set_dac(&mut self, voltage: ElectricPotential, dac_out_v_max: ElectricPotential) -> ElectricPotential { pub fn set_dac(&mut self, voltage: ElectricPotential, dac_out_v_max: ElectricPotential) -> ElectricPotential {
let value = ((voltage / dac_out_v_max).get::<ratio>() let value = ((voltage / dac_out_v_max).get::<ratio>()
* (max5719::MAX_VALUE as f64)) as u32; * (max5719::MAX_VALUE as f64)) as u32;

View File

@ -74,7 +74,6 @@ fn main() -> ! {
wd.feed(); wd.feed();
info!("looping"); info!("looping");
info!("curr_ld_power: {:?}", mili_watt_fmt.with(laser.get_ld_power_output()));
info!("curr_ld_drive_cuurent: {:?}", mili_amp_fmt.with(laser.get_ld_drive_current())); info!("curr_ld_drive_cuurent: {:?}", mili_amp_fmt.with(laser.get_ld_drive_current()));
info!("curr_dac_vfb: {:?}", volt_fmt.with(thermostat.get_dac_vfb())); info!("curr_dac_vfb: {:?}", volt_fmt.with(thermostat.get_dac_vfb()));