kirdy/src/laser_diode/ld_ctrl.rs

71 lines
2.0 KiB
Rust
Raw Normal View History

2022-10-21 12:05:55 +08:00
use stm32f4xx_hal::{
2024-01-24 12:11:35 +08:00
gpio::{gpioa::*, gpiob::*, gpiod::*, Alternate, Output, PushPull},
hal::{blocking::spi::Transfer, digital::v2::OutputPin},
2024-01-24 12:11:35 +08:00
pac::SPI2,
2022-10-22 15:49:01 +08:00
spi::{NoMiso, Spi, TransferModeNormal},
2022-10-21 12:05:55 +08:00
};
use uom::si::{
ratio::ratio,
f64::{ElectricPotential, ElectricCurrent},
};
use crate::laser_diode::max5719::{self, Dac};
2024-01-18 15:21:59 +08:00
use crate::laser_diode::laser_diode::TransimpedanceUnit;
2024-01-05 15:00:50 +08:00
pub trait ChannelPins {
2022-10-22 15:49:01 +08:00
type CurrentSourceShort: OutputPin;
type Max5719Load: OutputPin;
type Max5719Cs: OutputPin;
type Max5719Spi: Transfer<u8>;
2022-10-21 12:05:55 +08:00
}
2024-01-18 16:13:08 +08:00
pub struct LdCtrlPhy<C: ChannelPins> {
pub dac: Dac<C::Max5719Spi, C::Max5719Cs, C::Max5719Load>,
pub current_source_short_pin: C::CurrentSourceShort,
2022-10-21 12:05:55 +08:00
}
2024-01-05 15:00:50 +08:00
pub struct Channel0;
2022-10-21 12:05:55 +08:00
2024-01-05 15:00:50 +08:00
impl ChannelPins for Channel0 {
2022-10-22 15:49:01 +08:00
type CurrentSourceShort = PA4<Output<PushPull>>;
type Max5719Load = DacLoad;
type Max5719Cs = DacCs;
type Max5719Spi = DacSpi;
2022-10-21 12:05:55 +08:00
}
type DacSpi = Spi<SPI2, (PB10<Alternate<5>>, NoMiso, PB15<Alternate<5>>), TransferModeNormal>;
type DacCs = PD8<Output<PushPull>>;
type DacLoad = PB14<Output<PushPull>>;
2022-10-21 12:05:55 +08:00
2024-01-18 16:13:08 +08:00
pub struct LdCtrl{
pub phy: LdCtrlPhy<Channel0>,
2022-10-22 01:58:18 +08:00
}
2024-01-18 16:13:08 +08:00
impl LdCtrl {
2024-01-24 12:11:35 +08:00
pub fn new(phy_ch0: LdCtrlPhy<Channel0>) -> Self {
2024-01-18 16:13:08 +08:00
LdCtrl {
phy: phy_ch0,
}
}
// LD Terminals are shorted together
pub fn ld_short_enable(&mut self) {
self.phy.current_source_short_pin.set_low();
}
// LD Current flows from anode to cathode
pub fn ld_short_disable(&mut self) {
self.phy.current_source_short_pin.set_high();
}
pub fn set_dac(&mut self, voltage: ElectricPotential, dac_out_v_max: ElectricPotential) -> ElectricPotential {
let value = ((voltage / dac_out_v_max).get::<ratio>()
* (max5719::MAX_VALUE as f64)) as u32;
self.phy.dac.set(value).unwrap();
voltage
}
2024-01-09 16:53:34 +08:00
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
}
}