use crate::pac::{bkp, BKP, PMU};
use crate::rcu::APB1;
use core::marker::PhantomData;
pub trait BkpExt {
fn split(self, apb1: &mut APB1, pmu: &mut PMU) -> Parts;
}
impl BkpExt for BKP {
fn split(self, apb1: &mut APB1, pmu: &mut PMU) -> Parts {
riscv::interrupt::free(|_| {
apb1.en()
.modify(|_, w| w.pmuen().set_bit().bkpien().set_bit());
pmu.ctl.write(|w| w.bkpwen().set_bit());
});
Parts {
data: Data {
_owned_incontinuous_storage: PhantomData,
},
tamper: Tamper { _ownership: () },
octl: OCTL { _ownership: () },
}
}
}
pub struct Parts {
pub data: Data,
pub tamper: Tamper,
pub octl: OCTL,
}
pub struct Data {
_owned_incontinuous_storage: PhantomData<[u16; 42]>,
}
impl Data {
#[inline]
pub fn read(&self, idx: usize) -> u16 {
unsafe { *Self::get_ptr(idx) }
}
#[inline]
pub fn write(&mut self, idx: usize, data: u16) {
unsafe { *Self::get_ptr(idx) = data }
}
#[inline]
fn get_ptr(idx: usize) -> *mut u16 {
if idx <= 9 {
unsafe { (BKP::ptr() as *mut u8).add(idx * 0x04 + 0x04) as *mut u16 }
} else if idx <= 41 {
unsafe { (BKP::ptr() as *mut u8).add((idx - 10) * 0x04 + 0x40) as *mut u16 }
} else {
panic!("invalid index")
}
}
}
pub struct Tamper {
_ownership: (),
}
impl Tamper {
pub fn enable(&mut self) {
unsafe { &*BKP::ptr() }
.tpctl
.modify(|_, w| w.tpen().set_bit());
}
pub fn disable(&mut self) {
unsafe { &*BKP::ptr() }
.tpctl
.modify(|_, w| w.tpen().clear_bit());
}
pub fn set_pin_active_high(&mut self) {
unsafe { &*BKP::ptr() }
.tpctl
.modify(|_, w| w.tpal().clear_bit());
}
pub fn set_pin_active_low(&mut self) {
unsafe { &*BKP::ptr() }
.tpctl
.modify(|_, w| w.tpal().set_bit());
}
pub fn check_event(&self) -> bool {
unsafe { &*BKP::ptr() }.tpcs.read().tef().bit()
}
pub fn clear_event_bit(&mut self) {
unsafe { &*BKP::ptr() }
.tpcs
.modify(|_, w| w.ter().set_bit());
}
pub fn enable_interrupt(&mut self) {
unsafe { &*BKP::ptr() }
.tpcs
.modify(|_, w| w.tpie().set_bit());
}
pub fn disable_interrupt(&mut self) {
unsafe { &*BKP::ptr() }
.tpcs
.modify(|_, w| w.tpie().clear_bit());
}
pub fn check_interrupt(&self) -> bool {
unsafe { &*BKP::ptr() }.tpcs.read().tif().bit()
}
pub fn clear_interrupt_bit(&mut self) {
unsafe { &*BKP::ptr() }
.tpcs
.modify(|_, w| w.tir().set_bit());
}
}
pub struct OCTL {
_ownership: (),
}
impl OCTL {
}