2021-10-12 02:24:26 +02:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
2022-04-02 04:35:06 +02:00
|
|
|
use defmt::info;
|
2022-08-17 23:40:16 +02:00
|
|
|
use embassy_executor::Spawner;
|
2023-02-01 00:48:33 +01:00
|
|
|
use embassy_nrf::saadc::{CallbackResult, ChannelConfig, Config, Saadc};
|
2022-03-07 02:45:37 +01:00
|
|
|
use embassy_nrf::timer::Frequency;
|
2023-03-05 21:56:22 +01:00
|
|
|
use embassy_nrf::{bind_interrupts, saadc};
|
2022-06-12 22:15:44 +02:00
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
2021-10-12 02:24:26 +02:00
|
|
|
|
|
|
|
// Demonstrates both continuous sampling and scanning multiple channels driven by a PPI linked timer
|
|
|
|
|
2023-03-05 21:56:22 +01:00
|
|
|
bind_interrupts!(struct Irqs {
|
|
|
|
SAADC => saadc::InterruptHandler;
|
|
|
|
});
|
|
|
|
|
2022-07-29 21:58:35 +02:00
|
|
|
#[embassy_executor::main]
|
2022-08-17 18:49:55 +02:00
|
|
|
async fn main(_p: Spawner) {
|
|
|
|
let mut p = embassy_nrf::init(Default::default());
|
2021-10-12 02:24:26 +02:00
|
|
|
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,
|
2023-03-05 21:56:22 +01:00
|
|
|
Irqs,
|
2021-10-12 02:24:26 +02:00
|
|
|
config,
|
|
|
|
[channel_1_config, channel_2_config, channel_3_config],
|
|
|
|
);
|
|
|
|
|
2022-02-26 08:15:37 +01:00
|
|
|
// This delay demonstrates that starting the timer prior to running
|
|
|
|
// the task sampler is benign given the calibration that follows.
|
2023-10-15 01:57:25 +02:00
|
|
|
embassy_time::Timer::after_millis(500).await;
|
2022-02-26 08:15:37 +01:00
|
|
|
saadc.calibrate().await;
|
|
|
|
|
|
|
|
let mut bufs = [[[0; 3]; 500]; 2];
|
2021-10-15 08:44:23 +02:00
|
|
|
|
|
|
|
let mut c = 0;
|
|
|
|
let mut a: i32 = 0;
|
|
|
|
|
2021-10-12 02:24:26 +02:00
|
|
|
saadc
|
2022-02-26 08:15:37 +01:00
|
|
|
.run_task_sampler(
|
2022-03-07 02:45:37 +01:00
|
|
|
&mut p.TIMER0,
|
|
|
|
&mut p.PPI_CH0,
|
|
|
|
&mut p.PPI_CH1,
|
|
|
|
Frequency::F1MHz,
|
|
|
|
1000, // We want to sample at 1KHz
|
2022-02-26 08:15:37 +01:00
|
|
|
&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;
|
|
|
|
}
|
2023-02-01 00:48:33 +01:00
|
|
|
CallbackResult::Continue
|
2022-02-26 08:15:37 +01:00
|
|
|
},
|
|
|
|
)
|
2021-10-12 02:24:26 +02:00
|
|
|
.await;
|
|
|
|
}
|