aboutsummaryrefslogtreecommitdiff
path: root/examples/nrf/src/bin/saadc_continuous.rs
diff options
context:
space:
mode:
authorhuntc <[email protected]>2021-10-12 11:24:26 +1100
committerhuntc <[email protected]>2021-10-18 10:26:11 +1100
commit103a3305e2a7bb9ef87d72a1237cbc5237aa8156 (patch)
tree5523c43673d7a885b4f059fff0c34872878fd137 /examples/nrf/src/bin/saadc_continuous.rs
parent90f6b56cba8123d51631aec2ceea7262eee149c5 (diff)
Implements continuous sampling for the nRF SAADC
Implements continuous sampling for the nRF SAADC and also renames `OneShot` to `Saadc`. The one-shot behaviour is retained with the `sample` method and a new `run_sampler` method is provided for efficiently (i.e. zero copying) sampler processing. A double buffer is used for continuously sampling, which wlll be swapped once sampling has taken place. A sample frequency is provided and will set the internal timer of the SAADC when there is just the one channel being sampled. Otherwise, PPI will be used to hook up the TIMER peripheral to drive the sampling task.
Diffstat (limited to 'examples/nrf/src/bin/saadc_continuous.rs')
-rw-r--r--examples/nrf/src/bin/saadc_continuous.rs49
1 files changed, 49 insertions, 0 deletions
diff --git a/examples/nrf/src/bin/saadc_continuous.rs b/examples/nrf/src/bin/saadc_continuous.rs
new file mode 100644
index 000000000..fba00ebdf
--- /dev/null
+++ b/examples/nrf/src/bin/saadc_continuous.rs
@@ -0,0 +1,49 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5#[path = "../example_common.rs"]
6mod example_common;
7use defmt::panic;
8use embassy::executor::Spawner;
9use embassy_nrf::ppi::Ppi;
10use embassy_nrf::saadc::{ChannelConfig, Config, Mode, Saadc, SamplerState};
11use embassy_nrf::timer::{Frequency, Timer};
12use embassy_nrf::{interrupt, Peripherals};
13use example_common::*;
14
15// Demonstrates both continuous sampling and scanning multiple channels driven by a PPI linked timer
16
17#[embassy::main]
18async fn main(_spawner: Spawner, mut p: Peripherals) {
19 let config = Config::default();
20 let channel_1_config = ChannelConfig::single_ended(&mut p.P0_02);
21 let channel_2_config = ChannelConfig::single_ended(&mut p.P0_03);
22 let channel_3_config = ChannelConfig::single_ended(&mut p.P0_04);
23 let mut saadc = Saadc::new(
24 p.SAADC,
25 interrupt::take!(SAADC),
26 config,
27 [channel_1_config, channel_2_config, channel_3_config],
28 );
29
30 let mut timer = Timer::new(p.TIMER0);
31 timer.set_frequency(Frequency::F1MHz);
32 timer.cc(0).write(100); // We want to sample at 10KHz
33 timer.cc(0).short_compare_clear();
34
35 let mut ppi = Ppi::new(p.PPI_CH0);
36 ppi.set_event(timer.cc(0).event_compare());
37 ppi.set_task(saadc.task_sample());
38 ppi.enable();
39
40 timer.start();
41
42 let mut bufs = [[0; 3 * 500]; 2]; // Each buffer of the double buffer has to be large enough for all channels.
43 saadc
44 .run_sampler(&mut bufs, Mode::Task, |buf| {
45 info!("sample len={}", buf.len());
46 SamplerState::Sampled
47 })
48 .await;
49}