From b0e0b5144fec39f66c49d9b551d6be5b9d658f25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Tue, 24 Nov 2020 09:27:47 +0100 Subject: [PATCH 01/19] processing: use faster unsafe truncate --- src/main.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4c945e5..6a72552 100644 --- a/src/main.rs +++ b/src/main.rs @@ -779,7 +779,11 @@ const APP: () = { let x0 = f32::from(a as i16); let y0 = c.resources.iir_ch[1].update(&mut c.resources.iir_state[1], x0); - y0 as i16 as u16 ^ 0x8000 + // note(unsafe): The filter limits ensure that the value is in range. + // The truncation introduces 1/2 LSB distortion. + let y0 = unsafe { y0.to_int_unchecked::() }; + // convert to DAC code + y0 as u16 ^ 0x8000 }; c.resources.dac1.send(output).unwrap(); @@ -792,7 +796,11 @@ const APP: () = { let x0 = f32::from(a as i16); let y0 = c.resources.iir_ch[0].update(&mut c.resources.iir_state[0], x0); - y0 as i16 as u16 ^ 0x8000 + // note(unsafe): The filter limits ensure that the value is in range. + // The truncation introduces 1/2 LSB distortion. + let y0 = unsafe { y0.to_int_unchecked::() }; + // convert to DAC code + y0 as u16 ^ 0x8000 }; c.resources.dac0.send(output).unwrap(); From d9e4f6a052757abcc6a084d47603d51259c02b2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Wed, 25 Nov 2020 17:24:49 +0100 Subject: [PATCH 02/19] iir: copy_within is better than rotate_right --- dsp/src/iir.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/dsp/src/iir.rs b/dsp/src/iir.rs index fac1c4c..8d25c27 100644 --- a/dsp/src/iir.rs +++ b/dsp/src/iir.rs @@ -159,10 +159,12 @@ impl IIR { /// * `xy` - Current filter state. /// * `x0` - New input. pub fn update(&self, xy: &mut IIRState, x0: f32) -> f32 { + let n = self.ba.len(); + debug_assert!(xy.len() == n); // `xy` contains x0 x1 y0 y1 y2 // Increment time x1 x2 y1 y2 y3 // Rotate y3 x1 x2 y1 y2 - xy.rotate_right(1); + xy.copy_within(0..n - 1, 1); // unrolls better than xy.rotate_right(1) // Store x0 x0 x1 x2 y1 y2 xy[0] = x0; // Compute y0 by multiply-accumulate @@ -170,7 +172,7 @@ impl IIR { // Limit y0 let y0 = max(self.y_min, min(self.y_max, y0)); // Store y0 x0 x1 y0 y1 y2 - xy[xy.len() / 2] = y0; + xy[n / 2] = y0; y0 } } From 4c9c65bf2d5d9146e130ce55d7d615f578937091 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Wed, 25 Nov 2020 17:33:16 +0100 Subject: [PATCH 03/19] cargo-config: cm7 features --- .cargo/config | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.cargo/config b/.cargo/config index 382c36e..9968846 100644 --- a/.cargo/config +++ b/.cargo/config @@ -1,6 +1,10 @@ [target.'cfg(all(target_arch = "arm", target_os = "none"))'] runner = "gdb-multiarch -q -x openocd.gdb" -rustflags = ["-C", "link-arg=-Tlink.x"] +rustflags = [ + "-C", "link-arg=-Tlink.x", + "-C", "target-cpu=cortex-m7", + "-C", "target-feature=+fp-armv8d16", +] [build] target = "thumbv7em-none-eabihf" From 38dfd48c149efaabce5dfa390b7bb6724b187dde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Wed, 25 Nov 2020 17:53:13 +0100 Subject: [PATCH 04/19] iir: fix comment [nfc] --- dsp/src/iir.rs | 2 +- src/main.rs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/dsp/src/iir.rs b/dsp/src/iir.rs index 8d25c27..f58fb64 100644 --- a/dsp/src/iir.rs +++ b/dsp/src/iir.rs @@ -163,7 +163,7 @@ impl IIR { debug_assert!(xy.len() == n); // `xy` contains x0 x1 y0 y1 y2 // Increment time x1 x2 y1 y2 y3 - // Rotate y3 x1 x2 y1 y2 + // Shift x1 x1 x2 y1 y2 xy.copy_within(0..n - 1, 1); // unrolls better than xy.rotate_right(1) // Store x0 x0 x1 x2 y1 y2 xy[0] = x0; diff --git a/src/main.rs b/src/main.rs index ac9f1a6..3173158 100644 --- a/src/main.rs +++ b/src/main.rs @@ -758,10 +758,10 @@ const APP: () = { let x0 = f32::from(*adc0 as i16); let y0 = c.resources.iir_ch[0] .update(&mut c.resources.iir_state[0], x0); - // note(unsafe): The filter limits ensure that the value is in range. + // Note(unsafe): The filter limits ensure that the value is in range. // The truncation introduces 1/2 LSB distortion. let y0 = unsafe { y0.to_int_unchecked::() }; - // convert to DAC code + // Convert to DAC code y0 as u16 ^ 0x8000 }; @@ -769,10 +769,10 @@ const APP: () = { let x1 = f32::from(*adc1 as i16); let y1 = c.resources.iir_ch[1] .update(&mut c.resources.iir_state[1], x1); - // note(unsafe): The filter limits ensure that the value is in range. + // Note(unsafe): The filter limits ensure that the value is in range. // The truncation introduces 1/2 LSB distortion. let y1 = unsafe { y1.to_int_unchecked::() }; - // convert to DAC code + // Convert to DAC code y1 as u16 ^ 0x8000 }; } From cc64f470049ec363f547f4643c1b3de3613db12d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Wed, 25 Nov 2020 18:55:07 +0100 Subject: [PATCH 05/19] iir: fmt [nfc] --- dsp/src/iir.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dsp/src/iir.rs b/dsp/src/iir.rs index f58fb64..04d7c8e 100644 --- a/dsp/src/iir.rs +++ b/dsp/src/iir.rs @@ -164,7 +164,8 @@ impl IIR { // `xy` contains x0 x1 y0 y1 y2 // Increment time x1 x2 y1 y2 y3 // Shift x1 x1 x2 y1 y2 - xy.copy_within(0..n - 1, 1); // unrolls better than xy.rotate_right(1) + // This unrolls better than xy.rotate_right(1) + xy.copy_within(0..n - 1, 1); // Store x0 x0 x1 x2 y1 y2 xy[0] = x0; // Compute y0 by multiply-accumulate From 1906185286dbd9cf25eb4810fda6619848f2450d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Thu, 26 Nov 2020 10:58:11 +0100 Subject: [PATCH 06/19] adc: macro --- src/adc.rs | 518 +++++++++++++++++----------------------------------- src/main.rs | 10 +- 2 files changed, 177 insertions(+), 351 deletions(-) diff --git a/src/adc.rs b/src/adc.rs index 9d344dc..a16976a 100644 --- a/src/adc.rs +++ b/src/adc.rs @@ -27,358 +27,184 @@ static mut SPI_START: [u16; 1] = [0x00]; // The following global buffers are used for the ADC sample DMA transfers. Two buffers are used for // each transfer in a ping-pong buffer configuration (one is being acquired while the other is being // processed). Note that the contents of AXI SRAM is uninitialized, so the buffer contents on -// startup are undefined. +// startup are undefined. The dimension are `ADC_BUF[adc_index][ping_pong_index][sample_index]`. #[link_section = ".axisram.buffers"] -static mut ADC0_BUF0: [u16; SAMPLE_BUFFER_SIZE] = [0; SAMPLE_BUFFER_SIZE]; +static mut ADC_BUF: [[[u16; SAMPLE_BUFFER_SIZE]; 2]; 2] = + [[[0; SAMPLE_BUFFER_SIZE]; 2]; 2]; -#[link_section = ".axisram.buffers"] -static mut ADC0_BUF1: [u16; SAMPLE_BUFFER_SIZE] = [0; SAMPLE_BUFFER_SIZE]; - -#[link_section = ".axisram.buffers"] -static mut ADC1_BUF0: [u16; SAMPLE_BUFFER_SIZE] = [0; SAMPLE_BUFFER_SIZE]; - -#[link_section = ".axisram.buffers"] -static mut ADC1_BUF1: [u16; SAMPLE_BUFFER_SIZE] = [0; SAMPLE_BUFFER_SIZE]; - -/// SPI2 is used as a ZST (zero-sized type) for indicating a DMA transfer into the SPI2 TX FIFO -/// whenever the tim2 update dma request occurs. -struct SPI2 { - _channel: sampling_timer::tim2::Channel1, -} -impl SPI2 { - pub fn new(_channel: sampling_timer::tim2::Channel1) -> Self { - Self { _channel } - } -} - -// Note(unsafe): This structure is only safe to instantiate once. The DMA request is hard-coded and -// may only be used if ownership of the timer2 channel 1 compare channel is assured, which is -// ensured by maintaining ownership of the channel. -unsafe impl TargetAddress for SPI2 { - /// SPI2 is configured to operate using 16-bit transfer words. - type MemSize = u16; - - /// SPI2 DMA requests are generated whenever TIM2 CH1 comparison occurs. - const REQUEST_LINE: Option = Some(DMAReq::TIM2_CH1 as u8); - - /// Whenever the DMA request occurs, it should write into SPI2's TX FIFO to start a DMA - /// transfer. - fn address(&self) -> u32 { - // Note(unsafe): It is assumed that SPI2 is owned by another DMA transfer and this DMA is - // only used for the transmit-half of DMA. - let regs = unsafe { &*hal::stm32::SPI2::ptr() }; - ®s.txdr as *const _ as u32 - } -} - -/// SPI3 is used as a ZST (zero-sized type) for indicating a DMA transfer into the SPI3 TX FIFO -/// whenever the tim2 update dma request occurs. -struct SPI3 { - _channel: sampling_timer::tim2::Channel2, -} -impl SPI3 { - pub fn new(_channel: sampling_timer::tim2::Channel2) -> Self { - Self { _channel } - } -} - -// Note(unsafe): This structure is only safe to instantiate once. The DMA request is hard-coded and -// may only be used if ownership of the timer2 channel 2 compare channel is assured, which is -// ensured by maintaining ownership of the channel. -unsafe impl TargetAddress for SPI3 { - /// SPI3 is configured to operate using 16-bit transfer words. - type MemSize = u16; - - /// SPI3 DMA requests are generated whenever TIM2 CH2 comparison occurs. - const REQUEST_LINE: Option = Some(DMAReq::TIM2_CH2 as u8); - - /// Whenever the DMA request occurs, it should write into SPI3's TX FIFO to start a DMA - /// transfer. - fn address(&self) -> u32 { - // Note(unsafe): It is assumed that SPI3 is owned by another DMA transfer and this DMA is - // only used for the transmit-half of DMA. - let regs = unsafe { &*hal::stm32::SPI3::ptr() }; - ®s.txdr as *const _ as u32 - } -} - -/// Represents both ADC input channels. -pub struct AdcInputs { - adc0: Adc0Input, - adc1: Adc1Input, -} - -impl AdcInputs { - /// Construct the ADC inputs. - pub fn new(adc0: Adc0Input, adc1: Adc1Input) -> Self { - Self { adc0, adc1 } - } - - /// Interrupt handler to handle when the sample collection DMA transfer completes. - /// - /// # Returns - /// (adc0, adc1) where adcN is a reference to the collected ADC samples. Two array references - /// are returned - one for each ADC sample stream. - pub fn transfer_complete_handler( - &mut self, - ) -> (&[u16; SAMPLE_BUFFER_SIZE], &[u16; SAMPLE_BUFFER_SIZE]) { - let adc0_buffer = self.adc0.transfer_complete_handler(); - let adc1_buffer = self.adc1.transfer_complete_handler(); - (adc0_buffer, adc1_buffer) - } -} - -/// Represents data associated with ADC0. -pub struct Adc0Input { - next_buffer: Option<&'static mut [u16; SAMPLE_BUFFER_SIZE]>, - transfer: Transfer< - hal::dma::dma::Stream1, - hal::spi::Spi, - PeripheralToMemory, - &'static mut [u16; SAMPLE_BUFFER_SIZE], - >, - _trigger_transfer: Transfer< - hal::dma::dma::Stream0, - SPI2, - MemoryToPeripheral, - &'static mut [u16; 1], - >, -} - -impl Adc0Input { - /// Construct the ADC0 input channel. - /// - /// # Args - /// * `spi` - The SPI interface used to communicate with the ADC. - /// * `trigger_stream` - The DMA stream used to trigger each ADC transfer by writing a word into - /// the SPI TX FIFO. - /// * `data_stream` - The DMA stream used to read samples received over SPI into a data buffer. - /// * `_trigger_channel` - The ADC sampling timer output compare channel for read triggers. - pub fn new( - spi: hal::spi::Spi, - trigger_stream: hal::dma::dma::Stream0, - data_stream: hal::dma::dma::Stream1, - trigger_channel: sampling_timer::tim2::Channel1, - ) -> 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 using a static word (dont-care - // contents). Thus, neither the memory or peripheral address ever change. This is run in - // circular mode to be completed at every DMA request. - let trigger_config = DmaConfig::default() - .priority(Priority::High) - .circular_buffer(true); - - // Construct the trigger stream to write from memory to the peripheral. - let mut trigger_transfer: Transfer<_, _, MemoryToPeripheral, _> = - Transfer::init( - trigger_stream, - SPI2::new(trigger_channel), - // Note(unsafe): Because this is a Memory->Peripheral transfer, this data is never - // actually modified. It technically only needs to be immutably borrowed, but the - // current HAL API only supports mutable borrows. - unsafe { &mut SPI_START }, - None, - trigger_config, - ); - - // The data stream constantly reads from the SPI RX FIFO into a RAM buffer. The peripheral - // stalls reads of the SPI RX FIFO until data is available, so the DMA transfer completes - // after the requested number of samples have been collected. Note that only ADC1's data - // stream is used to trigger a transfer completion interrupt. - let data_config = DmaConfig::default() - .memory_increment(true) - .priority(Priority::VeryHigh); - - // A SPI peripheral error interrupt is used to determine if the RX FIFO overflows. This - // indicates that samples were dropped due to excessive processing time in the main - // application (e.g. a second DMA transfer completes before the first was done with - // processing). This is used as a flow control indicator to guarantee that no ADC samples - // are lost. - let mut spi = spi.disable(); - spi.listen(hal::spi::Event::Error); - - // The data transfer is always a transfer of data from the peripheral to a RAM buffer. - let mut data_transfer: Transfer<_, _, PeripheralToMemory, _> = - Transfer::init( - data_stream, - spi, - // Note(unsafe): The ADC0_BUF0 is "owned" by this peripheral. It shall not be used - // anywhere else in the module. - unsafe { &mut ADC0_BUF0 }, - None, - data_config, - ); - - data_transfer.start(|spi| { - // Allow the SPI FIFOs to operate using only DMA data channels. - spi.enable_dma_rx(); - 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()); - }); - - trigger_transfer.start(|_| {}); - - Self { - // Note(unsafe): The ADC0_BUF1 is "owned" by this peripheral. It shall not be used - // anywhere else in the module. - next_buffer: unsafe { Some(&mut ADC0_BUF1) }, - transfer: data_transfer, - _trigger_transfer: trigger_transfer, +macro_rules! adc_input { + ($name:ident, $index:literal, $trigger_stream:ident, $data_stream:ident, + $spi:ident, $trigger_channel:ident, $dma_req:ident) => { + /// SPI is used as a ZST (zero-sized type) for indicating a DMA transfer into the SPI TX FIFO + /// whenever the tim2 update dma request occurs. + struct $spi { + _channel: sampling_timer::tim2::$trigger_channel, } - } - - /// Handle a transfer completion. - /// - /// # Returns - /// A reference to the underlying buffer that has been filled with ADC samples. - pub fn transfer_complete_handler(&mut self) -> &[u16; SAMPLE_BUFFER_SIZE] { - let next_buffer = self.next_buffer.take().unwrap(); - - // Wait for the transfer to fully complete before continuing. - // 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. - 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); - self.next_buffer.as_ref().unwrap() - } -} - -/// Represents the data input stream from ADC1 -pub struct Adc1Input { - next_buffer: Option<&'static mut [u16; SAMPLE_BUFFER_SIZE]>, - transfer: Transfer< - hal::dma::dma::Stream3, - hal::spi::Spi, - PeripheralToMemory, - &'static mut [u16; SAMPLE_BUFFER_SIZE], - >, - _trigger_transfer: Transfer< - hal::dma::dma::Stream2, - SPI3, - MemoryToPeripheral, - &'static mut [u16; 1], - >, -} - -impl Adc1Input { - /// Construct a new ADC1 input data stream. - /// - /// # Args - /// * `spi` - The SPI interface connected to ADC1. - /// * `trigger_stream` - The DMA stream used to trigger ADC conversions on the SPI interface. - /// * `data_stream` - The DMA stream used to read ADC samples from the SPI RX FIFO. - /// * `trigger_channel` - The ADC sampling timer output compare channel for read triggers. - pub fn new( - spi: hal::spi::Spi, - trigger_stream: hal::dma::dma::Stream2, - data_stream: hal::dma::dma::Stream3, - trigger_channel: sampling_timer::tim2::Channel2, - ) -> 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 using a static word (dont-care - // contents). Thus, neither the memory or peripheral address ever change. This is run in - // circular mode to be completed at every DMA request. - let trigger_config = DmaConfig::default() - .priority(Priority::High) - .circular_buffer(true); - - // Construct the trigger stream to write from memory to the peripheral. - let mut trigger_transfer: Transfer<_, _, MemoryToPeripheral, _> = - Transfer::init( - trigger_stream, - SPI3::new(trigger_channel), - // Note(unsafe). This transaction is read-only and SPI_START is a dont-care value, - // so it is always safe to share. - unsafe { &mut SPI_START }, - None, - trigger_config, - ); - - // The data stream constantly reads from the SPI RX FIFO into a RAM buffer. The peripheral - // stalls reads of the SPI RX FIFO until data is available, so the DMA transfer completes - // after the requested number of samples have been collected. Note that only ADC1's data - // stream is used to trigger a transfer completion interrupt. - let data_config = DmaConfig::default() - .memory_increment(true) - .transfer_complete_interrupt(true) - .priority(Priority::VeryHigh); - - // A SPI peripheral error interrupt is used to determine if the RX FIFO overflows. This - // indicates that samples were dropped due to excessive processing time in the main - // application (e.g. a second DMA transfer completes before the first was done with - // processing). This is used as a flow control indicator to guarantee that no ADC samples - // are lost. - let mut spi = spi.disable(); - spi.listen(hal::spi::Event::Error); - - // The data transfer is always a transfer of data from the peripheral to a RAM buffer. - let mut data_transfer: Transfer<_, _, PeripheralToMemory, _> = - Transfer::init( - data_stream, - spi, - // Note(unsafe): The ADC1_BUF0 is "owned" by this peripheral. It shall not be used - // anywhere else in the module. - unsafe { &mut ADC1_BUF0 }, - None, - data_config, - ); - - data_transfer.start(|spi| { - // Allow the SPI FIFOs to operate using only DMA data channels. - spi.enable_dma_rx(); - 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()); - }); - - trigger_transfer.start(|_| {}); - - Self { - // Note(unsafe): The ADC1_BUF1 is "owned" by this peripheral. It shall not be used - // anywhere else in the module. - next_buffer: unsafe { Some(&mut ADC1_BUF1) }, - transfer: data_transfer, - _trigger_transfer: trigger_transfer, + impl $spi { + pub fn new( + _channel: sampling_timer::tim2::$trigger_channel, + ) -> Self { + Self { _channel } + } } - } - /// Handle a transfer completion. - /// - /// # Returns - /// A reference to the underlying buffer that has been filled with ADC samples. - pub fn transfer_complete_handler(&mut self) -> &[u16; SAMPLE_BUFFER_SIZE] { - let next_buffer = self.next_buffer.take().unwrap(); + // Note(unsafe): This structure is only safe to instantiate once. The DMA request is hard-coded and + // may only be used if ownership of the timer2 $trigger_channel compare channel is assured, which is + // ensured by maintaining ownership of the channel. + unsafe impl TargetAddress for $spi { + /// SPI is configured to operate using 16-bit transfer words. + type MemSize = u16; - // Wait for the transfer to fully complete before continuing. - // 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. - while self.transfer.get_transfer_complete_flag() == false {} + /// SPI DMA requests are generated whenever TIM2 CH1 comparison occurs. + const REQUEST_LINE: Option = Some(DMAReq::$dma_req as u8); - // Start the next transfer. - self.transfer.clear_interrupts(); - let (prev_buffer, _) = - self.transfer.next_transfer(next_buffer).unwrap(); + /// Whenever the DMA request occurs, it should write into SPI's TX FIFO to start a DMA + /// transfer. + fn address(&self) -> u32 { + // Note(unsafe): It is assumed that SPI is owned by another DMA transfer and this DMA is + // only used for the transmit-half of DMA. + let regs = unsafe { &*hal::stm32::$spi::ptr() }; + ®s.txdr as *const _ as u32 + } + } - self.next_buffer.replace(prev_buffer); - self.next_buffer.as_ref().unwrap() - } + /// Represents data associated with ADC0. + pub struct $name { + next_buffer: Option<&'static mut [u16; SAMPLE_BUFFER_SIZE]>, + transfer: Transfer< + hal::dma::dma::$data_stream, + hal::spi::Spi, + PeripheralToMemory, + &'static mut [u16; SAMPLE_BUFFER_SIZE], + >, + _trigger_transfer: Transfer< + hal::dma::dma::$trigger_stream, + $spi, + MemoryToPeripheral, + &'static mut [u16; 1], + >, + } + + impl $name { + /// Construct the ADC0 input channel. + /// + /// # Args + /// * `spi` - The SPI interface used to communicate with the ADC. + /// * `trigger_stream` - The DMA stream used to trigger each ADC transfer by writing a word into + /// the SPI TX FIFO. + /// * `data_stream` - The DMA stream used to read samples received over SPI into a data buffer. + /// * `_trigger_channel` - The ADC sampling timer output compare channel for read triggers. + pub fn new( + spi: hal::spi::Spi, + trigger_stream: hal::dma::dma::$trigger_stream< + hal::stm32::DMA1, + >, + data_stream: hal::dma::dma::$data_stream, + trigger_channel: sampling_timer::tim2::$trigger_channel, + ) -> 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 using a static word (dont-care + // contents). Thus, neither the memory or peripheral address ever change. This is run in + // circular mode to be completed at every DMA request. + let trigger_config = DmaConfig::default() + .priority(Priority::High) + .circular_buffer(true); + + // Construct the trigger stream to write from memory to the peripheral. + let mut trigger_transfer: Transfer< + _, + _, + MemoryToPeripheral, + _, + > = Transfer::init( + trigger_stream, + $spi::new(trigger_channel), + // Note(unsafe): Because this is a Memory->Peripheral transfer, this data is never + // actually modified. It technically only needs to be immutably borrowed, but the + // current HAL API only supports mutable borrows. + unsafe { &mut SPI_START }, + None, + trigger_config, + ); + + // The data stream constantly reads from the SPI RX FIFO into a RAM buffer. The peripheral + // stalls reads of the SPI RX FIFO until data is available, so the DMA transfer completes + // after the requested number of samples have been collected. Note that only ADC1's (sic!) + // data stream is used to trigger a transfer completion interrupt. + let data_config = DmaConfig::default() + .memory_increment(true) + .priority(Priority::VeryHigh); + + // A SPI peripheral error interrupt is used to determine if the RX FIFO overflows. This + // indicates that samples were dropped due to excessive processing time in the main + // application (e.g. a second DMA transfer completes before the first was done with + // processing). This is used as a flow control indicator to guarantee that no ADC samples + // are lost. + let mut spi = spi.disable(); + spi.listen(hal::spi::Event::Error); + + // The data transfer is always a transfer of data from the peripheral to a RAM buffer. + let mut data_transfer: Transfer<_, _, PeripheralToMemory, _> = + Transfer::init( + data_stream, + spi, + // Note(unsafe): The ADC_BUF[$index][0] is "owned" by this peripheral. + // It shall not be used anywhere else in the module. + unsafe { &mut ADC_BUF[$index][0] }, + None, + data_config, + ); + + data_transfer.start(|spi| { + // Allow the SPI FIFOs to operate using only DMA data channels. + spi.enable_dma_rx(); + 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()); + }); + + trigger_transfer.start(|_| {}); + + Self { + // Note(unsafe): The ADC_BUF[$index][1] is "owned" by this peripheral. It shall not be used + // anywhere else in the module. + next_buffer: unsafe { Some(&mut ADC_BUF[$index][1]) }, + transfer: data_transfer, + _trigger_transfer: trigger_transfer, + } + } + + /// Handle a transfer completion. + /// + /// # Returns + /// A reference to the underlying buffer that has been filled with ADC samples. + pub fn transfer_complete_handler( + &mut self, + ) -> &[u16; SAMPLE_BUFFER_SIZE] { + let next_buffer = self.next_buffer.take().unwrap(); + + // Wait for the transfer to fully complete before continuing. + // 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. + 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); + self.next_buffer.as_ref().unwrap() + } + } + }; } + +adc_input!(Adc0Input, 0, Stream0, Stream1, SPI2, Channel1, TIM2_CH1); +adc_input!(Adc1Input, 1, Stream2, Stream3, SPI3, Channel2, TIM2_CH2); diff --git a/src/main.rs b/src/main.rs index ece4340..4bf67cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -69,7 +69,7 @@ mod pounder; mod sampling_timer; mod server; -use adc::{Adc0Input, Adc1Input, AdcInputs}; +use adc::{Adc0Input, Adc1Input}; use dac::{Dac0Output, Dac1Output, DacOutputs}; use dsp::iir; @@ -188,7 +188,7 @@ const APP: () = { afe0: AFE0, afe1: AFE1, - adcs: AdcInputs, + adcs: (Adc0Input, Adc1Input), dacs: DacOutputs, eeprom_i2c: hal::i2c::I2c, @@ -356,7 +356,7 @@ const APP: () = { ) }; - AdcInputs::new(adc0, adc1) + (adc0, adc1) }; let dacs = { @@ -746,8 +746,8 @@ const APP: () = { #[task(binds=DMA1_STR3, resources=[adcs, dacs, iir_state, iir_ch], priority=2)] fn adc_update(c: adc_update::Context) { - let (adc0_samples, adc1_samples) = - c.resources.adcs.transfer_complete_handler(); + let adc0_samples = c.resources.adcs.0.transfer_complete_handler(); + let adc1_samples = c.resources.adcs.1.transfer_complete_handler(); let (dac0, dac1) = c.resources.dacs.prepare_data(); From c04180635b9b50e347c8bfba48c9c6370d06c2b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Thu, 26 Nov 2020 11:16:08 +0100 Subject: [PATCH 07/19] dacs: macros --- src/dac.rs | 427 ++++++++++++++++------------------------------------ src/main.rs | 12 +- 2 files changed, 139 insertions(+), 300 deletions(-) diff --git a/src/dac.rs b/src/dac.rs index d6325c2..7f99001 100644 --- a/src/dac.rs +++ b/src/dac.rs @@ -11,306 +11,143 @@ use super::{ // 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. +// startup are undefined. The dimension are `ADC_BUF[adc_index][ping_pong_index][sample_index]`. #[link_section = ".axisram.buffers"] -static mut DAC0_BUF0: [u16; SAMPLE_BUFFER_SIZE] = [0; SAMPLE_BUFFER_SIZE]; +static mut DAC_BUF: [[[u16; SAMPLE_BUFFER_SIZE]; 2]; 2] = + [[[0; SAMPLE_BUFFER_SIZE]; 2]; 2]; -#[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]; - -/// SPI4 is used as a type for indicating a DMA transfer into the SPI4 TX FIFO -struct SPI4 { - spi: hal::spi::Spi, - _channel: sampling_timer::tim2::Channel3, -} - -impl SPI4 { - pub fn new( - _channel: sampling_timer::tim2::Channel3, - spi: hal::spi::Spi, - ) -> Self { - Self { _channel, spi } - } -} - -// Note(unsafe): This is safe because the DMA request line is logically owned by this module. -// Additionally, the SPI is owned by this structure and is known to be configured for u16 word -// sizes. -unsafe impl TargetAddress 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 = Some(DMAReq::TIM2_CH3 as u8); - - /// Whenever the DMA request occurs, it should write into SPI4's TX FIFO. - fn address(&self) -> u32 { - &self.spi.inner().txdr as *const _ as u32 - } -} - -/// SPI5 is used as a ZST (zero-sized type) for indicating a DMA transfer into the SPI5 TX FIFO -struct SPI5 { - _channel: sampling_timer::tim2::Channel4, - spi: hal::spi::Spi, -} - -impl SPI5 { - pub fn new( - _channel: sampling_timer::tim2::Channel4, - spi: hal::spi::Spi, - ) -> Self { - Self { _channel, spi } - } -} - -// Note(unsafe): This is safe because the DMA request line is logically owned by this module. -// Additionally, the SPI is owned by this structure and is known to be configured for u16 word -// sizes. -unsafe impl TargetAddress 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 = Some(DMAReq::TIM2_CH4 as u8); - - /// Whenever the DMA request occurs, it should write into SPI5's TX FIFO - fn address(&self) -> u32 { - &self.spi.inner().txdr as *const _ as u32 - } -} - -/// Represents both DAC output channels. -pub struct DacOutputs { - dac0: Dac0Output, - dac1: Dac1Output, -} - -impl DacOutputs { - /// Construct the DAC outputs. - pub fn new(dac0: Dac0Output, dac1: Dac1Output) -> Self { - Self { dac0, dac1 } - } - - /// Borrow the next DAC output buffers to populate the DAC output codes in-place. - /// - /// # Returns - /// (dac0, dac1) where each value is a mutable reference to the output code array for DAC0 and - /// DAC1 respectively. - pub fn prepare_data( - &mut self, - ) -> ( - &mut [u16; SAMPLE_BUFFER_SIZE], - &mut [u16; SAMPLE_BUFFER_SIZE], - ) { - (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(); - } -} - -/// Represents data associated with DAC0. -pub struct Dac0Output { - next_buffer: Option<&'static mut [u16; SAMPLE_BUFFER_SIZE]>, - // Note: SPI TX functionality may not be used from this structure to ensure safety with DMA. - transfer: Transfer< - hal::dma::dma::Stream4, - SPI4, - MemoryToPeripheral, - &'static mut [u16; SAMPLE_BUFFER_SIZE], - >, - first_transfer: bool, -} - -impl Dac0Output { - /// Construct the DAC0 output channel. - /// - /// # Args - /// * `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. - pub fn new( - spi: hal::spi::Spi, - stream: hal::dma::dma::Stream4, - trigger_channel: sampling_timer::tim2::Channel3, - ) -> 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 stream constantly writes to the TX FIFO to write new update codes. - let trigger_config = DmaConfig::default() - .memory_increment(true) - .peripheral_increment(false); - - // 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()); - - // 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, - ); - - Self { - transfer, - // Note(unsafe): This buffer is only used once and provided for the next DMA transfer. - next_buffer: unsafe { Some(&mut DAC0_BUF1) }, - first_transfer: true, - } - } - - /// 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. - /// - /// # Args - /// * `data` - The next data to write to the DAC. - pub fn commit_buffer(&mut self) { - 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 { - // 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. - while self.transfer.get_transfer_complete_flag() == false {} +macro_rules! dac_output { + ($name:ident, $index:literal, $data_stream:ident, + $spi:ident, $trigger_channel:ident, $dma_req:ident) => { + /// SPI is used as a type for indicating a DMA transfer into the SPI TX FIFO + struct $spi { + spi: hal::spi::Spi, + _channel: sampling_timer::tim2::$trigger_channel, } - // Start the next transfer. - self.transfer.clear_interrupts(); - let (prev_buffer, _) = - self.transfer.next_transfer(next_buffer).unwrap(); - - self.next_buffer.replace(prev_buffer); - } -} - -/// 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, - SPI5, - MemoryToPeripheral, - &'static mut [u16; SAMPLE_BUFFER_SIZE], - >, - first_transfer: bool, -} - -impl Dac1Output { - /// Construct a new DAC1 output data stream. - /// - /// # 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, - stream: hal::dma::dma::Stream5, - trigger_channel: sampling_timer::tim2::Channel4, - ) -> 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()); - - // 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, - ); - - Self { - // Note(unsafe): This buffer is only used once and provided for the next DMA transfer. - next_buffer: unsafe { Some(&mut DAC1_BUF1) }, - transfer, - first_transfer: true, - } - } - - /// 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. - /// - /// # Args - /// * `data` - The next data to write to the DAC. - pub fn commit_buffer(&mut self) { - 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 { - // 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. - while self.transfer.get_transfer_complete_flag() == false {} + impl $spi { + pub fn new( + _channel: sampling_timer::tim2::$trigger_channel, + spi: hal::spi::Spi, + ) -> Self { + Self { _channel, spi } + } } - // Start the next transfer. - self.transfer.clear_interrupts(); - let (prev_buffer, _) = - self.transfer.next_transfer(next_buffer).unwrap(); + // Note(unsafe): This is safe because the DMA request line is logically owned by this module. + // Additionally, the SPI is owned by this structure and is known to be configured for u16 word + // sizes. + unsafe impl TargetAddress for $spi { + /// SPI is configured to operate using 16-bit transfer words. + type MemSize = u16; - self.next_buffer.replace(prev_buffer); - } + /// SPI DMA requests are generated whenever TIM2 CH3 comparison occurs. + const REQUEST_LINE: Option = Some(DMAReq::$dma_req as u8); + + /// Whenever the DMA request occurs, it should write into SPI's TX FIFO. + fn address(&self) -> u32 { + &self.spi.inner().txdr as *const _ as u32 + } + } + + /// Represents data associated with DAC0. + pub struct $name { + next_buffer: Option<&'static mut [u16; SAMPLE_BUFFER_SIZE]>, + // Note: SPI TX functionality may not be used from this structure to ensure safety with DMA. + transfer: Transfer< + hal::dma::dma::$data_stream, + $spi, + MemoryToPeripheral, + &'static mut [u16; SAMPLE_BUFFER_SIZE], + >, + first_transfer: bool, + } + + impl $name { + /// Construct the DAC output channel. + /// + /// # Args + /// * `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. + pub fn new( + spi: hal::spi::Spi, + stream: hal::dma::dma::$data_stream, + trigger_channel: sampling_timer::tim2::$trigger_channel, + ) -> 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 stream constantly writes to the TX FIFO to write new update codes. + let trigger_config = DmaConfig::default() + .memory_increment(true) + .peripheral_increment(false); + + // 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()); + + // Construct the trigger stream to write from memory to the peripheral. + let transfer: Transfer<_, _, MemoryToPeripheral, _> = + Transfer::init( + stream, + $spi::new(trigger_channel, spi), + // Note(unsafe): This buffer is only used once and provided for the DMA transfer. + unsafe { &mut DAC_BUF[$index][0] }, + None, + trigger_config, + ); + + Self { + transfer, + // Note(unsafe): This buffer is only used once and provided for the next DMA transfer. + next_buffer: unsafe { Some(&mut DAC_BUF[$index][1]) }, + first_transfer: true, + } + } + + /// 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. + /// + /// # Args + /// * `data` - The next data to write to the DAC. + pub fn commit_buffer(&mut self) { + 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 { + // 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. + 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); + } + } + }; } + +dac_output!(Dac0Output, 0, Stream4, SPI4, Channel3, TIM2_CH3); +dac_output!(Dac1Output, 1, Stream5, SPI5, Channel4, TIM2_CH4); diff --git a/src/main.rs b/src/main.rs index 4bf67cf..4dd91e3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -70,7 +70,7 @@ mod sampling_timer; mod server; use adc::{Adc0Input, Adc1Input}; -use dac::{Dac0Output, Dac1Output, DacOutputs}; +use dac::{Dac0Output, Dac1Output}; use dsp::iir; #[cfg(not(feature = "semihosting"))] @@ -189,7 +189,7 @@ const APP: () = { afe1: AFE1, adcs: (Adc0Input, Adc1Input), - dacs: DacOutputs, + dacs: (Dac0Output, Dac1Output), eeprom_i2c: hal::i2c::I2c, @@ -441,7 +441,7 @@ const APP: () = { dma_streams.5, sampling_timer_channels.ch4, ); - DacOutputs::new(dac0, dac1) + (dac0, dac1) }; let mut fp_led_0 = gpiod.pd5.into_push_pull_output(); @@ -749,7 +749,8 @@ const APP: () = { let adc0_samples = c.resources.adcs.0.transfer_complete_handler(); let adc1_samples = c.resources.adcs.1.transfer_complete_handler(); - let (dac0, dac1) = c.resources.dacs.prepare_data(); + let dac0 = c.resources.dacs.0.prepare_buffer(); + let dac1 = c.resources.dacs.1.prepare_buffer(); for (i, (adc0, adc1)) in adc0_samples.iter().zip(adc1_samples.iter()).enumerate() @@ -769,7 +770,8 @@ const APP: () = { }; } - c.resources.dacs.commit_data(); + c.resources.dacs.0.commit_buffer(); + c.resources.dacs.1.commit_buffer(); } #[idle(resources=[net_interface, pounder, mac_addr, eth_mac, iir_state, iir_ch, afe0, afe1])] From 911fb661461eb97df144b402bb8c4dc9dfa1cea1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Thu, 26 Nov 2020 11:29:16 +0100 Subject: [PATCH 08/19] main: process() code duplication a bit --- src/main.rs | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4dd91e3..a4e2fc4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -745,29 +745,23 @@ const APP: () = { } #[task(binds=DMA1_STR3, resources=[adcs, dacs, iir_state, iir_ch], priority=2)] - fn adc_update(c: adc_update::Context) { - let adc0_samples = c.resources.adcs.0.transfer_complete_handler(); - let adc1_samples = c.resources.adcs.1.transfer_complete_handler(); + fn process(c: process::Context) { + let adc_samples = [ + c.resources.adcs.0.transfer_complete_handler(), + c.resources.adcs.1.transfer_complete_handler(), + ]; + let dac_samples = [ + c.resources.dacs.0.prepare_buffer(), + c.resources.dacs.1.prepare_buffer(), + ]; - let dac0 = c.resources.dacs.0.prepare_buffer(); - let dac1 = c.resources.dacs.1.prepare_buffer(); - - for (i, (adc0, adc1)) in - adc0_samples.iter().zip(adc1_samples.iter()).enumerate() - { - dac0[i] = { - let x0 = f32::from(*adc0 as i16); - let y0 = c.resources.iir_ch[0] - .update(&mut c.resources.iir_state[0], x0); - y0 as i16 as u16 ^ 0x8000 - }; - - dac1[i] = { - let x1 = f32::from(*adc1 as i16); - let y1 = c.resources.iir_ch[1] - .update(&mut c.resources.iir_state[1], x1); - y1 as i16 as u16 ^ 0x8000 - }; + for channel in 0..adc_samples.len() { + for sample in 0..adc_samples[0].len() { + let x = f32::from(adc_samples[channel][sample] as i16); + let y = c.resources.iir_ch[channel] + .update(&mut c.resources.iir_state[channel], x); + dac_samples[channel][sample] = y as i16 as u16 ^ 0x8000; + } } c.resources.dacs.0.commit_buffer(); From 71898308968ef39c82e87d021a18044afcc69b2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Thu, 26 Nov 2020 11:33:08 +0100 Subject: [PATCH 09/19] main: put AFEs into a tuple --- src/main.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index a4e2fc4..d7f30c7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -185,8 +185,7 @@ macro_rules! route_request { #[rtic::app(device = stm32h7xx_hal::stm32, peripherals = true, monotonic = rtic::cyccnt::CYCCNT)] const APP: () = { struct Resources { - afe0: AFE0, - afe1: AFE1, + afes: (AFE0, AFE1), adcs: (Adc0Input, Adc1Input), dacs: (Dac0Output, Dac1Output), @@ -729,8 +728,7 @@ const APP: () = { sampling_timer.start(); init::LateResources { - afe0: afe0, - afe1: afe1, + afes: (afe0, afe1), adcs, dacs, @@ -768,7 +766,7 @@ const APP: () = { c.resources.dacs.1.commit_buffer(); } - #[idle(resources=[net_interface, pounder, mac_addr, eth_mac, iir_state, iir_ch, afe0, afe1])] + #[idle(resources=[net_interface, pounder, mac_addr, eth_mac, iir_state, iir_ch, afes])] fn idle(mut c: idle::Context) -> ! { let mut socket_set_entries: [_; 8] = Default::default(); let mut sockets = @@ -828,8 +826,8 @@ const APP: () = { Ok::(state) }), - "stabilizer/afe0/gain": (|| c.resources.afe0.get_gain()), - "stabilizer/afe1/gain": (|| c.resources.afe1.get_gain()), + "stabilizer/afe0/gain": (|| c.resources.afes.0.get_gain()), + "stabilizer/afe1/gain": (|| c.resources.afes.1.get_gain()), "pounder/in0": (|| { match c.resources.pounder { Some(pounder) => @@ -924,10 +922,10 @@ const APP: () = { } }), "stabilizer/afe0/gain": afe::Gain, (|gain| { - Ok::<(), ()>(c.resources.afe0.set_gain(gain)) + Ok::<(), ()>(c.resources.afes.0.set_gain(gain)) }), "stabilizer/afe1/gain": afe::Gain, (|gain| { - Ok::<(), ()>(c.resources.afe1.set_gain(gain)) + Ok::<(), ()>(c.resources.afes.1.set_gain(gain)) }) ] ) From 754ebed50db05d05859bc2b6076c527bbfbb0e06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Thu, 26 Nov 2020 11:55:15 +0100 Subject: [PATCH 10/19] adc: transfer complete interrupt on Adc1 --- src/adc.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/adc.rs b/src/adc.rs index a16976a..2849f00 100644 --- a/src/adc.rs +++ b/src/adc.rs @@ -137,6 +137,7 @@ macro_rules! adc_input { // data stream is used to trigger a transfer completion interrupt. let data_config = DmaConfig::default() .memory_increment(true) + .transfer_complete_interrupt($index == 1) .priority(Priority::VeryHigh); // A SPI peripheral error interrupt is used to determine if the RX FIFO overflows. This From d8c6f39d0f7207e5b1ea58de532227242270295f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Thu, 26 Nov 2020 13:51:39 +0100 Subject: [PATCH 11/19] adc/dac: make the buffer handling more symmetric --- src/adc.rs | 26 ++++++++++++++++++-------- src/dac.rs | 18 +++++++++++------- src/main.rs | 17 ++++++++++------- 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/src/adc.rs b/src/adc.rs index 2849f00..145c5af 100644 --- a/src/adc.rs +++ b/src/adc.rs @@ -181,27 +181,37 @@ macro_rules! adc_input { } } - /// Handle a transfer completion. + /// Obtain a buffer filled with ADC samples. /// /// # Returns /// A reference to the underlying buffer that has been filled with ADC samples. - pub fn transfer_complete_handler( + pub fn acquire_buffer( &mut self, - ) -> &[u16; SAMPLE_BUFFER_SIZE] { - let next_buffer = self.next_buffer.take().unwrap(); - + ) -> &'static mut [u16; SAMPLE_BUFFER_SIZE] { // Wait for the transfer to fully complete before continuing. // 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. - while self.transfer.get_transfer_complete_flag() == false {} + while !self.transfer.get_transfer_complete_flag() {} + + let next_buffer = self.next_buffer.take().unwrap(); // Start the next transfer. self.transfer.clear_interrupts(); let (prev_buffer, _) = self.transfer.next_transfer(next_buffer).unwrap(); - self.next_buffer.replace(prev_buffer); - self.next_buffer.as_ref().unwrap() + prev_buffer + } + + /// Release a buffer of ADC samples to the pool. + /// + /// # Args + /// * `next_buffer` - Buffer of ADC samples to be re-used. + pub fn release_buffer( + &mut self, + next_buffer: &'static mut [u16; SAMPLE_BUFFER_SIZE], + ) { + self.next_buffer.replace(next_buffer); // .unwrap_none() https://github.com/rust-lang/rust/issues/62633 } } }; diff --git a/src/dac.rs b/src/dac.rs index 7f99001..2c61b10 100644 --- a/src/dac.rs +++ b/src/dac.rs @@ -116,18 +116,21 @@ macro_rules! dac_output { } } - /// 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() + /// Acquire the next output buffer to populate it with DAC codes. + pub fn acquire_buffer( + &mut self, + ) -> &'static mut [u16; SAMPLE_BUFFER_SIZE] { + self.next_buffer.take().unwrap() } /// Enqueue the next buffer for transmission to the DAC. /// /// # Args /// * `data` - The next data to write to the DAC. - pub fn commit_buffer(&mut self) { - let next_buffer = self.next_buffer.take().unwrap(); - + pub fn release_buffer( + &mut self, + next_buffer: &'static mut [u16; SAMPLE_BUFFER_SIZE], + ) { // 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 { @@ -135,7 +138,7 @@ macro_rules! dac_output { } else { // 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. - while self.transfer.get_transfer_complete_flag() == false {} + while !self.transfer.get_transfer_complete_flag() {} } // Start the next transfer. @@ -143,6 +146,7 @@ macro_rules! dac_output { let (prev_buffer, _) = self.transfer.next_transfer(next_buffer).unwrap(); + // .unwrap_none() https://github.com/rust-lang/rust/issues/62633 self.next_buffer.replace(prev_buffer); } } diff --git a/src/main.rs b/src/main.rs index d7f30c7..925e277 100644 --- a/src/main.rs +++ b/src/main.rs @@ -745,12 +745,12 @@ const APP: () = { #[task(binds=DMA1_STR3, resources=[adcs, dacs, iir_state, iir_ch], priority=2)] fn process(c: process::Context) { let adc_samples = [ - c.resources.adcs.0.transfer_complete_handler(), - c.resources.adcs.1.transfer_complete_handler(), + c.resources.adcs.0.acquire_buffer(), + c.resources.adcs.1.acquire_buffer(), ]; let dac_samples = [ - c.resources.dacs.0.prepare_buffer(), - c.resources.dacs.1.prepare_buffer(), + c.resources.dacs.0.acquire_buffer(), + c.resources.dacs.1.acquire_buffer(), ]; for channel in 0..adc_samples.len() { @@ -761,9 +761,12 @@ const APP: () = { dac_samples[channel][sample] = y as i16 as u16 ^ 0x8000; } } - - c.resources.dacs.0.commit_buffer(); - c.resources.dacs.1.commit_buffer(); + let [adc0, adc1] = adc_samples; + c.resources.adcs.0.release_buffer(adc0); + c.resources.adcs.0.release_buffer(adc1); + let [dac0, dac1] = dac_samples; + c.resources.dacs.0.release_buffer(dac0); + c.resources.dacs.1.release_buffer(dac1); } #[idle(resources=[net_interface, pounder, mac_addr, eth_mac, iir_state, iir_ch, afes])] From 468929690df7493baef5ae6537f09bee9443ec73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Thu, 26 Nov 2020 14:19:09 +0100 Subject: [PATCH 12/19] iir: vminnm/vmaxnm --- Cargo.toml | 2 +- dsp/Cargo.toml | 3 +++ dsp/src/iir.rs | 27 +++++++++++++++++++++++++++ dsp/src/lib.rs | 1 + src/main.rs | 3 +++ 5 files changed, 35 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 301956c..896eecf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -62,7 +62,7 @@ branch = "dma" [features] semihosting = ["panic-semihosting", "cortex-m-log/semihosting"] bkpt = [ ] -nightly = ["cortex-m/inline-asm"] +nightly = ["cortex-m/inline-asm", "dsp/nightly"] [profile.dev] codegen-units = 1 diff --git a/dsp/Cargo.toml b/dsp/Cargo.toml index 625d0f0..c8ef52b 100644 --- a/dsp/Cargo.toml +++ b/dsp/Cargo.toml @@ -6,3 +6,6 @@ edition = "2018" [dependencies] serde = { version = "1.0", features = ["derive"], default-features = false } + +[features] +nightly = [] diff --git a/dsp/src/iir.rs b/dsp/src/iir.rs index 04d7c8e..e081556 100644 --- a/dsp/src/iir.rs +++ b/dsp/src/iir.rs @@ -24,6 +24,7 @@ fn copysign(x: f32, y: f32) -> f32 { } } +#[cfg(not(feature = "nightly"))] fn max(x: f32, y: f32) -> f32 { if x > y { x @@ -32,6 +33,7 @@ fn max(x: f32, y: f32) -> f32 { } } +#[cfg(not(feature = "nightly"))] fn min(x: f32, y: f32) -> f32 { if x < y { x @@ -40,6 +42,31 @@ fn min(x: f32, y: f32) -> f32 { } } +#[cfg(feature = "nightly")] +fn max(x: f32, y: f32) -> f32 { + let o: f32; + unsafe { + asm!("vmaxnm.f32 {}, {}, {}", + lateout(sreg) o, in(sreg) x, in(sreg) y, + options(pure, nomem, nostack, preserves_flags) + ); + } + o +} + +#[cfg(feature = "nightly")] +fn min(x: f32, y: f32) -> f32 { + let o: f32; + unsafe { + asm!("vminnm.f32 {}, {}, {}", + lateout(sreg) o, in(sreg) x, in(sreg) y, + options(pure, nomem, nostack, preserves_flags) + ); + } + o +} + + // Multiply-accumulate vectors `x` and `a`. // // A.k.a. dot product. diff --git a/dsp/src/lib.rs b/dsp/src/lib.rs index 3c44bbc..ac25d1e 100644 --- a/dsp/src/lib.rs +++ b/dsp/src/lib.rs @@ -1,3 +1,4 @@ #![no_std] +#![cfg_attr(feature = "nightly", feature(asm))] pub mod iir; diff --git a/src/main.rs b/src/main.rs index 3173158..15046b8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,9 @@ fn panic(_info: &core::panic::PanicInfo) -> ! { let gpiod = unsafe { &*hal::stm32::GPIOD::ptr() }; gpiod.odr.modify(|_, w| w.odr6().high().odr12().high()); // FP_LED_1, FP_LED_3 + #[cfg(feature = "nightly")] + core::intrinsics::abort(); + #[cfg(not(feature = "nightly"))] unsafe { core::intrinsics::abort(); } From ea3e343c39d7768fa6050c774ee360ec9473add4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Thu, 26 Nov 2020 14:30:09 +0100 Subject: [PATCH 13/19] cargo fmt [nfc] --- dsp/src/iir.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/dsp/src/iir.rs b/dsp/src/iir.rs index e081556..33d8fdf 100644 --- a/dsp/src/iir.rs +++ b/dsp/src/iir.rs @@ -66,7 +66,6 @@ fn min(x: f32, y: f32) -> f32 { o } - // Multiply-accumulate vectors `x` and `a`. // // A.k.a. dot product. From 8cf380a488e5964a58ee420bb2312d2877dcc632 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Thu, 26 Nov 2020 14:40:24 +0100 Subject: [PATCH 14/19] dac/adc: doc cleanup [nfc] --- src/adc.rs | 6 +++--- src/dac.rs | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/adc.rs b/src/adc.rs index 145c5af..b275d27 100644 --- a/src/adc.rs +++ b/src/adc.rs @@ -35,7 +35,7 @@ static mut ADC_BUF: [[[u16; SAMPLE_BUFFER_SIZE]; 2]; 2] = macro_rules! adc_input { ($name:ident, $index:literal, $trigger_stream:ident, $data_stream:ident, $spi:ident, $trigger_channel:ident, $dma_req:ident) => { - /// SPI is used as a ZST (zero-sized type) for indicating a DMA transfer into the SPI TX FIFO + /// $spi is used as a ZST (zero-sized type) for indicating a DMA transfer into the SPI TX FIFO /// whenever the tim2 update dma request occurs. struct $spi { _channel: sampling_timer::tim2::$trigger_channel, @@ -68,7 +68,7 @@ macro_rules! adc_input { } } - /// Represents data associated with ADC0. + /// Represents data associated with ADC. pub struct $name { next_buffer: Option<&'static mut [u16; SAMPLE_BUFFER_SIZE]>, transfer: Transfer< @@ -86,7 +86,7 @@ macro_rules! adc_input { } impl $name { - /// Construct the ADC0 input channel. + /// Construct the ADC input channel. /// /// # Args /// * `spi` - The SPI interface used to communicate with the ADC. diff --git a/src/dac.rs b/src/dac.rs index 2c61b10..95ae8b0 100644 --- a/src/dac.rs +++ b/src/dac.rs @@ -19,7 +19,7 @@ static mut DAC_BUF: [[[u16; SAMPLE_BUFFER_SIZE]; 2]; 2] = macro_rules! dac_output { ($name:ident, $index:literal, $data_stream:ident, $spi:ident, $trigger_channel:ident, $dma_req:ident) => { - /// SPI is used as a type for indicating a DMA transfer into the SPI TX FIFO + /// $spi is used as a type for indicating a DMA transfer into the SPI TX FIFO struct $spi { spi: hal::spi::Spi, _channel: sampling_timer::tim2::$trigger_channel, @@ -50,7 +50,7 @@ macro_rules! dac_output { } } - /// Represents data associated with DAC0. + /// Represents data associated with DAC. pub struct $name { next_buffer: Option<&'static mut [u16; SAMPLE_BUFFER_SIZE]>, // Note: SPI TX functionality may not be used from this structure to ensure safety with DMA. From ab50f550623627d377a7fef7bc7dc0861bed4999 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Thu, 26 Nov 2020 15:41:19 +0100 Subject: [PATCH 15/19] adc/dac: docstrings --- src/adc.rs | 2 +- src/dac.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/adc.rs b/src/adc.rs index b275d27..02f686f 100644 --- a/src/adc.rs +++ b/src/adc.rs @@ -55,7 +55,7 @@ macro_rules! adc_input { /// SPI is configured to operate using 16-bit transfer words. type MemSize = u16; - /// SPI DMA requests are generated whenever TIM2 CH1 comparison occurs. + /// SPI DMA requests are generated whenever TIM2 CHx ($dma_req) comparison occurs. const REQUEST_LINE: Option = Some(DMAReq::$dma_req as u8); /// Whenever the DMA request occurs, it should write into SPI's TX FIFO to start a DMA diff --git a/src/dac.rs b/src/dac.rs index 95ae8b0..c586e2f 100644 --- a/src/dac.rs +++ b/src/dac.rs @@ -41,7 +41,7 @@ macro_rules! dac_output { /// SPI is configured to operate using 16-bit transfer words. type MemSize = u16; - /// SPI DMA requests are generated whenever TIM2 CH3 comparison occurs. + /// SPI DMA requests are generated whenever TIM2 CHx ($dma_req) comparison occurs. const REQUEST_LINE: Option = Some(DMAReq::$dma_req as u8); /// Whenever the DMA request occurs, it should write into SPI's TX FIFO. From 74349e5d68f4663deff5b9df9d1ac52d069ff294 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Fri, 27 Nov 2020 10:36:30 +0100 Subject: [PATCH 16/19] iir: more generic math helpers, use core::intrinsics --- dsp/src/iir.rs | 54 +++++++++++++++++++++++++------------------------- dsp/src/lib.rs | 2 +- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/dsp/src/iir.rs b/dsp/src/iir.rs index 33d8fdf..c6f2100 100644 --- a/dsp/src/iir.rs +++ b/dsp/src/iir.rs @@ -1,4 +1,4 @@ -use core::ops::{Add, Mul}; +use core::ops::{Add, Mul, Neg}; use serde::{Deserialize, Serialize}; use core::f32; @@ -8,16 +8,24 @@ use core::f32; // `compiler-intrinsics`/llvm should have better (robust, universal, and // faster) implementations. -fn abs(x: f32) -> f32 { - if x >= 0. { +fn abs(x: T) -> T +where + T: PartialOrd + Default + Neg, +{ + if x >= T::default() { x } else { -x } } -fn copysign(x: f32, y: f32) -> f32 { - if (x >= 0. && y >= 0.) || (x <= 0. && y <= 0.) { +fn copysign(x: T, y: T) -> T +where + T: PartialOrd + Default + Neg, +{ + if (x >= T::default() && y >= T::default()) + || (x <= T::default() && y <= T::default()) + { x } else { -x @@ -25,7 +33,10 @@ fn copysign(x: f32, y: f32) -> f32 { } #[cfg(not(feature = "nightly"))] -fn max(x: f32, y: f32) -> f32 { +fn max(x: T, y: T) -> T +where + T: PartialOrd, +{ if x > y { x } else { @@ -34,7 +45,10 @@ fn max(x: f32, y: f32) -> f32 { } #[cfg(not(feature = "nightly"))] -fn min(x: f32, y: f32) -> f32 { +fn min(x: T, y: T) -> T +where + T: PartialOrd, +{ if x < y { x } else { @@ -44,26 +58,12 @@ fn min(x: f32, y: f32) -> f32 { #[cfg(feature = "nightly")] fn max(x: f32, y: f32) -> f32 { - let o: f32; - unsafe { - asm!("vmaxnm.f32 {}, {}, {}", - lateout(sreg) o, in(sreg) x, in(sreg) y, - options(pure, nomem, nostack, preserves_flags) - ); - } - o + core::intrinsics::maxnumf32(x, y) } #[cfg(feature = "nightly")] fn min(x: f32, y: f32) -> f32 { - let o: f32; - unsafe { - asm!("vminnm.f32 {}, {}, {}", - lateout(sreg) o, in(sreg) x, in(sreg) y, - options(pure, nomem, nostack, preserves_flags) - ); - } - o + core::intrinsics::minnumf32(x, y) } // Multiply-accumulate vectors `x` and `a`. @@ -76,7 +76,7 @@ where { x.iter() .zip(a) - .map(|(&x, &a)| x * a) + .map(|(x, a)| *x * *a) .fold(y0, |y, xa| y + xa) } @@ -84,10 +84,10 @@ where /// /// To represent the IIR state (input and output memory) during the filter update /// this contains the three inputs (x0, x1, x2) and the two outputs (y1, y2) -/// concatenated. +/// concatenated. Lower indices correspond to more recent samples. /// To represent the IIR coefficients, this contains the feed-forward -/// coefficients (b0, b1, b2) followd by the feed-back coefficients (a1, a2), -/// all normalized such that a0 = 1. +/// coefficients (b0, b1, b2) followd by the negated feed-back coefficients +/// (-a1, -a2), all five normalized such that a0 = 1. pub type IIRState = [f32; 5]; /// IIR configuration. diff --git a/dsp/src/lib.rs b/dsp/src/lib.rs index ac25d1e..b2acf34 100644 --- a/dsp/src/lib.rs +++ b/dsp/src/lib.rs @@ -1,4 +1,4 @@ #![no_std] -#![cfg_attr(feature = "nightly", feature(asm))] +#![cfg_attr(feature = "nightly", feature(asm, core_intrinsics))] pub mod iir; From feb229ddd5befd9fdb6fa873458db502d722e82c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Mon, 30 Nov 2020 12:02:14 +0100 Subject: [PATCH 17/19] cargo: add docs for target cpu/features --- .cargo/config | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.cargo/config b/.cargo/config index 9968846..ea1d6c0 100644 --- a/.cargo/config +++ b/.cargo/config @@ -2,8 +2,14 @@ runner = "gdb-multiarch -q -x openocd.gdb" rustflags = [ "-C", "link-arg=-Tlink.x", +# The target (below) defaults to cortex-m4 +# There currently are two different options to go beyond that: +# 1. cortex-m7 has the right flags and instructions (FPU) but no instruction schedule yet "-C", "target-cpu=cortex-m7", +# 2. cortex-m4 with the additional fpv5 instructions and a potentially +# better-than-nothing instruction schedule "-C", "target-feature=+fp-armv8d16", +# When combined they are equivalent to (1) alone ] [build] From b1301a61849ef0fe7669073ed178130d72d66f53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Mon, 30 Nov 2020 15:04:31 +0100 Subject: [PATCH 18/19] dac,adc: spelling --- src/adc.rs | 4 ++-- src/dac.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/adc.rs b/src/adc.rs index 02f686f..43fb0a5 100644 --- a/src/adc.rs +++ b/src/adc.rs @@ -27,7 +27,7 @@ static mut SPI_START: [u16; 1] = [0x00]; // The following global buffers are used for the ADC sample DMA transfers. Two buffers are used for // each transfer in a ping-pong buffer configuration (one is being acquired while the other is being // processed). Note that the contents of AXI SRAM is uninitialized, so the buffer contents on -// startup are undefined. The dimension are `ADC_BUF[adc_index][ping_pong_index][sample_index]`. +// startup are undefined. The dimensions are `ADC_BUF[adc_index][ping_pong_index][sample_index]`. #[link_section = ".axisram.buffers"] static mut ADC_BUF: [[[u16; SAMPLE_BUFFER_SIZE]; 2]; 2] = [[[0; SAMPLE_BUFFER_SIZE]; 2]; 2]; @@ -35,7 +35,7 @@ static mut ADC_BUF: [[[u16; SAMPLE_BUFFER_SIZE]; 2]; 2] = macro_rules! adc_input { ($name:ident, $index:literal, $trigger_stream:ident, $data_stream:ident, $spi:ident, $trigger_channel:ident, $dma_req:ident) => { - /// $spi is used as a ZST (zero-sized type) for indicating a DMA transfer into the SPI TX FIFO + /// $spi is used as a type for indicating a DMA transfer into the SPI TX FIFO /// whenever the tim2 update dma request occurs. struct $spi { _channel: sampling_timer::tim2::$trigger_channel, diff --git a/src/dac.rs b/src/dac.rs index c586e2f..d96109c 100644 --- a/src/dac.rs +++ b/src/dac.rs @@ -11,7 +11,7 @@ use super::{ // 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. The dimension are `ADC_BUF[adc_index][ping_pong_index][sample_index]`. +// startup are undefined. The dimensions are `ADC_BUF[adc_index][ping_pong_index][sample_index]`. #[link_section = ".axisram.buffers"] static mut DAC_BUF: [[[u16; SAMPLE_BUFFER_SIZE]; 2]; 2] = [[[0; SAMPLE_BUFFER_SIZE]; 2]; 2]; From 34d59dac5dd876b1f1ae2ea20333f1cc35cdfb70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Mon, 30 Nov 2020 15:38:14 +0100 Subject: [PATCH 19/19] adc: merge acquire_buffer and release_buffer again --- src/adc.rs | 17 +++-------------- src/main.rs | 3 --- 2 files changed, 3 insertions(+), 17 deletions(-) diff --git a/src/adc.rs b/src/adc.rs index 43fb0a5..e9120aa 100644 --- a/src/adc.rs +++ b/src/adc.rs @@ -185,9 +185,7 @@ macro_rules! adc_input { /// /// # Returns /// A reference to the underlying buffer that has been filled with ADC samples. - pub fn acquire_buffer( - &mut self, - ) -> &'static mut [u16; SAMPLE_BUFFER_SIZE] { + pub fn acquire_buffer(&mut self) -> &[u16; SAMPLE_BUFFER_SIZE] { // Wait for the transfer to fully complete before continuing. // 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. @@ -200,18 +198,9 @@ macro_rules! adc_input { let (prev_buffer, _) = self.transfer.next_transfer(next_buffer).unwrap(); - prev_buffer - } + self.next_buffer.replace(prev_buffer); // .unwrap_none() https://github.com/rust-lang/rust/issues/62633 - /// Release a buffer of ADC samples to the pool. - /// - /// # Args - /// * `next_buffer` - Buffer of ADC samples to be re-used. - pub fn release_buffer( - &mut self, - next_buffer: &'static mut [u16; SAMPLE_BUFFER_SIZE], - ) { - self.next_buffer.replace(next_buffer); // .unwrap_none() https://github.com/rust-lang/rust/issues/62633 + self.next_buffer.as_ref().unwrap() } } }; diff --git a/src/main.rs b/src/main.rs index 0aaf669..e6f83b5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -763,9 +763,6 @@ const APP: () = { dac_samples[channel][sample] = y as i16 as u16 ^ 0x8000; } } - let [adc0, adc1] = adc_samples; - c.resources.adcs.0.release_buffer(adc0); - c.resources.adcs.0.release_buffer(adc1); let [dac0, dac1] = dac_samples; c.resources.dacs.0.release_buffer(dac0); c.resources.dacs.1.release_buffer(dac1);