Skip to content

Commit abe8d56

Browse files
Add a basic spi example
1 parent bc723e1 commit abe8d56

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

examples/serial_spi_bridge.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#![no_main]
2+
#![no_std]
3+
4+
#[allow(unused)]
5+
use panic_halt;
6+
7+
use stm32f0xx_hal as hal;
8+
9+
use crate::hal::prelude::*;
10+
use crate::hal::serial::Serial;
11+
use crate::hal::spi::Spi;
12+
use crate::hal::spi::{Mode, Phase, Polarity};
13+
use crate::hal::stm32;
14+
15+
use nb::block;
16+
17+
use cortex_m_rt::entry;
18+
19+
/// A basic serial to spi example
20+
///
21+
/// If you connect MOSI & MISO pins together, you'll see all characters
22+
/// that you typed in your serial terminal echoed back
23+
///
24+
/// If you connect MISO to GND, you'll see nothing coming back
25+
#[entry]
26+
fn main() -> ! {
27+
const MODE: Mode = Mode {
28+
polarity: Polarity::IdleHigh,
29+
phase: Phase::CaptureOnSecondTransition,
30+
};
31+
32+
if let Some(p) = stm32::Peripherals::take() {
33+
let rcc = p.RCC.constrain();
34+
let clocks = rcc.cfgr.freeze();
35+
let gpioa = p.GPIOA.split();
36+
37+
// Configure pins for SPI
38+
let sck = gpioa.pa5.into_alternate_af0();
39+
let miso = gpioa.pa6.into_alternate_af0();
40+
let mosi = gpioa.pa7.into_alternate_af0();
41+
42+
// Configure SPI with 1MHz rate
43+
let mut spi = Spi::spi1(p.SPI1, (sck, miso, mosi), MODE, 1.mhz(), clocks);
44+
45+
let tx = gpioa.pa9.into_alternate_af1();
46+
let rx = gpioa.pa10.into_alternate_af1();
47+
48+
let serial = Serial::usart1(p.USART1, (tx, rx), 115_200.bps(), clocks);
49+
50+
let (mut tx, mut rx) = serial.split();
51+
52+
loop {
53+
let serial_received = block!(rx.read()).unwrap();
54+
55+
block!(spi.send(serial_received)).ok();
56+
57+
let spi_received = block!(spi.read()).unwrap();
58+
59+
block!(tx.write(spi_received)).ok();
60+
}
61+
}
62+
63+
loop {
64+
continue;
65+
}
66+
}

0 commit comments

Comments
 (0)