-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy pathadc-dma-circ.rs
57 lines (39 loc) · 1.57 KB
/
adc-dma-circ.rs
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
//! ADC interface circular DMA RX transfer test
#![allow(clippy::empty_loop)]
#![no_main]
#![no_std]
use panic_halt as _;
use cortex_m::{asm, singleton};
use cortex_m_rt::entry;
use stm32f1xx_hal::{adc, dma::Half, pac, prelude::*};
#[entry]
fn main() -> ! {
// Acquire peripherals
let p = pac::Peripherals::take().unwrap();
let mut flash = p.FLASH.constrain();
let rcc = p.RCC.constrain();
// Configure ADC clocks
// Default value is the slowest possible ADC clock: PCLK2 / 8. Meanwhile ADC
// clock is configurable. So its frequency may be tweaked to meet certain
// practical needs. User specified value is be approximated using supported
// prescaler values 2/4/6/8.
let clocks = rcc.cfgr.adcclk(2.MHz()).freeze(&mut flash.acr);
let dma_ch1 = p.DMA1.split().1;
// Setup ADC
let adc1 = adc::Adc::adc1(p.ADC1, &clocks);
// Setup GPIOA
let mut gpioa = p.GPIOA.split();
// Configure pa0 as an analog input
let adc_ch0 = gpioa.pa0.into_analog(&mut gpioa.crl);
let adc_dma = adc1.with_dma(adc_ch0, dma_ch1);
let buf = singleton!(: [[u16; 8]; 2] = [[0; 8]; 2]).unwrap();
let mut circ_buffer = adc_dma.circ_read(buf);
while circ_buffer.readable_half().unwrap() != Half::First {}
let _first_half = circ_buffer.peek(|half, _| *half).unwrap();
while circ_buffer.readable_half().unwrap() != Half::Second {}
let _second_half = circ_buffer.peek(|half, _| *half).unwrap();
let (_buf, adc_dma) = circ_buffer.stop();
let (_adc1, _adc_ch0, _dma_ch1) = adc_dma.split();
asm::bkpt();
loop {}
}