diff options
Diffstat (limited to 'examples/stm32f4/src/bin/spi.rs')
| -rw-r--r-- | examples/stm32f4/src/bin/spi.rs | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/examples/stm32f4/src/bin/spi.rs b/examples/stm32f4/src/bin/spi.rs new file mode 100644 index 000000000..af0d57412 --- /dev/null +++ b/examples/stm32f4/src/bin/spi.rs | |||
| @@ -0,0 +1,71 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(trait_alias)] | ||
| 4 | #![feature(min_type_alias_impl_trait)] | ||
| 5 | #![feature(impl_trait_in_bindings)] | ||
| 6 | #![feature(type_alias_impl_trait)] | ||
| 7 | #![allow(incomplete_features)] | ||
| 8 | |||
| 9 | #[path = "../example_common.rs"] | ||
| 10 | mod example_common; | ||
| 11 | |||
| 12 | use embassy_stm32::gpio::{Level, Output}; | ||
| 13 | use embedded_hal::digital::v2::OutputPin; | ||
| 14 | use example_common::*; | ||
| 15 | |||
| 16 | use cortex_m_rt::entry; | ||
| 17 | use embassy_stm32::spi::{Config, Spi}; | ||
| 18 | use embassy_stm32::time::Hertz; | ||
| 19 | use embedded_hal::blocking::spi::Transfer; | ||
| 20 | use stm32f4::stm32f429 as pac; | ||
| 21 | |||
| 22 | #[entry] | ||
| 23 | fn main() -> ! { | ||
| 24 | info!("Hello World, dude!"); | ||
| 25 | |||
| 26 | let pp = pac::Peripherals::take().unwrap(); | ||
| 27 | |||
| 28 | pp.DBGMCU.cr.modify(|_, w| { | ||
| 29 | w.dbg_sleep().set_bit(); | ||
| 30 | w.dbg_standby().set_bit(); | ||
| 31 | w.dbg_stop().set_bit() | ||
| 32 | }); | ||
| 33 | pp.RCC.ahb1enr.modify(|_, w| w.dma1en().set_bit()); | ||
| 34 | |||
| 35 | pp.RCC.apb1enr.modify(|_, w| { | ||
| 36 | w.spi3en().enabled(); | ||
| 37 | w | ||
| 38 | }); | ||
| 39 | |||
| 40 | pp.RCC.ahb1enr.modify(|_, w| { | ||
| 41 | w.gpioaen().enabled(); | ||
| 42 | w.gpioben().enabled(); | ||
| 43 | w.gpiocen().enabled(); | ||
| 44 | w.gpioden().enabled(); | ||
| 45 | w.gpioeen().enabled(); | ||
| 46 | w.gpiofen().enabled(); | ||
| 47 | w | ||
| 48 | }); | ||
| 49 | |||
| 50 | let p = embassy_stm32::init(Default::default()); | ||
| 51 | |||
| 52 | let mut spi = Spi::new( | ||
| 53 | Hertz(16_000_000), | ||
| 54 | p.SPI3, | ||
| 55 | p.PC10, | ||
| 56 | p.PC12, | ||
| 57 | p.PC11, | ||
| 58 | Hertz(1_000_000), | ||
| 59 | Config::default(), | ||
| 60 | ); | ||
| 61 | |||
| 62 | let mut cs = Output::new(p.PE0, Level::High); | ||
| 63 | |||
| 64 | loop { | ||
| 65 | let mut buf = [0x0A; 4]; | ||
| 66 | unwrap!(cs.set_low()); | ||
| 67 | unwrap!(spi.transfer(&mut buf)); | ||
| 68 | unwrap!(cs.set_high()); | ||
| 69 | info!("xfer {=[u8]:x}", buf); | ||
| 70 | } | ||
| 71 | } | ||
