pounder_test/src/dac.rs

97 lines
2.4 KiB
Rust
Raw Normal View History

2020-11-03 16:41:14 +08:00
use super::hal;
use heapless::consts;
pub struct Dac0Output {
outputs: heapless::spsc::Queue<u16, consts::U32>,
spi: hal::spi::Spi<hal::stm32::SPI4, hal::spi::Enabled, u16>,
2020-11-03 17:52:37 +08:00
timer: hal::timer::Timer<hal::stm32::TIM3>,
2020-11-03 16:41:14 +08:00
}
impl Dac0Output {
2020-11-03 16:41:45 +08:00
pub fn new(
spi: hal::spi::Spi<hal::stm32::SPI4, 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 16:41:14 +08:00
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 {
spi,
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
}
pub fn push(&mut self, value: u16) {
self.outputs.enqueue(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() {
Some(value) => self.write(value),
2020-11-03 17:52:37 +08:00
None => self.timer.pause(),
2020-11-03 16:41:14 +08:00
}
}
pub fn write(&mut self, value: u16) {
unsafe {
2020-11-03 16:41:45 +08:00
core::ptr::write_volatile(
&self.spi.inner().txdr as *const _ as *mut u16,
value,
);
2020-11-03 16:41:14 +08:00
}
}
}
pub struct Dac1Output {
outputs: heapless::spsc::Queue<u16, consts::U32>,
spi: hal::spi::Spi<hal::stm32::SPI5, hal::spi::Enabled, u16>,
2020-11-03 17:52:37 +08:00
timer: hal::timer::Timer<hal::stm32::TIM4>,
2020-11-03 16:41:14 +08:00
}
impl Dac1Output {
pub fn new(
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::TIM4>,
2020-11-03 16:41:14 +08:00
) -> Self {
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:14 +08:00
2020-11-03 16:41:45 +08:00
Self {
spi,
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
}
pub fn push(&mut self, value: u16) {
self.outputs.enqueue(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() {
Some(value) => self.write(value),
2020-11-03 17:52:37 +08:00
None => self.timer.pause(),
2020-11-03 16:41:14 +08:00
}
}
pub fn write(&mut self, value: u16) {
unsafe {
2020-11-03 16:41:45 +08:00
core::ptr::write_volatile(
&self.spi.inner().txdr as *const _ as *mut u16,
value,
);
2020-11-03 16:41:14 +08:00
}
}
}