spi-dma.rs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #![no_std]
  2. #![no_main]
  3. /**
  4. Transmits data over an SPI port using DMA
  5. */
  6. use panic_halt as _;
  7. use stm32f1xx_hal::{
  8. prelude::*,
  9. pac,
  10. spi::{Spi, Mode, Polarity, Phase},
  11. };
  12. use cortex_m_rt::entry;
  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 GPIOA 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(
  36. dp.SPI2,
  37. pins,
  38. spi_mode,
  39. 100.khz(),
  40. clocks,
  41. &mut rcc.apb1
  42. );
  43. // Set up the DMA device
  44. let dma = dp.DMA1.split(&mut rcc.ahb);
  45. // Connect the SPI device to the DMA
  46. let spi_dma = spi.with_tx_dma(dma.5);
  47. // Start a DMA transfer
  48. let transfer = spi_dma.write(b"hello, world");
  49. // Wait for it to finnish. The transfer takes ownership over the SPI device
  50. // and the data being sent anb those things are returned by transfer.wait
  51. let (_buffer, _spi_dma) = transfer.wait();
  52. loop {
  53. }
  54. }