Skip to content

Commit

Permalink
Adds support for reading and writing to the Backup Data Register (#83).
Browse files Browse the repository at this point in the history
  • Loading branch information
mjepronk committed Jul 29, 2019
1 parent ecbe3d9 commit c932545
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) => {
unsafe {
$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!
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!
pub fn write_data_register_high(&self, register: usize, data: u16) {
write_drx!(self, dr, register, data)
}
}

0 comments on commit c932545

Please sign in to comment.