blinky.rs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //! Blinks an LED
  2. //!
  3. //! This assumes that a LED is connected to pc13 as is the case on the blue pill board.
  4. //!
  5. //! Note: Without additional hardware, PC13 should not be used to drive an LED, see page 5.1.2 of
  6. //! the reference manaual for an explanation. This is not an issue on the blue pill.
  7. //! This example assumes that a LED is connected to PC13 which is where an LED is connected on the
  8. //! [blue_pill] board. If you have different hardware, you need to change which gpio pin is used.
  9. //!
  10. //! Note: PC13 can not source much current and should not be used to drive an LED directly.
  11. //!
  12. //! [blue_pill]: http://wiki.stm32duino.com/index.php?title=Blue_Pill
  13. #![deny(unsafe_code)]
  14. #![deny(warnings)]
  15. #![no_std]
  16. #![no_main]
  17. extern crate panic_halt;
  18. use nb::block;
  19. use stm32f1xx_hal::{
  20. prelude::*,
  21. pac,
  22. timer::Timer,
  23. };
  24. use cortex_m_rt::entry;
  25. #[entry]
  26. fn main() -> ! {
  27. // Get access to the core peripherals from the cortex-m crate
  28. let cp = cortex_m::Peripherals::take().unwrap();
  29. // Get access to the device specific peripherals from the peripheral access crate
  30. let dp = pac::Peripherals::take().unwrap();
  31. // Take ownership over the raw flash and rcc devices and convert them into the corresponding
  32. // HAL structs
  33. let mut flash = dp.FLASH.constrain();
  34. let mut rcc = dp.RCC.constrain();
  35. // Freeze the configuration of all the clocks in the system and store the frozen frequencies in
  36. // `clocks`
  37. let clocks = rcc.cfgr.freeze(&mut flash.acr);
  38. // Acquire the GPIOC peripheral
  39. let mut gpioc = dp.GPIOC.split(&mut rcc.apb2);
  40. // Configure gpio C pin 13 as a push-pull output. The `crh` register is passed to the function
  41. // in order to configure the port. For pins 0-7, crl should be passed instead.
  42. let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
  43. // Configure the syst timer to trigger an update every second
  44. let mut timer = Timer::syst(cp.SYST, 1.hz(), clocks);
  45. // Wait for the timer to trigger an update and change the state of the LED
  46. loop {
  47. block!(timer.wait()).unwrap();
  48. led.set_high();
  49. block!(timer.wait()).unwrap();
  50. led.set_low();
  51. }
  52. }