diff options
| author | Quentin Smith <[email protected]> | 2022-08-20 17:58:54 -0400 |
|---|---|---|
| committer | Quentin Smith <[email protected]> | 2022-08-20 17:58:54 -0400 |
| commit | 0963b5f92c9588ab00f556a6c521fad059eac72e (patch) | |
| tree | 1cda1bcd1e345728af9746409da82c14e91602d6 /examples/nrf/src | |
| parent | 530f192acceb5a10c416e1823dc27a749e68b7dc (diff) | |
Add continuous PDM sampling with example
Diffstat (limited to 'examples/nrf/src')
| -rw-r--r-- | examples/nrf/src/bin/pdm.rs | 3 | ||||
| -rw-r--r-- | examples/nrf/src/bin/pdm_continuous.rs | 50 |
2 files changed, 52 insertions, 1 deletions
diff --git a/examples/nrf/src/bin/pdm.rs b/examples/nrf/src/bin/pdm.rs index a73d01fb9..85a59a529 100644 --- a/examples/nrf/src/bin/pdm.rs +++ b/examples/nrf/src/bin/pdm.rs | |||
| @@ -25,7 +25,7 @@ async fn main(_p: Spawner) { | |||
| 25 | pdm.set_gain(gain, gain); | 25 | pdm.set_gain(gain, gain); |
| 26 | info!("Gain = {} dB", defmt::Debug2Format(&gain)); | 26 | info!("Gain = {} dB", defmt::Debug2Format(&gain)); |
| 27 | for _ in 0..10 { | 27 | for _ in 0..10 { |
| 28 | let mut buf = [0; 128]; | 28 | let mut buf = [0; 1500]; |
| 29 | pdm.sample(&mut buf).await; | 29 | pdm.sample(&mut buf).await; |
| 30 | info!( | 30 | info!( |
| 31 | "{} samples, min {=i16}, max {=i16}, RMS {=i16}", | 31 | "{} samples, min {=i16}, max {=i16}, RMS {=i16}", |
| @@ -36,6 +36,7 @@ async fn main(_p: Spawner) { | |||
| 36 | buf.iter().map(|v| i32::from(*v).pow(2)).fold(0i32, |a,b| a.saturating_add(b)) | 36 | buf.iter().map(|v| i32::from(*v).pow(2)).fold(0i32, |a,b| a.saturating_add(b)) |
| 37 | / buf.len() as i32).sqrt() as i16, | 37 | / buf.len() as i32).sqrt() as i16, |
| 38 | ); | 38 | ); |
| 39 | info!("samples = {}", &buf); | ||
| 39 | Timer::after(Duration::from_millis(100)).await; | 40 | Timer::after(Duration::from_millis(100)).await; |
| 40 | } | 41 | } |
| 41 | } | 42 | } |
diff --git a/examples/nrf/src/bin/pdm_continuous.rs b/examples/nrf/src/bin/pdm_continuous.rs new file mode 100644 index 000000000..e7d1806bb --- /dev/null +++ b/examples/nrf/src/bin/pdm_continuous.rs | |||
| @@ -0,0 +1,50 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::info; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_nrf::interrupt; | ||
| 8 | use embassy_nrf::pdm::{Config, Channels, Pdm, SamplerState}; | ||
| 9 | use embassy_nrf::timer::Frequency; | ||
| 10 | use fixed::types::I7F1; | ||
| 11 | use num_integer::Roots; | ||
| 12 | use {defmt_rtt as _, panic_probe as _}; | ||
| 13 | |||
| 14 | // Demonstrates both continuous sampling and scanning multiple channels driven by a PPI linked timer | ||
| 15 | |||
| 16 | #[embassy_executor::main] | ||
| 17 | async fn main(_p: Spawner) { | ||
| 18 | let mut p = embassy_nrf::init(Default::default()); | ||
| 19 | let mut config = Config::default(); | ||
| 20 | // Pins are correct for the onboard microphone on the Feather nRF52840 Sense. | ||
| 21 | config.channels = Channels::Mono; | ||
| 22 | config.gain_left = I7F1::from_bits(5); // 2.5 dB | ||
| 23 | let mut pdm = Pdm::new(p.PDM, interrupt::take!(PDM), &mut p.P0_00, &mut p.P0_01, config); | ||
| 24 | |||
| 25 | let mut bufs = [[0; 500]; 2]; | ||
| 26 | |||
| 27 | pdm | ||
| 28 | .run_task_sampler( | ||
| 29 | &mut bufs, | ||
| 30 | move |buf| { | ||
| 31 | // NOTE: It is important that the time spent within this callback | ||
| 32 | // does not exceed the time taken to acquire the 1500 samples we | ||
| 33 | // have in this example, which would be 10us + 2us per | ||
| 34 | // sample * 1500 = 18ms. You need to measure the time taken here | ||
| 35 | // and set the sample buffer size accordingly. Exceeding this | ||
| 36 | // time can lead to the peripheral re-writing the other buffer. | ||
| 37 | info!( | ||
| 38 | "{} samples, min {=i16}, max {=i16}, RMS {=i16}", | ||
| 39 | buf.len(), | ||
| 40 | buf.iter().min().unwrap(), | ||
| 41 | buf.iter().max().unwrap(), | ||
| 42 | ( | ||
| 43 | buf.iter().map(|v| i32::from(*v).pow(2)).fold(0i32, |a,b| a.saturating_add(b)) | ||
| 44 | / buf.len() as i32).sqrt() as i16, | ||
| 45 | ); | ||
| 46 | SamplerState::Sampled | ||
| 47 | }, | ||
| 48 | ) | ||
| 49 | .await; | ||
| 50 | } | ||
