forked from M-Labs/kirdy
Rewrite MAX5719 Driver into a separated file
This commit is contained in:
parent
c7d36bc8d5
commit
f2c026ffdd
53
src/laser_diode/max5719.rs
Normal file
53
src/laser_diode/max5719.rs
Normal file
@ -0,0 +1,53 @@
|
||||
use crate::device::sys_timer::sleep;
|
||||
use fugit::MegahertzU32;
|
||||
use stm32f4xx_hal::{
|
||||
hal::{blocking::spi::Transfer, digital::v2::OutputPin},
|
||||
spi,
|
||||
};
|
||||
|
||||
pub const SPI_MODE: spi::Mode = spi::Mode {
|
||||
polarity: spi::Polarity::IdleLow,
|
||||
phase: spi::Phase::CaptureOnFirstTransition,
|
||||
};
|
||||
pub const SPI_CLOCK_MHZ: MegahertzU32 = MegahertzU32::from_raw(30);
|
||||
|
||||
pub const MAX_VALUE: u32 = 0xFFFFF;
|
||||
|
||||
pub struct Dac<SPI: Transfer<u8>, S1: OutputPin, S2:OutputPin> {
|
||||
spi: SPI,
|
||||
cs_n: S1,
|
||||
load_n: S2,
|
||||
}
|
||||
|
||||
impl<SPI: Transfer<u8>, S1: OutputPin, S2: OutputPin> Dac<SPI, S1, S2> {
|
||||
pub fn new(spi: SPI, mut cs_n: S1, mut load_n: S2) -> Self {
|
||||
let _ = cs_n.set_high();
|
||||
let _ = load_n.set_high();
|
||||
|
||||
Dac { spi, cs_n, load_n}
|
||||
}
|
||||
|
||||
fn write(&mut self, buf: &mut [u8]) -> Result<(), SPI::Error> {
|
||||
let _ = self.cs_n.set_low();
|
||||
self.spi.transfer(buf)?;
|
||||
let _ = self.cs_n.set_high();
|
||||
sleep(1);
|
||||
// must be high for >= 20 ns
|
||||
let _ = self.load_n.set_low();
|
||||
// must be low for >= 20 ns
|
||||
sleep(1);
|
||||
let _ = self.load_n.set_high();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn set(&mut self, value: u32) -> Result<u32, SPI::Error> {
|
||||
let value = value.min(MAX_VALUE) << 4;
|
||||
let mut buf = [
|
||||
((value >> 16) & 0xFF) as u8,
|
||||
((value >> 8) & 0xFF) as u8,
|
||||
((value >> 0) & 0xFF) as u8,
|
||||
];
|
||||
self.write(&mut buf)?;
|
||||
Ok(value)
|
||||
}
|
||||
}
|
@ -1 +1,2 @@
|
||||
pub mod current_sources;
|
||||
pub mod current_sources;
|
||||
pub mod max5719;
|
Loading…
Reference in New Issue
Block a user