Skip to content

Commit

Permalink
writer offset as a struct field
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Oct 22, 2023
1 parent 76155af commit 5115c73
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 79 deletions.
72 changes: 37 additions & 35 deletions src/generate/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,51 +304,55 @@ pub mod raw {
}
}

pub struct FieldWriter<'a, REG, const WI: u8, const O: u8, FI = u8, Safety = Unsafe>
pub struct FieldWriter<'a, REG, const WI: u8, FI = u8, Safety = Unsafe>
where
REG: Writable + RegisterSpec,
FI: FieldSpec,
{
pub(crate) w: &'a mut W<REG>,
pub(crate) o: u8,
_field: marker::PhantomData<(FI, Safety)>,
}

impl<'a, REG, const WI: u8, const O: u8, FI, Safety> FieldWriter<'a, REG, WI, O, FI, Safety>
impl<'a, REG, const WI: u8, FI, Safety> FieldWriter<'a, REG, WI, FI, Safety>
where
REG: Writable + RegisterSpec,
FI: FieldSpec,
{
/// Creates a new instance of the writer
#[allow(unused)]
#[inline(always)]
pub(crate) fn new(w: &'a mut W<REG>) -> Self {
pub(crate) fn new(w: &'a mut W<REG>, o: u8) -> Self {
Self {
w,
o,
_field: marker::PhantomData,
}
}
}

pub struct BitWriter<'a, REG, const O: u8, FI = bool, M = BitM>
pub struct BitWriter<'a, REG, FI = bool, M = BitM>
where
REG: Writable + RegisterSpec,
bool: From<FI>,
{
pub(crate) w: &'a mut W<REG>,
pub(crate) o: u8,
_field: marker::PhantomData<(FI, M)>,
}

impl<'a, REG, const O: u8, FI, M> BitWriter<'a, REG, O, FI, M>
impl<'a, REG, FI, M> BitWriter<'a, REG, FI, M>
where
REG: Writable + RegisterSpec,
bool: From<FI>,
{
/// Creates a new instance of the writer
#[allow(unused)]
#[inline(always)]
pub(crate) fn new(w: &'a mut W<REG>) -> Self {
pub(crate) fn new(w: &'a mut W<REG>, o: u8) -> Self {
Self {
w,
o,
_field: marker::PhantomData,
}
}
Expand Down Expand Up @@ -447,13 +451,11 @@ pub struct Safe;
pub struct Unsafe;

/// Write field Proxy with unsafe `bits`
pub type FieldWriter<'a, REG, const WI: u8, const O: u8, FI = u8> =
raw::FieldWriter<'a, REG, WI, O, FI, Unsafe>;
pub type FieldWriter<'a, REG, const WI: u8, FI = u8> = raw::FieldWriter<'a, REG, WI, FI, Unsafe>;
/// Write field Proxy with safe `bits`
pub type FieldWriterSafe<'a, REG, const WI: u8, const O: u8, FI = u8> =
raw::FieldWriter<'a, REG, WI, O, FI, Safe>;
pub type FieldWriterSafe<'a, REG, const WI: u8, FI = u8> = raw::FieldWriter<'a, REG, WI, FI, Safe>;

impl<'a, REG, const WI: u8, const OF: u8, FI> FieldWriter<'a, REG, WI, OF, FI>
impl<'a, REG, const WI: u8, FI> FieldWriter<'a, REG, WI, FI>
where
REG: Writable + RegisterSpec,
FI: FieldSpec,
Expand All @@ -469,8 +471,8 @@ where
/// Passing incorrect value can cause undefined behaviour. See reference manual
#[inline(always)]
pub unsafe fn bits(self, value: FI::Ux) -> &'a mut W<REG> {
self.w.bits &= !(REG::Ux::mask::<WI>() << OF);
self.w.bits |= (REG::Ux::from(value) & REG::Ux::mask::<WI>()) << OF;
self.w.bits &= !(REG::Ux::mask::<WI>() << self.o);
self.w.bits |= (REG::Ux::from(value) & REG::Ux::mask::<WI>()) << self.o;
self.w
}
/// Writes `variant` to the field
Expand All @@ -480,7 +482,7 @@ where
}
}

impl<'a, REG, const WI: u8, const OF: u8, FI> FieldWriterSafe<'a, REG, WI, OF, FI>
impl<'a, REG, const WI: u8, FI> FieldWriterSafe<'a, REG, WI, FI>
where
REG: Writable + RegisterSpec,
FI: FieldSpec,
Expand All @@ -492,8 +494,8 @@ where
/// Writes raw bits to the field
#[inline(always)]
pub fn bits(self, value: FI::Ux) -> &'a mut W<REG> {
self.w.bits &= !(REG::Ux::mask::<WI>() << OF);
self.w.bits |= (REG::Ux::from(value) & REG::Ux::mask::<WI>()) << OF;
self.w.bits &= !(REG::Ux::mask::<WI>() << self.o);
self.w.bits |= (REG::Ux::from(value) & REG::Ux::mask::<WI>()) << self.o;
self.w
}
/// Writes `variant` to the field
Expand All @@ -509,9 +511,9 @@ macro_rules! bit_proxy {
pub struct $mwv;

/// Bit-wise write field proxy
pub type $writer<'a, REG, const O: u8, FI = bool> = raw::BitWriter<'a, REG, O, FI, $mwv>;
pub type $writer<'a, REG, FI = bool> = raw::BitWriter<'a, REG, FI, $mwv>;

impl<'a, REG, const OF: u8, FI> $writer<'a, REG, OF, FI>
impl<'a, REG, FI> $writer<'a, REG, FI>
where
REG: Writable + RegisterSpec,
bool: From<FI>,
Expand All @@ -522,8 +524,8 @@ macro_rules! bit_proxy {
/// Writes bit to the field
#[inline(always)]
pub fn bit(self, value: bool) -> &'a mut W<REG> {
self.w.bits &= !(REG::Ux::one() << OF);
self.w.bits |= (REG::Ux::from(value) & REG::Ux::one()) << OF;
self.w.bits &= !(REG::Ux::one() << self.o);
self.w.bits |= (REG::Ux::from(value) & REG::Ux::one()) << self.o;
self.w
}
/// Writes `variant` to the field
Expand All @@ -543,99 +545,99 @@ bit_proxy!(BitWriter0S, Bit0S);
bit_proxy!(BitWriter1T, Bit1T);
bit_proxy!(BitWriter0T, Bit0T);

impl<'a, REG, const OF: u8, FI> BitWriter<'a, REG, OF, FI>
impl<'a, REG, FI> BitWriter<'a, REG, FI>
where
REG: Writable + RegisterSpec,
bool: From<FI>,
{
/// Sets the field bit
#[inline(always)]
pub fn set_bit(self) -> &'a mut W<REG> {
self.w.bits |= REG::Ux::one() << OF;
self.w.bits |= REG::Ux::one() << self.o;
self.w
}
/// Clears the field bit
#[inline(always)]
pub fn clear_bit(self) -> &'a mut W<REG> {
self.w.bits &= !(REG::Ux::one() << OF);
self.w.bits &= !(REG::Ux::one() << self.o);
self.w
}
}

impl<'a, REG, const OF: u8, FI> BitWriter1S<'a, REG, OF, FI>
impl<'a, REG, FI> BitWriter1S<'a, REG, FI>
where
REG: Writable + RegisterSpec,
bool: From<FI>,
{
/// Sets the field bit
#[inline(always)]
pub fn set_bit(self) -> &'a mut W<REG> {
self.w.bits |= REG::Ux::one() << OF;
self.w.bits |= REG::Ux::one() << self.o;
self.w
}
}

impl<'a, REG, const OF: u8, FI> BitWriter0C<'a, REG, OF, FI>
impl<'a, REG, FI> BitWriter0C<'a, REG, FI>
where
REG: Writable + RegisterSpec,
bool: From<FI>,
{
/// Clears the field bit
#[inline(always)]
pub fn clear_bit(self) -> &'a mut W<REG> {
self.w.bits &= !(REG::Ux::one() << OF);
self.w.bits &= !(REG::Ux::one() << self.o);
self.w
}
}

impl<'a, REG, const OF: u8, FI> BitWriter1C<'a, REG, OF, FI>
impl<'a, REG, FI> BitWriter1C<'a, REG, FI>
where
REG: Writable + RegisterSpec,
bool: From<FI>,
{
///Clears the field bit by passing one
#[inline(always)]
pub fn clear_bit_by_one(self) -> &'a mut W<REG> {
self.w.bits |= REG::Ux::one() << OF;
self.w.bits |= REG::Ux::one() << self.o;
self.w
}
}

impl<'a, REG, const OF: u8, FI> BitWriter0S<'a, REG, OF, FI>
impl<'a, REG, FI> BitWriter0S<'a, REG, FI>
where
REG: Writable + RegisterSpec,
bool: From<FI>,
{
///Sets the field bit by passing zero
#[inline(always)]
pub fn set_bit_by_zero(self) -> &'a mut W<REG> {
self.w.bits &= !(REG::Ux::one() << OF);
self.w.bits &= !(REG::Ux::one() << self.o);
self.w
}
}

impl<'a, REG, const OF: u8, FI> BitWriter1T<'a, REG, OF, FI>
impl<'a, REG, FI> BitWriter1T<'a, REG, FI>
where
REG: Writable + RegisterSpec,
bool: From<FI>,
{
///Toggle the field bit by passing one
#[inline(always)]
pub fn toggle_bit(self) -> &'a mut W<REG> {
self.w.bits |= REG::Ux::one() << OF;
self.w.bits |= REG::Ux::one() << self.o;
self.w
}
}

impl<'a, REG, const OF: u8, FI> BitWriter0T<'a, REG, OF, FI>
impl<'a, REG, FI> BitWriter0T<'a, REG, FI>
where
REG: Writable + RegisterSpec,
bool: From<FI>,
{
///Toggle the field bit by passing zero
#[inline(always)]
pub fn toggle_bit(self) -> &'a mut W<REG> {
self.w.bits &= !(REG::Ux::one() << OF);
self.w.bits &= !(REG::Ux::one() << self.o);
self.w
}
}
Loading

0 comments on commit 5115c73

Please sign in to comment.