From dbb1b03638b1887fa0b03e3cbf158105e9a1b327 Mon Sep 17 00:00:00 2001 From: topquark12 Date: Sat, 22 Oct 2022 01:58:18 +0800 Subject: [PATCH] current source working --- .gitignore | 2 ++ src/device/boot.rs | 34 +++++++++++------- src/device/gpio.rs | 46 +++++++++++------------- src/laser_diode/current_sources.rs | 58 ++++++++++++++++++++++++------ src/laser_diode/max5719.rs | 17 --------- 5 files changed, 91 insertions(+), 66 deletions(-) delete mode 100644 src/laser_diode/max5719.rs diff --git a/.gitignore b/.gitignore index ff69d57..d6964cf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ target/ result + +*.jdebug* diff --git a/src/device/boot.rs b/src/device/boot.rs index 4f8dbd6..0a1c26f 100644 --- a/src/device/boot.rs +++ b/src/device/boot.rs @@ -6,18 +6,15 @@ use stm32f4xx_hal::{ }; use fugit::ExtU32; use log::info; -use super::{log_setup, sys_timer, usb}; -use super::gpio; -use crate::laser_diode::current_sources; +use super::{log_setup, sys_timer, usb, gpio}; +use crate::laser_diode::current_sources::*; #[cfg(not(feature = "semihosting"))] const WATCHDOG_PERIOD: u32 = 1000; #[cfg(feature = "semihosting")] const WATCHDOG_PERIOD: u32 = 30000; -pub fn bootup (mut core_perif : CorePeripherals, perif : Peripherals) -> ( - IndependentWatchdog -) { +pub fn bootup (mut core_perif : CorePeripherals, perif : Peripherals) -> IndependentWatchdog { log_setup::init_log(); info!("Kirdy init"); @@ -35,24 +32,37 @@ pub fn bootup (mut core_perif : CorePeripherals, perif : Peripherals) -> ( .pclk2(MegaHertz::from_raw(64).convert()) .freeze(); - let (eth_pins, - usb, - current_source) = gpio::setup( + sys_timer::setup(core_perif.SYST, clocks); + + let ( + eth_pins, + usb, + current_source_phy) = gpio::setup( clocks, perif.TIM4, perif.GPIOA, perif.GPIOB, perif.GPIOC, perif.GPIOD, perif.GPIOE, perif.GPIOF, perif.GPIOG, perif.SPI1, perif.SPI2, perif.SPI3, perif.ADC1, perif.OTG_FS_GLOBAL, perif.OTG_FS_DEVICE, perif.OTG_FS_PWRCLK, ); + + usb::State::setup(usb); + + let mut laser = current_source { + phy: current_source_phy, + setting: current_source_settings_construct { + output_current : 0.0 + } + }; + + laser.setup(); + laser.set_current(0.1); let mut wd = IndependentWatchdog::new(perif.IWDG); wd.start(WATCHDOG_PERIOD.millis()); wd.feed(); - - sys_timer::setup(core_perif.SYST, clocks); info!("Kirdy setup complete"); - (wd) + wd } \ No newline at end of file diff --git a/src/device/gpio.rs b/src/device/gpio.rs index e1d36d8..d08f1c2 100644 --- a/src/device/gpio.rs +++ b/src/device/gpio.rs @@ -52,7 +52,7 @@ pub fn setup( ) -> ( EthernetPins, USB, - current_source_construct, + current_source_phy_construct, // photo_diode_phy, // thermostat_phy ) { @@ -75,38 +75,32 @@ pub fn setup( }; let eth_pins = EthPins { - ref_clk: gpioa.pa1, - crs: gpioa.pa7, + ref_clk: gpioa.pa1, + crs: gpioa.pa7, tx_en: gpiob.pb11, - tx_d0: gpiog.pg13, + tx_d0: gpiog.pg13, tx_d1: gpiob.pb13, - rx_d0: gpioc.pc4, + rx_d0: gpioc.pc4, rx_d1: gpioc.pc5, }; - - let sck = gpiob.pb10.into_alternate(); - let mosi = gpiob.pb15.into_alternate(); - let mode = spi::Mode { - polarity: spi::Polarity::IdleHigh, - phase: spi::Phase::CaptureOnSecondTransition, - }; - let spi2 = Spi::new( - spi2, - (sck, NoMiso {}, mosi), - mode, - 10_u32.MHz(), - &clocks); - - let current_source: current_source_construct = current_source_construct { - max5719_spi: spi2, - max5719_load: gpioe.pe4.into_push_pull_output(), - current_source_ldo_en: gpioe.pe10.into_push_pull_output(), - current_source_short: gpioa.pa4.into_push_pull_output(), - output_current: 0.0 + let current_source_phy = current_source_phy_construct { + max5719_spi: Spi::new( + spi2, + (gpiob.pb10.into_alternate(), NoMiso {}, gpiob.pb15.into_alternate()), + 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() }; - (eth_pins, usb, current_source) + (eth_pins, usb, current_source_phy) } diff --git a/src/laser_diode/current_sources.rs b/src/laser_diode/current_sources.rs index ad07df7..12a2117 100644 --- a/src/laser_diode/current_sources.rs +++ b/src/laser_diode/current_sources.rs @@ -5,37 +5,73 @@ use stm32f4xx_hal::{ gpioa::*, gpiob::*, gpioe::*, + gpiod::*, Output, - PushPull, + PushPull, PD9, }, - hal::{self, blocking::spi::Transfer, digital::v2::OutputPin}, + hal::{self, blocking::spi::Write, digital::v2::OutputPin}, spi::{Spi, NoMiso, TransferModeNormal}, pac::SPI2, }; +use crate::device::sys_timer::sleep; pub trait current_source_phy { - type max5719_spi: Transfer; - type max5719_load: OutputPin; type current_source_ldo_en: OutputPin; type current_source_short: OutputPin; + type max5719_load: OutputPin; + type max5719_cs: OutputPin; + type max5719_spi: Write; } -pub struct current_source_construct { +pub struct current_source_phy_construct { pub max5719_spi: C::max5719_spi, pub max5719_load: C::max5719_load, + pub max5719_cs: C::max5719_cs, pub current_source_ldo_en: C::current_source_ldo_en, pub current_source_short: C::current_source_short, - pub output_current : f32, +} +pub struct current_source_settings_construct { + pub output_current: f32 +} +pub struct current_source { + pub phy : current_source_phy_construct, + pub setting: current_source_settings_construct } -pub struct current_source_0; +pub struct current_source_phy_ch0; -impl current_source_phy for current_source_0 { - type max5719_spi = Spi>, NoMiso, PB15>), TransferModeNormal>; - type max5719_load = PE4>; - type current_source_ldo_en = PE10>; +impl current_source_phy for current_source_phy_ch0 { + type current_source_ldo_en = PD9>; type current_source_short = PA4>; + type max5719_load = PB14>; + type max5719_cs = PD8>; + type max5719_spi = Spi>, NoMiso, PB15>), TransferModeNormal>; } +impl current_source { + 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); + } + 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(); + } + +} diff --git a/src/laser_diode/max5719.rs b/src/laser_diode/max5719.rs deleted file mode 100644 index 70af2b7..0000000 --- a/src/laser_diode/max5719.rs +++ /dev/null @@ -1,17 +0,0 @@ -use stm32f4xx_hal::{ - adc::Adc, - gpio::{ - AF5, Alternate, Analog, Input, - gpioa::*, - gpiob::*, - gpiod::*, - Output, PushPull, Pin, - }, - hal::{self, blocking::spi::Transfer, digital::v2::OutputPin}, - spi::{Spi, NoMiso, TransferModeNormal}, - pac::{GPIOB, SPI2} -}; - - - -