blinky.rs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //! Blinks an LED
  2. #![deny(unsafe_code)]
  3. #![deny(warnings)]
  4. #![no_std]
  5. #![no_main]
  6. extern crate cortex_m;
  7. extern crate cortex_m_rt as rt;
  8. extern crate panic_semihosting;
  9. extern crate stm32f1xx_hal as hal;
  10. #[macro_use(block)]
  11. extern crate nb;
  12. use hal::prelude::*;
  13. use hal::stm32f103xx;
  14. use hal::timer::Timer;
  15. use rt::{entry, exception, ExceptionFrame};
  16. #[entry]
  17. fn main() -> ! {
  18. let cp = cortex_m::Peripherals::take().unwrap();
  19. let dp = stm32f103xx::Peripherals::take().unwrap();
  20. let mut flash = dp.FLASH.constrain();
  21. let mut rcc = dp.RCC.constrain();
  22. // Try a different clock configuration
  23. let clocks = rcc.cfgr.freeze(&mut flash.acr);
  24. // let clocks = rcc.cfgr
  25. // .sysclk(64.mhz())
  26. // .pclk1(32.mhz())
  27. // .freeze(&mut flash.acr);
  28. let mut gpioc = dp.GPIOC.split(&mut rcc.apb2);
  29. let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
  30. // Try a different timer (even SYST)
  31. let mut timer = Timer::syst(cp.SYST, 1.hz(), clocks);
  32. loop {
  33. block!(timer.wait()).unwrap();
  34. led.set_high();
  35. block!(timer.wait()).unwrap();
  36. led.set_low();
  37. }
  38. }
  39. #[exception]
  40. fn HardFault(ef: &ExceptionFrame) -> ! {
  41. panic!("{:#?}", ef);
  42. }
  43. #[exception]
  44. fn DefaultHandler(irqn: i16) {
  45. panic!("Unhandled exception (IRQn = {})", irqn);
  46. }