aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/nrf/src/bin/saadc_continuous.rs59
-rw-r--r--examples/stm32l4/src/bin/spi_dma.rs2
2 files changed, 35 insertions, 26 deletions
diff --git a/examples/nrf/src/bin/saadc_continuous.rs b/examples/nrf/src/bin/saadc_continuous.rs
index 81559237b..bdf552fba 100644
--- a/examples/nrf/src/bin/saadc_continuous.rs
+++ b/examples/nrf/src/bin/saadc_continuous.rs
@@ -5,9 +5,9 @@
5#[path = "../example_common.rs"] 5#[path = "../example_common.rs"]
6mod example_common; 6mod example_common;
7use embassy::executor::Spawner; 7use embassy::executor::Spawner;
8use embassy_nrf::ppi::Ppi; 8use embassy::time::Duration;
9use embassy_nrf::saadc::{ChannelConfig, Config, Saadc, SamplerState}; 9use embassy_nrf::saadc::{ChannelConfig, Config, Saadc, SamplerState};
10use embassy_nrf::timer::{Frequency, Timer}; 10use embassy_nrf::timer::Frequency;
11use embassy_nrf::{interrupt, Peripherals}; 11use embassy_nrf::{interrupt, Peripherals};
12use example_common::*; 12use example_common::*;
13 13
@@ -26,34 +26,43 @@ async fn main(_spawner: Spawner, mut p: Peripherals) {
26 [channel_1_config, channel_2_config, channel_3_config], 26 [channel_1_config, channel_2_config, channel_3_config],
27 ); 27 );
28 28
29 let mut timer = Timer::new(p.TIMER0); 29 // This delay demonstrates that starting the timer prior to running
30 timer.set_frequency(Frequency::F1MHz); 30 // the task sampler is benign given the calibration that follows.
31 timer.cc(0).write(100); // We want to sample at 10KHz 31 embassy::time::Timer::after(Duration::from_millis(500)).await;
32 timer.cc(0).short_compare_clear(); 32 saadc.calibrate().await;
33 33
34 let mut ppi = Ppi::new_one_to_one(p.PPI_CH0, timer.cc(0).event_compare(), saadc.task_sample()); 34 let mut bufs = [[[0; 3]; 500]; 2];
35 ppi.enable();
36
37 timer.start();
38
39 let mut bufs = [[[0; 3]; 50]; 2];
40 35
41 let mut c = 0; 36 let mut c = 0;
42 let mut a: i32 = 0; 37 let mut a: i32 = 0;
43 38
44 saadc 39 saadc
45 .run_task_sampler(&mut bufs, move |buf| { 40 .run_task_sampler(
46 for b in buf { 41 &mut p.TIMER0,
47 a += b[0] as i32; 42 &mut p.PPI_CH0,
48 } 43 &mut p.PPI_CH1,
49 c += buf.len(); 44 Frequency::F1MHz,
50 if c > 10000 { 45 1000, // We want to sample at 1KHz
51 a = a / c as i32; 46 &mut bufs,
52 info!("channel 1: {=i32}", a); 47 move |buf| {
53 c = 0; 48 // NOTE: It is important that the time spent within this callback
54 a = 0; 49 // does not exceed the time taken to acquire the 1500 samples we
55 } 50 // have in this example, which would be 10us + 2us per
56 SamplerState::Sampled 51 // sample * 1500 = 18ms. You need to measure the time taken here
57 }) 52 // and set the sample buffer size accordingly. Exceeding this
53 // time can lead to the peripheral re-writing the other buffer.
54 for b in buf {
55 a += b[0] as i32;
56 }
57 c += buf.len();
58 if c > 1000 {
59 a = a / c as i32;
60 info!("channel 1: {=i32}", a);
61 c = 0;
62 a = 0;
63 }
64 SamplerState::Sampled
65 },
66 )
58 .await; 67 .await;
59} 68}
diff --git a/examples/stm32l4/src/bin/spi_dma.rs b/examples/stm32l4/src/bin/spi_dma.rs
index b4d5091b2..a9327f8fe 100644
--- a/examples/stm32l4/src/bin/spi_dma.rs
+++ b/examples/stm32l4/src/bin/spi_dma.rs
@@ -21,8 +21,8 @@ async fn main(_spawner: Spawner, p: Peripherals) {
21 p.PC10, 21 p.PC10,
22 p.PC12, 22 p.PC12,
23 p.PC11, 23 p.PC11,
24 p.DMA1_CH0,
25 p.DMA1_CH1, 24 p.DMA1_CH1,
25 p.DMA1_CH2,
26 Hertz(1_000_000), 26 Hertz(1_000_000),
27 Config::default(), 27 Config::default(),
28 ); 28 );