pounder_test/src/dac.rs

63 lines
1.8 KiB
Rust
Raw Normal View History

2020-11-03 16:41:14 +08:00
use super::hal;
use heapless::consts;
2020-11-03 23:09:00 +08:00
pub struct DacOutputs {
dac0_spi: hal::spi::Spi<hal::stm32::SPI4, hal::spi::Enabled, u16>,
dac1_spi: hal::spi::Spi<hal::stm32::SPI5, hal::spi::Enabled, u16>,
outputs: heapless::spsc::Queue<(u16, u16), consts::U32>,
2020-11-03 17:52:37 +08:00
timer: hal::timer::Timer<hal::stm32::TIM3>,
2020-11-03 16:41:14 +08:00
}
2020-11-03 23:09:00 +08:00
impl DacOutputs {
2020-11-03 16:41:45 +08:00
pub fn new(
2020-11-03 23:09:00 +08:00
dac0_spi: hal::spi::Spi<hal::stm32::SPI4, hal::spi::Enabled, u16>,
dac1_spi: hal::spi::Spi<hal::stm32::SPI5, hal::spi::Enabled, u16>,
2020-11-03 17:52:37 +08:00
mut timer: hal::timer::Timer<hal::stm32::TIM3>,
2020-11-03 16:41:45 +08:00
) -> Self {
2020-11-03 23:09:00 +08:00
dac0_spi.inner().cr1.modify(|_, w| w.cstart().started());
dac1_spi.inner().cr1.modify(|_, w| w.cstart().started());
2020-11-03 17:52:37 +08:00
timer.pause();
timer.reset_counter();
timer.clear_irq();
timer.listen(hal::timer::Event::TimeOut);
2020-11-03 16:41:45 +08:00
Self {
2020-11-03 23:09:00 +08:00
dac0_spi,
dac1_spi,
2020-11-03 16:41:45 +08:00
outputs: heapless::spsc::Queue::new(),
2020-11-03 17:52:37 +08:00
timer,
2020-11-03 16:41:45 +08:00
}
2020-11-03 16:41:14 +08:00
}
2020-11-03 23:09:00 +08:00
pub fn push(&mut self, dac0_value: u16, dac1_value: u16) {
self.outputs.enqueue((dac0_value, dac1_value)).unwrap();
2020-11-03 17:52:37 +08:00
self.timer.resume();
2020-11-03 16:41:14 +08:00
}
pub fn update(&mut self) {
2020-11-03 17:52:37 +08:00
self.timer.clear_irq();
2020-11-03 16:41:14 +08:00
match self.outputs.dequeue() {
2020-11-03 23:09:00 +08:00
Some((dac0, dac1)) => self.write(dac0, dac1),
None => {
self.timer.pause();
self.timer.reset_counter();
self.timer.clear_irq();
}
};
2020-11-03 16:41:14 +08:00
}
2020-11-03 23:09:00 +08:00
pub fn write(&mut self, dac0_value: u16, dac1_value: u16) {
2020-11-03 16:41:14 +08:00
unsafe {
2020-11-03 16:41:45 +08:00
core::ptr::write_volatile(
2020-11-03 23:09:00 +08:00
&self.dac0_spi.inner().txdr as *const _ as *mut u16,
dac0_value,
2020-11-03 16:41:45 +08:00
);
2020-11-03 16:41:14 +08:00
2020-11-03 16:41:45 +08:00
core::ptr::write_volatile(
2020-11-03 23:09:00 +08:00
&self.dac1_spi.inner().txdr as *const _ as *mut u16,
dac1_value,
2020-11-03 16:41:45 +08:00
);
2020-11-03 16:41:14 +08:00
}
}
}