blinky_generic.rs 1.3 KB

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