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