diff --git a/src/device/boot.rs b/src/device/boot.rs index 193a6d1..4f8dbd6 100644 --- a/src/device/boot.rs +++ b/src/device/boot.rs @@ -2,11 +2,13 @@ use stm32f4xx_hal::{ watchdog::IndependentWatchdog, rcc::RccExt, pac::{CorePeripherals, Peripherals}, - time::MegaHertz, prelude::_stm32f4xx_hal_gpio_GpioExt, + time::MegaHertz }; use fugit::ExtU32; use log::info; use super::{log_setup, sys_timer, usb}; +use super::gpio; +use crate::laser_diode::current_sources; #[cfg(not(feature = "semihosting"))] const WATCHDOG_PERIOD: u32 = 1000; @@ -33,13 +35,15 @@ pub fn bootup (mut core_perif : CorePeripherals, perif : Peripherals) -> ( .pclk2(MegaHertz::from_raw(64).convert()) .freeze(); - // let (pins, eth_pins, usb) = Pins::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, - // ); + let (eth_pins, + usb, + current_source) = 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, + ); let mut wd = IndependentWatchdog::new(perif.IWDG); wd.start(WATCHDOG_PERIOD.millis()); @@ -47,6 +51,8 @@ pub fn bootup (mut core_perif : CorePeripherals, perif : Peripherals) -> ( sys_timer::setup(core_perif.SYST, clocks); + info!("Kirdy setup complete"); + (wd) } \ No newline at end of file diff --git a/src/device/gpio.rs b/src/device/gpio.rs index b9b1a19..e1d36d8 100644 --- a/src/device/gpio.rs +++ b/src/device/gpio.rs @@ -1,10 +1,11 @@ use stm32f4xx_hal::{ adc::Adc, gpio::{ - AF5, Alternate, AlternateOD, Analog, Floating, Input, + AF5, Alternate, Analog, Input, gpioa::*, gpiob::*, gpioc::*, + gpiod::*, gpioe::*, gpiof::*, gpiog::*, @@ -14,7 +15,7 @@ use stm32f4xx_hal::{ hal::{self, blocking::spi::Transfer, digital::v2::OutputPin}, otg_fs::USB, rcc::Clocks, - pwm::{self, PwmChannels}, + spi, spi::{Spi, NoMiso, TransferModeNormal}, pac::{ ADC1, @@ -25,63 +26,87 @@ use stm32f4xx_hal::{ }, timer::Timer, time::U32Ext, + time::MegaHertz }; - +use fugit::RateExtU32; use stm32_eth::EthPins; +use crate::laser_diode::current_sources::*; + pub type EthernetPins = EthPins< - PA1>, - PA7>, - PB11>, - PG13>, - PB13>, - PC4>, - PC5>, + PA1, + PA7, + PB11, + PG13, + PB13, + PC4, + PC5, >; -impl thermostatPins for thermostat { - type DacSpi = Dac0Spi; - type DacSync = PE4>; - type Shdn = PE10>; - type VRefPin = PA0; - type ItecPin = PA6; - type DacFeedbackPin = PA4; - type TecUMeasPin = PC2; +pub fn setup( + clocks: Clocks, + tim4: TIM4, + gpioa: GPIOA, gpiob: GPIOB, gpioc: GPIOC, gpiod: GPIOD, gpioe: GPIOE, gpiof: GPIOF, gpiog: GPIOG, + spi1: SPI1, spi2: SPI2, spi3: SPI3, + adc1: ADC1, + otg_fs_global: OTG_FS_GLOBAL, otg_fs_device: OTG_FS_DEVICE, otg_fs_pwrclk: OTG_FS_PWRCLK, +) -> ( + EthernetPins, + USB, + current_source_construct, + // photo_diode_phy, + // thermostat_phy +) { + + let gpioa = gpioa.split(); + let gpiob = gpiob.split(); + let gpioc = gpioc.split(); + let gpiod = gpiod.split(); + let gpioe = gpioe.split(); + let gpiof = gpiof.split(); + let gpiog = gpiog.split(); + + let usb = USB { + usb_global: otg_fs_global, + usb_device: otg_fs_device, + usb_pwrclk: otg_fs_pwrclk, + pin_dm: gpioa.pa11.into_alternate(), + pin_dp: gpioa.pa12.into_alternate(), + hclk: clocks.hclk(), + }; + + let eth_pins = EthPins { + ref_clk: gpioa.pa1, + crs: gpioa.pa7, + tx_en: gpiob.pb11, + tx_d0: gpiog.pg13, + tx_d1: gpiob.pb13, + 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 + }; + + (eth_pins, usb, current_source) + } -pub struct thermostatPinSet { - pub dac_spi: Transfer, - pub dac_sync: OutputPin, - pub shdn: OutputPin, - pub vref_pin: VRefPin, - pub itec_pin: ItecPin, - pub dac_feedback_pin: DacFeedbackPin, - pub tec_u_meas_pin: TecUMeasPin, -} - -pub struct Pins { - pub thermostat: thermostatPinSet, -} - -impl Pins { - - pub fn setup( - clocks: Clocks, - tim4: TIM4, - gpioa: GPIOA, gpiob: GPIOB, gpioc: GPIOC, gpiod: GPIOD, gpioe: GPIOE, gpiof: GPIOF, gpiog: GPIOG, - spi1: SPI1, spi2: SPI2, spi3: SPI3, - adc1: ADC1, - otg_fs_global: OTG_FS_GLOBAL, otg_fs_device: OTG_FS_DEVICE, otg_fs_pwrclk: OTG_FS_PWRCLK, - ) -> (Self, EthernetPins, USB) { - let gpioa = gpioa.split(); - let gpiob = gpiob.split(); - let gpioc = gpioc.split(); - let gpiod = gpiod.split(); - let gpioe = gpioe.split(); - let gpiof = gpiof.split(); - let gpiog = gpiog.split(); - - - - } - -} \ No newline at end of file diff --git a/src/device/mod.rs b/src/device/mod.rs index 094bf6f..c912372 100644 --- a/src/device/mod.rs +++ b/src/device/mod.rs @@ -2,4 +2,4 @@ pub mod log_setup; pub mod usb; pub mod sys_timer; pub mod boot; -// pub mod init_gpio; \ No newline at end of file +pub mod gpio; \ No newline at end of file diff --git a/src/laser_diode/current_sources.rs b/src/laser_diode/current_sources.rs new file mode 100644 index 0000000..ad07df7 --- /dev/null +++ b/src/laser_diode/current_sources.rs @@ -0,0 +1,41 @@ +use stm32f4xx_hal::{ + adc::Adc, + gpio::{ + AF5, Alternate, + gpioa::*, + gpiob::*, + gpioe::*, + Output, + PushPull, + }, + hal::{self, blocking::spi::Transfer, digital::v2::OutputPin}, + spi::{Spi, NoMiso, TransferModeNormal}, + pac::SPI2, +}; + +pub trait current_source_phy { + type max5719_spi: Transfer; + type max5719_load: OutputPin; + type current_source_ldo_en: OutputPin; + type current_source_short: OutputPin; +} + +pub struct current_source_construct { + pub max5719_spi: C::max5719_spi, + pub max5719_load: C::max5719_load, + 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_0; + +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>; + type current_source_short = PA4>; +} + + + diff --git a/src/laser_diode/max5719.rs b/src/laser_diode/max5719.rs new file mode 100644 index 0000000..70af2b7 --- /dev/null +++ b/src/laser_diode/max5719.rs @@ -0,0 +1,17 @@ +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} +}; + + + + diff --git a/src/laser_diode/mod.rs b/src/laser_diode/mod.rs new file mode 100644 index 0000000..e15efa5 --- /dev/null +++ b/src/laser_diode/mod.rs @@ -0,0 +1 @@ +pub mod current_sources; diff --git a/src/main.rs b/src/main.rs index 9807aa7..0114cc2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,15 +4,16 @@ use panic_halt as _; use cortex_m_rt::entry; use stm32f4xx_hal::{ - watchdog::IndependentWatchdog, - rcc::RccExt, - pac::{CorePeripherals, Peripherals}, - time::MegaHertz, prelude::_stm32f4xx_hal_gpio_GpioExt, + pac::{CorePeripherals, Peripherals} }; use log::info; + mod device; -use device::boot::bootup; -use device::sys_timer; +mod laser_diode; +use device::{ + boot::bootup, + sys_timer +}; #[entry] fn main() -> ! { diff --git a/src/current_source/max5719.rs b/src/photo_diode/mod.rs similarity index 100% rename from src/current_source/max5719.rs rename to src/photo_diode/mod.rs diff --git a/src/thermostat/mod.rs b/src/thermostat/mod.rs new file mode 100644 index 0000000..e69de29