use core::cell::UnsafeCell;
use core::marker::PhantomData;
use crate::interfaces::{Readable, Writeable};
use crate::{RegisterLongName, UIntLike};
#[repr(transparent)]
pub struct ReadWrite<T: UIntLike, R: RegisterLongName = ()> {
value: UnsafeCell<T>,
associated_register: PhantomData<R>,
}
impl<T: UIntLike, R: RegisterLongName> Readable for ReadWrite<T, R> {
type T = T;
type R = R;
#[inline]
fn get(&self) -> Self::T {
unsafe { ::core::ptr::read_volatile(self.value.get()) }
}
}
impl<T: UIntLike, R: RegisterLongName> Writeable for ReadWrite<T, R> {
type T = T;
type R = R;
#[inline]
fn set(&self, value: T) {
unsafe { ::core::ptr::write_volatile(self.value.get(), value) }
}
}
#[repr(transparent)]
pub struct ReadOnly<T: UIntLike, R: RegisterLongName = ()> {
value: T,
associated_register: PhantomData<R>,
}
impl<T: UIntLike, R: RegisterLongName> Readable for ReadOnly<T, R> {
type T = T;
type R = R;
#[inline]
fn get(&self) -> T {
unsafe { ::core::ptr::read_volatile(&self.value) }
}
}
#[repr(transparent)]
pub struct WriteOnly<T: UIntLike, R: RegisterLongName = ()> {
value: UnsafeCell<T>,
associated_register: PhantomData<R>,
}
impl<T: UIntLike, R: RegisterLongName> Writeable for WriteOnly<T, R> {
type T = T;
type R = R;
#[inline]
fn set(&self, value: T) {
unsafe { ::core::ptr::write_volatile(self.value.get(), value) }
}
}
#[repr(transparent)]
pub struct Aliased<T: UIntLike, R: RegisterLongName = (), W: RegisterLongName = ()> {
value: UnsafeCell<T>,
associated_register: PhantomData<(R, W)>,
}
impl<T: UIntLike, R: RegisterLongName, W: RegisterLongName> Readable for Aliased<T, R, W> {
type T = T;
type R = R;
#[inline]
fn get(&self) -> Self::T {
unsafe { ::core::ptr::read_volatile(self.value.get()) }
}
}
impl<T: UIntLike, R: RegisterLongName, W: RegisterLongName> Writeable for Aliased<T, R, W> {
type T = T;
type R = W;
#[inline]
fn set(&self, value: Self::T) {
unsafe { ::core::ptr::write_volatile(self.value.get(), value) }
}
}
#[repr(transparent)]
pub struct InMemoryRegister<T: UIntLike, R: RegisterLongName = ()> {
value: UnsafeCell<T>,
associated_register: PhantomData<R>,
}
impl<T: UIntLike, R: RegisterLongName> InMemoryRegister<T, R> {
pub const fn new(value: T) -> Self {
InMemoryRegister {
value: UnsafeCell::new(value),
associated_register: PhantomData,
}
}
}
impl<T: UIntLike, R: RegisterLongName> Readable for InMemoryRegister<T, R> {
type T = T;
type R = R;
#[inline]
fn get(&self) -> Self::T {
unsafe { ::core::ptr::read_volatile(self.value.get()) }
}
}
impl<T: UIntLike, R: RegisterLongName> Writeable for InMemoryRegister<T, R> {
type T = T;
type R = R;
#[inline]
fn set(&self, value: T) {
unsafe { ::core::ptr::write_volatile(self.value.get(), value) }
}
}