2023-07-18 03:31:43 +02:00
|
|
|
//! Pulse Density Modulation (PDM) mirophone driver.
|
|
|
|
|
2022-08-20 22:37:51 +02:00
|
|
|
#![macro_use]
|
|
|
|
|
2023-10-31 10:13:27 +01:00
|
|
|
use core::future::poll_fn;
|
2023-07-18 03:31:43 +02:00
|
|
|
use core::marker::PhantomData;
|
2022-08-20 22:37:51 +02:00
|
|
|
use core::sync::atomic::{compiler_fence, Ordering};
|
|
|
|
use core::task::Poll;
|
|
|
|
|
2023-07-28 13:23:22 +02:00
|
|
|
use embassy_hal_internal::drop::OnDrop;
|
|
|
|
use embassy_hal_internal::{into_ref, PeripheralRef};
|
2022-08-20 22:37:51 +02:00
|
|
|
use fixed::types::I7F1;
|
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
use crate::chip::EASY_DMA_SIZE;
|
|
|
|
use crate::gpio::sealed::Pin;
|
|
|
|
use crate::gpio::{AnyPin, Pin as GpioPin};
|
|
|
|
use crate::interrupt::typelevel::Interrupt;
|
|
|
|
use crate::pac::pdm::mode::{EDGE_A, OPERATION_A};
|
|
|
|
pub use crate::pac::pdm::pdmclkctrl::FREQ_A as Frequency;
|
2023-07-19 00:31:32 +02:00
|
|
|
#[cfg(any(
|
|
|
|
feature = "nrf52840",
|
|
|
|
feature = "nrf52833",
|
|
|
|
feature = "_nrf5340-app",
|
|
|
|
feature = "_nrf9160",
|
|
|
|
))]
|
|
|
|
pub use crate::pac::pdm::ratio::RATIO_A as Ratio;
|
2023-07-19 00:32:19 +02:00
|
|
|
use crate::{interrupt, Peripheral};
|
2023-07-19 00:31:32 +02:00
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
/// Interrupt handler.
|
|
|
|
pub struct InterruptHandler<T: Instance> {
|
|
|
|
_phantom: PhantomData<T>,
|
|
|
|
}
|
2022-08-20 22:37:51 +02:00
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
impl<T: Instance> interrupt::typelevel::Handler<T::Interrupt> for InterruptHandler<T> {
|
|
|
|
unsafe fn on_interrupt() {
|
|
|
|
let r = T::regs();
|
|
|
|
|
|
|
|
if r.events_end.read().bits() != 0 {
|
|
|
|
r.intenclr.write(|w| w.end().clear());
|
|
|
|
}
|
2022-08-20 22:37:51 +02:00
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
if r.events_started.read().bits() != 0 {
|
|
|
|
r.intenclr.write(|w| w.started().clear());
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.events_stopped.read().bits() != 0 {
|
|
|
|
r.intenclr.write(|w| w.stopped().clear());
|
|
|
|
}
|
|
|
|
|
|
|
|
T::state().waker.wake();
|
|
|
|
}
|
2022-08-20 22:37:51 +02:00
|
|
|
}
|
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
/// PDM microphone interface
|
|
|
|
pub struct Pdm<'d, T: Instance> {
|
|
|
|
_peri: PeripheralRef<'d, T>,
|
|
|
|
}
|
2022-08-20 22:37:51 +02:00
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
/// PDM error.
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
2022-08-20 22:37:51 +02:00
|
|
|
#[non_exhaustive]
|
2023-07-18 03:31:43 +02:00
|
|
|
pub enum Error {
|
|
|
|
/// Buffer is too long.
|
|
|
|
BufferTooLong,
|
|
|
|
/// Buffer is empty
|
|
|
|
BufferZeroLength,
|
|
|
|
/// PDM is not running
|
|
|
|
NotRunning,
|
|
|
|
/// PDM is already running
|
|
|
|
AlreadyRunning,
|
2022-08-20 22:37:51 +02:00
|
|
|
}
|
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
static DUMMY_BUFFER: [i16; 1] = [0; 1];
|
2022-08-20 22:37:51 +02:00
|
|
|
|
|
|
|
/// The state of a continuously running sampler. While it reflects
|
|
|
|
/// the progress of a sampler, it also signals what should be done
|
|
|
|
/// next. For example, if the sampler has stopped then the Pdm implementation
|
|
|
|
/// can then tear down its infrastructure.
|
|
|
|
#[derive(PartialEq)]
|
|
|
|
pub enum SamplerState {
|
2023-07-18 03:31:43 +02:00
|
|
|
/// The sampler processed the samples and is ready for more.
|
2022-08-20 22:37:51 +02:00
|
|
|
Sampled,
|
2023-07-18 03:31:43 +02:00
|
|
|
/// The sampler is done processing samples.
|
2022-08-20 22:37:51 +02:00
|
|
|
Stopped,
|
|
|
|
}
|
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
impl<'d, T: Instance> Pdm<'d, T> {
|
|
|
|
/// Create PDM driver
|
2022-08-20 22:37:51 +02:00
|
|
|
pub fn new(
|
2023-07-18 03:31:43 +02:00
|
|
|
pdm: impl Peripheral<P = T> + 'd,
|
|
|
|
_irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd,
|
|
|
|
clk: impl Peripheral<P = impl GpioPin> + 'd,
|
|
|
|
din: impl Peripheral<P = impl GpioPin> + 'd,
|
|
|
|
config: Config,
|
|
|
|
) -> Self {
|
|
|
|
into_ref!(pdm, clk, din);
|
|
|
|
Self::new_inner(pdm, clk.map_into(), din.map_into(), config)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn new_inner(
|
|
|
|
pdm: PeripheralRef<'d, T>,
|
|
|
|
clk: PeripheralRef<'d, AnyPin>,
|
|
|
|
din: PeripheralRef<'d, AnyPin>,
|
2022-08-20 22:37:51 +02:00
|
|
|
config: Config,
|
|
|
|
) -> Self {
|
2023-07-18 03:31:43 +02:00
|
|
|
into_ref!(pdm);
|
2022-08-20 22:37:51 +02:00
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
let r = T::regs();
|
2022-08-20 22:37:51 +02:00
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
// setup gpio pins
|
|
|
|
din.conf().write(|w| w.input().set_bit());
|
|
|
|
r.psel.din.write(|w| unsafe { w.bits(din.psel_bits()) });
|
|
|
|
clk.set_low();
|
|
|
|
clk.conf().write(|w| w.dir().output());
|
|
|
|
r.psel.clk.write(|w| unsafe { w.bits(clk.psel_bits()) });
|
2022-08-20 22:37:51 +02:00
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
// configure
|
|
|
|
r.pdmclkctrl.write(|w| w.freq().variant(config.frequency));
|
2023-07-19 00:31:32 +02:00
|
|
|
#[cfg(any(
|
|
|
|
feature = "nrf52840",
|
|
|
|
feature = "nrf52833",
|
|
|
|
feature = "_nrf5340-app",
|
|
|
|
feature = "_nrf9160",
|
|
|
|
))]
|
2023-07-18 03:31:43 +02:00
|
|
|
r.ratio.write(|w| w.ratio().variant(config.ratio));
|
2022-08-20 22:37:51 +02:00
|
|
|
r.mode.write(|w| {
|
2023-07-18 03:31:43 +02:00
|
|
|
w.operation().variant(config.operation_mode.into());
|
|
|
|
w.edge().variant(config.edge.into());
|
2022-08-20 22:37:51 +02:00
|
|
|
w
|
|
|
|
});
|
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
Self::_set_gain(r, config.gain_left, config.gain_right);
|
2022-08-20 22:37:51 +02:00
|
|
|
|
|
|
|
// Disable all events interrupts
|
|
|
|
r.intenclr.write(|w| unsafe { w.bits(0x003F_FFFF) });
|
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
// IRQ
|
|
|
|
T::Interrupt::unpend();
|
|
|
|
unsafe { T::Interrupt::enable() };
|
2022-08-20 22:37:51 +02:00
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
r.enable.write(|w| w.enable().set_bit());
|
2022-08-20 23:58:54 +02:00
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
Self { _peri: pdm }
|
2022-08-20 22:37:51 +02:00
|
|
|
}
|
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
fn _set_gain(r: &crate::pac::pdm::RegisterBlock, gain_left: I7F1, gain_right: I7F1) {
|
2023-07-18 23:17:04 +02:00
|
|
|
let gain_left = gain_left
|
|
|
|
.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);
|
2022-08-20 23:08:29 +02:00
|
|
|
|
|
|
|
r.gainl.write(|w| unsafe { w.gainl().bits(gain_left) });
|
|
|
|
r.gainr.write(|w| unsafe { w.gainr().bits(gain_right) });
|
|
|
|
}
|
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
/// Adjust the gain of the PDM microphone on the fly
|
2022-08-20 23:08:29 +02:00
|
|
|
pub fn set_gain(&mut self, gain_left: I7F1, gain_right: I7F1) {
|
2023-07-18 03:31:43 +02:00
|
|
|
Self::_set_gain(T::regs(), gain_left, gain_right)
|
2022-08-20 23:08:29 +02:00
|
|
|
}
|
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
/// Start sampling microphon data into a dummy buffer
|
|
|
|
/// Usefull to start the microphon and keep it active between recording samples
|
|
|
|
pub async fn start(&mut self) {
|
|
|
|
let r = T::regs();
|
|
|
|
|
|
|
|
// start dummy sampling because microphon needs some setup time
|
|
|
|
r.sample
|
|
|
|
.ptr
|
|
|
|
.write(|w| unsafe { w.sampleptr().bits(DUMMY_BUFFER.as_ptr() as u32) });
|
|
|
|
r.sample
|
|
|
|
.maxcnt
|
|
|
|
.write(|w| unsafe { w.buffsize().bits(DUMMY_BUFFER.len() as _) });
|
|
|
|
|
|
|
|
r.tasks_start.write(|w| unsafe { w.bits(1) });
|
2022-08-20 22:37:51 +02:00
|
|
|
}
|
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
/// Stop sampling microphon data inta a dummy buffer
|
|
|
|
pub async fn stop(&mut self) {
|
|
|
|
let r = T::regs();
|
|
|
|
r.tasks_stop.write(|w| unsafe { w.bits(1) });
|
|
|
|
r.events_started.reset();
|
|
|
|
}
|
2022-08-20 22:37:51 +02:00
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
/// Sample data into the given buffer.
|
|
|
|
pub async fn sample(&mut self, buffer: &mut [i16]) -> Result<(), Error> {
|
|
|
|
if buffer.len() == 0 {
|
|
|
|
return Err(Error::BufferZeroLength);
|
|
|
|
}
|
|
|
|
if buffer.len() > EASY_DMA_SIZE {
|
|
|
|
return Err(Error::BufferTooLong);
|
|
|
|
}
|
2022-08-20 22:37:51 +02:00
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
let r = T::regs();
|
2022-08-20 22:37:51 +02:00
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
if r.events_started.read().bits() == 0 {
|
|
|
|
return Err(Error::NotRunning);
|
|
|
|
}
|
2022-08-20 22:37:51 +02:00
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
let drop = OnDrop::new(move || {
|
|
|
|
r.intenclr.write(|w| w.end().clear());
|
|
|
|
r.events_stopped.reset();
|
2022-08-20 22:37:51 +02:00
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
// reset to dummy buffer
|
|
|
|
r.sample
|
|
|
|
.ptr
|
|
|
|
.write(|w| unsafe { w.sampleptr().bits(DUMMY_BUFFER.as_ptr() as u32) });
|
|
|
|
r.sample
|
|
|
|
.maxcnt
|
|
|
|
.write(|w| unsafe { w.buffsize().bits(DUMMY_BUFFER.len() as _) });
|
|
|
|
|
|
|
|
while r.events_stopped.read().bits() == 0 {}
|
2022-08-21 08:04:11 +02:00
|
|
|
});
|
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
// setup user buffer
|
|
|
|
let ptr = buffer.as_ptr();
|
|
|
|
let len = buffer.len();
|
|
|
|
r.sample.ptr.write(|w| unsafe { w.sampleptr().bits(ptr as u32) });
|
|
|
|
r.sample.maxcnt.write(|w| unsafe { w.buffsize().bits(len as _) });
|
2022-08-20 22:37:51 +02:00
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
// wait till the current sample is finished and the user buffer sample is started
|
|
|
|
Self::wait_for_sample().await;
|
2022-08-20 22:37:51 +02:00
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
// reset the buffer back to the dummy buffer
|
|
|
|
r.sample
|
|
|
|
.ptr
|
|
|
|
.write(|w| unsafe { w.sampleptr().bits(DUMMY_BUFFER.as_ptr() as u32) });
|
|
|
|
r.sample
|
|
|
|
.maxcnt
|
|
|
|
.write(|w| unsafe { w.buffsize().bits(DUMMY_BUFFER.len() as _) });
|
2022-08-21 07:44:04 +02:00
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
// wait till the user buffer is sampled
|
|
|
|
Self::wait_for_sample().await;
|
|
|
|
|
|
|
|
drop.defuse();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn wait_for_sample() {
|
|
|
|
let r = T::regs();
|
|
|
|
|
|
|
|
r.events_end.reset();
|
|
|
|
r.intenset.write(|w| w.end().set());
|
|
|
|
|
|
|
|
compiler_fence(Ordering::SeqCst);
|
|
|
|
|
|
|
|
poll_fn(|cx| {
|
|
|
|
T::state().waker.register(cx.waker());
|
|
|
|
if r.events_end.read().bits() != 0 {
|
2022-08-20 23:58:54 +02:00
|
|
|
return Poll::Ready(());
|
|
|
|
}
|
|
|
|
Poll::Pending
|
|
|
|
})
|
|
|
|
.await;
|
2023-07-18 03:31:43 +02:00
|
|
|
|
|
|
|
compiler_fence(Ordering::SeqCst);
|
2022-08-20 23:58:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Continuous sampling with double buffers.
|
|
|
|
///
|
|
|
|
/// A sampler closure is provided that receives the buffer of samples, noting
|
|
|
|
/// that the size of this buffer can be less than the original buffer's size.
|
|
|
|
/// A command is return from the closure that indicates whether the sampling
|
|
|
|
/// should continue or stop.
|
|
|
|
///
|
|
|
|
/// NOTE: The time spent within the callback supplied should not exceed the time
|
|
|
|
/// taken to acquire the samples into a single buffer. You should measure the
|
|
|
|
/// time taken by the callback and set the sample buffer size accordingly.
|
|
|
|
/// Exceeding this time can lead to samples becoming dropped.
|
|
|
|
pub async fn run_task_sampler<S, const N: usize>(
|
|
|
|
&mut self,
|
|
|
|
bufs: &mut [[i16; N]; 2],
|
|
|
|
mut sampler: S,
|
2023-07-18 23:17:04 +02:00
|
|
|
) -> Result<(), Error>
|
|
|
|
where
|
2022-08-20 23:58:54 +02:00
|
|
|
S: FnMut(&[i16; N]) -> SamplerState,
|
|
|
|
{
|
2023-07-18 03:31:43 +02:00
|
|
|
let r = T::regs();
|
|
|
|
|
|
|
|
if r.events_started.read().bits() != 0 {
|
|
|
|
return Err(Error::AlreadyRunning);
|
|
|
|
}
|
2022-08-20 23:58:54 +02:00
|
|
|
|
2023-07-18 23:17:04 +02:00
|
|
|
r.sample
|
|
|
|
.ptr
|
|
|
|
.write(|w| unsafe { w.sampleptr().bits(bufs[0].as_mut_ptr() as u32) });
|
2022-08-20 23:58:54 +02:00
|
|
|
r.sample.maxcnt.write(|w| unsafe { w.buffsize().bits(N as _) });
|
|
|
|
|
|
|
|
// Reset and enable the events
|
|
|
|
r.events_end.reset();
|
|
|
|
r.events_started.reset();
|
|
|
|
r.events_stopped.reset();
|
|
|
|
r.intenset.write(|w| {
|
|
|
|
w.end().set();
|
|
|
|
w.started().set();
|
|
|
|
w.stopped().set();
|
|
|
|
w
|
|
|
|
});
|
|
|
|
|
|
|
|
// Don't reorder the start event before the previous writes. Hopefully self
|
|
|
|
// wouldn't happen anyway.
|
|
|
|
compiler_fence(Ordering::SeqCst);
|
|
|
|
|
2023-07-19 00:31:32 +02:00
|
|
|
r.tasks_start.write(|w| unsafe { w.bits(1) });
|
2022-08-20 23:58:54 +02:00
|
|
|
|
|
|
|
let mut current_buffer = 0;
|
|
|
|
|
|
|
|
let mut done = false;
|
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
let drop = OnDrop::new(|| {
|
2023-07-19 00:31:32 +02:00
|
|
|
r.tasks_stop.write(|w| unsafe { w.bits(1) });
|
2022-08-21 08:04:11 +02:00
|
|
|
// N.B. It would be better if this were async, but Drop only support sync code.
|
|
|
|
while r.events_stopped.read().bits() != 0 {}
|
|
|
|
});
|
|
|
|
|
2022-08-20 23:58:54 +02:00
|
|
|
// Wait for events and complete when the sampler indicates it has had enough.
|
|
|
|
poll_fn(|cx| {
|
2023-07-18 03:31:43 +02:00
|
|
|
let r = T::regs();
|
2022-08-20 23:58:54 +02:00
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
T::state().waker.register(cx.waker());
|
2022-08-20 23:58:54 +02:00
|
|
|
|
|
|
|
if r.events_end.read().bits() != 0 {
|
|
|
|
compiler_fence(Ordering::SeqCst);
|
|
|
|
|
2022-08-20 22:37:51 +02:00
|
|
|
r.events_end.reset();
|
2022-08-20 23:58:54 +02:00
|
|
|
r.intenset.write(|w| w.end().set());
|
|
|
|
|
2023-07-18 23:17:04 +02:00
|
|
|
if !done {
|
|
|
|
// Discard the last buffer after the user requested a stop.
|
2022-08-20 23:58:54 +02:00
|
|
|
if sampler(&bufs[current_buffer]) == SamplerState::Sampled {
|
|
|
|
let next_buffer = 1 - current_buffer;
|
|
|
|
current_buffer = next_buffer;
|
|
|
|
} else {
|
2023-07-19 00:31:32 +02:00
|
|
|
r.tasks_stop.write(|w| unsafe { w.bits(1) });
|
2022-08-20 23:58:54 +02:00
|
|
|
done = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.events_started.read().bits() != 0 {
|
|
|
|
r.events_started.reset();
|
|
|
|
r.intenset.write(|w| w.started().set());
|
|
|
|
|
|
|
|
let next_buffer = 1 - current_buffer;
|
|
|
|
r.sample
|
|
|
|
.ptr
|
|
|
|
.write(|w| unsafe { w.sampleptr().bits(bufs[next_buffer].as_mut_ptr() as u32) });
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.events_stopped.read().bits() != 0 {
|
2022-08-20 22:37:51 +02:00
|
|
|
return Poll::Ready(());
|
|
|
|
}
|
|
|
|
|
|
|
|
Poll::Pending
|
|
|
|
})
|
|
|
|
.await;
|
2023-07-18 03:31:43 +02:00
|
|
|
drop.defuse();
|
|
|
|
Ok(())
|
2022-08-20 22:37:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
/// PDM microphone driver Config
|
|
|
|
pub struct Config {
|
|
|
|
/// Use stero or mono operation
|
|
|
|
pub operation_mode: OperationMode,
|
|
|
|
/// On which edge the left channel should be samples
|
|
|
|
pub edge: Edge,
|
|
|
|
/// Clock frequency
|
|
|
|
pub frequency: Frequency,
|
|
|
|
/// Clock ratio
|
2023-07-19 00:31:32 +02:00
|
|
|
#[cfg(any(
|
|
|
|
feature = "nrf52840",
|
|
|
|
feature = "nrf52833",
|
|
|
|
feature = "_nrf5340-app",
|
|
|
|
feature = "_nrf9160",
|
|
|
|
))]
|
2023-07-18 03:31:43 +02:00
|
|
|
pub ratio: Ratio,
|
|
|
|
/// Gain left in dB
|
|
|
|
pub gain_left: I7F1,
|
|
|
|
/// Gain right in dB
|
|
|
|
pub gain_right: I7F1,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Config {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
operation_mode: OperationMode::Mono,
|
|
|
|
edge: Edge::LeftFalling,
|
|
|
|
frequency: Frequency::DEFAULT,
|
2023-07-19 00:31:32 +02:00
|
|
|
#[cfg(any(
|
|
|
|
feature = "nrf52840",
|
|
|
|
feature = "nrf52833",
|
|
|
|
feature = "_nrf5340-app",
|
|
|
|
feature = "_nrf9160",
|
|
|
|
))]
|
2023-07-18 03:31:43 +02:00
|
|
|
ratio: Ratio::RATIO80,
|
|
|
|
gain_left: I7F1::ZERO,
|
|
|
|
gain_right: I7F1::ZERO,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// PDM operation mode.
|
|
|
|
#[derive(PartialEq)]
|
|
|
|
pub enum OperationMode {
|
|
|
|
/// Mono (1 channel)
|
|
|
|
Mono,
|
|
|
|
/// Stereo (2 channels)
|
|
|
|
Stereo,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<OperationMode> for OPERATION_A {
|
|
|
|
fn from(mode: OperationMode) -> Self {
|
|
|
|
match mode {
|
|
|
|
OperationMode::Mono => OPERATION_A::MONO,
|
|
|
|
OperationMode::Stereo => OPERATION_A::STEREO,
|
|
|
|
}
|
2022-08-20 23:08:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
/// PDM edge polarity
|
|
|
|
#[derive(PartialEq)]
|
2022-08-20 22:37:51 +02:00
|
|
|
pub enum Edge {
|
2023-07-18 03:31:43 +02:00
|
|
|
/// Left edge is rising
|
|
|
|
LeftRising,
|
|
|
|
/// Left edge is falling
|
|
|
|
LeftFalling,
|
2022-08-20 22:37:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Edge> for EDGE_A {
|
|
|
|
fn from(edge: Edge) -> Self {
|
|
|
|
match edge {
|
2023-07-18 03:31:43 +02:00
|
|
|
Edge::LeftRising => EDGE_A::LEFT_RISING,
|
|
|
|
Edge::LeftFalling => EDGE_A::LEFT_FALLING,
|
2022-08-20 22:37:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
impl<'d, T: Instance> Drop for Pdm<'d, T> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
let r = T::regs();
|
|
|
|
|
|
|
|
r.tasks_stop.write(|w| unsafe { w.bits(1) });
|
|
|
|
|
|
|
|
r.enable.write(|w| w.enable().disabled());
|
|
|
|
|
|
|
|
r.psel.din.reset();
|
|
|
|
r.psel.clk.reset();
|
|
|
|
}
|
2022-08-20 22:37:51 +02:00
|
|
|
}
|
|
|
|
|
2023-07-18 03:31:43 +02:00
|
|
|
pub(crate) mod sealed {
|
|
|
|
use embassy_sync::waitqueue::AtomicWaker;
|
|
|
|
|
|
|
|
/// Peripheral static state
|
|
|
|
pub struct State {
|
|
|
|
pub waker: AtomicWaker,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl State {
|
|
|
|
pub const fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
waker: AtomicWaker::new(),
|
|
|
|
}
|
2022-08-20 22:37:51 +02:00
|
|
|
}
|
|
|
|
}
|
2023-07-18 03:31:43 +02:00
|
|
|
|
|
|
|
pub trait Instance {
|
|
|
|
fn regs() -> &'static crate::pac::pdm::RegisterBlock;
|
|
|
|
fn state() -> &'static State;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// PDM peripheral instance.
|
|
|
|
pub trait Instance: Peripheral<P = Self> + sealed::Instance + 'static + Send {
|
|
|
|
/// Interrupt for this peripheral.
|
|
|
|
type Interrupt: interrupt::typelevel::Interrupt;
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! impl_pdm {
|
|
|
|
($type:ident, $pac_type:ident, $irq:ident) => {
|
|
|
|
impl crate::pdm::sealed::Instance for peripherals::$type {
|
|
|
|
fn regs() -> &'static crate::pac::pdm::RegisterBlock {
|
|
|
|
unsafe { &*pac::$pac_type::ptr() }
|
|
|
|
}
|
|
|
|
fn state() -> &'static crate::pdm::sealed::State {
|
|
|
|
static STATE: crate::pdm::sealed::State = crate::pdm::sealed::State::new();
|
|
|
|
&STATE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl crate::pdm::Instance for peripherals::$type {
|
|
|
|
type Interrupt = crate::interrupt::typelevel::$irq;
|
|
|
|
}
|
|
|
|
};
|
2023-07-18 23:13:43 +02:00
|
|
|
}
|