cargo fmt

This commit is contained in:
Quentin Smith 2023-07-18 17:17:04 -04:00
parent c333d855fc
commit 2c01f277c2
3 changed files with 56 additions and 43 deletions

View File

@ -8,17 +8,17 @@ use core::task::Poll;
use embassy_hal_common::drop::OnDrop; use embassy_hal_common::drop::OnDrop;
use embassy_hal_common::{into_ref, PeripheralRef}; use embassy_hal_common::{into_ref, PeripheralRef};
use futures::future::poll_fn;
use fixed::types::I7F1; use fixed::types::I7F1;
use futures::future::poll_fn;
use crate::chip::EASY_DMA_SIZE; use crate::chip::EASY_DMA_SIZE;
use crate::gpio::sealed::Pin; use crate::gpio::sealed::Pin;
use crate::gpio::{AnyPin, Pin as GpioPin}; use crate::gpio::{AnyPin, Pin as GpioPin};
use crate::interrupt::typelevel::Interrupt; use crate::interrupt::typelevel::Interrupt;
use crate::{interrupt, Peripheral};
use crate::pac::pdm::mode::{EDGE_A, OPERATION_A}; use crate::pac::pdm::mode::{EDGE_A, OPERATION_A};
pub use crate::pac::pdm::pdmclkctrl::FREQ_A as Frequency; pub use crate::pac::pdm::pdmclkctrl::FREQ_A as Frequency;
pub use crate::pac::pdm::ratio::RATIO_A as Ratio; pub use crate::pac::pdm::ratio::RATIO_A as Ratio;
use crate::{interrupt, Peripheral};
/// Interrupt handler. /// Interrupt handler.
pub struct InterruptHandler<T: Instance> { pub struct InterruptHandler<T: Instance> {
@ -133,8 +133,14 @@ impl<'d, T: Instance> Pdm<'d, T> {
} }
fn _set_gain(r: &crate::pac::pdm::RegisterBlock, gain_left: I7F1, gain_right: I7F1) { fn _set_gain(r: &crate::pac::pdm::RegisterBlock, gain_left: I7F1, gain_right: I7F1) {
let gain_left = gain_left.saturating_add(I7F1::from_bits(40)).saturating_to_num::<u8>().clamp(0, 0x50); let gain_left = gain_left
let gain_right = gain_right.saturating_add(I7F1::from_bits(40)).saturating_to_num::<u8>().clamp(0, 0x50); .saturating_add(I7F1::from_bits(40))
.saturating_to_num::<u8>()
.clamp(0, 0x50);
let gain_right = gain_right
.saturating_add(I7F1::from_bits(40))
.saturating_to_num::<u8>()
.clamp(0, 0x50);
r.gainl.write(|w| unsafe { w.gainl().bits(gain_left) }); r.gainl.write(|w| unsafe { w.gainl().bits(gain_left) });
r.gainr.write(|w| unsafe { w.gainr().bits(gain_right) }); r.gainr.write(|w| unsafe { w.gainr().bits(gain_right) });
@ -258,7 +264,8 @@ impl<'d, T: Instance> Pdm<'d, T> {
&mut self, &mut self,
bufs: &mut [[i16; N]; 2], bufs: &mut [[i16; N]; 2],
mut sampler: S, mut sampler: S,
) -> Result<(), Error> where ) -> Result<(), Error>
where
S: FnMut(&[i16; N]) -> SamplerState, S: FnMut(&[i16; N]) -> SamplerState,
{ {
let r = T::regs(); let r = T::regs();
@ -267,7 +274,9 @@ impl<'d, T: Instance> Pdm<'d, T> {
return Err(Error::AlreadyRunning); return Err(Error::AlreadyRunning);
} }
r.sample.ptr.write(|w| unsafe { w.sampleptr().bits(bufs[0].as_mut_ptr() as u32) }); r.sample
.ptr
.write(|w| unsafe { w.sampleptr().bits(bufs[0].as_mut_ptr() as u32) });
r.sample.maxcnt.write(|w| unsafe { w.buffsize().bits(N as _) }); r.sample.maxcnt.write(|w| unsafe { w.buffsize().bits(N as _) });
// Reset and enable the events // Reset and enable the events
@ -285,7 +294,7 @@ impl<'d, T: Instance> Pdm<'d, T> {
// wouldn't happen anyway. // wouldn't happen anyway.
compiler_fence(Ordering::SeqCst); compiler_fence(Ordering::SeqCst);
r.tasks_start.write(|w| { w.tasks_start().set_bit() }); r.tasks_start.write(|w| w.tasks_start().set_bit());
let mut current_buffer = 0; let mut current_buffer = 0;
@ -309,7 +318,8 @@ impl<'d, T: Instance> Pdm<'d, T> {
r.events_end.reset(); r.events_end.reset();
r.intenset.write(|w| w.end().set()); r.intenset.write(|w| w.end().set());
if !done { // Discard the last buffer after the user requested a stop. if !done {
// Discard the last buffer after the user requested a stop.
if sampler(&bufs[current_buffer]) == SamplerState::Sampled { if sampler(&bufs[current_buffer]) == SamplerState::Sampled {
let next_buffer = 1 - current_buffer; let next_buffer = 1 - current_buffer;
current_buffer = next_buffer; current_buffer = next_buffer;

View File

@ -41,9 +41,11 @@ async fn main(_p: Spawner) {
buf.iter().min().unwrap(), buf.iter().min().unwrap(),
buf.iter().max().unwrap(), buf.iter().max().unwrap(),
mean, mean,
( (buf.iter()
buf.iter().map(|v| i32::from(*v - mean).pow(2)).fold(0i32, |a,b| a.saturating_add(b)) .map(|v| i32::from(*v - mean).pow(2))
/ buf.len() as i32).sqrt() as i16, .fold(0i32, |a, b| a.saturating_add(b))
/ buf.len() as i32)
.sqrt() as i16,
); );
info!("samples: {:?}", &buf); info!("samples: {:?}", &buf);

View File

@ -2,14 +2,15 @@
#![no_main] #![no_main]
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]
use defmt::info;
use core::cmp::Ordering; use core::cmp::Ordering;
use defmt::info;
use embassy_executor::Spawner; use embassy_executor::Spawner;
use embassy_nrf::pdm::{self, Config, Frequency, OperationMode, Pdm, Ratio, SamplerState};
use embassy_nrf::{bind_interrupts, peripherals}; use embassy_nrf::{bind_interrupts, peripherals};
use embassy_nrf::pdm::{self, Config, OperationMode, Pdm, SamplerState, Frequency, Ratio};
use fixed::types::I7F1; use fixed::types::I7F1;
use num_integer::Roots;
use microfft::real::rfft_1024; use microfft::real::rfft_1024;
use num_integer::Roots;
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
// Demonstrates both continuous sampling and scanning multiple channels driven by a PPI linked timer // Demonstrates both continuous sampling and scanning multiple channels driven by a PPI linked timer
@ -31,10 +32,7 @@ async fn main(_p: Spawner) {
let mut bufs = [[0; 1024]; 2]; let mut bufs = [[0; 1024]; 2];
pdm pdm.run_task_sampler(&mut bufs, move |buf| {
.run_task_sampler(
&mut bufs,
move |buf| {
// NOTE: It is important that the time spent within this callback // NOTE: It is important that the time spent within this callback
// does not exceed the time taken to acquire the 1500 samples we // does not exceed the time taken to acquire the 1500 samples we
// have in this example, which would be 10us + 2us per // have in this example, which would be 10us + 2us per
@ -50,15 +48,18 @@ async fn main(_p: Spawner) {
buf.iter().min().unwrap(), buf.iter().min().unwrap(),
buf.iter().max().unwrap(), buf.iter().max().unwrap(),
mean, mean,
( (buf.iter()
buf.iter().map(|v| i32::from(*v - mean).pow(2)).fold(0i32, |a,b| a.saturating_add(b)) .map(|v| i32::from(*v - mean).pow(2))
/ buf.len() as i32).sqrt() as i16, .fold(0i32, |a, b| a.saturating_add(b))
peak_mag, peak_freq, / buf.len() as i32)
.sqrt() as i16,
peak_mag,
peak_freq,
); );
SamplerState::Sampled SamplerState::Sampled
}, })
) .await
.await.unwrap(); .unwrap();
} }
fn fft_peak_freq(input: &[i16; 1024]) -> (usize, u32) { fn fft_peak_freq(input: &[i16; 1024]) -> (usize, u32) {