diff options
Diffstat (limited to 'examples/nrf/src/bin/saadc_continuous.rs')
| -rw-r--r-- | examples/nrf/src/bin/saadc_continuous.rs | 49 |
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"] | ||
| 6 | mod example_common; | ||
| 7 | use defmt::panic; | ||
| 8 | use embassy::executor::Spawner; | ||
| 9 | use embassy_nrf::ppi::Ppi; | ||
| 10 | use embassy_nrf::saadc::{ChannelConfig, Config, Mode, Saadc, SamplerState}; | ||
| 11 | use embassy_nrf::timer::{Frequency, Timer}; | ||
| 12 | use embassy_nrf::{interrupt, Peripherals}; | ||
| 13 | use example_common::*; | ||
| 14 | |||
| 15 | // Demonstrates both continuous sampling and scanning multiple channels driven by a PPI linked timer | ||
| 16 | |||
| 17 | #[embassy::main] | ||
| 18 | async 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 | } | ||
