aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorseth <[email protected]>2024-06-24 17:53:59 -0700
committerseth <[email protected]>2024-06-24 17:53:59 -0700
commit7056783fa23eb25629e1e57da0021916a073a432 (patch)
tree27e6c2ca665475fe63c24c103aed1420bfd29f46 /examples
parent27b83fdbcfe7e9e64a8b65fdcb8dc82d6dc3e58c (diff)
second adc added to example + API todos completed
Diffstat (limited to 'examples')
-rw-r--r--examples/stm32f4/src/bin/adc_dma.rs32
1 files changed, 26 insertions, 6 deletions
diff --git a/examples/stm32f4/src/bin/adc_dma.rs b/examples/stm32f4/src/bin/adc_dma.rs
index 88822a507..dd19caf1d 100644
--- a/examples/stm32f4/src/bin/adc_dma.rs
+++ b/examples/stm32f4/src/bin/adc_dma.rs
@@ -13,15 +13,18 @@ async fn main(_spawner: Spawner) {
13 let mut p = embassy_stm32::init(Default::default()); 13 let mut p = embassy_stm32::init(Default::default());
14 14
15 let adc_data: &mut [u16; ADC_BUF_SIZE] = singleton!(ADCDAT : [u16; ADC_BUF_SIZE] = [0u16; ADC_BUF_SIZE]).unwrap(); 15 let adc_data: &mut [u16; ADC_BUF_SIZE] = singleton!(ADCDAT : [u16; ADC_BUF_SIZE] = [0u16; ADC_BUF_SIZE]).unwrap();
16 let adc_data2: &mut [u16; ADC_BUF_SIZE] = singleton!(ADCDAT2 : [u16; ADC_BUF_SIZE] = [0u16; ADC_BUF_SIZE]).unwrap();
16 17
17 let adc = Adc::new(p.ADC1); 18 let adc = Adc::new(p.ADC1);
19 let adc2 = Adc::new(p.ADC2);
18 20
19 let mut adc: RingBufferedAdc<embassy_stm32::peripherals::ADC1> = adc.into_ring_buffered(p.DMA2_CH0, adc_data); 21 let mut adc: RingBufferedAdc<embassy_stm32::peripherals::ADC1> = adc.into_ring_buffered(p.DMA2_CH0, adc_data);
22 let mut adc2: RingBufferedAdc<embassy_stm32::peripherals::ADC2> = adc2.into_ring_buffered(p.DMA2_CH2, adc_data2);
20 23
21 adc.set_sample_sequence(Sequence::One, &mut p.PA0, SampleTime::CYCLES112); 24 adc.set_sample_sequence(Sequence::One, &mut p.PA0, SampleTime::CYCLES112);
22 adc.set_sample_sequence(Sequence::Two, &mut p.PA2, SampleTime::CYCLES112); 25 adc.set_sample_sequence(Sequence::Two, &mut p.PA2, SampleTime::CYCLES112);
23 adc.set_sample_sequence(Sequence::Three, &mut p.PA1, SampleTime::CYCLES112); 26 adc2.set_sample_sequence(Sequence::One, &mut p.PA1, SampleTime::CYCLES112);
24 adc.set_sample_sequence(Sequence::Four, &mut p.PA3, SampleTime::CYCLES112); 27 adc2.set_sample_sequence(Sequence::Two, &mut p.PA3, SampleTime::CYCLES112);
25 28
26 // Note that overrun is a big consideration in this implementation. Whatever task is running the adc.read() calls absolutely must circle back around 29 // Note that overrun is a big consideration in this implementation. Whatever task is running the adc.read() calls absolutely must circle back around
27 // to the adc.read() call before the DMA buffer is wrapped around > 1 time. At this point, the overrun is so significant that the context of 30 // to the adc.read() call before the DMA buffer is wrapped around > 1 time. At this point, the overrun is so significant that the context of
@@ -31,10 +34,12 @@ async fn main(_spawner: Spawner) {
31 // An interrupt executor with a higher priority than other tasks may be a good approach here, allowing this task to wake and read the buffer most 34 // An interrupt executor with a higher priority than other tasks may be a good approach here, allowing this task to wake and read the buffer most
32 // frequently. 35 // frequently.
33 let mut tic = Instant::now(); 36 let mut tic = Instant::now();
34 let mut buffer1: [u16; 256] = [0u16; 256]; 37 let mut buffer1 = [0u16; 256];
38 let mut buffer2 = [0u16; 256];
35 let _ = adc.start(); 39 let _ = adc.start();
40 let _ = adc2.start();
36 loop { 41 loop {
37 match adc.read(&mut buffer1).await { 42 match adc.read_exact(&mut buffer1).await {
38 Ok(_data) => { 43 Ok(_data) => {
39 let toc = Instant::now(); 44 let toc = Instant::now();
40 info!( 45 info!(
@@ -49,10 +54,25 @@ async fn main(_spawner: Spawner) {
49 warn!("Error: {:?}", e); 54 warn!("Error: {:?}", e);
50 buffer1 = [0u16; 256]; 55 buffer1 = [0u16; 256];
51 let _ = adc.start(); 56 let _ = adc.start();
52 continue;
53 } 57 }
54 } 58 }
55 59
56 Timer::after_micros(300).await; 60 match adc2.read_exact(&mut buffer2).await {
61 Ok(_data) => {
62 let toc = Instant::now();
63 info!(
64 "\n adc2: {} dt = {}, n = {}",
65 buffer2[0..16],
66 (toc - tic).as_micros(),
67 _data
68 );
69 tic = toc;
70 }
71 Err(e) => {
72 warn!("Error: {:?}", e);
73 buffer2 = [0u16; 256];
74 let _ = adc2.start();
75 }
76 }
57 } 77 }
58} 78}