Skip to content

Commit 9f08026

Browse files
mjepronkTheZoq2
authored andcommitted
Add support for reading and writing to the Backup Data Register. (#86)
* Adds support for reading and writing to the Backup Data Register (#83). * Removed `unsafe` blocks. * Added feature gate for high backup registers.
1 parent 33d175c commit 9f08026

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1515
- Update existing ADC example according to ADC API changes
1616
- Add new ADC example to read ambient temperature using ADC1 CH16
1717
- Add `listen` and `unlisten` to `serial::Tx` and `serial::Rx`.
18-
18+
- Add methods `read_data_register` and `write_data_register` to
19+
`backup_domain::BackupDomain`, which allow read and write access to the Backup
20+
Data Register.
1921

2022
### Breaking changes
2123

src/backup_domain.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,53 @@ use crate::pac::BKP;
2222
pub struct BackupDomain {
2323
pub(crate) _regs: BKP,
2424
}
25+
26+
macro_rules! write_drx {
27+
($self:ident, $drx:ident, $idx:expr, $new:expr) => {
28+
$self._regs.$drx[$idx].write(|w| w.d().bits($new));
29+
};
30+
}
31+
32+
macro_rules! read_drx {
33+
($self:ident, $drx:ident, $idx:expr) => {
34+
$self._regs.$drx[$idx].read().d().bits();
35+
};
36+
}
37+
38+
impl 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(feature = "high")]
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(feature = "high")]
71+
pub fn write_data_register_high(&self, register: usize, data: u16) {
72+
write_drx!(self, bkp_dr, register, data)
73+
}
74+
}

0 commit comments

Comments
 (0)