aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorGabriel Smith <[email protected]>2025-06-13 12:59:56 +0000
committerGabriel Smith <[email protected]>2025-08-22 14:56:19 -0400
commit5a1be543ac8838963a6597dda2ddf3918397e39b (patch)
tree2a5affe05d84f1e57d0e1fb8292131639bbfc116 /examples
parentd65a5078f26f860881b2f4a0db84e0d8f3160624 (diff)
stm32/adc/v3: allow DMA reads to loop through enabled channels
Tested on an STM32H533RE. Documentation of other chips has been reviewed, but not extensively.
Diffstat (limited to 'examples')
-rw-r--r--examples/stm32h5/src/bin/adc_dma.rs94
1 files changed, 94 insertions, 0 deletions
diff --git a/examples/stm32h5/src/bin/adc_dma.rs b/examples/stm32h5/src/bin/adc_dma.rs
new file mode 100644
index 000000000..20073e22f
--- /dev/null
+++ b/examples/stm32h5/src/bin/adc_dma.rs
@@ -0,0 +1,94 @@
1#![no_std]
2#![no_main]
3
4use defmt::*;
5use embassy_executor::Spawner;
6use embassy_stm32::adc::{self, Adc, AdcChannel, RxDma, SampleTime};
7use embassy_stm32::peripherals::{ADC1, ADC2, GPDMA1_CH0, GPDMA1_CH1, PA0, PA1, PA2, PA3};
8use embassy_stm32::{Config, Peri};
9use embassy_time::Instant;
10use {defmt_rtt as _, panic_probe as _};
11
12#[embassy_executor::main]
13async fn main(spawner: Spawner) {
14 let mut config = Config::default();
15 {
16 use embassy_stm32::rcc::*;
17 config.rcc.hsi = Some(HSIPrescaler::DIV1);
18 config.rcc.csi = true;
19 config.rcc.pll1 = Some(Pll {
20 source: PllSource::HSI,
21 prediv: PllPreDiv::DIV4,
22 mul: PllMul::MUL25,
23 divp: Some(PllDiv::DIV2),
24 divq: Some(PllDiv::DIV4), // SPI1 cksel defaults to pll1_q
25 divr: None,
26 });
27 config.rcc.pll2 = Some(Pll {
28 source: PllSource::HSI,
29 prediv: PllPreDiv::DIV4,
30 mul: PllMul::MUL25,
31 divp: None,
32 divq: None,
33 divr: Some(PllDiv::DIV4), // 100mhz
34 });
35 config.rcc.sys = Sysclk::PLL1_P; // 200 Mhz
36 config.rcc.ahb_pre = AHBPrescaler::DIV1; // 200 Mhz
37 config.rcc.apb1_pre = APBPrescaler::DIV2; // 100 Mhz
38 config.rcc.apb2_pre = APBPrescaler::DIV2; // 100 Mhz
39 config.rcc.apb3_pre = APBPrescaler::DIV2; // 100 Mhz
40 config.rcc.voltage_scale = VoltageScale::Scale1;
41 config.rcc.mux.adcdacsel = mux::Adcdacsel::PLL2_R;
42 }
43 let p = embassy_stm32::init(config);
44
45 spawner.must_spawn(adc1_task(p.ADC1, p.GPDMA1_CH0, p.PA0, p.PA2));
46 spawner.must_spawn(adc2_task(p.ADC2, p.GPDMA1_CH1, p.PA1, p.PA3));
47}
48
49#[embassy_executor::task]
50async fn adc1_task(
51 adc: Peri<'static, ADC1>,
52 dma: Peri<'static, GPDMA1_CH0>,
53 pin1: Peri<'static, PA0>,
54 pin2: Peri<'static, PA2>,
55) {
56 adc_task(adc, dma, pin1, pin2).await;
57}
58
59#[embassy_executor::task]
60async fn adc2_task(
61 adc: Peri<'static, ADC2>,
62 dma: Peri<'static, GPDMA1_CH1>,
63 pin1: Peri<'static, PA1>,
64 pin2: Peri<'static, PA3>,
65) {
66 adc_task(adc, dma, pin1, pin2).await;
67}
68
69async fn adc_task<'a, T: adc::Instance>(
70 adc: Peri<'a, T>,
71 mut dma: Peri<'a, impl RxDma<T>>,
72 pin1: impl AdcChannel<T>,
73 pin2: impl AdcChannel<T>,
74) {
75 let mut adc = Adc::new(adc);
76 let mut pin1 = pin1.degrade_adc();
77 let mut pin2 = pin2.degrade_adc();
78
79 let mut tic = Instant::now();
80 let mut buffer = [0u16; 512];
81 loop {
82 // This is not a true continuous read as there is downtime between each
83 // call to `Adc::read` where the ADC is sitting idle.
84 adc.read(
85 dma.reborrow(),
86 [(&mut pin1, SampleTime::CYCLES2_5), (&mut pin2, SampleTime::CYCLES2_5)].into_iter(),
87 &mut buffer,
88 )
89 .await;
90 let toc = Instant::now();
91 info!("\n adc1: {} dt = {}", buffer[0..16], (toc - tic).as_micros());
92 tic = toc;
93 }
94}