backup_domain.rs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*!
  2. Registers that are not reset as long as Vbat or Vdd has power.
  3. The registers retain their values during wakes from standby mode or system resets. They also
  4. retain their value when Vdd is switched off as long as V_BAT is powered.
  5. The backup domain also contains tamper protection and writes to it must be enabled in order
  6. to use the real time clock (RTC).
  7. Write access to the backup domain is enabled in RCC using the `rcc::Rcc::BKP::constrain()`
  8. function.
  9. Only the RTC functionality is currently implemented.
  10. */
  11. use crate::pac::BKP;
  12. /**
  13. The existence of this struct indicates that writing to the the backup
  14. domain has been enabled. It is acquired by calling `constrain` on `rcc::Rcc::BKP`
  15. */
  16. pub struct BackupDomain {
  17. pub(crate) _regs: BKP,
  18. }
  19. macro_rules! write_drx {
  20. ($self:ident, $drx:ident, $idx:expr, $new:expr) => {
  21. $self._regs.$drx[$idx].write(|w| w.d().bits($new));
  22. };
  23. }
  24. macro_rules! read_drx {
  25. ($self:ident, $drx:ident, $idx:expr) => {
  26. $self._regs.$drx[$idx].read().d().bits();
  27. };
  28. }
  29. impl BackupDomain {
  30. /// Read a 16-bit value from one of the DR1 to DR10 registers part of the
  31. /// Backup Data Register. The register argument is a zero based index to the
  32. /// DRx registers: 0 is DR1, up to 9 for DR10. Providing a number above 9
  33. /// will panic.
  34. pub fn read_data_register_low(&self, register: usize) -> u16 {
  35. read_drx!(self, dr, register)
  36. }
  37. /// Read a 16-bit value from one of the DR11 to DR42 registers part of the
  38. /// Backup Data Register. The register argument is a zero based index to the
  39. /// DRx registers: 0 is DR11, up to 31 for DR42. Providing a number above 31
  40. /// will panic.
  41. /// NOTE: not available on medium- and low-density devices!
  42. #[cfg(any(feature = "high", feature = "connectivity"))]
  43. pub fn read_data_register_high(&self, register: usize) -> u16 {
  44. read_drx!(self, bkp_dr, register)
  45. }
  46. /// Write a 16-bit value to one of the DR1 to DR10 registers part of the
  47. /// Backup Data Register. The register argument is a zero based index to the
  48. /// DRx registers: 0 is DR1, up to 9 for DR10. Providing a number above 9
  49. /// will panic.
  50. pub fn write_data_register_low(&self, register: usize, data: u16) {
  51. write_drx!(self, dr, register, data)
  52. }
  53. /// Write a 16-bit value to one of the DR11 to DR42 registers part of the
  54. /// Backup Data Register. The register argument is a zero based index to the
  55. /// DRx registers: 0 is DR11, up to 31 for DR42. Providing a number above 31
  56. /// will panic.
  57. /// NOTE: not available on medium- and low-density devices!
  58. #[cfg(any(feature = "high", feature = "connectivity"))]
  59. pub fn write_data_register_high(&self, register: usize, data: u16) {
  60. write_drx!(self, bkp_dr, register, data)
  61. }
  62. }