diff --git a/examples/spi_slave.rs b/examples/spi_slave.rs new file mode 100644 index 00000000..0e1ab605 --- /dev/null +++ b/examples/spi_slave.rs @@ -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; + } +}