current source working
This commit is contained in:
parent
361c2ff316
commit
dbb1b03638
|
@ -1,2 +1,4 @@
|
|||
target/
|
||||
result
|
||||
|
||||
*.jdebug*
|
||||
|
|
|
@ -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,9 +32,12 @@ 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,
|
||||
|
@ -45,14 +45,24 @@ pub fn bootup (mut core_perif : CorePeripherals, perif : Peripherals) -> (
|
|||
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
|
||||
|
||||
}
|
|
@ -52,7 +52,7 @@ pub fn setup(
|
|||
) -> (
|
||||
EthernetPins,
|
||||
USB,
|
||||
current_source_construct<current_source_0>,
|
||||
current_source_phy_construct<current_source_phy_ch0>,
|
||||
// photo_diode_phy,
|
||||
// thermostat_phy
|
||||
) {
|
||||
|
@ -84,29 +84,23 @@ pub fn setup(
|
|||
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 {
|
||||
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)
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -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<u8>;
|
||||
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<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_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<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 {
|
||||
type max5719_spi = Spi<SPI2, (PB10<Alternate<5>>, NoMiso, PB15<Alternate<5>>), TransferModeNormal>;
|
||||
type max5719_load = PE4<Output<PushPull>>;
|
||||
type current_source_ldo_en = PE10<Output<PushPull>>;
|
||||
impl current_source_phy for current_source_phy_ch0 {
|
||||
type current_source_ldo_en = PD9<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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue