diff options
| author | Quentin Smith <[email protected]> | 2022-08-20 17:58:54 -0400 |
|---|---|---|
| committer | Quentin Smith <[email protected]> | 2022-08-20 17:58:54 -0400 |
| commit | 0963b5f92c9588ab00f556a6c521fad059eac72e (patch) | |
| tree | 1cda1bcd1e345728af9746409da82c14e91602d6 | |
| parent | 530f192acceb5a10c416e1823dc27a749e68b7dc (diff) | |
Add continuous PDM sampling with example
| -rw-r--r-- | embassy-nrf/src/pdm.rs | 118 | ||||
| -rw-r--r-- | examples/nrf/src/bin/pdm.rs | 3 | ||||
| -rw-r--r-- | examples/nrf/src/bin/pdm_continuous.rs | 50 |
3 files changed, 167 insertions, 4 deletions
diff --git a/embassy-nrf/src/pdm.rs b/embassy-nrf/src/pdm.rs index b3cc87603..db4c74afd 100644 --- a/embassy-nrf/src/pdm.rs +++ b/embassy-nrf/src/pdm.rs | |||
| @@ -115,6 +115,11 @@ impl<'d> Pdm<'d> { | |||
| 115 | r.intenclr.write(|w| w.started().clear()); | 115 | r.intenclr.write(|w| w.started().clear()); |
| 116 | WAKER.wake(); | 116 | WAKER.wake(); |
| 117 | } | 117 | } |
| 118 | |||
| 119 | if r.events_stopped.read().bits() != 0 { | ||
| 120 | r.intenclr.write(|w| w.stopped().clear()); | ||
| 121 | WAKER.wake(); | ||
| 122 | } | ||
| 118 | } | 123 | } |
| 119 | 124 | ||
| 120 | fn _set_gain(r: &pdm::RegisterBlock, gain_left: I7F1, gain_right: I7F1) { | 125 | fn _set_gain(r: &pdm::RegisterBlock, gain_left: I7F1, gain_right: I7F1) { |
| @@ -141,15 +146,20 @@ impl<'d> Pdm<'d> { | |||
| 141 | r.sample.ptr.write(|w| unsafe { w.sampleptr().bits(buf.as_mut_ptr() as u32) }); | 146 | r.sample.ptr.write(|w| unsafe { w.sampleptr().bits(buf.as_mut_ptr() as u32) }); |
| 142 | r.sample.maxcnt.write(|w| unsafe { w.buffsize().bits(N as _) }); | 147 | r.sample.maxcnt.write(|w| unsafe { w.buffsize().bits(N as _) }); |
| 143 | 148 | ||
| 144 | // Reset and enable the end event | 149 | // Reset and enable the events |
| 145 | r.events_end.reset(); | 150 | r.events_end.reset(); |
| 146 | r.intenset.write(|w| w.end().set()); | 151 | r.events_stopped.reset(); |
| 152 | r.intenset.write(|w| { | ||
| 153 | w.end().set(); | ||
| 154 | w.stopped().set(); | ||
| 155 | w | ||
| 156 | }); | ||
| 147 | 157 | ||
| 148 | // Don't reorder the start event before the previous writes. Hopefully self | 158 | // Don't reorder the start event before the previous writes. Hopefully self |
| 149 | // wouldn't happen anyway. | 159 | // wouldn't happen anyway. |
| 150 | compiler_fence(Ordering::SeqCst); | 160 | compiler_fence(Ordering::SeqCst); |
| 151 | 161 | ||
| 152 | r.tasks_start.write(|w| { w.tasks_start().set_bit() }); | 162 | r.tasks_start.write(|w| w.tasks_start().set_bit()); |
| 153 | 163 | ||
| 154 | // Wait for 'end' event. | 164 | // Wait for 'end' event. |
| 155 | poll_fn(|cx| { | 165 | poll_fn(|cx| { |
| @@ -158,7 +168,109 @@ impl<'d> Pdm<'d> { | |||
| 158 | WAKER.register(cx.waker()); | 168 | WAKER.register(cx.waker()); |
| 159 | 169 | ||
| 160 | if r.events_end.read().bits() != 0 { | 170 | if r.events_end.read().bits() != 0 { |
| 171 | // END means the whole buffer has been received. | ||
| 172 | r.events_end.reset(); | ||
| 173 | // Note that the beginning of the buffer might be overwritten before the task fully stops :( | ||
| 174 | r.tasks_stop.write(|w| w.tasks_stop().set_bit()); | ||
| 175 | } | ||
| 176 | |||
| 177 | if r.events_stopped.read().bits() != 0 { | ||
| 178 | r.events_stopped.reset(); | ||
| 179 | return Poll::Ready(()); | ||
| 180 | } | ||
| 181 | |||
| 182 | Poll::Pending | ||
| 183 | }) | ||
| 184 | .await; | ||
| 185 | } | ||
| 186 | |||
| 187 | /// Continuous sampling with double buffers. | ||
| 188 | /// | ||
| 189 | /// A TIMER and two PPI peripherals are passed in so that precise sampling | ||
| 190 | /// can be attained. The sampling interval is expressed by selecting a | ||
| 191 | /// timer clock frequency to use along with a counter threshold to be reached. | ||
| 192 | /// For example, 1KHz can be achieved using a frequency of 1MHz and a counter | ||
| 193 | /// threshold of 1000. | ||
| 194 | /// | ||
| 195 | /// A sampler closure is provided that receives the buffer of samples, noting | ||
| 196 | /// that the size of this buffer can be less than the original buffer's size. | ||
| 197 | /// A command is return from the closure that indicates whether the sampling | ||
| 198 | /// should continue or stop. | ||
| 199 | /// | ||
| 200 | /// NOTE: The time spent within the callback supplied should not exceed the time | ||
| 201 | /// taken to acquire the samples into a single buffer. You should measure the | ||
| 202 | /// time taken by the callback and set the sample buffer size accordingly. | ||
| 203 | /// Exceeding this time can lead to samples becoming dropped. | ||
| 204 | pub async fn run_task_sampler<S, const N: usize>( | ||
| 205 | &mut self, | ||
| 206 | bufs: &mut [[i16; N]; 2], | ||
| 207 | mut sampler: S, | ||
| 208 | ) where | ||
| 209 | S: FnMut(&[i16; N]) -> SamplerState, | ||
| 210 | { | ||
| 211 | let r = Self::regs(); | ||
| 212 | |||
| 213 | r.sample.ptr.write(|w| unsafe { w.sampleptr().bits(bufs[0].as_mut_ptr() as u32) }); | ||
| 214 | r.sample.maxcnt.write(|w| unsafe { w.buffsize().bits(N as _) }); | ||
| 215 | |||
| 216 | // Reset and enable the events | ||
| 217 | r.events_end.reset(); | ||
| 218 | r.events_started.reset(); | ||
| 219 | r.events_stopped.reset(); | ||
| 220 | r.intenset.write(|w| { | ||
| 221 | w.end().set(); | ||
| 222 | w.started().set(); | ||
| 223 | w.stopped().set(); | ||
| 224 | w | ||
| 225 | }); | ||
| 226 | |||
| 227 | // Don't reorder the start event before the previous writes. Hopefully self | ||
| 228 | // wouldn't happen anyway. | ||
| 229 | compiler_fence(Ordering::SeqCst); | ||
| 230 | |||
| 231 | r.tasks_start.write(|w| { w.tasks_start().set_bit() }); | ||
| 232 | |||
| 233 | let mut current_buffer = 0; | ||
| 234 | |||
| 235 | let mut done = false; | ||
| 236 | |||
| 237 | // Wait for events and complete when the sampler indicates it has had enough. | ||
| 238 | poll_fn(|cx| { | ||
| 239 | let r = Self::regs(); | ||
| 240 | |||
| 241 | WAKER.register(cx.waker()); | ||
| 242 | |||
| 243 | if r.events_end.read().bits() != 0 { | ||
| 244 | compiler_fence(Ordering::SeqCst); | ||
| 245 | |||
| 161 | r.events_end.reset(); | 246 | r.events_end.reset(); |
| 247 | r.intenset.write(|w| w.end().set()); | ||
| 248 | |||
| 249 | if !done { // Discard the last buffer after the user requested a stop. | ||
| 250 | if sampler(&bufs[current_buffer]) == SamplerState::Sampled { | ||
| 251 | let next_buffer = 1 - current_buffer; | ||
| 252 | current_buffer = next_buffer; | ||
| 253 | } else { | ||
| 254 | r.tasks_stop.write(|w| w.tasks_stop().set_bit()); | ||
| 255 | done = true; | ||
| 256 | }; | ||
| 257 | }; | ||
| 258 | } | ||
| 259 | |||
| 260 | if r.events_started.read().bits() != 0 { | ||
| 261 | r.events_started.reset(); | ||
| 262 | r.intenset.write(|w| w.started().set()); | ||
| 263 | |||
| 264 | let next_buffer = 1 - current_buffer; | ||
| 265 | r.sample | ||
| 266 | .ptr | ||
| 267 | .write(|w| unsafe { w.sampleptr().bits(bufs[next_buffer].as_mut_ptr() as u32) }); | ||
| 268 | } | ||
| 269 | |||
| 270 | if r.events_stopped.read().bits() != 0 { | ||
| 271 | r.events_stopped.reset(); | ||
| 272 | r.intenset.write(|w| w.stopped().set()); | ||
| 273 | |||
| 162 | return Poll::Ready(()); | 274 | return Poll::Ready(()); |
| 163 | } | 275 | } |
| 164 | 276 | ||
diff --git a/examples/nrf/src/bin/pdm.rs b/examples/nrf/src/bin/pdm.rs index a73d01fb9..85a59a529 100644 --- a/examples/nrf/src/bin/pdm.rs +++ b/examples/nrf/src/bin/pdm.rs | |||
| @@ -25,7 +25,7 @@ async fn main(_p: Spawner) { | |||
| 25 | pdm.set_gain(gain, gain); | 25 | pdm.set_gain(gain, gain); |
| 26 | info!("Gain = {} dB", defmt::Debug2Format(&gain)); | 26 | info!("Gain = {} dB", defmt::Debug2Format(&gain)); |
| 27 | for _ in 0..10 { | 27 | for _ in 0..10 { |
| 28 | let mut buf = [0; 128]; | 28 | let mut buf = [0; 1500]; |
| 29 | pdm.sample(&mut buf).await; | 29 | pdm.sample(&mut buf).await; |
| 30 | info!( | 30 | info!( |
| 31 | "{} samples, min {=i16}, max {=i16}, RMS {=i16}", | 31 | "{} samples, min {=i16}, max {=i16}, RMS {=i16}", |
| @@ -36,6 +36,7 @@ async fn main(_p: Spawner) { | |||
| 36 | buf.iter().map(|v| i32::from(*v).pow(2)).fold(0i32, |a,b| a.saturating_add(b)) | 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, | 37 | / buf.len() as i32).sqrt() as i16, |
| 38 | ); | 38 | ); |
| 39 | info!("samples = {}", &buf); | ||
| 39 | Timer::after(Duration::from_millis(100)).await; | 40 | Timer::after(Duration::from_millis(100)).await; |
| 40 | } | 41 | } |
| 41 | } | 42 | } |
diff --git a/examples/nrf/src/bin/pdm_continuous.rs b/examples/nrf/src/bin/pdm_continuous.rs new file mode 100644 index 000000000..e7d1806bb --- /dev/null +++ b/examples/nrf/src/bin/pdm_continuous.rs | |||
| @@ -0,0 +1,50 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::info; | ||
| 6 | use embassy_executor::Spawner; | ||
| 7 | use embassy_nrf::interrupt; | ||
| 8 | use embassy_nrf::pdm::{Config, Channels, Pdm, SamplerState}; | ||
| 9 | use embassy_nrf::timer::Frequency; | ||
| 10 | use fixed::types::I7F1; | ||
| 11 | use num_integer::Roots; | ||
| 12 | use {defmt_rtt as _, panic_probe as _}; | ||
| 13 | |||
| 14 | // Demonstrates both continuous sampling and scanning multiple channels driven by a PPI linked timer | ||
| 15 | |||
| 16 | #[embassy_executor::main] | ||
| 17 | async fn main(_p: Spawner) { | ||
| 18 | let mut p = embassy_nrf::init(Default::default()); | ||
| 19 | let mut config = Config::default(); | ||
| 20 | // Pins are correct for the onboard microphone on the Feather nRF52840 Sense. | ||
| 21 | config.channels = Channels::Mono; | ||
| 22 | 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 | |||
| 25 | let mut bufs = [[0; 500]; 2]; | ||
| 26 | |||
| 27 | pdm | ||
| 28 | .run_task_sampler( | ||
| 29 | &mut bufs, | ||
| 30 | move |buf| { | ||
| 31 | // NOTE: It is important that the time spent within this callback | ||
| 32 | // does not exceed the time taken to acquire the 1500 samples we | ||
| 33 | // have in this example, which would be 10us + 2us per | ||
| 34 | // sample * 1500 = 18ms. You need to measure the time taken here | ||
| 35 | // and set the sample buffer size accordingly. Exceeding this | ||
| 36 | // time can lead to the peripheral re-writing the other buffer. | ||
| 37 | info!( | ||
| 38 | "{} samples, min {=i16}, max {=i16}, RMS {=i16}", | ||
| 39 | buf.len(), | ||
| 40 | buf.iter().min().unwrap(), | ||
| 41 | buf.iter().max().unwrap(), | ||
| 42 | ( | ||
| 43 | buf.iter().map(|v| i32::from(*v).pow(2)).fold(0i32, |a,b| a.saturating_add(b)) | ||
| 44 | / buf.len() as i32).sqrt() as i16, | ||
| 45 | ); | ||
| 46 | SamplerState::Sampled | ||
| 47 | }, | ||
| 48 | ) | ||
| 49 | .await; | ||
| 50 | } | ||
