2020-11-13 17:47:44 +08:00
|
|
|
///! Stabilizer DAC management interface
|
2020-11-11 19:09:27 +08:00
|
|
|
///!
|
2020-11-13 17:47:44 +08:00
|
|
|
///! The Stabilizer DAC utilize a DMA channel to generate output updates. A timer channel is
|
|
|
|
///! configured to generate a DMA write into the SPI TXFIFO, which initiates a SPI transfer and
|
|
|
|
///! results in DAC update for both channels.
|
|
|
|
use super::{
|
|
|
|
hal, sampling_timer, DMAReq, DmaConfig, MemoryToPeripheral, TargetAddress,
|
|
|
|
Transfer, SAMPLE_BUFFER_SIZE,
|
|
|
|
};
|
2020-11-03 16:41:14 +08:00
|
|
|
|
2020-11-13 17:47:44 +08:00
|
|
|
// The following global buffers are used for the DAC code DMA transfers. Two buffers are used for
|
|
|
|
// each transfer in a ping-pong buffer configuration (one is being prepared while the other is being
|
|
|
|
// processed). Note that the contents of AXI SRAM is uninitialized, so the buffer contents on
|
|
|
|
// startup are undefined.
|
|
|
|
#[link_section = ".axisram.buffers"]
|
|
|
|
static mut DAC0_BUF0: [u16; SAMPLE_BUFFER_SIZE] = [0; SAMPLE_BUFFER_SIZE];
|
|
|
|
|
|
|
|
#[link_section = ".axisram.buffers"]
|
|
|
|
static mut DAC0_BUF1: [u16; SAMPLE_BUFFER_SIZE] = [0; SAMPLE_BUFFER_SIZE];
|
|
|
|
|
|
|
|
#[link_section = ".axisram.buffers"]
|
|
|
|
static mut DAC1_BUF0: [u16; SAMPLE_BUFFER_SIZE] = [0; SAMPLE_BUFFER_SIZE];
|
|
|
|
|
|
|
|
#[link_section = ".axisram.buffers"]
|
|
|
|
static mut DAC1_BUF1: [u16; SAMPLE_BUFFER_SIZE] = [0; SAMPLE_BUFFER_SIZE];
|
|
|
|
|
2020-11-25 00:21:14 +08:00
|
|
|
/// SPI4 is used as a type for indicating a DMA transfer into the SPI4 TX FIFO
|
2020-11-23 21:30:29 +08:00
|
|
|
struct SPI4 {
|
2020-11-25 00:21:14 +08:00
|
|
|
spi: hal::spi::Spi<hal::stm32::SPI4, hal::spi::Disabled, u16>,
|
2020-11-23 21:30:29 +08:00
|
|
|
_channel: sampling_timer::tim2::Channel3,
|
|
|
|
}
|
2020-11-25 00:21:14 +08:00
|
|
|
|
2020-11-13 17:47:44 +08:00
|
|
|
impl SPI4 {
|
2020-11-25 00:21:14 +08:00
|
|
|
pub fn new(
|
|
|
|
_channel: sampling_timer::tim2::Channel3,
|
|
|
|
spi: hal::spi::Spi<hal::stm32::SPI4, hal::spi::Disabled, u16>,
|
|
|
|
) -> Self {
|
|
|
|
Self { _channel, spi }
|
2020-11-13 17:47:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-23 21:30:29 +08:00
|
|
|
// Note(unsafe): This is safe because the DMA request line is logically owned by this module.
|
2020-11-25 00:21:14 +08:00
|
|
|
// Additionally, the SPI is owned by this structure and is known to be configured for u16 word
|
|
|
|
// sizes.
|
2020-11-13 17:47:44 +08:00
|
|
|
unsafe impl TargetAddress<MemoryToPeripheral> for SPI4 {
|
|
|
|
/// SPI2 is configured to operate using 16-bit transfer words.
|
|
|
|
type MemSize = u16;
|
|
|
|
|
|
|
|
/// SPI4 DMA requests are generated whenever TIM2 CH3 comparison occurs.
|
|
|
|
const REQUEST_LINE: Option<u8> = Some(DMAReq::TIM2_CH3 as u8);
|
|
|
|
|
|
|
|
/// Whenever the DMA request occurs, it should write into SPI4's TX FIFO.
|
|
|
|
fn address(&self) -> u32 {
|
2020-11-25 00:21:14 +08:00
|
|
|
&self.spi.inner().txdr as *const _ as u32
|
2020-11-13 17:47:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// SPI5 is used as a ZST (zero-sized type) for indicating a DMA transfer into the SPI5 TX FIFO
|
2020-11-23 21:30:29 +08:00
|
|
|
struct SPI5 {
|
|
|
|
_channel: sampling_timer::tim2::Channel4,
|
2020-11-25 00:21:14 +08:00
|
|
|
spi: hal::spi::Spi<hal::stm32::SPI5, hal::spi::Disabled, u16>,
|
2020-11-23 21:30:29 +08:00
|
|
|
}
|
2020-11-25 00:21:14 +08:00
|
|
|
|
2020-11-13 17:47:44 +08:00
|
|
|
impl SPI5 {
|
2020-11-25 00:21:14 +08:00
|
|
|
pub fn new(
|
|
|
|
_channel: sampling_timer::tim2::Channel4,
|
|
|
|
spi: hal::spi::Spi<hal::stm32::SPI5, hal::spi::Disabled, u16>,
|
|
|
|
) -> Self {
|
|
|
|
Self { _channel, spi }
|
2020-11-13 17:47:44 +08:00
|
|
|
}
|
|
|
|
}
|
2020-11-11 19:09:27 +08:00
|
|
|
|
2020-11-23 21:30:29 +08:00
|
|
|
// Note(unsafe): This is safe because the DMA request line is logically owned by this module.
|
2020-11-25 00:21:14 +08:00
|
|
|
// Additionally, the SPI is owned by this structure and is known to be configured for u16 word
|
|
|
|
// sizes.
|
2020-11-13 17:47:44 +08:00
|
|
|
unsafe impl TargetAddress<MemoryToPeripheral> for SPI5 {
|
|
|
|
/// SPI5 is configured to operate using 16-bit transfer words.
|
|
|
|
type MemSize = u16;
|
|
|
|
|
|
|
|
/// SPI5 DMA requests are generated whenever TIM2 CH4 comparison occurs.
|
|
|
|
const REQUEST_LINE: Option<u8> = Some(DMAReq::TIM2_CH4 as u8);
|
|
|
|
|
|
|
|
/// Whenever the DMA request occurs, it should write into SPI5's TX FIFO
|
|
|
|
fn address(&self) -> u32 {
|
2020-11-25 00:21:14 +08:00
|
|
|
&self.spi.inner().txdr as *const _ as u32
|
2020-11-13 17:47:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Represents both DAC output channels.
|
|
|
|
pub struct DacOutputs {
|
|
|
|
dac0: Dac0Output,
|
|
|
|
dac1: Dac1Output,
|
2020-11-03 16:41:14 +08:00
|
|
|
}
|
|
|
|
|
2020-11-03 23:09:00 +08:00
|
|
|
impl DacOutputs {
|
2020-11-13 17:47:44 +08:00
|
|
|
/// Construct the DAC outputs.
|
|
|
|
pub fn new(dac0: Dac0Output, dac1: Dac1Output) -> Self {
|
|
|
|
Self { dac0, dac1 }
|
|
|
|
}
|
|
|
|
|
2020-11-24 23:57:36 +08:00
|
|
|
/// Borrow the next DAC output buffers to populate the DAC output codes in-place.
|
2020-11-13 17:47:44 +08:00
|
|
|
///
|
2020-11-24 23:57:36 +08:00
|
|
|
/// # Returns
|
|
|
|
/// (dac0, dac1) where each value is a mutable reference to the output code array for DAC0 and
|
|
|
|
/// DAC1 respectively.
|
|
|
|
pub fn prepare_data(
|
2020-11-13 17:47:44 +08:00
|
|
|
&mut self,
|
2020-11-24 23:57:36 +08:00
|
|
|
) -> (
|
|
|
|
&mut [u16; SAMPLE_BUFFER_SIZE],
|
|
|
|
&mut [u16; SAMPLE_BUFFER_SIZE],
|
2020-11-13 17:47:44 +08:00
|
|
|
) {
|
2020-11-24 23:57:36 +08:00
|
|
|
(self.dac0.prepare_buffer(), self.dac1.prepare_buffer())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Enqueue the next DAC output codes for transmission.
|
|
|
|
///
|
|
|
|
/// # Note
|
|
|
|
/// It is assumed that data was populated using `prepare_data()` before this function is
|
|
|
|
/// called.
|
|
|
|
pub fn commit_data(&mut self) {
|
|
|
|
self.dac0.commit_buffer();
|
|
|
|
self.dac1.commit_buffer();
|
2020-11-13 17:47:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Represents data associated with DAC0.
|
|
|
|
pub struct Dac0Output {
|
|
|
|
next_buffer: Option<&'static mut [u16; SAMPLE_BUFFER_SIZE]>,
|
2020-11-23 21:30:29 +08:00
|
|
|
// Note: SPI TX functionality may not be used from this structure to ensure safety with DMA.
|
2020-11-13 17:47:44 +08:00
|
|
|
transfer: Transfer<
|
|
|
|
hal::dma::dma::Stream4<hal::stm32::DMA1>,
|
|
|
|
SPI4,
|
|
|
|
MemoryToPeripheral,
|
|
|
|
&'static mut [u16; SAMPLE_BUFFER_SIZE],
|
|
|
|
>,
|
|
|
|
first_transfer: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Dac0Output {
|
|
|
|
/// Construct the DAC0 output channel.
|
2020-11-11 19:09:27 +08:00
|
|
|
///
|
|
|
|
/// # Args
|
2020-11-13 17:47:44 +08:00
|
|
|
/// * `spi` - The SPI interface used to communicate with the ADC.
|
|
|
|
/// * `stream` - The DMA stream used to write DAC codes over SPI.
|
|
|
|
/// * `trigger_channel` - The sampling timer output compare channel for update triggers.
|
2020-11-03 16:41:45 +08:00
|
|
|
pub fn new(
|
2020-11-13 17:47:44 +08:00
|
|
|
spi: hal::spi::Spi<hal::stm32::SPI4, hal::spi::Enabled, u16>,
|
|
|
|
stream: hal::dma::dma::Stream4<hal::stm32::DMA1>,
|
2020-11-23 21:30:29 +08:00
|
|
|
trigger_channel: sampling_timer::tim2::Channel3,
|
2020-11-03 16:41:45 +08:00
|
|
|
) -> Self {
|
2020-11-13 17:47:44 +08:00
|
|
|
// Generate DMA events when an output compare of the timer hitting zero (timer roll over)
|
|
|
|
// occurs.
|
|
|
|
trigger_channel.listen_dma();
|
|
|
|
trigger_channel.to_output_compare(0);
|
2020-11-11 19:09:27 +08:00
|
|
|
|
2020-11-13 17:47:44 +08:00
|
|
|
// The stream constantly writes to the TX FIFO to write new update codes.
|
|
|
|
let trigger_config = DmaConfig::default()
|
|
|
|
.memory_increment(true)
|
|
|
|
.peripheral_increment(false);
|
2020-11-11 19:09:27 +08:00
|
|
|
|
2020-11-13 17:47:44 +08:00
|
|
|
// Listen for any potential SPI error signals, which may indicate that we are not generating
|
|
|
|
// update codes.
|
|
|
|
let mut spi = spi.disable();
|
|
|
|
spi.listen(hal::spi::Event::Error);
|
|
|
|
|
|
|
|
// Allow the SPI FIFOs to operate using only DMA data channels.
|
|
|
|
spi.enable_dma_tx();
|
|
|
|
|
|
|
|
// Enable SPI and start it in infinite transaction mode.
|
|
|
|
spi.inner().cr1.modify(|_, w| w.spe().set_bit());
|
|
|
|
spi.inner().cr1.modify(|_, w| w.cstart().started());
|
2020-11-03 17:52:37 +08:00
|
|
|
|
2020-11-25 00:21:14 +08:00
|
|
|
// Construct the trigger stream to write from memory to the peripheral.
|
|
|
|
let transfer: Transfer<_, _, MemoryToPeripheral, _> = Transfer::init(
|
|
|
|
stream,
|
|
|
|
SPI4::new(trigger_channel, spi),
|
|
|
|
// Note(unsafe): This buffer is only used once and provided for the DMA transfer.
|
|
|
|
unsafe { &mut DAC0_BUF0 },
|
|
|
|
None,
|
|
|
|
trigger_config,
|
|
|
|
);
|
|
|
|
|
2020-11-03 16:41:45 +08:00
|
|
|
Self {
|
2020-11-13 17:47:44 +08:00
|
|
|
transfer,
|
2020-11-23 21:30:29 +08:00
|
|
|
// Note(unsafe): This buffer is only used once and provided for the next DMA transfer.
|
2020-11-13 17:47:44 +08:00
|
|
|
next_buffer: unsafe { Some(&mut DAC0_BUF1) },
|
|
|
|
first_transfer: true,
|
2020-11-03 16:41:45 +08:00
|
|
|
}
|
2020-11-03 16:41:14 +08:00
|
|
|
}
|
|
|
|
|
2020-11-24 23:57:36 +08:00
|
|
|
/// Mutably borrow the next output buffer to populate it with DAC codes.
|
|
|
|
pub fn prepare_buffer(&mut self) -> &mut [u16; SAMPLE_BUFFER_SIZE] {
|
|
|
|
self.next_buffer.as_mut().unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Enqueue the next buffer for transmission to the DAC.
|
2020-11-11 19:09:27 +08:00
|
|
|
///
|
|
|
|
/// # Args
|
2020-11-24 23:57:36 +08:00
|
|
|
/// * `data` - The next data to write to the DAC.
|
|
|
|
pub fn commit_buffer(&mut self) {
|
2020-11-13 17:47:44 +08:00
|
|
|
let next_buffer = self.next_buffer.take().unwrap();
|
|
|
|
|
|
|
|
// If the last transfer was not complete, we didn't write all our previous DAC codes.
|
|
|
|
// Wait for all the DAC codes to get written as well.
|
|
|
|
if self.first_transfer {
|
|
|
|
self.first_transfer = false
|
|
|
|
} else {
|
2020-11-25 23:46:42 +08:00
|
|
|
// Note: If a device hangs up, check that this conditional is passing correctly, as
|
|
|
|
// there is no time-out checks here in the interest of execution speed.
|
2020-11-13 17:47:44 +08:00
|
|
|
while self.transfer.get_transfer_complete_flag() == false {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the next transfer.
|
|
|
|
self.transfer.clear_interrupts();
|
|
|
|
let (prev_buffer, _) =
|
|
|
|
self.transfer.next_transfer(next_buffer).unwrap();
|
|
|
|
|
|
|
|
self.next_buffer.replace(prev_buffer);
|
2020-11-03 16:41:14 +08:00
|
|
|
}
|
2020-11-13 17:47:44 +08:00
|
|
|
}
|
2020-11-03 16:41:14 +08:00
|
|
|
|
2020-11-13 17:47:44 +08:00
|
|
|
/// Represents the data output stream from DAC1.
|
|
|
|
pub struct Dac1Output {
|
|
|
|
next_buffer: Option<&'static mut [u16; SAMPLE_BUFFER_SIZE]>,
|
|
|
|
transfer: Transfer<
|
|
|
|
hal::dma::dma::Stream5<hal::stm32::DMA1>,
|
|
|
|
SPI5,
|
|
|
|
MemoryToPeripheral,
|
|
|
|
&'static mut [u16; SAMPLE_BUFFER_SIZE],
|
|
|
|
>,
|
|
|
|
first_transfer: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Dac1Output {
|
|
|
|
/// Construct a new DAC1 output data stream.
|
2020-11-11 19:09:27 +08:00
|
|
|
///
|
2020-11-13 17:47:44 +08:00
|
|
|
/// # Args
|
|
|
|
/// * `spi` - The SPI interface connected to DAC1.
|
|
|
|
/// * `stream` - The DMA stream used to write DAC codes the SPI TX FIFO.
|
|
|
|
/// * `trigger_channel` - The timer channel used to generate DMA requests for DAC updates.
|
|
|
|
pub fn new(
|
|
|
|
spi: hal::spi::Spi<hal::stm32::SPI5, hal::spi::Enabled, u16>,
|
|
|
|
stream: hal::dma::dma::Stream5<hal::stm32::DMA1>,
|
2020-11-23 21:30:29 +08:00
|
|
|
trigger_channel: sampling_timer::tim2::Channel4,
|
2020-11-13 17:47:44 +08:00
|
|
|
) -> Self {
|
|
|
|
// Generate DMA events when an output compare of the timer hitting zero (timer roll over)
|
|
|
|
// occurs.
|
|
|
|
trigger_channel.listen_dma();
|
|
|
|
trigger_channel.to_output_compare(0);
|
|
|
|
|
|
|
|
// The trigger stream constantly writes to the TX FIFO to generate DAC updates.
|
|
|
|
let trigger_config = DmaConfig::default()
|
|
|
|
.memory_increment(true)
|
|
|
|
.peripheral_increment(false)
|
|
|
|
.circular_buffer(true);
|
|
|
|
|
|
|
|
// Listen for any SPI errors, as this may indicate that we are not generating updates on the
|
|
|
|
// DAC.
|
|
|
|
let mut spi = spi.disable();
|
|
|
|
spi.listen(hal::spi::Event::Error);
|
|
|
|
|
|
|
|
// Allow the SPI FIFOs to operate using only DMA data channels.
|
|
|
|
spi.enable_dma_tx();
|
|
|
|
|
|
|
|
// Enable SPI and start it in infinite transaction mode.
|
|
|
|
spi.inner().cr1.modify(|_, w| w.spe().set_bit());
|
|
|
|
spi.inner().cr1.modify(|_, w| w.cstart().started());
|
|
|
|
|
2020-11-25 00:21:14 +08:00
|
|
|
// Construct the stream to write from memory to the peripheral.
|
|
|
|
let transfer: Transfer<_, _, MemoryToPeripheral, _> = Transfer::init(
|
|
|
|
stream,
|
|
|
|
SPI5::new(trigger_channel, spi),
|
|
|
|
// Note(unsafe): This buffer is only used once and provided to the transfer.
|
|
|
|
unsafe { &mut DAC1_BUF0 },
|
|
|
|
None,
|
|
|
|
trigger_config,
|
|
|
|
);
|
|
|
|
|
2020-11-13 17:47:44 +08:00
|
|
|
Self {
|
2020-11-23 21:30:29 +08:00
|
|
|
// Note(unsafe): This buffer is only used once and provided for the next DMA transfer.
|
2020-11-13 17:47:44 +08:00
|
|
|
next_buffer: unsafe { Some(&mut DAC1_BUF1) },
|
|
|
|
transfer,
|
|
|
|
first_transfer: true,
|
|
|
|
}
|
2020-11-03 16:41:14 +08:00
|
|
|
}
|
|
|
|
|
2020-11-24 23:57:36 +08:00
|
|
|
/// Mutably borrow the next output buffer to populate it with DAC codes.
|
|
|
|
pub fn prepare_buffer(&mut self) -> &mut [u16; SAMPLE_BUFFER_SIZE] {
|
|
|
|
self.next_buffer.as_mut().unwrap()
|
|
|
|
}
|
|
|
|
|
2020-11-13 17:47:44 +08:00
|
|
|
/// Enqueue the next buffer for transmission to the DAC.
|
2020-11-11 19:09:27 +08:00
|
|
|
///
|
|
|
|
/// # Args
|
2020-11-13 17:47:44 +08:00
|
|
|
/// * `data` - The next data to write to the DAC.
|
2020-11-24 23:57:36 +08:00
|
|
|
pub fn commit_buffer(&mut self) {
|
2020-11-13 17:47:44 +08:00
|
|
|
let next_buffer = self.next_buffer.take().unwrap();
|
|
|
|
|
|
|
|
// If the last transfer was not complete, we didn't write all our previous DAC codes.
|
|
|
|
// Wait for all the DAC codes to get written as well.
|
|
|
|
if self.first_transfer {
|
|
|
|
self.first_transfer = false
|
|
|
|
} else {
|
2020-11-25 23:46:42 +08:00
|
|
|
// Note: If a device hangs up, check that this conditional is passing correctly, as
|
|
|
|
// there is no time-out checks here in the interest of execution speed.
|
2020-11-13 17:47:44 +08:00
|
|
|
while self.transfer.get_transfer_complete_flag() == false {}
|
2020-11-03 16:41:14 +08:00
|
|
|
}
|
2020-11-13 17:47:44 +08:00
|
|
|
|
|
|
|
// Start the next transfer.
|
|
|
|
self.transfer.clear_interrupts();
|
|
|
|
let (prev_buffer, _) =
|
|
|
|
self.transfer.next_transfer(next_buffer).unwrap();
|
|
|
|
|
|
|
|
self.next_buffer.replace(prev_buffer);
|
2020-11-03 16:41:14 +08:00
|
|
|
}
|
|
|
|
}
|