aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhuntc <[email protected]>2021-10-17 07:51:53 +1100
committerhuntc <[email protected]>2021-10-18 10:26:11 +1100
commit785030df963c9071a51fbc6e57e545faccc483c1 (patch)
tree8d24e0b85bebe470789a7d7a0ebbe2445b5d9acd
parent0c317a64f6f0f845dd2ca3323c9579fda09be410 (diff)
Use types to strengthen the buffer dimensioning
-rw-r--r--embassy-nrf/src/saadc.rs22
-rw-r--r--examples/nrf/src/bin/saadc_continuous.rs10
2 files changed, 13 insertions, 19 deletions
diff --git a/embassy-nrf/src/saadc.rs b/embassy-nrf/src/saadc.rs
index 6e0bcc167..fd7e64e7b 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> {
249 /// should continue or stop. 249 /// should continue or stop.
250 pub async fn run_task_sampler<S, const N0: usize>( 250 pub async fn run_task_sampler<S, const N0: usize>(
251 &mut self, 251 &mut self,
252 bufs: &mut [[i16; N0]; 2], 252 bufs: &mut [[[i16; N]; N0]; 2],
253 sampler: S, 253 sampler: S,
254 ) where 254 ) where
255 S: FnMut(&[i16]) -> SamplerState, 255 S: FnMut(&[[i16; N]]) -> SamplerState,
256 { 256 {
257 assert!(
258 N0 % N == 0,
259 "The buffer size must be a multiple of the number of channels"
260 );
261 self.run_sampler(bufs, None, sampler).await; 257 self.run_sampler(bufs, None, sampler).await;
262 } 258 }
263 259
264 async fn run_sampler<S, const N0: usize>( 260 async fn run_sampler<S, const N0: usize>(
265 &mut self, 261 &mut self,
266 bufs: &mut [[i16; N0]; 2], 262 bufs: &mut [[[i16; N]; N0]; 2],
267 sample_rate: Option<u16>, 263 sample_rate: Option<u16>,
268 mut sampler: S, 264 mut sampler: S,
269 ) where 265 ) where
270 S: FnMut(&[i16]) -> SamplerState, 266 S: FnMut(&[[i16; N]]) -> SamplerState,
271 { 267 {
272 let r = Self::regs(); 268 let r = Self::regs();
273 269
@@ -294,7 +290,7 @@ impl<'d, const N: usize> Saadc<'d, N> {
294 .write(|w| unsafe { w.ptr().bits(bufs[0].as_mut_ptr() as u32) }); 290 .write(|w| unsafe { w.ptr().bits(bufs[0].as_mut_ptr() as u32) });
295 r.result 291 r.result
296 .maxcnt 292 .maxcnt
297 .write(|w| unsafe { w.maxcnt().bits(N0 as _) }); 293 .write(|w| unsafe { w.maxcnt().bits((N0 * N) as _) });
298 294
299 // Reset and enable the events 295 // Reset and enable the events
300 r.events_end.reset(); 296 r.events_end.reset();
@@ -323,7 +319,7 @@ impl<'d, const N: usize> Saadc<'d, N> {
323 r.events_end.reset(); 319 r.events_end.reset();
324 r.intenset.write(|w| w.end().set()); 320 r.intenset.write(|w| w.end().set());
325 321
326 if sampler(&bufs[current_buffer][0..r.result.amount.read().bits() as usize]) 322 if sampler(&bufs[current_buffer][0..r.result.amount.read().bits() as usize / N])
327 == SamplerState::Sampled 323 == SamplerState::Sampled
328 { 324 {
329 let next_buffer = 1 - current_buffer; 325 let next_buffer = 1 - current_buffer;
@@ -358,7 +354,7 @@ impl<'d, const N: usize> Saadc<'d, N> {
358} 354}
359 355
360impl<'d> Saadc<'d, 1> { 356impl<'d> Saadc<'d, 1> {
361 /// Continuous sampling on a single channel with double buffers. The sample 357 /// Continuous sampling on a single channel with double buffers. The sample
362 /// buffers generally should be a multiple of the number of channels configured. 358 /// buffers generally should be a multiple of the number of channels configured.
363 /// 359 ///
364 /// The internal clock is to be used with a sample rate expressed as a divisor of 360 /// 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> {
371 /// should continue or stop. 367 /// should continue or stop.
372 pub async fn run_timer_sampler<S, const N0: usize>( 368 pub async fn run_timer_sampler<S, const N0: usize>(
373 &mut self, 369 &mut self,
374 bufs: &mut [[i16; N0]; 2], 370 bufs: &mut [[[i16; 1]; N0]; 2],
375 sample_rate: u16, 371 sample_rate: u16,
376 sampler: S, 372 sampler: S,
377 ) where 373 ) where
378 S: FnMut(&[i16]) -> SamplerState, 374 S: FnMut(&[[i16; 1]]) -> SamplerState,
379 { 375 {
380 self.run_sampler(bufs, Some(sample_rate), sampler).await; 376 self.run_sampler(bufs, Some(sample_rate), sampler).await;
381 } 377 }
diff --git a/examples/nrf/src/bin/saadc_continuous.rs b/examples/nrf/src/bin/saadc_continuous.rs
index d936a7d21..149b9c60c 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) {
39 39
40 timer.start(); 40 timer.start();
41 41
42 let mut bufs = [[0; 3 * 50]; 2]; // Each buffer of the double buffer has to be large enough for all channels. 42 let mut bufs = [[[0; 3]; 50]; 2];
43 43
44 let mut c = 0; 44 let mut c = 0;
45 let mut a: i32 = 0; 45 let mut a: i32 = 0;
46 46
47 saadc 47 saadc
48 .run_task_sampler(&mut bufs, move |buf| { 48 .run_task_sampler(&mut bufs, move |buf| {
49 for (i, b) in buf.iter().enumerate() { 49 for b in buf {
50 if i % 3 == 0 { 50 a += b[0] as i32;
51 a += *b as i32;
52 c += 1;
53 }
54 } 51 }
52 c += buf.len();
55 if c > 10000 { 53 if c > 10000 {
56 a = a / c as i32; 54 a = a / c as i32;
57 info!("channel 1: {=i32}", a); 55 info!("channel 1: {=i32}", a);