blinky_rtc.rs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //! Blinks an LED using the real time clock to time the blinks
  2. #![deny(unsafe_code)]
  3. #![deny(warnings)]
  4. #![no_std]
  5. #![no_main]
  6. extern crate panic_halt;
  7. use stm32f1xx_hal::{
  8. prelude::*,
  9. pac,
  10. rtc::Rtc,
  11. };
  12. use nb::block;
  13. use cortex_m_rt::entry;
  14. #[entry]
  15. fn main() -> ! {
  16. let dp = pac::Peripherals::take().unwrap();
  17. let mut pwr = dp.PWR;
  18. let mut rcc = dp.RCC.constrain();
  19. // Set up the GPIO pin
  20. let mut gpioc = dp.GPIOC.split(&mut rcc.apb2);
  21. let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
  22. // Set up the RTC
  23. // Enable writes to the backup domain
  24. let mut backup_domain = rcc.bkp.constrain(dp.BKP, &mut rcc.apb1, &mut pwr);
  25. // Start the RTC
  26. let mut rtc = Rtc::rtc(dp.RTC, &mut backup_domain);
  27. let mut led_on = false;
  28. loop {
  29. // Set the current time to 0
  30. rtc.set_seconds(0);
  31. // Trigger the alarm in 5 seconds
  32. rtc.set_alarm(5);
  33. block!(rtc.wait_alarm()).unwrap();
  34. if led_on {
  35. led.set_low();
  36. led_on = false;
  37. }
  38. else {
  39. led.set_high();
  40. led_on = true;
  41. }
  42. }
  43. }