adc-dma-rx.rs 1.8 KB

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