current source working

This commit is contained in:
topquark12 2022-10-22 01:58:18 +08:00
parent 361c2ff316
commit dbb1b03638
5 changed files with 91 additions and 66 deletions

2
.gitignore vendored
View File

@ -1,2 +1,4 @@
target/ target/
result result
*.jdebug*

View File

@ -6,18 +6,15 @@ use stm32f4xx_hal::{
}; };
use fugit::ExtU32; use fugit::ExtU32;
use log::info; use log::info;
use super::{log_setup, sys_timer, usb}; use super::{log_setup, sys_timer, usb, gpio};
use super::gpio; use crate::laser_diode::current_sources::*;
use crate::laser_diode::current_sources;
#[cfg(not(feature = "semihosting"))] #[cfg(not(feature = "semihosting"))]
const WATCHDOG_PERIOD: u32 = 1000; const WATCHDOG_PERIOD: u32 = 1000;
#[cfg(feature = "semihosting")] #[cfg(feature = "semihosting")]
const WATCHDOG_PERIOD: u32 = 30000; const WATCHDOG_PERIOD: u32 = 30000;
pub fn bootup (mut core_perif : CorePeripherals, perif : Peripherals) -> ( pub fn bootup (mut core_perif : CorePeripherals, perif : Peripherals) -> IndependentWatchdog {
IndependentWatchdog
) {
log_setup::init_log(); log_setup::init_log();
info!("Kirdy init"); info!("Kirdy init");
@ -35,24 +32,37 @@ pub fn bootup (mut core_perif : CorePeripherals, perif : Peripherals) -> (
.pclk2(MegaHertz::from_raw(64).convert()) .pclk2(MegaHertz::from_raw(64).convert())
.freeze(); .freeze();
let (eth_pins, sys_timer::setup(core_perif.SYST, clocks);
usb,
current_source) = gpio::setup( let (
eth_pins,
usb,
current_source_phy) = gpio::setup(
clocks, perif.TIM4, clocks, perif.TIM4,
perif.GPIOA, perif.GPIOB, perif.GPIOC, perif.GPIOD, perif.GPIOE, perif.GPIOF, perif.GPIOG, perif.GPIOA, perif.GPIOB, perif.GPIOC, perif.GPIOD, perif.GPIOE, perif.GPIOF, perif.GPIOG,
perif.SPI1, perif.SPI2, perif.SPI3, perif.SPI1, perif.SPI2, perif.SPI3,
perif.ADC1, perif.ADC1,
perif.OTG_FS_GLOBAL, perif.OTG_FS_DEVICE, perif.OTG_FS_PWRCLK, 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); let mut wd = IndependentWatchdog::new(perif.IWDG);
wd.start(WATCHDOG_PERIOD.millis()); wd.start(WATCHDOG_PERIOD.millis());
wd.feed(); wd.feed();
sys_timer::setup(core_perif.SYST, clocks);
info!("Kirdy setup complete"); info!("Kirdy setup complete");
(wd) wd
} }

View File

@ -52,7 +52,7 @@ pub fn setup(
) -> ( ) -> (
EthernetPins, EthernetPins,
USB, USB,
current_source_construct<current_source_0>, current_source_phy_construct<current_source_phy_ch0>,
// photo_diode_phy, // photo_diode_phy,
// thermostat_phy // thermostat_phy
) { ) {
@ -75,38 +75,32 @@ pub fn setup(
}; };
let eth_pins = EthPins { let eth_pins = EthPins {
ref_clk: gpioa.pa1, ref_clk: gpioa.pa1,
crs: gpioa.pa7, crs: gpioa.pa7,
tx_en: gpiob.pb11, tx_en: gpiob.pb11,
tx_d0: gpiog.pg13, tx_d0: gpiog.pg13,
tx_d1: gpiob.pb13, tx_d1: gpiob.pb13,
rx_d0: gpioc.pc4, rx_d0: gpioc.pc4,
rx_d1: gpioc.pc5, 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_0> = current_source_construct { let current_source_phy = current_source_phy_construct {
max5719_spi: spi2, max5719_spi: Spi::new(
max5719_load: gpioe.pe4.into_push_pull_output(), spi2,
current_source_ldo_en: gpioe.pe10.into_push_pull_output(), (gpiob.pb10.into_alternate(), NoMiso {}, gpiob.pb15.into_alternate()),
current_source_short: gpioa.pa4.into_push_pull_output(), spi::Mode {
output_current: 0.0 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)
} }

View File

@ -5,37 +5,73 @@ use stm32f4xx_hal::{
gpioa::*, gpioa::*,
gpiob::*, gpiob::*,
gpioe::*, gpioe::*,
gpiod::*,
Output, Output,
PushPull, PushPull, PD9,
}, },
hal::{self, blocking::spi::Transfer, digital::v2::OutputPin}, hal::{self, blocking::spi::Write, digital::v2::OutputPin},
spi::{Spi, NoMiso, TransferModeNormal}, spi::{Spi, NoMiso, TransferModeNormal},
pac::SPI2, pac::SPI2,
}; };
use crate::device::sys_timer::sleep;
pub trait current_source_phy { pub trait current_source_phy {
type max5719_spi: Transfer<u8>;
type max5719_load: OutputPin;
type current_source_ldo_en: OutputPin; type current_source_ldo_en: OutputPin;
type current_source_short: OutputPin; type current_source_short: OutputPin;
type max5719_load: OutputPin;
type max5719_cs: OutputPin;
type max5719_spi: Write<u8>;
} }
pub struct current_source_construct<C: current_source_phy> { pub struct current_source_phy_construct<C: current_source_phy> {
pub max5719_spi: C::max5719_spi, pub max5719_spi: C::max5719_spi,
pub max5719_load: C::max5719_load, 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_ldo_en: C::current_source_ldo_en,
pub current_source_short: C::current_source_short, 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<C:current_source_phy> {
pub phy : current_source_phy_construct<C>,
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 { impl current_source_phy for current_source_phy_ch0 {
type max5719_spi = Spi<SPI2, (PB10<Alternate<5>>, NoMiso, PB15<Alternate<5>>), TransferModeNormal>; type current_source_ldo_en = PD9<Output<PushPull>>;
type max5719_load = PE4<Output<PushPull>>;
type current_source_ldo_en = PE10<Output<PushPull>>;
type current_source_short = PA4<Output<PushPull>>; type current_source_short = PA4<Output<PushPull>>;
type max5719_load = PB14<Output<PushPull>>;
type max5719_cs = PD8<Output<PushPull>>;
type max5719_spi = Spi<SPI2, (PB10<Alternate<5>>, NoMiso, PB15<Alternate<5>>), TransferModeNormal>;
} }
impl<C:current_source_phy> current_source<C> {
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();
}
}

View File

@ -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}
};