aboutsummaryrefslogtreecommitdiff
path: root/examples/nrf52840/src/bin/saadc_continuous.rs
blob: a25e1746538f6a94701dbe39668ad8365f535f3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]

use defmt::info;
use embassy_executor::Spawner;
use embassy_nrf::saadc::{CallbackResult, ChannelConfig, Config, Saadc};
use embassy_nrf::timer::Frequency;
use embassy_nrf::{bind_interrupts, saadc};
use embassy_time::Duration;
use {defmt_rtt as _, panic_probe as _};

// Demonstrates both continuous sampling and scanning multiple channels driven by a PPI linked timer

bind_interrupts!(struct Irqs {
    SAADC => saadc::InterruptHandler;
});

#[embassy_executor::main]
async fn main(_p: Spawner) {
    let mut p = embassy_nrf::init(Default::default());
    let config = Config::default();
    let channel_1_config = ChannelConfig::single_ended(&mut p.P0_02);
    let channel_2_config = ChannelConfig::single_ended(&mut p.P0_03);
    let channel_3_config = ChannelConfig::single_ended(&mut p.P0_04);
    let mut saadc = Saadc::new(
        p.SAADC,
        Irqs,
        config,
        [channel_1_config, channel_2_config, channel_3_config],
    );

    // This delay demonstrates that starting the timer prior to running
    // the task sampler is benign given the calibration that follows.
    embassy_time::Timer::after(Duration::from_millis(500)).await;
    saadc.calibrate().await;

    let mut bufs = [[[0; 3]; 500]; 2];

    let mut c = 0;
    let mut a: i32 = 0;

    saadc
        .run_task_sampler(
            &mut p.TIMER0,
            &mut p.PPI_CH0,
            &mut p.PPI_CH1,
            Frequency::F1MHz,
            1000, // We want to sample at 1KHz
            &mut bufs,
            move |buf| {
                // NOTE: It is important that the time spent within this callback
                // does not exceed the time taken to acquire the 1500 samples we
                // have in this example, which would be 10us + 2us per
                // sample * 1500 = 18ms. You need to measure the time taken here
                // and set the sample buffer size accordingly. Exceeding this
                // time can lead to the peripheral re-writing the other buffer.
                for b in buf {
                    a += b[0] as i32;
                }
                c += buf.len();
                if c > 1000 {
                    a = a / c as i32;
                    info!("channel 1: {=i32}", a);
                    c = 0;
                    a = 0;
                }
                CallbackResult::Continue
            },
        )
        .await;
}