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