watchdog.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //! Watchdog peripherals
  2. use crate::{
  3. hal::watchdog::{Watchdog, WatchdogEnable},
  4. pac::{DBGMCU as DBG, IWDG},
  5. time::MilliSeconds,
  6. };
  7. /// Wraps the Independent Watchdog (IWDG) peripheral
  8. pub struct IndependentWatchdog {
  9. iwdg: IWDG,
  10. }
  11. const LSI_KHZ: u32 = 40;
  12. const MAX_PR: u8 = 8;
  13. const MAX_RL: u16 = 0xFFF;
  14. const KR_ACCESS: u16 = 0x5555;
  15. const KR_RELOAD: u16 = 0xAAAA;
  16. const KR_START: u16 = 0xCCCC;
  17. impl IndependentWatchdog {
  18. /// Wrap and start the watchdog
  19. pub fn new(iwdg: IWDG) -> Self {
  20. IndependentWatchdog { iwdg }
  21. }
  22. /// Debug independent watchdog stopped when core is halted
  23. pub fn stop_on_debug(&self, dbg: &DBG, stop: bool) {
  24. dbg.cr.modify(|_, w| w.dbg_iwdg_stop().bit(stop));
  25. }
  26. fn setup(&self, timeout_ms: u32) {
  27. let mut pr = 0;
  28. while pr < MAX_PR && Self::timeout_period(pr, MAX_RL) < timeout_ms {
  29. pr += 1;
  30. }
  31. let max_period = Self::timeout_period(pr, MAX_RL);
  32. let max_rl = u32::from(MAX_RL);
  33. let rl = (timeout_ms * max_rl / max_period).min(max_rl) as u16;
  34. self.access_registers(|iwdg| {
  35. iwdg.pr.modify(|_, w| w.pr().bits(pr));
  36. iwdg.rlr.modify(|_, w| w.rl().bits(rl));
  37. });
  38. }
  39. fn is_pr_updating(&self) -> bool {
  40. self.iwdg.sr.read().pvu().bit()
  41. }
  42. /// Returns the interval in ms
  43. pub fn interval(&self) -> MilliSeconds {
  44. while self.is_pr_updating() {}
  45. let pr = self.iwdg.pr.read().pr().bits();
  46. let rl = self.iwdg.rlr.read().rl().bits();
  47. let ms = Self::timeout_period(pr, rl);
  48. MilliSeconds(ms)
  49. }
  50. /// pr: Prescaler divider bits, rl: reload value
  51. ///
  52. /// Returns ms
  53. fn timeout_period(pr: u8, rl: u16) -> u32 {
  54. let divider: u32 = match pr {
  55. 0b000 => 4,
  56. 0b001 => 8,
  57. 0b010 => 16,
  58. 0b011 => 32,
  59. 0b100 => 64,
  60. 0b101 => 128,
  61. 0b110 => 256,
  62. 0b111 => 256,
  63. _ => panic!("Invalid IWDG prescaler divider"),
  64. };
  65. (u32::from(rl) + 1) * divider / LSI_KHZ
  66. }
  67. fn access_registers<A, F: FnMut(&IWDG) -> A>(&self, mut f: F) -> A {
  68. // Unprotect write access to registers
  69. self.iwdg.kr.write(|w| unsafe { w.key().bits(KR_ACCESS) });
  70. let a = f(&self.iwdg);
  71. // Protect again
  72. self.iwdg.kr.write(|w| unsafe { w.key().bits(KR_RELOAD) });
  73. a
  74. }
  75. }
  76. impl WatchdogEnable for IndependentWatchdog {
  77. type Time = MilliSeconds;
  78. fn start<T: Into<Self::Time>>(&mut self, period: T) {
  79. self.setup(period.into().0);
  80. self.iwdg.kr.write(|w| unsafe { w.key().bits(KR_START) });
  81. }
  82. }
  83. impl Watchdog for IndependentWatchdog {
  84. fn feed(&mut self) {
  85. self.iwdg.kr.write(|w| unsafe { w.key().bits(KR_RELOAD) });
  86. }
  87. }