adc-dma-circ.rs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //! ADC interface circular 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. dma::Half,
  11. };
  12. use cortex_m_rt::entry;
  13. #[entry]
  14. fn main() -> ! {
  15. // Acquire peripherals
  16. let p = pac::Peripherals::take().unwrap();
  17. let mut flash = p.FLASH.constrain();
  18. let mut rcc = p.RCC.constrain();
  19. // Configure ADC clocks
  20. // Default value is the slowest possible ADC clock: PCLK2 / 8. Meanwhile ADC
  21. // clock is configurable. So its frequency may be tweaked to meet certain
  22. // practical needs. User specified value is be approximated using supported
  23. // prescaler values 2/4/6/8.
  24. let clocks = rcc.cfgr.adcclk(2.mhz()).freeze(&mut flash.acr);
  25. let dma_ch1 = p.DMA1.split(&mut rcc.ahb).1;
  26. // Setup ADC
  27. let adc1 = adc::Adc::adc1(p.ADC1, &mut rcc.apb2, clocks);
  28. // Setup GPIOA
  29. let mut gpioa = p.GPIOA.split(&mut rcc.apb2);
  30. // Configure pa0 as an analog input
  31. let adc_ch0 = gpioa.pa0.into_analog(&mut gpioa.crl);
  32. let adc_dma = adc1.with_dma(adc_ch0, dma_ch1);
  33. let buf = singleton!(: [[u16; 8]; 2] = [[0; 8]; 2]).unwrap();
  34. let mut circ_buffer = adc_dma.circ_read(buf);
  35. while circ_buffer.readable_half().unwrap() != Half::First {}
  36. let _first_half = circ_buffer.peek(|half, _| *half).unwrap();
  37. while circ_buffer.readable_half().unwrap() != Half::Second {}
  38. let _second_half = circ_buffer.peek(|half, _| *half).unwrap();
  39. let (_buf, adc_dma) = circ_buffer.stop();
  40. let (_adc1, _adc_ch0, _dma_ch1) = adc_dma.split();
  41. asm::bkpt();
  42. loop {}
  43. }