device.rs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //! Using a device crate
  2. //!
  3. //! Crates generated using [`svd2rust`] are referred to as device crates. These crates provide an
  4. //! API to access the peripherals of a device.
  5. //!
  6. //! [`svd2rust`]: https://crates.io/crates/svd2rust
  7. //!
  8. //! This example depends on the [`stm32f3`] crate so you'll have to
  9. //! uncomment it in your Cargo.toml.
  10. //!
  11. //! [`stm32f3`]: https://crates.io/crates/stm32f3
  12. //!
  13. //! ```
  14. //! $ edit Cargo.toml && tail $_
  15. //! [dependencies.stm32f3]
  16. //! features = ["stm32f303", "rt"]
  17. //! version = "0.7.1"
  18. //! ```
  19. //!
  20. //! You also need to set the build target to thumbv7em-none-eabihf,
  21. //! typically by editing `.cargo/config` and uncommenting the relevant target line.
  22. //!
  23. //! ---
  24. #![no_main]
  25. #![no_std]
  26. #[allow(unused_extern_crates)]
  27. use panic_halt as _;
  28. use cortex_m::peripheral::syst::SystClkSource;
  29. use cortex_m_rt::entry;
  30. use cortex_m_semihosting::hprint;
  31. use stm32f3::stm32f303::{interrupt, Interrupt, NVIC};
  32. #[entry]
  33. fn main() -> ! {
  34. let p = cortex_m::Peripherals::take().unwrap();
  35. let mut syst = p.SYST;
  36. let mut nvic = p.NVIC;
  37. nvic.enable(Interrupt::EXTI0);
  38. // configure the system timer to wrap around every second
  39. syst.set_clock_source(SystClkSource::Core);
  40. syst.set_reload(8_000_000); // 1s
  41. syst.enable_counter();
  42. loop {
  43. // busy wait until the timer wraps around
  44. while !syst.has_wrapped() {}
  45. // trigger the `EXTI0` interrupt
  46. NVIC::pend(Interrupt::EXTI0);
  47. }
  48. }
  49. #[interrupt]
  50. fn EXTI0() {
  51. hprint!(".").unwrap();
  52. }