kirdy/src/laser_diode/max5719.rs

55 lines
1.5 KiB
Rust

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,
};
// MAX5719 Max Frequency: 50MHz | SPI2 Max Frequency: 21MHz
pub const SPI_CLOCK_MHZ: MegahertzU32 = MegahertzU32::from_raw(21);
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)
}
}