forked from stm32-rs/stm32f1xx-hal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spi-dma.rs
59 lines (46 loc) · 1.6 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
56
57
58
59
#![no_std]
#![no_main]
/**
Transmits data over an SPI port using DMA
*/
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 mut gpiob = dp.GPIOB.split();
let pins = (
gpiob.pb13.into_alternate_push_pull(&mut gpiob.crh),
gpiob.pb14.into_floating_input(&mut gpiob.crh),
gpiob.pb15.into_alternate_push_pull(&mut gpiob.crh),
);
let spi_mode = Mode {
polarity: Polarity::IdleLow,
phase: Phase::CaptureOnFirstTransition,
};
let spi = Spi::spi2(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 {}
}