1
+ //! ADC interface DMA RX transfer test
2
+
3
+ #![ no_main]
4
+ #![ no_std]
5
+
6
+ use panic_halt as _;
7
+
8
+ use cortex_m:: { asm, singleton} ;
9
+
10
+ use stm32f1xx_hal:: {
11
+ prelude:: * ,
12
+ pac,
13
+ adc,
14
+ } ;
15
+ use cortex_m_rt:: entry;
16
+
17
+ #[ entry]
18
+ fn main ( ) -> ! {
19
+ // Aquire peripherals
20
+ let p = pac:: Peripherals :: take ( ) . unwrap ( ) ;
21
+ let mut flash = p. FLASH . constrain ( ) ;
22
+ let mut rcc = p. RCC . constrain ( ) ;
23
+
24
+ // Configure ADC clocks
25
+ // Default value is the slowest possible ADC clock: PCLK2 / 8. Meanwhile ADC
26
+ // clock is configurable. So its frequency may be tweaked to meet certain
27
+ // practical needs. User specified value is be approximated using supported
28
+ // prescaler values 2/4/6/8.
29
+ let clocks = rcc. cfgr . adcclk ( 2 . mhz ( ) ) . freeze ( & mut flash. acr ) ;
30
+
31
+ let dma_ch1 = p. DMA1 . split ( & mut rcc. ahb ) . 1 ;
32
+
33
+ // Setup ADC
34
+ let adc1 = adc:: Adc :: adc1 ( p. ADC1 , & mut rcc. apb2 , clocks) ;
35
+
36
+ // Setup GPIOA
37
+ let mut gpioa = p. GPIOA . split ( & mut rcc. apb2 ) ;
38
+
39
+ // Configure pa0 as an analog input
40
+ let adc_ch0 = gpioa. pa0 . into_analog ( & mut gpioa. crl ) ;
41
+
42
+ let adc_dma = adc1. with_dma ( adc_ch0, dma_ch1) ;
43
+ let buf = singleton ! ( : [ u16 ; 8 ] = [ 0 ; 8 ] ) . unwrap ( ) ;
44
+
45
+ // The read method consumes the buf and self, starts the adc and dma transfer and returns a
46
+ // RxDma struct. The wait method consumes the RxDma struct, waits for the whole transfer to be
47
+ // completed and then returns the updated buf and underlying adc_dma struct. For non blocking,
48
+ // one can call the is_done method of RxDma and only call wait after that method returns true.
49
+ let ( _buf, adc_dma) = adc_dma. read ( buf) . wait ( ) ;
50
+ asm:: bkpt ( ) ;
51
+
52
+ // Consumes the AdcDma struct, restores adc configuration to previous state and returns the
53
+ // Adc struct in normal mode.
54
+ let ( _adc1, _adc_ch0, _dma_ch1) = adc_dma. split ( ) ;
55
+ asm:: bkpt ( ) ;
56
+
57
+ loop { }
58
+ }
0 commit comments