spi-dma.rs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #![no_std]
  2. #![no_main]
  3. /**
  4. Transmits data over an SPI port using DMA
  5. */
  6. use panic_halt as _;
  7. use cortex_m_rt::entry;
  8. use stm32f1xx_hal::{
  9. pac,
  10. prelude::*,
  11. spi::{Mode, Phase, Polarity, Spi},
  12. };
  13. #[entry]
  14. fn main() -> ! {
  15. // Get access to the device specific peripherals from the peripheral access crate
  16. let dp = pac::Peripherals::take().unwrap();
  17. // Take ownership over the raw flash and rcc devices and convert them into the corresponding
  18. // HAL structs
  19. let mut flash = dp.FLASH.constrain();
  20. let mut rcc = dp.RCC.constrain();
  21. // Freeze the configuration of all the clocks in the system and store the frozen frequencies in
  22. // `clocks`
  23. let clocks = rcc.cfgr.freeze(&mut flash.acr);
  24. // Acquire the GPIOB peripheral
  25. let mut gpiob = dp.GPIOB.split(&mut rcc.apb2);
  26. let pins = (
  27. gpiob.pb13.into_alternate_push_pull(&mut gpiob.crh),
  28. gpiob.pb14.into_floating_input(&mut gpiob.crh),
  29. gpiob.pb15.into_alternate_push_pull(&mut gpiob.crh),
  30. );
  31. let spi_mode = Mode {
  32. polarity: Polarity::IdleLow,
  33. phase: Phase::CaptureOnFirstTransition,
  34. };
  35. let spi = Spi::spi2(dp.SPI2, pins, spi_mode, 100.khz(), clocks, &mut rcc.apb1);
  36. // Set up the DMA device
  37. let dma = dp.DMA1.split(&mut rcc.ahb);
  38. // Connect the SPI device to the DMA
  39. let spi_dma = spi.with_tx_dma(dma.5);
  40. // Start a DMA transfer
  41. let transfer = spi_dma.write(b"hello, world");
  42. // Wait for it to finnish. The transfer takes ownership over the SPI device
  43. // and the data being sent anb those things are returned by transfer.wait
  44. let (_buffer, _spi_dma) = transfer.wait();
  45. loop {}
  46. }