1
0
Fork 0

Rewrite current_source obj and add gpio init

This commit is contained in:
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::current_sources::{self, CurrentSourcePhy};
use crate::laser_diode::pd_mon::PdMonPhy; use crate::laser_diode::max5719;
use crate::thermostat::ad5680; 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 fugit::RateExtU32;
use stm32_eth::EthPins; use stm32_eth::EthPins;
use stm32f4xx_hal::{ use stm32f4xx_hal::{
@ -36,9 +36,8 @@ pub fn setup(
) -> ( ) -> (
EthernetPins, EthernetPins,
USB, USB,
CurrentSourcePhyConstruct<CurrentSourcePhyCh0>, CurrentSourcePhy<current_sources::Channel0>,
PdMonPhy, MAX1968Phy<max1968::Channel0>,
MAX1968Phy<Channel0>,
// thermostat_phy // thermostat_phy
) { ) {
let gpioa = gpioa.split(); let gpioa = gpioa.split();
@ -66,30 +65,25 @@ pub fn setup(
rx_d1: gpioc.pc5, rx_d1: gpioc.pc5,
}; };
let current_source_phy = CurrentSourcePhyConstruct { let current_source_phy = CurrentSourcePhy {
max5719_spi: Spi::new( dac: max5719::Dac::new(Spi::new(
spi2, spi2,
( (
gpiob.pb10.into_alternate(), gpiob.pb10.into_alternate(),
NoMiso {}, NoMiso {},
gpiob.pb15.into_alternate(), gpiob.pb15.into_alternate(),
), ),
spi::Mode { max5719::SPI_MODE,
polarity: spi::Polarity::IdleLow, max5719::SPI_CLOCK_MHZ.convert(),
phase: spi::Phase::CaptureOnFirstTransition,
},
10_u32.MHz(),
&clocks, &clocks,
), gpiod.pd8.into_push_pull_output(),
gpiob.pb14.into_push_pull_output(),
), ),
max5719_load: gpiob.pb14.into_push_pull_output(), current_source_ldo_en_pin: gpiod.pd9.into_push_pull_output(),
max5719_cs: gpiod.pd8.into_push_pull_output(), current_source_short_pin: gpioa.pa4.into_push_pull_output(),
current_source_ldo_en: gpiod.pd9.into_push_pull_output(), pd_mon_pin: gpioa.pa3.into_analog()
current_source_short: gpioa.pa4.into_push_pull_output(),
}; };
let pd_mon_phy = PdMonPhy {
ch0_pin: gpioa.pa3.into_analog()
};
let pwm_chs = ( let pwm_chs = (
gpiob.pb6.into_alternate(), gpiob.pb6.into_alternate(),
@ -126,5 +120,5 @@ pub fn setup(
max_i_neg: max_i_neg0, 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::{ use stm32f4xx_hal::{
adc::{
config::{self, AdcConfig},
Adc,
},
gpio::{gpioa::*, gpiob::*, gpiod::*, Alternate, Output, PushPull, Analog, PD9}, gpio::{gpioa::*, gpiob::*, gpiod::*, Alternate, Output, PushPull, Analog, PD9},
hal::{blocking::spi::Write, digital::v2::OutputPin}, hal::{blocking::spi::Transfer, digital::v2::OutputPin},
pac::SPI2, pac::{SPI2, ADC2},
spi::{NoMiso, Spi, TransferModeNormal}, spi::{NoMiso, Spi, TransferModeNormal},
}; };
use crate::device::sys_timer::sleep; use crate::laser_diode::max5719::{self, Dac};
pub trait ChannelPins { pub trait ChannelPins {
type PdMonPin; type PdMonPin;
type CurrentSourceLdoEn: OutputPin; type CurrentSourceLdoEn: OutputPin;
type CurrentSourceShort: OutputPin; type CurrentSourceShort: OutputPin;
type Max5719Load: OutputPin; type Max5719Load: OutputPin;
type Max5719Cs: OutputPin; type Max5719Cs: OutputPin;
type Max5719Spi: Write<u8>; type Max5719Spi: Transfer<u8>;
} }
pub struct CurrentSourcePhyConstruct<C: ChannelPins> { pub struct CurrentSourcePhy<C: ChannelPins> {
pub max5719_spi: C::Max5719Spi, pub dac: Dac<C::Max5719Spi, C::Max5719Cs, C::Max5719Load>,
pub max5719_load: C::Max5719Load, pub current_source_ldo_en_pin: C::CurrentSourceLdoEn,
pub max5719_cs: C::Max5719Cs, pub current_source_short_pin: C::CurrentSourceShort,
pub current_source_ldo_en: C::CurrentSourceLdoEn, pub pd_mon_pin : C::PdMonPin,
pub current_source_short: C::CurrentSourceShort,
} }
pub struct CurrentSourceSettings {
pub output_current: f32,
}
pub struct CurrentSource<C: ChannelPins> {
pub phy: CurrentSourcePhyConstruct<C>,
pub setting: CurrentSourceSettings,
}
pub struct Channel0; pub struct Channel0;
impl ChannelPins for Channel0 { impl ChannelPins for Channel0 {
type PdMonPin = PA3<Analog>; type PdMonPin = PA3<Analog>;
type CurrentSourceLdoEn = PD9<Output<PushPull>>; type CurrentSourceLdoEn = PD9<Output<PushPull>>;
type CurrentSourceShort = PA4<Output<PushPull>>; type CurrentSourceShort = PA4<Output<PushPull>>;
type Max5719Load = PB14<Output<PushPull>>; type Max5719Load = DacLoad;
type Max5719Cs = PD8<Output<PushPull>>; type Max5719Cs = DacCs;
type Max5719Spi = type Max5719Spi = DacSpi;
Spi<SPI2, (PB10<Alternate<5>>, NoMiso, PB15<Alternate<5>>), TransferModeNormal>;
} }
impl<C: ChannelPins> CurrentSource<C> { type DacSpi = Spi<SPI2, (PB10<Alternate<5>>, NoMiso, PB15<Alternate<5>>), TransferModeNormal>;
pub fn setup(&mut self) { type DacCs = PD8<Output<PushPull>>;
let _ = self.phy.max5719_load.set_high(); type DacLoad = PB14<Output<PushPull>>;
let _ = self.phy.max5719_cs.set_high();
let _ = self.phy.current_source_ldo_en.set_high();
sleep(10_u32); pub struct CurrentSource{
let _ = self.phy.current_source_short.set_high(); pub phy: CurrentSourcePhy<Channel0>,
sleep(10_u32); pub pins_adc: Adc<ADC2>
} }
pub fn set_current( impl CurrentSource {
&mut self, pub fn new(phy_ch0: CurrentSourcePhy<Channel0>, adc2: ADC2) -> Self {
current: f32, let config = AdcConfig::default()
) -> Result<(), <<C as ChannelPins>::Max5719Spi as Write<u8>>::Error> { .clock(config::Clock::Pclk2_div_2)
let _ = self.phy.max5719_load.set_high(); .default_sample_time(config::SampleTime::Cycles_480);
let _ = self.phy.max5719_cs.set_low();
self.setting.output_current = current * 10.0 / 0.75; let pins_adc = Adc::adc2(adc2, true, config);
let word = (((self.setting.output_current / 4.096) * 1048576.0) as u32) << 4;
let mut buf = [ CurrentSource {
((word >> 16) & 0xFF) as u8, phy: phy_ch0,
((word >> 8) & 0xFF) as u8, pins_adc: pins_adc,
((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(())
} }
} }