blinky.rs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 manual for an explanation. This is not an issue on the blue pill.
  7. #![deny(unsafe_code)]
  8. #![no_std]
  9. #![no_main]
  10. use panic_halt as _;
  11. use nb::block;
  12. use stm32f1xx_hal::{
  13. prelude::*,
  14. pac,
  15. timer::Timer,
  16. };
  17. use cortex_m_rt::entry;
  18. use embedded_hal::digital::v2::OutputPin;
  19. #[entry]
  20. fn main() -> ! {
  21. // Get access to the core peripherals from the cortex-m crate
  22. let cp = cortex_m::Peripherals::take().unwrap();
  23. // Get access to the device specific peripherals from the peripheral access crate
  24. let dp = pac::Peripherals::take().unwrap();
  25. // Take ownership over the raw flash and rcc devices and convert them into the corresponding
  26. // HAL structs
  27. let mut flash = dp.FLASH.constrain();
  28. let mut rcc = dp.RCC.constrain();
  29. // Freeze the configuration of all the clocks in the system and store the frozen frequencies in
  30. // `clocks`
  31. let clocks = rcc.cfgr.freeze(&mut flash.acr);
  32. // Acquire the GPIOC peripheral
  33. let mut gpioc = dp.GPIOC.split(&mut rcc.apb2);
  34. // Configure gpio C pin 13 as a push-pull output. The `crh` register is passed to the function
  35. // in order to configure the port. For pins 0-7, crl should be passed instead.
  36. let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
  37. // Configure the syst timer to trigger an update every second
  38. let mut timer = Timer::syst(cp.SYST, &clocks).start_count_down(1.hz());
  39. // Wait for the timer to trigger an update and change the state of the LED
  40. loop {
  41. block!(timer.wait()).unwrap();
  42. led.set_high().unwrap();
  43. block!(timer.wait()).unwrap();
  44. led.set_low().unwrap();
  45. }
  46. }