blinky_generic.rs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //! Blinks several LEDs stored in an array
  2. #![deny(unsafe_code)]
  3. #![no_std]
  4. #![no_main]
  5. use panic_halt as _;
  6. use nb::block;
  7. use cortex_m_rt::entry;
  8. use embedded_hal::digital::v2::OutputPin;
  9. use stm32f1xx_hal::{pac, prelude::*, timer::Timer};
  10. #[entry]
  11. fn main() -> ! {
  12. let cp = cortex_m::Peripherals::take().unwrap();
  13. let dp = pac::Peripherals::take().unwrap();
  14. let mut flash = dp.FLASH.constrain();
  15. let mut rcc = dp.RCC.constrain();
  16. let clocks = rcc.cfgr.freeze(&mut flash.acr);
  17. // Acquire the GPIO peripherals
  18. let mut gpioa = dp.GPIOA.split(&mut rcc.apb2);
  19. let mut gpioc = dp.GPIOC.split(&mut rcc.apb2);
  20. // Configure the syst timer to trigger an update every second
  21. let mut timer = Timer::syst(cp.SYST, &clocks).start_count_down(1.hz());
  22. // Create an array of LEDS to blink
  23. let mut leds = [
  24. gpioc.pc13.into_push_pull_output(&mut gpioc.crh).downgrade(),
  25. gpioa.pa1.into_push_pull_output(&mut gpioa.crl).downgrade(),
  26. ];
  27. // Wait for the timer to trigger an update and change the state of the LED
  28. loop {
  29. block!(timer.wait()).unwrap();
  30. for led in leds.iter_mut() {
  31. led.set_high().unwrap();
  32. }
  33. block!(timer.wait()).unwrap();
  34. for led in leds.iter_mut() {
  35. led.set_low().unwrap();
  36. }
  37. }
  38. }