diff options
| author | Quentin Smith <[email protected]> | 2022-08-21 02:16:26 -0400 |
|---|---|---|
| committer | Quentin Smith <[email protected]> | 2022-08-21 02:16:26 -0400 |
| commit | ed97e61dbecc636c3cc9f67778d4b7eb48cff893 (patch) | |
| tree | 5e7a83a0167f0fd4eee31b4db2b1fb16a7277e5b /examples | |
| parent | 029713eca0e6c4a17283891de77c69f27bbf1a54 (diff) | |
PDM clock frequency control
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/nrf/src/bin/pdm_continuous.rs | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/examples/nrf/src/bin/pdm_continuous.rs b/examples/nrf/src/bin/pdm_continuous.rs index e7d1806bb..33ba1e274 100644 --- a/examples/nrf/src/bin/pdm_continuous.rs +++ b/examples/nrf/src/bin/pdm_continuous.rs | |||
| @@ -5,8 +5,7 @@ | |||
| 5 | use defmt::info; | 5 | use defmt::info; |
| 6 | use embassy_executor::Spawner; | 6 | use embassy_executor::Spawner; |
| 7 | use embassy_nrf::interrupt; | 7 | use embassy_nrf::interrupt; |
| 8 | use embassy_nrf::pdm::{Config, Channels, Pdm, SamplerState}; | 8 | use embassy_nrf::pdm::{Config, Channels, Pdm, SamplerState, Frequency, Ratio}; |
| 9 | use embassy_nrf::timer::Frequency; | ||
| 10 | use fixed::types::I7F1; | 9 | use fixed::types::I7F1; |
| 11 | use num_integer::Roots; | 10 | use num_integer::Roots; |
| 12 | use {defmt_rtt as _, panic_probe as _}; | 11 | use {defmt_rtt as _, panic_probe as _}; |
| @@ -18,11 +17,13 @@ async fn main(_p: Spawner) { | |||
| 18 | let mut p = embassy_nrf::init(Default::default()); | 17 | let mut p = embassy_nrf::init(Default::default()); |
| 19 | let mut config = Config::default(); | 18 | let mut config = Config::default(); |
| 20 | // Pins are correct for the onboard microphone on the Feather nRF52840 Sense. | 19 | // Pins are correct for the onboard microphone on the Feather nRF52840 Sense. |
| 20 | config.frequency = Frequency::_1280K; // 16 kHz sample rate | ||
| 21 | config.ratio = Ratio::RATIO80; | ||
| 21 | config.channels = Channels::Mono; | 22 | config.channels = Channels::Mono; |
| 22 | config.gain_left = I7F1::from_bits(5); // 2.5 dB | 23 | 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 | let mut pdm = Pdm::new(p.PDM, interrupt::take!(PDM), &mut p.P0_00, &mut p.P0_01, config); |
| 24 | 25 | ||
| 25 | let mut bufs = [[0; 500]; 2]; | 26 | let mut bufs = [[0; 1024]; 2]; |
| 26 | 27 | ||
| 27 | pdm | 28 | pdm |
| 28 | .run_task_sampler( | 29 | .run_task_sampler( |
| @@ -34,13 +35,15 @@ async fn main(_p: Spawner) { | |||
| 34 | // sample * 1500 = 18ms. You need to measure the time taken here | 35 | // sample * 1500 = 18ms. You need to measure the time taken here |
| 35 | // and set the sample buffer size accordingly. Exceeding this | 36 | // and set the sample buffer size accordingly. Exceeding this |
| 36 | // time can lead to the peripheral re-writing the other buffer. | 37 | // time can lead to the peripheral re-writing the other buffer. |
| 38 | let mean = (buf.iter().map(|v| i32::from(*v)).sum::<i32>() / buf.len() as i32) as i16; | ||
| 37 | info!( | 39 | info!( |
| 38 | "{} samples, min {=i16}, max {=i16}, RMS {=i16}", | 40 | "{} samples, min {=i16}, max {=i16}, mean {=i16}, AC RMS {=i16}", |
| 39 | buf.len(), | 41 | buf.len(), |
| 40 | buf.iter().min().unwrap(), | 42 | buf.iter().min().unwrap(), |
| 41 | buf.iter().max().unwrap(), | 43 | buf.iter().max().unwrap(), |
| 44 | mean, | ||
| 42 | ( | 45 | ( |
| 43 | buf.iter().map(|v| i32::from(*v).pow(2)).fold(0i32, |a,b| a.saturating_add(b)) | 46 | buf.iter().map(|v| i32::from(*v - mean).pow(2)).fold(0i32, |a,b| a.saturating_add(b)) |
| 44 | / buf.len() as i32).sqrt() as i16, | 47 | / buf.len() as i32).sqrt() as i16, |
| 45 | ); | 48 | ); |
| 46 | SamplerState::Sampled | 49 | SamplerState::Sampled |
