blinky_rtc.rs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 a LED, see
  6. //! section 5.1.2 of the reference manual for an explanation.
  7. //! This is not an issue on the blue pill.
  8. #![deny(unsafe_code)]
  9. #![no_std]
  10. #![no_main]
  11. use panic_halt as _;
  12. use stm32f1xx_hal::{pac, prelude::*, rtc::Rtc};
  13. use cortex_m_rt::entry;
  14. use embedded_hal::digital::v2::OutputPin;
  15. use nb::block;
  16. #[entry]
  17. fn main() -> ! {
  18. let dp = pac::Peripherals::take().unwrap();
  19. let mut pwr = dp.PWR;
  20. let mut rcc = dp.RCC.constrain();
  21. // Set up the GPIO pin
  22. let mut gpioc = dp.GPIOC.split(&mut rcc.apb2);
  23. let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
  24. // Set up the RTC
  25. // Enable writes to the backup domain
  26. let mut backup_domain = rcc.bkp.constrain(dp.BKP, &mut rcc.apb1, &mut pwr);
  27. // Start the RTC
  28. let mut rtc = Rtc::rtc(dp.RTC, &mut backup_domain);
  29. let mut led_on = false;
  30. loop {
  31. // Set the current time to 0
  32. rtc.set_time(0);
  33. // Trigger the alarm in 5 seconds
  34. rtc.set_alarm(5);
  35. block!(rtc.wait_alarm()).unwrap();
  36. if led_on {
  37. led.set_low().unwrap();
  38. led_on = false;
  39. } else {
  40. led.set_high().unwrap();
  41. led_on = true;
  42. }
  43. }
  44. }