current_source: add driver to read pd voltage

This commit is contained in:
linuswck 2024-01-09 16:05:08 +08:00
parent d1f7a20c15
commit a99dde8b38

View File

@ -11,6 +11,7 @@ use stm32f4xx_hal::{
use uom::si::{
ratio::ratio,
electric_potential::millivolt,
f64::{ElectricPotential, ElectricCurrent},
};
@ -89,6 +90,15 @@ impl CurrentSource {
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 {
let value = ((voltage / dac_out_v_max).get::<ratio>()
* (max5719::MAX_VALUE as f64)) as u32;