adc-dma-rx.rs 1.8 KB

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