blinky_rtc.rs 1.4 KB

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