forked from stm32-rs/stm32h7xx-hal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spi.rs
62 lines (50 loc) · 1.43 KB
/
spi.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
60
61
62
#![deny(warnings)]
#![no_main]
#![no_std]
use cortex_m_rt::entry;
#[macro_use]
mod utilities;
use stm32h7xx_hal::{pac, prelude::*, spi};
use log::info;
use nb::block;
#[entry]
fn main() -> ! {
utilities::logger::init();
let dp = pac::Peripherals::take().unwrap();
// Constrain and Freeze power
info!("Setup PWR... ");
let pwr = dp.PWR.constrain();
let pwrcfg = example_power!(pwr).freeze();
// Constrain and Freeze clock
info!("Setup RCC... ");
let rcc = dp.RCC.constrain();
let ccdr = rcc
.sys_ck(96.MHz())
.pll1_q_ck(48.MHz())
.freeze(pwrcfg, &dp.SYSCFG);
// Acquire the GPIOC peripheral. This also enables the clock for
// GPIOC in the RCC register.
let gpioc = dp.GPIOC.split(ccdr.peripheral.GPIOC);
let sck = gpioc.pc10.into_alternate();
let miso = gpioc.pc11.into_alternate();
let mosi = gpioc.pc12.into_alternate();
info!("");
info!("stm32h7xx-hal example - SPI");
info!("");
// Initialise the SPI peripheral.
let mut spi = dp.SPI3.spi(
(sck, miso, mosi),
spi::MODE_0,
3.MHz(),
ccdr.peripheral.SPI3,
&ccdr.clocks,
);
// Write fixed data
spi.write(&[0x11u8, 0x22, 0x33]).unwrap();
// Echo what is received on the SPI
let mut received = 0;
loop {
block!(spi.send(received)).ok();
received = block!(spi.read()).unwrap();
}
}