From 9f08026ef671215142042cd339207a3d4e54efe8 Mon Sep 17 00:00:00 2001 From: Matthias P <34212319+mjepronk@users.noreply.github.com> Date: Fri, 9 Aug 2019 19:14:10 +0200 Subject: [PATCH] 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. --- CHANGELOG.md | 4 +++- src/backup_domain.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e2bd5c5..935e1656 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/backup_domain.rs b/src/backup_domain.rs index 5169ce72..55bffd3b 100644 --- a/src/backup_domain.rs +++ b/src/backup_domain.rs @@ -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) + } +}