From 2953d4edde761bd04e5172fa1f3a304e2cb8a98b Mon Sep 17 00:00:00 2001 From: linuswck Date: Tue, 9 Jan 2024 15:39:31 +0800 Subject: [PATCH] Rewrite current_source obj and add gpio init --- src/device/gpio.rs | 50 ++++++++--------- src/laser_diode/current_sources.rs | 88 +++++++++++++----------------- 2 files changed, 61 insertions(+), 77 deletions(-) diff --git a/src/device/gpio.rs b/src/device/gpio.rs index e89abeb..b27bc40 100644 --- a/src/device/gpio.rs +++ b/src/device/gpio.rs @@ -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, - PdMonPhy, - MAX1968Phy, + CurrentSourcePhy, + MAX1968Phy, // 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) } diff --git a/src/laser_diode/current_sources.rs b/src/laser_diode/current_sources.rs index f329050..3a10384 100644 --- a/src/laser_diode/current_sources.rs +++ b/src/laser_diode/current_sources.rs @@ -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; + type Max5719Spi: Transfer; } -pub struct CurrentSourcePhyConstruct { - 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 { + pub dac: Dac, + 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 { - pub phy: CurrentSourcePhyConstruct, - pub setting: CurrentSourceSettings, -} - pub struct Channel0; impl ChannelPins for Channel0 { type PdMonPin = PA3; type CurrentSourceLdoEn = PD9>; type CurrentSourceShort = PA4>; - type Max5719Load = PB14>; - type Max5719Cs = PD8>; - type Max5719Spi = - Spi>, NoMiso, PB15>), TransferModeNormal>; + type Max5719Load = DacLoad; + type Max5719Cs = DacCs; + type Max5719Spi = DacSpi; } -impl CurrentSource { - 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>, NoMiso, PB15>), TransferModeNormal>; +type DacCs = PD8>; +type DacLoad = PB14>; - pub fn set_current( - &mut self, - current: f32, - ) -> Result<(), <::Max5719Spi as Write>::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, + pub pins_adc: Adc } + +impl CurrentSource { + pub fn new(phy_ch0: CurrentSourcePhy, 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, + } + } +} \ No newline at end of file