Skip to content

Commit

Permalink
spi_slave example
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Apr 20, 2023
1 parent 1178ec4 commit 8f0dc46
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions examples/spi_slave.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#![no_main]
#![no_std]

use panic_halt as _;

use crate::hal::spi::{Mode, Phase, Polarity, SpiSlave};
use crate::hal::{pac, prelude::*};
use cortex_m::asm;
use cortex_m_rt::entry;
use stm32f4xx_hal as hal;

/// SPI mode
pub const MODE: Mode = Mode {
phase: Phase::CaptureOnFirstTransition,
polarity: Polarity::IdleLow,
};

#[entry]
fn main() -> ! {
let p = pac::Peripherals::take().unwrap();

let rcc = p.RCC.constrain();
let _clocks = rcc.cfgr.freeze();

let gpioa = p.GPIOA.split();

let sck = gpioa.pa5;
let miso = gpioa.pa6;
let mosi = gpioa.pa7;

// clock speed is determined by the master
let mut spi = SpiSlave::new(p.SPI1, (sck, miso, mosi, Some(gpioa.pa4.into())), MODE);
// alternativelly you could use software `chip select`
// let mut spi = SpiSlave::new(p.SPI1, (sck, miso, mosi, None), MODE);
// spi.set_internal_nss(false);

let mut data = [0x1];
// this will block until the master starts the clock
spi.transfer_in_place(&mut data).unwrap();

// when you reach this breakpoint you'll be able to inspect the variable `data` which contains the
// data sent by the master
asm::bkpt();

loop {
continue;
}
}

0 comments on commit 8f0dc46

Please sign in to comment.