diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/nrf/src/bin/saadc.rs | 4 | ||||
| -rw-r--r-- | examples/nrf/src/bin/saadc_continuous.rs | 49 |
2 files changed, 51 insertions, 2 deletions
diff --git a/examples/nrf/src/bin/saadc.rs b/examples/nrf/src/bin/saadc.rs index d12717c04..c6eac555b 100644 --- a/examples/nrf/src/bin/saadc.rs +++ b/examples/nrf/src/bin/saadc.rs | |||
| @@ -7,7 +7,7 @@ mod example_common; | |||
| 7 | use defmt::panic; | 7 | use defmt::panic; |
| 8 | use embassy::executor::Spawner; | 8 | use embassy::executor::Spawner; |
| 9 | use embassy::time::{Duration, Timer}; | 9 | use embassy::time::{Duration, Timer}; |
| 10 | use embassy_nrf::saadc::{ChannelConfig, Config, OneShot}; | 10 | use embassy_nrf::saadc::{ChannelConfig, Config, Saadc}; |
| 11 | use embassy_nrf::{interrupt, Peripherals}; | 11 | use embassy_nrf::{interrupt, Peripherals}; |
| 12 | use example_common::*; | 12 | use example_common::*; |
| 13 | 13 | ||
| @@ -15,7 +15,7 @@ use example_common::*; | |||
| 15 | async fn main(_spawner: Spawner, mut p: Peripherals) { | 15 | async fn main(_spawner: Spawner, mut p: Peripherals) { |
| 16 | let config = Config::default(); | 16 | let config = Config::default(); |
| 17 | let channel_config = ChannelConfig::single_ended(&mut p.P0_02); | 17 | let channel_config = ChannelConfig::single_ended(&mut p.P0_02); |
| 18 | let mut saadc = OneShot::new(p.SAADC, interrupt::take!(SAADC), config, [channel_config]); | 18 | let mut saadc = Saadc::new(p.SAADC, interrupt::take!(SAADC), config, [channel_config]); |
| 19 | 19 | ||
| 20 | loop { | 20 | loop { |
| 21 | let mut buf = [0; 1]; | 21 | let mut buf = [0; 1]; |
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 | } | ||
