kirdy/src/laser_diode/current_sources.rs

63 lines
1.8 KiB
Rust
Raw Normal View History

2022-10-21 12:05:55 +08:00
use stm32f4xx_hal::{
adc::{
config::{self, AdcConfig},
Adc,
},
2024-01-05 15:00:50 +08:00
gpio::{gpioa::*, gpiob::*, gpiod::*, Alternate, Output, PushPull, Analog, PD9},
hal::{blocking::spi::Transfer, digital::v2::OutputPin},
pac::{SPI2, ADC2},
2022-10-22 15:49:01 +08:00
spi::{NoMiso, Spi, TransferModeNormal},
2022-10-21 12:05:55 +08:00
};
use crate::laser_diode::max5719::{self, Dac};
2024-01-05 15:00:50 +08:00
pub trait ChannelPins {
type PdMonPin;
2022-10-22 15:49:01 +08:00
type CurrentSourceLdoEn: OutputPin;
type CurrentSourceShort: OutputPin;
type Max5719Load: OutputPin;
type Max5719Cs: OutputPin;
type Max5719Spi: Transfer<u8>;
2022-10-21 12:05:55 +08:00
}
pub struct CurrentSourcePhy<C: ChannelPins> {
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 pd_mon_pin : C::PdMonPin,
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 {
type PdMonPin = PA3<Analog>;
2022-10-22 15:49:01 +08:00
type CurrentSourceLdoEn = PD9<Output<PushPull>>;
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
pub struct CurrentSource{
pub phy: CurrentSourcePhy<Channel0>,
pub pins_adc: Adc<ADC2>
2022-10-22 01:58:18 +08:00
}
impl CurrentSource {
pub fn new(phy_ch0: CurrentSourcePhy<Channel0>, adc2: ADC2) -> 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);
CurrentSource {
phy: phy_ch0,
pins_adc: pins_adc,
}
}
}