//! Interfacing the on-board L3GD20 (gyroscope) #![deny(unsafe_code)] // #![deny(warnings)] #![no_main] #![no_std] #[macro_use(entry, exception)] extern crate cortex_m_rt as rt; extern crate cortex_m; extern crate embedded_hal as ehal; extern crate panic_semihosting; extern crate stm32wb_hal as hal; use crate::ehal::spi::{Mode, Phase, Polarity}; use crate::hal::prelude::*; use crate::hal::spi::Spi; use crate::rt::ExceptionFrame; use cortex_m::asm; /// SPI mode pub const MODE: Mode = Mode { phase: Phase::CaptureOnFirstTransition, polarity: Polarity::IdleLow, }; #[entry] fn main() -> ! { let p = hal::stm32::Peripherals::take().unwrap(); let mut flash = p.FLASH.constrain(); let mut rcc = p.RCC.constrain(); // TRY the other clock configuration // let clocks = rcc.cfgr.freeze(&mut flash.acr); let clocks = rcc .cfgr .sysclk(80.mhz()) .pclk1(80.mhz()) .pclk2(80.mhz()) .freeze(&mut flash.acr); let mut gpioa = p.GPIOA.split(&mut rcc.ahb2); let mut gpiob = p.GPIOB.split(&mut rcc.ahb2); // let mut nss = gpiob // .pb0 // .into_push_pull_output(&mut gpiob.moder, &mut gpiob.otyper); let mut dc = gpiob .pb1 .into_push_pull_output(&mut gpiob.moder, &mut gpiob.otyper); // The `L3gd20` abstraction exposed by the `f3` crate requires a specific pin configuration to // be used and won't accept any configuration other than the one used here. Trying to use a // different pin configuration will result in a compiler error. let sck = gpioa.pa5.into_af5(&mut gpioa.moder, &mut gpioa.afrl); let miso = gpioa.pa6.into_af5(&mut gpioa.moder, &mut gpioa.afrl); let mosi = gpioa.pa7.into_af5(&mut gpioa.moder, &mut gpioa.afrl); // nss.set_high(); dc.set_low(); let mut spi = Spi::spi1( p.SPI1, (sck, miso, mosi), MODE, // 1.mhz(), 100.khz(), clocks, &mut rcc.apb2, ); // nss.set_low(); let data = [0x3C]; spi.write(&data).unwrap(); spi.write(&data).unwrap(); spi.write(&data).unwrap(); // nss.set_high(); // when you reach this breakpoint you'll be able to inspect the variable `_m` which contains the // gyroscope and the temperature sensor readings asm::bkpt(); loop {} } #[exception] fn HardFault(ef: &ExceptionFrame) -> ! { panic!("{:#?}", ef); }