From 785030df963c9071a51fbc6e57e545faccc483c1 Mon Sep 17 00:00:00 2001 From: huntc Date: Sun, 17 Oct 2021 07:51:53 +1100 Subject: [PATCH] Use types to strengthen the buffer dimensioning --- embassy-nrf/src/saadc.rs | 22 +++++++++------------- examples/nrf/src/bin/saadc_continuous.rs | 10 ++++------ 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/embassy-nrf/src/saadc.rs b/embassy-nrf/src/saadc.rs index 6e0bcc16..fd7e64e7 100644 --- a/embassy-nrf/src/saadc.rs +++ b/embassy-nrf/src/saadc.rs @@ -249,25 +249,21 @@ impl<'d, const N: usize> Saadc<'d, N> { /// should continue or stop. pub async fn run_task_sampler( &mut self, - bufs: &mut [[i16; N0]; 2], + bufs: &mut [[[i16; N]; N0]; 2], sampler: S, ) where - S: FnMut(&[i16]) -> SamplerState, + S: FnMut(&[[i16; N]]) -> SamplerState, { - assert!( - N0 % N == 0, - "The buffer size must be a multiple of the number of channels" - ); self.run_sampler(bufs, None, sampler).await; } async fn run_sampler( &mut self, - bufs: &mut [[i16; N0]; 2], + bufs: &mut [[[i16; N]; N0]; 2], sample_rate: Option, mut sampler: S, ) where - S: FnMut(&[i16]) -> SamplerState, + S: FnMut(&[[i16; N]]) -> SamplerState, { let r = Self::regs(); @@ -294,7 +290,7 @@ impl<'d, const N: usize> Saadc<'d, N> { .write(|w| unsafe { w.ptr().bits(bufs[0].as_mut_ptr() as u32) }); r.result .maxcnt - .write(|w| unsafe { w.maxcnt().bits(N0 as _) }); + .write(|w| unsafe { w.maxcnt().bits((N0 * N) as _) }); // Reset and enable the events r.events_end.reset(); @@ -323,7 +319,7 @@ impl<'d, const N: usize> Saadc<'d, N> { r.events_end.reset(); r.intenset.write(|w| w.end().set()); - if sampler(&bufs[current_buffer][0..r.result.amount.read().bits() as usize]) + if sampler(&bufs[current_buffer][0..r.result.amount.read().bits() as usize / N]) == SamplerState::Sampled { let next_buffer = 1 - current_buffer; @@ -358,7 +354,7 @@ impl<'d, const N: usize> Saadc<'d, N> { } impl<'d> Saadc<'d, 1> { - /// Continuous sampling on a single channel with double buffers. The sample + /// Continuous sampling on a single channel with double buffers. The sample /// buffers generally should be a multiple of the number of channels configured. /// /// The internal clock is to be used with a sample rate expressed as a divisor of @@ -371,11 +367,11 @@ impl<'d> Saadc<'d, 1> { /// should continue or stop. pub async fn run_timer_sampler( &mut self, - bufs: &mut [[i16; N0]; 2], + bufs: &mut [[[i16; 1]; N0]; 2], sample_rate: u16, sampler: S, ) where - S: FnMut(&[i16]) -> SamplerState, + S: FnMut(&[[i16; 1]]) -> SamplerState, { self.run_sampler(bufs, Some(sample_rate), sampler).await; } diff --git a/examples/nrf/src/bin/saadc_continuous.rs b/examples/nrf/src/bin/saadc_continuous.rs index d936a7d2..149b9c60 100644 --- a/examples/nrf/src/bin/saadc_continuous.rs +++ b/examples/nrf/src/bin/saadc_continuous.rs @@ -39,19 +39,17 @@ async fn main(_spawner: Spawner, mut p: Peripherals) { timer.start(); - let mut bufs = [[0; 3 * 50]; 2]; // Each buffer of the double buffer has to be large enough for all channels. + let mut bufs = [[[0; 3]; 50]; 2]; let mut c = 0; let mut a: i32 = 0; saadc .run_task_sampler(&mut bufs, move |buf| { - for (i, b) in buf.iter().enumerate() { - if i % 3 == 0 { - a += *b as i32; - c += 1; - } + for b in buf { + a += b[0] as i32; } + c += buf.len(); if c > 10000 { a = a / c as i32; info!("channel 1: {=i32}", a);