use core::marker;
pub trait RegisterSpec {
type Ux: Copy;
}
pub trait Readable: RegisterSpec {
type Reader: From<R<Self>> + core::ops::Deref<Target = R<Self>>;
}
pub trait Writable: RegisterSpec {
type Writer: From<W<Self>> + core::ops::DerefMut<Target = W<Self>>;
}
pub trait Resettable: RegisterSpec {
fn reset_value() -> Self::Ux;
}
#[repr(transparent)]
pub struct Reg<REG: RegisterSpec> {
register: vcell::VolatileCell<REG::Ux>,
_marker: marker::PhantomData<REG>,
}
unsafe impl<REG: RegisterSpec> Send for Reg<REG> where REG::Ux: Send {}
impl<REG: RegisterSpec> Reg<REG> {
#[inline(always)]
pub fn as_ptr(&self) -> *mut REG::Ux {
self.register.as_ptr()
}
}
impl<REG: Readable> Reg<REG> {
#[inline(always)]
pub fn read(&self) -> REG::Reader {
REG::Reader::from(R {
bits: self.register.get(),
_reg: marker::PhantomData,
})
}
}
impl<REG: Resettable + Writable> Reg<REG> {
#[inline(always)]
pub fn reset(&self) {
self.register.set(REG::reset_value())
}
#[inline(always)]
pub fn write<F>(&self, f: F)
where
F: FnOnce(&mut REG::Writer) -> &mut W<REG>,
{
self.register.set(
f(&mut REG::Writer::from(W {
bits: REG::reset_value(),
_reg: marker::PhantomData,
}))
.bits,
);
}
}
impl<REG: Writable> Reg<REG>
where
REG::Ux: Default,
{
#[inline(always)]
pub unsafe fn write_with_zero<F>(&self, f: F)
where
F: FnOnce(&mut REG::Writer) -> &mut W<REG>,
{
self.register.set(
(*f(&mut REG::Writer::from(W {
bits: REG::Ux::default(),
_reg: marker::PhantomData,
})))
.bits,
);
}
}
impl<REG: Readable + Writable> Reg<REG> {
#[inline(always)]
pub fn modify<F>(&self, f: F)
where
for<'w> F: FnOnce(®::Reader, &'w mut REG::Writer) -> &'w mut W<REG>,
{
let bits = self.register.get();
self.register.set(
f(
®::Reader::from(R {
bits,
_reg: marker::PhantomData,
}),
&mut REG::Writer::from(W {
bits,
_reg: marker::PhantomData,
}),
)
.bits,
);
}
}
pub struct R<REG: RegisterSpec + ?Sized> {
pub(crate) bits: REG::Ux,
_reg: marker::PhantomData<REG>,
}
impl<REG: RegisterSpec> R<REG> {
#[inline(always)]
pub fn bits(&self) -> REG::Ux {
self.bits
}
}
impl<REG: RegisterSpec, FI> PartialEq<FI> for R<REG>
where
REG::Ux: PartialEq,
FI: Copy + Into<REG::Ux>,
{
#[inline(always)]
fn eq(&self, other: &FI) -> bool {
self.bits.eq(&(*other).into())
}
}
pub struct W<REG: RegisterSpec + ?Sized> {
pub(crate) bits: REG::Ux,
_reg: marker::PhantomData<REG>,
}
impl<REG: RegisterSpec> W<REG> {
#[inline(always)]
pub unsafe fn bits(&mut self, bits: REG::Ux) -> &mut Self {
self.bits = bits;
self
}
}
pub struct FieldReader<U> {
pub(crate) bits: U,
}
impl<U> FieldReader<U>
where
U: Copy,
{
#[allow(unused)]
#[inline(always)]
pub(crate) fn new(bits: U) -> Self {
Self { bits }
}
#[inline(always)]
pub fn bits(&self) -> U {
self.bits
}
}
impl<U, T> PartialEq<T> for FieldReader<U>
where
U: PartialEq,
T: Copy + Into<U>,
{
#[inline(always)]
fn eq(&self, other: &T) -> bool {
self.bits.eq(&(*other).into())
}
}
impl FieldReader<bool> {
#[inline(always)]
pub fn bit(&self) -> bool {
self.bits
}
#[inline(always)]
pub fn bit_is_clear(&self) -> bool {
!self.bit()
}
#[inline(always)]
pub fn bit_is_set(&self) -> bool {
self.bit()
}
}