mpu9250.rs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //! Interfacing the MPU9250
  2. #![deny(unsafe_code)]
  3. #![deny(warnings)]
  4. #![no_main]
  5. #![no_std]
  6. extern crate cortex_m;
  7. extern crate cortex_m_rt as rt;
  8. extern crate mpu9250;
  9. extern crate panic_semihosting;
  10. extern crate stm32f1xx_hal as hal;
  11. use cortex_m::asm;
  12. use hal::delay::Delay;
  13. use hal::prelude::*;
  14. use hal::spi::Spi;
  15. use hal::stm32f103xx;
  16. use mpu9250::Mpu9250;
  17. use rt::{entry, exception, ExceptionFrame};
  18. #[entry]
  19. fn main() -> ! {
  20. let cp = cortex_m::Peripherals::take().unwrap();
  21. let dp = stm32f103xx::Peripherals::take().unwrap();
  22. let mut flash = dp.FLASH.constrain();
  23. let mut rcc = dp.RCC.constrain();
  24. let clocks = rcc.cfgr.freeze(&mut flash.acr);
  25. let mut afio = dp.AFIO.constrain(&mut rcc.apb2);
  26. let mut gpioa = dp.GPIOA.split(&mut rcc.apb2);
  27. // let mut gpiob = dp.GPIOB.split(&mut rcc.apb2);
  28. let nss = gpioa.pa4.into_push_pull_output(&mut gpioa.crl);
  29. // SPI1
  30. let sck = gpioa.pa5.into_alternate_push_pull(&mut gpioa.crl);
  31. let miso = gpioa.pa6;
  32. let mosi = gpioa.pa7.into_alternate_push_pull(&mut gpioa.crl);
  33. // SPI2
  34. // let sck = gpiob.pb13.into_alternate_push_pull(&mut gpiob.crh);
  35. // let miso = gpiob.pb14;
  36. // let mosi = gpiob.pb15.into_alternate_push_pull(&mut gpiob.crh);
  37. let spi = Spi::spi1(
  38. dp.SPI1,
  39. (sck, miso, mosi),
  40. &mut afio.mapr,
  41. mpu9250::MODE,
  42. 1.mhz(),
  43. clocks,
  44. &mut rcc.apb2,
  45. );
  46. let mut delay = Delay::new(cp.SYST, clocks);
  47. let mut mpu9250 = Mpu9250::marg(spi, nss, &mut delay).unwrap();
  48. // sanity checks
  49. assert_eq!(mpu9250.who_am_i().unwrap(), 0x71);
  50. assert_eq!(mpu9250.ak8963_who_am_i().unwrap(), 0x48);
  51. let _a = mpu9250.all().unwrap();
  52. asm::bkpt();
  53. loop {}
  54. }
  55. #[exception]
  56. fn HardFault(ef: &ExceptionFrame) -> ! {
  57. panic!("{:#?}", ef);
  58. }
  59. #[exception]
  60. fn DefaultHandler(irqn: i16) {
  61. panic!("Unhandled exception (IRQn = {})", irqn);
  62. }