blinky.rs 1.8 KB

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