-
Notifications
You must be signed in to change notification settings - Fork 67
/
spi.rs
62 lines (50 loc) · 1.8 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
//! Example of configuring spi.
//! Target board: STM32F3DISCOVERY
#![no_std]
#![no_main]
use panic_semihosting as _;
use stm32f3xx_hal as hal;
use cortex_m::asm;
use cortex_m_rt::entry;
use hal::pac;
use hal::prelude::*;
use hal::spi::Spi;
#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();
let mut flash = dp.FLASH.constrain();
let mut rcc = dp.RCC.constrain();
let mut gpioc = dp.GPIOC.split(&mut rcc.ahb);
let clocks = rcc
.cfgr
.use_hse(8.MHz())
.sysclk(48.MHz())
.pclk1(24.MHz())
.freeze(&mut flash.acr);
// Configure pins for SPI
let sck = gpioc
.pc10
.into_af_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
let miso = gpioc
.pc11
.into_af_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
let mosi = gpioc
.pc12
.into_af_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
let mut spi = Spi::new(dp.SPI3, (sck, miso, mosi), 3.MHz(), clocks, &mut rcc.apb1);
// Create an `u8` array, which can be transfered via SPI.
let msg_send: [u8; 8] = [0xD, 0xE, 0xA, 0xD, 0xB, 0xE, 0xE, 0xF];
// Copy the array, as it would be mutually shared in `transfer` while simultaneously would be
// immutable shared in `assert_eq`.
let mut msg_sending = msg_send;
// Transfer the content of the array via SPI and receive it's output.
// When MOSI and MISO pins are connected together, `msg_received` should receive the content.
// from `msg_sending`
let msg_received = spi.transfer(&mut msg_sending).unwrap();
// Check, if msg_send and msg_received are identical.
// This succeeds, when master and slave of the SPI are connected.
assert_eq!(msg_send, msg_received);
loop {
asm::wfi();
}
}