diff options
| author | Quentin Smith <[email protected]> | 2022-08-20 17:08:29 -0400 |
|---|---|---|
| committer | Quentin Smith <[email protected]> | 2022-08-20 17:08:29 -0400 |
| commit | 530f192acceb5a10c416e1823dc27a749e68b7dc (patch) | |
| tree | f3e281bee3f6d3269ace1b31e662018aa15c8aca | |
| parent | a46f33b2144df0b913b50bb8c78256e20bce84c8 (diff) | |
Set gain at runtime
| -rw-r--r-- | embassy-nrf/src/pdm.rs | 21 | ||||
| -rw-r--r-- | examples/nrf/Cargo.toml | 1 | ||||
| -rw-r--r-- | examples/nrf/src/bin/pdm.rs | 29 |
3 files changed, 41 insertions, 10 deletions
diff --git a/embassy-nrf/src/pdm.rs b/embassy-nrf/src/pdm.rs index 629eab99f..b3cc87603 100644 --- a/embassy-nrf/src/pdm.rs +++ b/embassy-nrf/src/pdm.rs | |||
| @@ -88,6 +88,8 @@ impl<'d> Pdm<'d> { | |||
| 88 | w | 88 | w |
| 89 | }); | 89 | }); |
| 90 | 90 | ||
| 91 | Self::_set_gain(r, gain_left, gain_right); | ||
| 92 | |||
| 91 | r.psel.din.write(|w| unsafe { w.bits(data.psel_bits()) }); | 93 | r.psel.din.write(|w| unsafe { w.bits(data.psel_bits()) }); |
| 92 | r.psel.clk.write(|w| unsafe { w.bits(clock.psel_bits()) }); | 94 | r.psel.clk.write(|w| unsafe { w.bits(clock.psel_bits()) }); |
| 93 | 95 | ||
| @@ -115,6 +117,18 @@ impl<'d> Pdm<'d> { | |||
| 115 | } | 117 | } |
| 116 | } | 118 | } |
| 117 | 119 | ||
| 120 | fn _set_gain(r: &pdm::RegisterBlock, gain_left: I7F1, gain_right: I7F1) { | ||
| 121 | let gain_left = gain_left.saturating_add(I7F1::from_bits(40)).saturating_to_num::<u8>().clamp(0, 0x50); | ||
| 122 | let gain_right = gain_right.saturating_add(I7F1::from_bits(40)).saturating_to_num::<u8>().clamp(0, 0x50); | ||
| 123 | |||
| 124 | r.gainl.write(|w| unsafe { w.gainl().bits(gain_left) }); | ||
| 125 | r.gainr.write(|w| unsafe { w.gainr().bits(gain_right) }); | ||
| 126 | } | ||
| 127 | |||
| 128 | pub fn set_gain(&mut self, gain_left: I7F1, gain_right: I7F1) { | ||
| 129 | Self::_set_gain(Self::regs(), gain_left, gain_right) | ||
| 130 | } | ||
| 131 | |||
| 118 | fn regs() -> &'static pdm::RegisterBlock { | 132 | fn regs() -> &'static pdm::RegisterBlock { |
| 119 | unsafe { &*PDM::ptr() } | 133 | unsafe { &*PDM::ptr() } |
| 120 | } | 134 | } |
| @@ -154,6 +168,13 @@ impl<'d> Pdm<'d> { | |||
| 154 | } | 168 | } |
| 155 | } | 169 | } |
| 156 | 170 | ||
| 171 | impl<'d> Drop for Pdm<'d> { | ||
| 172 | fn drop(&mut self) { | ||
| 173 | let r = Self::regs(); | ||
| 174 | r.enable.write(|w| w.enable().disabled()); | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 157 | #[derive(Clone, Copy, PartialEq)] | 178 | #[derive(Clone, Copy, PartialEq)] |
| 158 | pub enum Edge { | 179 | pub enum Edge { |
| 159 | FallingEdge, | 180 | FallingEdge, |
diff --git a/examples/nrf/Cargo.toml b/examples/nrf/Cargo.toml index 673bcfc6b..d0567ba8e 100644 --- a/examples/nrf/Cargo.toml +++ b/examples/nrf/Cargo.toml | |||
| @@ -31,3 +31,4 @@ fixed = "1.10.0" | |||
| 31 | embedded-storage = "0.3.0" | 31 | embedded-storage = "0.3.0" |
| 32 | usbd-hid = "0.5.2" | 32 | usbd-hid = "0.5.2" |
| 33 | serde = { version = "1.0.136", default-features = false } | 33 | serde = { version = "1.0.136", default-features = false } |
| 34 | num-integer = { version = "0.1.45", default-features = false } | ||
diff --git a/examples/nrf/src/bin/pdm.rs b/examples/nrf/src/bin/pdm.rs index d5e90e27a..a73d01fb9 100644 --- a/examples/nrf/src/bin/pdm.rs +++ b/examples/nrf/src/bin/pdm.rs | |||
| @@ -8,6 +8,7 @@ use embassy_nrf::interrupt; | |||
| 8 | use embassy_nrf::pdm::{Config, Channels, Pdm}; | 8 | use embassy_nrf::pdm::{Config, Channels, Pdm}; |
| 9 | use embassy_time::{Duration, Timer}; | 9 | use embassy_time::{Duration, Timer}; |
| 10 | use fixed::types::I7F1; | 10 | use fixed::types::I7F1; |
| 11 | use num_integer::Roots; | ||
| 11 | use {defmt_rtt as _, panic_probe as _}; | 12 | use {defmt_rtt as _, panic_probe as _}; |
| 12 | 13 | ||
| 13 | #[embassy_executor::main] | 14 | #[embassy_executor::main] |
| @@ -20,15 +21,23 @@ async fn main(_p: Spawner) { | |||
| 20 | let mut pdm = Pdm::new(p.PDM, interrupt::take!(PDM), &mut p.P0_00, &mut p.P0_01, config); | 21 | let mut pdm = Pdm::new(p.PDM, interrupt::take!(PDM), &mut p.P0_00, &mut p.P0_01, config); |
| 21 | 22 | ||
| 22 | loop { | 23 | loop { |
| 23 | let mut buf = [0; 128]; | 24 | for gain in [I7F1::from_num(-20), I7F1::from_num(0), I7F1::from_num(20)] { |
| 24 | pdm.sample(&mut buf).await; | 25 | pdm.set_gain(gain, gain); |
| 25 | info!( | 26 | info!("Gain = {} dB", defmt::Debug2Format(&gain)); |
| 26 | "{} samples, min {=i16}, max {=i16}, mean {=i16}", | 27 | for _ in 0..10 { |
| 27 | buf.len(), | 28 | let mut buf = [0; 128]; |
| 28 | buf.iter().min().unwrap(), | 29 | pdm.sample(&mut buf).await; |
| 29 | buf.iter().max().unwrap(), | 30 | info!( |
| 30 | (buf.iter().map(|v| i32::from(*v)).sum::<i32>() / buf.len() as i32) as i16, | 31 | "{} samples, min {=i16}, max {=i16}, RMS {=i16}", |
| 31 | ); | 32 | buf.len(), |
| 32 | Timer::after(Duration::from_millis(100)).await; | 33 | buf.iter().min().unwrap(), |
| 34 | buf.iter().max().unwrap(), | ||
| 35 | ( | ||
| 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, | ||
| 38 | ); | ||
| 39 | Timer::after(Duration::from_millis(100)).await; | ||
| 40 | } | ||
| 41 | } | ||
| 33 | } | 42 | } |
| 34 | } | 43 | } |
