2022-10-21 12:05:55 +08:00
|
|
|
use stm32f4xx_hal::{
|
2024-01-05 15:00:50 +08:00
|
|
|
gpio::{gpioa::*, gpiob::*, gpiod::*, Alternate, Output, PushPull, Analog, PD9},
|
2022-10-22 15:49:01 +08:00
|
|
|
hal::{blocking::spi::Write, digital::v2::OutputPin},
|
2022-10-21 12:05:55 +08:00
|
|
|
pac::SPI2,
|
2022-10-22 15:49:01 +08:00
|
|
|
spi::{NoMiso, Spi, TransferModeNormal},
|
2022-10-21 12:05:55 +08:00
|
|
|
};
|
|
|
|
|
2022-10-22 01:58:18 +08:00
|
|
|
use crate::device::sys_timer::sleep;
|
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: Write<u8>;
|
2022-10-21 12:05:55 +08:00
|
|
|
}
|
|
|
|
|
2024-01-05 15:00:50 +08:00
|
|
|
pub struct CurrentSourcePhyConstruct<C: ChannelPins> {
|
2022-10-22 15:49:01 +08:00
|
|
|
pub max5719_spi: C::Max5719Spi,
|
|
|
|
pub max5719_load: C::Max5719Load,
|
|
|
|
pub max5719_cs: C::Max5719Cs,
|
|
|
|
pub current_source_ldo_en: C::CurrentSourceLdoEn,
|
|
|
|
pub current_source_short: C::CurrentSourceShort,
|
2022-10-22 01:58:18 +08:00
|
|
|
}
|
2022-10-23 16:59:15 +08:00
|
|
|
pub struct CurrentSourceSettings {
|
2022-10-22 15:49:01 +08:00
|
|
|
pub output_current: f32,
|
2022-10-22 01:58:18 +08:00
|
|
|
}
|
2024-01-05 15:00:50 +08:00
|
|
|
pub struct CurrentSource<C: ChannelPins> {
|
2022-10-22 15:49:01 +08:00
|
|
|
pub phy: CurrentSourcePhyConstruct<C>,
|
2022-10-23 16:59:15 +08:00
|
|
|
pub setting: CurrentSourceSettings,
|
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 = PB14<Output<PushPull>>;
|
|
|
|
type Max5719Cs = PD8<Output<PushPull>>;
|
|
|
|
type Max5719Spi =
|
|
|
|
Spi<SPI2, (PB10<Alternate<5>>, NoMiso, PB15<Alternate<5>>), TransferModeNormal>;
|
2022-10-21 12:05:55 +08:00
|
|
|
}
|
|
|
|
|
2024-01-05 15:00:50 +08:00
|
|
|
impl<C: ChannelPins> CurrentSource<C> {
|
2022-10-22 01:58:18 +08:00
|
|
|
pub fn setup(&mut self) {
|
2022-10-22 15:49:01 +08:00
|
|
|
let _ = self.phy.max5719_load.set_high();
|
|
|
|
let _ = self.phy.max5719_cs.set_high();
|
|
|
|
let _ = self.phy.current_source_ldo_en.set_high();
|
|
|
|
sleep(10_u32);
|
|
|
|
let _ = self.phy.current_source_short.set_high();
|
|
|
|
sleep(10_u32);
|
2022-10-22 01:58:18 +08:00
|
|
|
}
|
2022-10-21 12:05:55 +08:00
|
|
|
|
2022-10-22 15:49:01 +08:00
|
|
|
pub fn set_current(
|
|
|
|
&mut self,
|
|
|
|
current: f32,
|
2024-01-05 15:00:50 +08:00
|
|
|
) -> Result<(), <<C as ChannelPins>::Max5719Spi as Write<u8>>::Error> {
|
2022-10-22 15:49:01 +08:00
|
|
|
let _ = self.phy.max5719_load.set_high();
|
|
|
|
let _ = self.phy.max5719_cs.set_low();
|
|
|
|
self.setting.output_current = current * 10.0 / 0.75;
|
|
|
|
let word = (((self.setting.output_current / 4.096) * 1048576.0) as u32) << 4;
|
2022-10-22 01:58:18 +08:00
|
|
|
let mut buf = [
|
|
|
|
((word >> 16) & 0xFF) as u8,
|
|
|
|
((word >> 8) & 0xFF) as u8,
|
|
|
|
((word >> 0) & 0xFF) as u8,
|
|
|
|
];
|
2022-10-22 15:49:01 +08:00
|
|
|
self.phy.max5719_spi.write(&mut buf)?;
|
|
|
|
let _ = self.phy.max5719_cs.set_high();
|
|
|
|
let _ = self.phy.max5719_load.set_low();
|
|
|
|
Ok(())
|
2022-10-22 01:58:18 +08:00
|
|
|
}
|
|
|
|
}
|