Rewrite current_source obj and add gpio init

master
linuswck 2024-01-09 15:39:31 +08:00
parent f2c026ffdd
commit 2953d4edde
2 changed files with 61 additions and 77 deletions

View File

@ -1,7 +1,7 @@
use crate::laser_diode::current_sources::*;
use crate::laser_diode::pd_mon::PdMonPhy;
use crate::laser_diode::current_sources::{self, CurrentSourcePhy};
use crate::laser_diode::max5719;
use crate::thermostat::ad5680;
use crate::thermostat::max1968::{Channel0, MAX1968PinSet, MAX1968Phy, PWM_FREQ_KHZ};
use crate::thermostat::max1968::{self, MAX1968PinSet, MAX1968Phy, PWM_FREQ_KHZ};
use fugit::RateExtU32;
use stm32_eth::EthPins;
use stm32f4xx_hal::{
@ -36,9 +36,8 @@ pub fn setup(
) -> (
EthernetPins,
USB,
CurrentSourcePhyConstruct<CurrentSourcePhyCh0>,
PdMonPhy,
MAX1968Phy<Channel0>,
CurrentSourcePhy<current_sources::Channel0>,
MAX1968Phy<max1968::Channel0>,
// thermostat_phy
) {
let gpioa = gpioa.split();
@ -66,30 +65,25 @@ pub fn setup(
rx_d1: gpioc.pc5,
};
let current_source_phy = CurrentSourcePhyConstruct {
max5719_spi: Spi::new(
spi2,
(
gpiob.pb10.into_alternate(),
NoMiso {},
gpiob.pb15.into_alternate(),
let current_source_phy = CurrentSourcePhy {
dac: max5719::Dac::new(Spi::new(
spi2,
(
gpiob.pb10.into_alternate(),
NoMiso {},
gpiob.pb15.into_alternate(),
),
max5719::SPI_MODE,
max5719::SPI_CLOCK_MHZ.convert(),
&clocks,
), gpiod.pd8.into_push_pull_output(),
gpiob.pb14.into_push_pull_output(),
),
spi::Mode {
polarity: spi::Polarity::IdleLow,
phase: spi::Phase::CaptureOnFirstTransition,
},
10_u32.MHz(),
&clocks,
),
max5719_load: gpiob.pb14.into_push_pull_output(),
max5719_cs: gpiod.pd8.into_push_pull_output(),
current_source_ldo_en: gpiod.pd9.into_push_pull_output(),
current_source_short: gpioa.pa4.into_push_pull_output(),
current_source_ldo_en_pin: gpiod.pd9.into_push_pull_output(),
current_source_short_pin: gpioa.pa4.into_push_pull_output(),
pd_mon_pin: gpioa.pa3.into_analog()
};
let pd_mon_phy = PdMonPhy {
ch0_pin: gpioa.pa3.into_analog()
};
let pwm_chs = (
gpiob.pb6.into_alternate(),
@ -126,5 +120,5 @@ pub fn setup(
max_i_neg: max_i_neg0,
});
(eth_pins, usb, current_source_phy, pd_mon_phy, max1968_phy)
(eth_pins, usb, current_source_phy, max1968_phy)
}

View File

@ -1,73 +1,63 @@
use stm32f4xx_hal::{
adc::{
config::{self, AdcConfig},
Adc,
},
gpio::{gpioa::*, gpiob::*, gpiod::*, Alternate, Output, PushPull, Analog, PD9},
hal::{blocking::spi::Write, digital::v2::OutputPin},
pac::SPI2,
hal::{blocking::spi::Transfer, digital::v2::OutputPin},
pac::{SPI2, ADC2},
spi::{NoMiso, Spi, TransferModeNormal},
};
use crate::device::sys_timer::sleep;
use crate::laser_diode::max5719::{self, Dac};
pub trait ChannelPins {
type PdMonPin;
type CurrentSourceLdoEn: OutputPin;
type CurrentSourceShort: OutputPin;
type Max5719Load: OutputPin;
type Max5719Cs: OutputPin;
type Max5719Spi: Write<u8>;
type Max5719Spi: Transfer<u8>;
}
pub struct CurrentSourcePhyConstruct<C: ChannelPins> {
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,
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,
}
pub struct CurrentSourceSettings {
pub output_current: f32,
}
pub struct CurrentSource<C: ChannelPins> {
pub phy: CurrentSourcePhyConstruct<C>,
pub setting: CurrentSourceSettings,
}
pub struct Channel0;
impl ChannelPins for Channel0 {
type PdMonPin = PA3<Analog>;
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>;
type Max5719Load = DacLoad;
type Max5719Cs = DacCs;
type Max5719Spi = DacSpi;
}
impl<C: ChannelPins> CurrentSource<C> {
pub fn setup(&mut self) {
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);
}
type DacSpi = Spi<SPI2, (PB10<Alternate<5>>, NoMiso, PB15<Alternate<5>>), TransferModeNormal>;
type DacCs = PD8<Output<PushPull>>;
type DacLoad = PB14<Output<PushPull>>;
pub fn set_current(
&mut self,
current: f32,
) -> Result<(), <<C as ChannelPins>::Max5719Spi as Write<u8>>::Error> {
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;
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)?;
let _ = self.phy.max5719_cs.set_high();
let _ = self.phy.max5719_load.set_low();
Ok(())
}
pub struct CurrentSource{
pub phy: CurrentSourcePhy<Channel0>,
pub pins_adc: Adc<ADC2>
}
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,
}
}
}