Use types to strengthen the buffer dimensioning
This commit is contained in:
		| @@ -249,25 +249,21 @@ impl<'d, const N: usize> Saadc<'d, N> { | |||||||
|     /// should continue or stop. |     /// should continue or stop. | ||||||
|     pub async fn run_task_sampler<S, const N0: usize>( |     pub async fn run_task_sampler<S, const N0: usize>( | ||||||
|         &mut self, |         &mut self, | ||||||
|         bufs: &mut [[i16; N0]; 2], |         bufs: &mut [[[i16; N]; N0]; 2], | ||||||
|         sampler: S, |         sampler: S, | ||||||
|     ) where |     ) 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; |         self.run_sampler(bufs, None, sampler).await; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     async fn run_sampler<S, const N0: usize>( |     async fn run_sampler<S, const N0: usize>( | ||||||
|         &mut self, |         &mut self, | ||||||
|         bufs: &mut [[i16; N0]; 2], |         bufs: &mut [[[i16; N]; N0]; 2], | ||||||
|         sample_rate: Option<u16>, |         sample_rate: Option<u16>, | ||||||
|         mut sampler: S, |         mut sampler: S, | ||||||
|     ) where |     ) where | ||||||
|         S: FnMut(&[i16]) -> SamplerState, |         S: FnMut(&[[i16; N]]) -> SamplerState, | ||||||
|     { |     { | ||||||
|         let r = Self::regs(); |         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) }); |             .write(|w| unsafe { w.ptr().bits(bufs[0].as_mut_ptr() as u32) }); | ||||||
|         r.result |         r.result | ||||||
|             .maxcnt |             .maxcnt | ||||||
|             .write(|w| unsafe { w.maxcnt().bits(N0 as _) }); |             .write(|w| unsafe { w.maxcnt().bits((N0 * N) as _) }); | ||||||
|  |  | ||||||
|         // Reset and enable the events |         // Reset and enable the events | ||||||
|         r.events_end.reset(); |         r.events_end.reset(); | ||||||
| @@ -323,7 +319,7 @@ impl<'d, const N: usize> Saadc<'d, N> { | |||||||
|                 r.events_end.reset(); |                 r.events_end.reset(); | ||||||
|                 r.intenset.write(|w| w.end().set()); |                 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 |                     == SamplerState::Sampled | ||||||
|                 { |                 { | ||||||
|                     let next_buffer = 1 - current_buffer; |                     let next_buffer = 1 - current_buffer; | ||||||
| @@ -371,11 +367,11 @@ impl<'d> Saadc<'d, 1> { | |||||||
|     /// should continue or stop. |     /// should continue or stop. | ||||||
|     pub async fn run_timer_sampler<S, const N0: usize>( |     pub async fn run_timer_sampler<S, const N0: usize>( | ||||||
|         &mut self, |         &mut self, | ||||||
|         bufs: &mut [[i16; N0]; 2], |         bufs: &mut [[[i16; 1]; N0]; 2], | ||||||
|         sample_rate: u16, |         sample_rate: u16, | ||||||
|         sampler: S, |         sampler: S, | ||||||
|     ) where |     ) where | ||||||
|         S: FnMut(&[i16]) -> SamplerState, |         S: FnMut(&[[i16; 1]]) -> SamplerState, | ||||||
|     { |     { | ||||||
|         self.run_sampler(bufs, Some(sample_rate), sampler).await; |         self.run_sampler(bufs, Some(sample_rate), sampler).await; | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -39,19 +39,17 @@ async fn main(_spawner: Spawner, mut p: Peripherals) { | |||||||
|  |  | ||||||
|     timer.start(); |     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 c = 0; | ||||||
|     let mut a: i32 = 0; |     let mut a: i32 = 0; | ||||||
|  |  | ||||||
|     saadc |     saadc | ||||||
|         .run_task_sampler(&mut bufs, move |buf| { |         .run_task_sampler(&mut bufs, move |buf| { | ||||||
|             for (i, b) in buf.iter().enumerate() { |             for b in buf { | ||||||
|                 if i % 3 == 0 { |                 a += b[0] as i32; | ||||||
|                     a += *b as i32; |  | ||||||
|                     c += 1; |  | ||||||
|                 } |  | ||||||
|             } |             } | ||||||
|  |             c += buf.len(); | ||||||
|             if c > 10000 { |             if c > 10000 { | ||||||
|                 a = a / c as i32; |                 a = a / c as i32; | ||||||
|                 info!("channel 1: {=i32}", a); |                 info!("channel 1: {=i32}", a); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user