Skip to content

Commit

Permalink
Add support for reading and writing to the Backup Data Register. (#86)
Browse files Browse the repository at this point in the history
* Adds support for reading and writing to the Backup Data Register (#83).

* Removed `unsafe` blocks.

* Added feature gate for high backup registers.
  • Loading branch information
mjepronk authored and TheZoq2 committed Aug 9, 2019
1 parent 33d175c commit 9f08026
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Update existing ADC example according to ADC API changes
- Add new ADC example to read ambient temperature using ADC1 CH16
- Add `listen` and `unlisten` to `serial::Tx` and `serial::Rx`.

- Add methods `read_data_register` and `write_data_register` to
`backup_domain::BackupDomain`, which allow read and write access to the Backup
Data Register.

### Breaking changes

Expand Down
50 changes: 50 additions & 0 deletions src/backup_domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,53 @@ use crate::pac::BKP;
pub struct BackupDomain {
pub(crate) _regs: BKP,
}

macro_rules! write_drx {
($self:ident, $drx:ident, $idx:expr, $new:expr) => {
$self._regs.$drx[$idx].write(|w| w.d().bits($new));
};
}

macro_rules! read_drx {
($self:ident, $drx:ident, $idx:expr) => {
$self._regs.$drx[$idx].read().d().bits();
};
}

impl BackupDomain {
/// Read a 16-bit value from one of the DR1 to DR10 registers part of the
/// Backup Data Register. The register argument is a zero based index to the
/// DRx registers: 0 is DR1, up to 9 for DR10. Providing a number above 9
/// will panic.
pub fn read_data_register_low(&self, register: usize) -> u16 {
read_drx!(self, dr, register)
}

/// Read a 16-bit value from one of the DR11 to DR42 registers part of the
/// Backup Data Register. The register argument is a zero based index to the
/// DRx registers: 0 is DR11, up to 31 for DR42. Providing a number above 31
/// will panic.
/// NOTE: not available on medium- and low-density devices!
#[cfg(feature = "high")]
pub fn read_data_register_high(&self, register: usize) -> u16 {
read_drx!(self, bkp_dr, register)
}

/// Write a 16-bit value to one of the DR1 to DR10 registers part of the
/// Backup Data Register. The register argument is a zero based index to the
/// DRx registers: 0 is DR1, up to 9 for DR10. Providing a number above 9
/// will panic.
pub fn write_data_register_low(&self, register: usize, data: u16) {
write_drx!(self, dr, register, data)
}

/// Write a 16-bit value to one of the DR11 to DR42 registers part of the
/// Backup Data Register. The register argument is a zero based index to the
/// DRx registers: 0 is DR11, up to 31 for DR42. Providing a number above 31
/// will panic.
/// NOTE: not available on medium- and low-density devices!
#[cfg(feature = "high")]
pub fn write_data_register_high(&self, register: usize, data: u16) {
write_drx!(self, bkp_dr, register, data)
}
}

0 comments on commit 9f08026

Please sign in to comment.