kirdy/src/laser_diode/laser_diode.rs

144 lines
4.2 KiB
Rust
Raw Normal View History

use miniconf::Miniconf;
2024-01-24 12:11:35 +08:00
use stm32f4xx_hal::pac::ADC2;
2024-01-18 16:13:08 +08:00
use crate::laser_diode::ld_ctrl::LdCtrl;
2024-01-26 11:43:53 +08:00
use crate::laser_diode::ld_pwr_exc_protector::{LdPwrExcProtector, self};
2024-01-26 12:25:26 +08:00
use crate::laser_diode::pd_responsitivity;
use core::marker::PhantomData;
use uom::si::{
2024-01-09 16:53:34 +08:00
electric_current::milliampere,
f64::{ElectricPotential, ElectricCurrent, Power},
};
use uom::{si::{ISQ, SI, Quantity}, typenum::*};
// Volt / Ampere
2024-01-09 16:53:34 +08:00
pub type TransimpedanceUnit = Quantity<ISQ<P2, P1, N3, N2, Z0, Z0, Z0>, SI<f64>, f64>;
// Ampere / Volt
2024-01-09 16:53:34 +08:00
type TransconductanceUnit = Quantity<ISQ<N2, N1, P3, P2, Z0, Z0, Z0>, SI<f64>, f64>;
// Watt / Ampere
2024-01-09 16:53:34 +08:00
pub type IToPowerUnit = Quantity<ISQ<P2, P1, N3, N1, Z0, Z0, Z0>, SI<f64>, f64>;
impl Settings{
pub const DAC_OUT_V_MAX: ElectricPotential = ElectricPotential {
dimension: PhantomData,
units: PhantomData,
value: 4.096,
};
// Unit: A/V
2024-01-09 16:53:34 +08:00
const PD_MON_TRANSCONDUCTANCE: TransconductanceUnit = TransconductanceUnit {
dimension: PhantomData,
units: PhantomData,
value: 0.001,
};
2024-01-09 16:53:34 +08:00
const LD_DRIVE_TRANSIMPEDANCE: TransimpedanceUnit = TransimpedanceUnit {
dimension: PhantomData,
units: PhantomData,
value: 10.0 / 0.75,
};
}
#[derive(Clone, Debug, Miniconf)]
pub struct Settings {
ld_drive_current: ElectricCurrent,
2024-01-11 12:51:08 +08:00
ld_drive_current_limit: ElectricCurrent,
#[miniconf(defer)]
2024-01-26 12:25:26 +08:00
pd_responsitivity: pd_responsitivity::Parameters,
}
impl Default for Settings {
fn default() -> Self {
Self {
ld_drive_current: ElectricCurrent::new::<milliampere>(0.0),
2024-01-11 12:51:08 +08:00
ld_drive_current_limit: ElectricCurrent::new::<milliampere>(0.0),
2024-01-26 12:25:26 +08:00
pd_responsitivity: pd_responsitivity::Parameters::default(),
}
}
}
2024-01-09 16:53:34 +08:00
pub struct LdDrive{
2024-01-18 16:13:08 +08:00
ctrl: LdCtrl,
settings: Settings,
}
2024-01-09 16:53:34 +08:00
impl LdDrive{
2024-01-26 11:43:53 +08:00
pub fn new(current_source: LdCtrl, pins_adc: ADC2, phy: ld_pwr_exc_protector::LdPwrExcProtectorPhy)-> Self {
LdPwrExcProtector::setup(pins_adc, phy);
2024-01-24 12:11:35 +08:00
2024-01-09 16:53:34 +08:00
LdDrive {
ctrl: current_source,
settings: Settings::default()
}
}
2024-01-09 16:13:42 +08:00
pub fn setup(&mut self) {
2024-01-26 11:43:53 +08:00
LdPwrExcProtector::pwr_off();
2024-01-09 16:13:42 +08:00
self.ld_set_i(ElectricCurrent::new::<milliampere>(0.0));
self.ld_short();
}
pub fn get_ld_drive_current(&mut self) -> ElectricCurrent{
self.settings.ld_drive_current
}
2024-01-11 12:51:08 +08:00
pub fn set_ld_drive_current_limit(&mut self, i_limit: ElectricCurrent){
self.settings.ld_drive_current_limit = i_limit;
}
pub fn ld_short(&mut self) {
self.ctrl.ld_short_enable();
}
pub fn ld_open(&mut self) {
self.ctrl.ld_short_disable();
}
2024-01-24 12:11:35 +08:00
pub fn power_up(&mut self){
2024-01-26 11:43:53 +08:00
LdPwrExcProtector::pwr_on_and_arm_protection();
2024-01-24 12:11:35 +08:00
}
pub fn power_down(&mut self){
2024-01-26 11:43:53 +08:00
LdPwrExcProtector::pwr_off();
2024-01-24 12:11:35 +08:00
}
pub fn get_pd_i(&mut self) -> ElectricCurrent {
2024-01-26 11:43:53 +08:00
LdPwrExcProtector::get_status().v * Settings::PD_MON_TRANSCONDUCTANCE
}
pub fn ld_set_i(&mut self, i: ElectricCurrent) -> ElectricCurrent {
2024-01-11 12:51:08 +08:00
let ld_i_set = i.min(self.settings.ld_drive_current_limit);
let ld_i_set = self.ctrl.set_i(ld_i_set, Settings::LD_DRIVE_TRANSIMPEDANCE, Settings::DAC_OUT_V_MAX);
self.settings.ld_drive_current = ld_i_set;
ld_i_set
}
2024-01-24 12:11:35 +08:00
// Set the calibrated VDDA value obtained from ADC1 calibration
pub fn set_pd_mon_calibrated_vdda(&mut self, val_cal: u32) {
2024-01-26 11:43:53 +08:00
LdPwrExcProtector::set_calibrated_vdda(val_cal)
2024-01-24 12:11:35 +08:00
}
2024-01-26 11:43:53 +08:00
pub fn pd_mon_status(&mut self) -> ld_pwr_exc_protector::Status {
LdPwrExcProtector::get_status()
2024-01-24 12:11:35 +08:00
}
pub fn pd_mon_clear_alarm(&mut self) {
2024-01-26 11:43:53 +08:00
LdPwrExcProtector::clear_alarm_status();
2024-01-24 12:11:35 +08:00
}
2024-01-26 12:25:26 +08:00
pub fn set_pd_responsitivity(&mut self, responsitivity: pd_responsitivity::ResponsitivityUnit){
self.settings.pd_responsitivity.responsitivity = responsitivity;
}
pub fn set_ld_power_limit(&mut self, pwr_limit: Power){
LdPwrExcProtector::set_trigger_threshold_v(self.settings.pd_responsitivity
.get_ld_i_from_ld_pwr(pwr_limit) / Settings::PD_MON_TRANSCONDUCTANCE
);
2024-01-24 12:11:35 +08:00
}
pub fn set_pd_i_limit(&mut self, i: ElectricCurrent){
2024-01-26 11:43:53 +08:00
LdPwrExcProtector::set_trigger_threshold_v(i / Settings::PD_MON_TRANSCONDUCTANCE);
2024-01-24 12:11:35 +08:00
}
}