PDM clock frequency control
This commit is contained in:
parent
029713eca0
commit
ed97e61dbe
@ -9,6 +9,8 @@ use embassy_util::waitqueue::AtomicWaker;
|
|||||||
use futures::future::poll_fn;
|
use futures::future::poll_fn;
|
||||||
use pac::{pdm, PDM};
|
use pac::{pdm, PDM};
|
||||||
use pdm::mode::{EDGE_A, OPERATION_A};
|
use pdm::mode::{EDGE_A, OPERATION_A};
|
||||||
|
pub use pdm::pdmclkctrl::FREQ_A as Frequency;
|
||||||
|
pub use pdm::ratio::RATIO_A as Ratio;
|
||||||
use fixed::types::I7F1;
|
use fixed::types::I7F1;
|
||||||
|
|
||||||
use crate::interrupt::InterruptExt;
|
use crate::interrupt::InterruptExt;
|
||||||
@ -32,8 +34,10 @@ static WAKER: AtomicWaker = AtomicWaker::new();
|
|||||||
/// See the `Default` impl for suitable default values.
|
/// See the `Default` impl for suitable default values.
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
/// Clock
|
/// Clock frequency
|
||||||
|
pub frequency: Frequency,
|
||||||
/// Clock ratio
|
/// Clock ratio
|
||||||
|
pub ratio: Ratio,
|
||||||
/// Channels
|
/// Channels
|
||||||
pub channels: Channels,
|
pub channels: Channels,
|
||||||
/// Edge to sample on
|
/// Edge to sample on
|
||||||
@ -48,6 +52,8 @@ impl Default for Config {
|
|||||||
/// Default configuration for single channel sampling.
|
/// Default configuration for single channel sampling.
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
frequency: Frequency::DEFAULT,
|
||||||
|
ratio: Ratio::RATIO80,
|
||||||
channels: Channels::Stereo,
|
channels: Channels::Stereo,
|
||||||
left_edge: Edge::FallingEdge,
|
left_edge: Edge::FallingEdge,
|
||||||
gain_left: I7F1::ZERO,
|
gain_left: I7F1::ZERO,
|
||||||
@ -78,11 +84,12 @@ impl<'d> Pdm<'d> {
|
|||||||
|
|
||||||
let r = unsafe { &*PDM::ptr() };
|
let r = unsafe { &*PDM::ptr() };
|
||||||
|
|
||||||
let Config { channels, left_edge, gain_left, gain_right } = config;
|
let Config { frequency, ratio, channels, left_edge, gain_left, gain_right } = config;
|
||||||
|
|
||||||
// Configure channels
|
// Configure channels
|
||||||
r.enable.write(|w| w.enable().enabled());
|
r.enable.write(|w| w.enable().enabled());
|
||||||
// TODO: Clock control
|
r.pdmclkctrl.write(|w| w.freq().variant(frequency));
|
||||||
|
r.ratio.write(|w| w.ratio().variant(ratio));
|
||||||
r.mode.write(|w| {
|
r.mode.write(|w| {
|
||||||
w.operation().variant(channels.into());
|
w.operation().variant(channels.into());
|
||||||
w.edge().variant(left_edge.into());
|
w.edge().variant(left_edge.into());
|
||||||
|
@ -5,8 +5,7 @@
|
|||||||
use defmt::info;
|
use defmt::info;
|
||||||
use embassy_executor::Spawner;
|
use embassy_executor::Spawner;
|
||||||
use embassy_nrf::interrupt;
|
use embassy_nrf::interrupt;
|
||||||
use embassy_nrf::pdm::{Config, Channels, Pdm, SamplerState};
|
use embassy_nrf::pdm::{Config, Channels, Pdm, SamplerState, Frequency, Ratio};
|
||||||
use embassy_nrf::timer::Frequency;
|
|
||||||
use fixed::types::I7F1;
|
use fixed::types::I7F1;
|
||||||
use num_integer::Roots;
|
use num_integer::Roots;
|
||||||
use {defmt_rtt as _, panic_probe as _};
|
use {defmt_rtt as _, panic_probe as _};
|
||||||
@ -18,11 +17,13 @@ async fn main(_p: Spawner) {
|
|||||||
let mut p = embassy_nrf::init(Default::default());
|
let mut p = embassy_nrf::init(Default::default());
|
||||||
let mut config = Config::default();
|
let mut config = Config::default();
|
||||||
// Pins are correct for the onboard microphone on the Feather nRF52840 Sense.
|
// Pins are correct for the onboard microphone on the Feather nRF52840 Sense.
|
||||||
|
config.frequency = Frequency::_1280K; // 16 kHz sample rate
|
||||||
|
config.ratio = Ratio::RATIO80;
|
||||||
config.channels = Channels::Mono;
|
config.channels = Channels::Mono;
|
||||||
config.gain_left = I7F1::from_bits(5); // 2.5 dB
|
config.gain_left = I7F1::from_bits(5); // 2.5 dB
|
||||||
let mut pdm = Pdm::new(p.PDM, interrupt::take!(PDM), &mut p.P0_00, &mut p.P0_01, config);
|
let mut pdm = Pdm::new(p.PDM, interrupt::take!(PDM), &mut p.P0_00, &mut p.P0_01, config);
|
||||||
|
|
||||||
let mut bufs = [[0; 500]; 2];
|
let mut bufs = [[0; 1024]; 2];
|
||||||
|
|
||||||
pdm
|
pdm
|
||||||
.run_task_sampler(
|
.run_task_sampler(
|
||||||
@ -34,13 +35,15 @@ async fn main(_p: Spawner) {
|
|||||||
// sample * 1500 = 18ms. You need to measure the time taken here
|
// sample * 1500 = 18ms. You need to measure the time taken here
|
||||||
// and set the sample buffer size accordingly. Exceeding this
|
// and set the sample buffer size accordingly. Exceeding this
|
||||||
// time can lead to the peripheral re-writing the other buffer.
|
// time can lead to the peripheral re-writing the other buffer.
|
||||||
|
let mean = (buf.iter().map(|v| i32::from(*v)).sum::<i32>() / buf.len() as i32) as i16;
|
||||||
info!(
|
info!(
|
||||||
"{} samples, min {=i16}, max {=i16}, RMS {=i16}",
|
"{} samples, min {=i16}, max {=i16}, mean {=i16}, AC RMS {=i16}",
|
||||||
buf.len(),
|
buf.len(),
|
||||||
buf.iter().min().unwrap(),
|
buf.iter().min().unwrap(),
|
||||||
buf.iter().max().unwrap(),
|
buf.iter().max().unwrap(),
|
||||||
|
mean,
|
||||||
(
|
(
|
||||||
buf.iter().map(|v| i32::from(*v).pow(2)).fold(0i32, |a,b| a.saturating_add(b))
|
buf.iter().map(|v| i32::from(*v - mean).pow(2)).fold(0i32, |a,b| a.saturating_add(b))
|
||||||
/ buf.len() as i32).sqrt() as i16,
|
/ buf.len() as i32).sqrt() as i16,
|
||||||
);
|
);
|
||||||
SamplerState::Sampled
|
SamplerState::Sampled
|
||||||
|
Loading…
Reference in New Issue
Block a user