2022-10-21 12:05:55 +08:00
|
|
|
use stm32f4xx_hal::{
|
|
|
|
adc::Adc,
|
|
|
|
gpio::{
|
|
|
|
AF5, Alternate,
|
|
|
|
gpioa::*,
|
|
|
|
gpiob::*,
|
|
|
|
gpioe::*,
|
2022-10-22 01:58:18 +08:00
|
|
|
gpiod::*,
|
2022-10-21 12:05:55 +08:00
|
|
|
Output,
|
2022-10-22 01:58:18 +08:00
|
|
|
PushPull, PD9,
|
2022-10-21 12:05:55 +08:00
|
|
|
},
|
2022-10-22 01:58:18 +08:00
|
|
|
hal::{self, blocking::spi::Write, digital::v2::OutputPin},
|
2022-10-21 12:05:55 +08:00
|
|
|
spi::{Spi, NoMiso, TransferModeNormal},
|
|
|
|
pac::SPI2,
|
|
|
|
};
|
|
|
|
|
2022-10-22 01:58:18 +08:00
|
|
|
use crate::device::sys_timer::sleep;
|
2022-10-21 12:05:55 +08:00
|
|
|
pub trait current_source_phy {
|
|
|
|
type current_source_ldo_en: OutputPin;
|
|
|
|
type current_source_short: OutputPin;
|
2022-10-22 01:58:18 +08:00
|
|
|
type max5719_load: OutputPin;
|
|
|
|
type max5719_cs: OutputPin;
|
|
|
|
type max5719_spi: Write<u8>;
|
2022-10-21 12:05:55 +08:00
|
|
|
}
|
|
|
|
|
2022-10-22 01:58:18 +08:00
|
|
|
pub struct current_source_phy_construct<C: current_source_phy> {
|
2022-10-21 12:05:55 +08:00
|
|
|
pub max5719_spi: C::max5719_spi,
|
|
|
|
pub max5719_load: C::max5719_load,
|
2022-10-22 01:58:18 +08:00
|
|
|
pub max5719_cs: C::max5719_cs,
|
2022-10-21 12:05:55 +08:00
|
|
|
pub current_source_ldo_en: C::current_source_ldo_en,
|
|
|
|
pub current_source_short: C::current_source_short,
|
2022-10-22 01:58:18 +08:00
|
|
|
}
|
|
|
|
pub struct current_source_settings_construct {
|
|
|
|
pub output_current: f32
|
|
|
|
}
|
|
|
|
pub struct current_source<C:current_source_phy> {
|
|
|
|
pub phy : current_source_phy_construct<C>,
|
|
|
|
pub setting: current_source_settings_construct
|
2022-10-21 12:05:55 +08:00
|
|
|
}
|
|
|
|
|
2022-10-22 01:58:18 +08:00
|
|
|
pub struct current_source_phy_ch0;
|
2022-10-21 12:05:55 +08:00
|
|
|
|
2022-10-22 01:58:18 +08:00
|
|
|
impl current_source_phy for current_source_phy_ch0 {
|
|
|
|
type current_source_ldo_en = PD9<Output<PushPull>>;
|
2022-10-21 12:05:55 +08:00
|
|
|
type current_source_short = PA4<Output<PushPull>>;
|
2022-10-22 01:58:18 +08:00
|
|
|
type max5719_load = PB14<Output<PushPull>>;
|
|
|
|
type max5719_cs = PD8<Output<PushPull>>;
|
|
|
|
type max5719_spi = Spi<SPI2, (PB10<Alternate<5>>, NoMiso, PB15<Alternate<5>>), TransferModeNormal>;
|
2022-10-21 12:05:55 +08:00
|
|
|
}
|
|
|
|
|
2022-10-22 01:58:18 +08:00
|
|
|
impl<C:current_source_phy> current_source<C> {
|
2022-10-21 12:05:55 +08:00
|
|
|
|
2022-10-22 01:58:18 +08:00
|
|
|
pub fn setup(&mut self) {
|
|
|
|
self.phy.max5719_load.set_high();
|
|
|
|
self.phy.max5719_cs.set_high();
|
|
|
|
self.phy.current_source_ldo_en.set_high();
|
|
|
|
sleep(50_u32);
|
|
|
|
self.phy.current_source_short.set_high();
|
|
|
|
sleep(50_u32);
|
|
|
|
}
|
2022-10-21 12:05:55 +08:00
|
|
|
|
2022-10-22 01:58:18 +08:00
|
|
|
pub fn set_current(&mut self, current: f32) {
|
|
|
|
self.phy.max5719_load.set_high();
|
|
|
|
self.phy.max5719_cs.set_low();
|
|
|
|
let v_dac = current * 10.0 / 0.75;
|
|
|
|
let word = (((v_dac / 4.096) * 1048576.0) as u32) << 4;
|
|
|
|
let mut buf = [
|
|
|
|
((word >> 16) & 0xFF) as u8,
|
|
|
|
((word >> 8) & 0xFF) as u8,
|
|
|
|
((word >> 0) & 0xFF) as u8,
|
|
|
|
];
|
|
|
|
self.phy.max5719_spi.write(&mut buf);
|
|
|
|
self.phy.max5719_cs.set_high();
|
|
|
|
self.phy.max5719_load.set_low();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|