Skip to content

Commit

Permalink
shorten names for constants
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Oct 22, 2023
1 parent 76155af commit 71ff37f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 63 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/).

## [Unreleased]

- Shorter names for `Writable` trait constants

## [v0.30.2] - 2023-10-22

- Fix documentation warnings
Expand Down Expand Up @@ -816,8 +818,7 @@ peripheral.register.write(|w| w.field().set());

- Initial version of the `svd2rust` tool

[Unreleased]: https://github.com/rust-embedded/svd2rust/compare/v0.30.2...HEAD
[v0.30.2]: https://github.com/rust-embedded/svd2rust/compare/v0.30.1...v0.30.2
[Unreleased]: https://github.com/rust-embedded/svd2rust/compare/v0.30.1...HEAD
[v0.30.1]: https://github.com/rust-embedded/svd2rust/compare/v0.30.0...v0.30.1
[v0.30.0]: https://github.com/rust-embedded/svd2rust/compare/v0.29.0...v0.30.0
[v0.29.0]: https://github.com/rust-embedded/svd2rust/compare/v0.28.0...v0.29.0
Expand Down
4 changes: 2 additions & 2 deletions src/generate/array_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ pub struct ArrayProxy<T, const COUNT: usize, const STRIDE: usize> {
/// As well as providing a PhantomData, this field is non-public, and
/// therefore ensures that code outside of this module can never create
/// an ArrayProxy.
_array: marker::PhantomData<T>,
_array: Marker<T>,
}
#[allow(clippy::len_without_is_empty)]
impl<T, const C: usize, const S: usize> ArrayProxy<T, C, S> {
/// Get a reference from an [ArrayProxy] with no bounds checking.
pub const unsafe fn get_ref(&self, index: usize) -> &T {
&*(self as *const Self).cast::<u8>().add(S * index).cast::<T>()
&*(self as *const Self).cast::<u8>().add(S * index).cast()
}
/// Get a reference from an [ArrayProxy], or return `None` if the index
/// is out of bounds.
Expand Down
88 changes: 34 additions & 54 deletions src/generate/generic.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::marker;
use core::marker::PhantomData as Marker;

/// Raw register type (`u8`, `u16`, `u32`, ...)
pub trait RawReg:
Expand All @@ -15,7 +15,7 @@ pub trait RawReg:
/// Mask for bits of width `WI`
fn mask<const WI: u8>() -> Self;
/// Mask for bits of width 1
fn one() -> Self;
const ONE: Self;
}

macro_rules! raw_reg {
Expand All @@ -25,10 +25,7 @@ macro_rules! raw_reg {
fn mask<const WI: u8>() -> Self {
$mask::<WI>()
}
#[inline(always)]
fn one() -> Self {
1
}
const ONE: Self = 1;
}
const fn $mask<const WI: u8>() -> $U {
<$U>::MAX >> ($size - WI)
Expand Down Expand Up @@ -68,10 +65,10 @@ pub trait Readable: RegisterSpec {}
/// Registers marked with `Readable` can be also be `modify`'ed.
pub trait Writable: RegisterSpec {
/// Specifies the register bits that are not changed if you pass `1` and are changed if you pass `0`
const ZERO_TO_MODIFY_FIELDS_BITMAP: Self::Ux;
const ZEROS_BITMAP: Self::Ux;

/// Specifies the register bits that are not changed if you pass `0` and are changed if you pass `1`
const ONE_TO_MODIFY_FIELDS_BITMAP: Self::Ux;
const ONES_BITMAP: Self::Ux;
}

/// Reset value of the register.
Expand All @@ -93,7 +90,7 @@ pub trait Resettable: RegisterSpec {
#[repr(transparent)]
pub struct Reg<REG: RegisterSpec> {
register: vcell::VolatileCell<REG::Ux>,
_marker: marker::PhantomData<REG>,
_marker: Marker<REG>,
}

unsafe impl<REG: RegisterSpec> Send for Reg<REG> where REG::Ux: Send {}
Expand Down Expand Up @@ -127,7 +124,7 @@ impl<REG: Readable> Reg<REG> {
pub fn read(&self) -> R<REG> {
R {
bits: self.register.get(),
_reg: marker::PhantomData,
_reg: Marker,
}
}
}
Expand Down Expand Up @@ -171,9 +168,8 @@ impl<REG: Resettable + Writable> Reg<REG> {
{
self.register.set(
f(&mut W {
bits: REG::RESET_VALUE & !REG::ONE_TO_MODIFY_FIELDS_BITMAP
| REG::ZERO_TO_MODIFY_FIELDS_BITMAP,
_reg: marker::PhantomData,
bits: REG::RESET_VALUE & !REG::ONES_BITMAP | REG::ZEROS_BITMAP,
_reg: Marker,
})
.bits,
);
Expand All @@ -196,7 +192,7 @@ impl<REG: Writable> Reg<REG> {
self.register.set(
f(&mut W {
bits: REG::Ux::default(),
_reg: marker::PhantomData,
_reg: Marker,
})
.bits,
);
Expand Down Expand Up @@ -237,14 +233,10 @@ impl<REG: Readable + Writable> Reg<REG> {
let bits = self.register.get();
self.register.set(
f(
&R {
bits,
_reg: marker::PhantomData,
},
&R { bits, _reg: Marker },
&mut W {
bits: bits & !REG::ONE_TO_MODIFY_FIELDS_BITMAP
| REG::ZERO_TO_MODIFY_FIELDS_BITMAP,
_reg: marker::PhantomData,
bits: bits & !REG::ONES_BITMAP | REG::ZEROS_BITMAP,
_reg: Marker,
},
)
.bits,
Expand All @@ -254,53 +246,47 @@ impl<REG: Readable + Writable> Reg<REG> {

#[doc(hidden)]
pub mod raw {
use super::{marker, BitM, FieldSpec, RegisterSpec, Unsafe, Writable};
use super::{BitM, FieldSpec, Marker, RegisterSpec, Unsafe, Writable};

pub struct R<REG: RegisterSpec> {
pub(crate) bits: REG::Ux,
pub(super) _reg: marker::PhantomData<REG>,
pub(super) _reg: Marker<REG>,
}

pub struct W<REG: RegisterSpec> {
///Writable bits
pub(crate) bits: REG::Ux,
pub(super) _reg: marker::PhantomData<REG>,
pub(super) _reg: Marker<REG>,
}

pub struct FieldReader<FI = u8>
where
FI: FieldSpec,
{
pub(crate) bits: FI::Ux,
_reg: marker::PhantomData<FI>,
_reg: Marker<FI>,
}

impl<FI: FieldSpec> FieldReader<FI> {
/// Creates a new instance of the reader.
#[allow(unused)]
#[inline(always)]
pub(crate) const fn new(bits: FI::Ux) -> Self {
Self {
bits,
_reg: marker::PhantomData,
}
Self { bits, _reg: Marker }
}
}

pub struct BitReader<FI = bool> {
pub(crate) bits: bool,
_reg: marker::PhantomData<FI>,
_reg: Marker<FI>,
}

impl<FI> BitReader<FI> {
/// Creates a new instance of the reader.
#[allow(unused)]
#[inline(always)]
pub(crate) const fn new(bits: bool) -> Self {
Self {
bits,
_reg: marker::PhantomData,
}
Self { bits, _reg: Marker }
}
}

Expand All @@ -310,7 +296,7 @@ pub mod raw {
FI: FieldSpec,
{
pub(crate) w: &'a mut W<REG>,
_field: marker::PhantomData<(FI, Safety)>,
_field: Marker<(FI, Safety)>,
}

impl<'a, REG, const WI: u8, const O: u8, FI, Safety> FieldWriter<'a, REG, WI, O, FI, Safety>
Expand All @@ -322,10 +308,7 @@ pub mod raw {
#[allow(unused)]
#[inline(always)]
pub(crate) fn new(w: &'a mut W<REG>) -> Self {
Self {
w,
_field: marker::PhantomData,
}
Self { w, _field: Marker }
}
}

Expand All @@ -335,7 +318,7 @@ pub mod raw {
bool: From<FI>,
{
pub(crate) w: &'a mut W<REG>,
_field: marker::PhantomData<(FI, M)>,
_field: Marker<(FI, M)>,
}

impl<'a, REG, const O: u8, FI, M> BitWriter<'a, REG, O, FI, M>
Expand All @@ -347,10 +330,7 @@ pub mod raw {
#[allow(unused)]
#[inline(always)]
pub(crate) fn new(w: &'a mut W<REG>) -> Self {
Self {
w,
_field: marker::PhantomData,
}
Self { w, _field: Marker }
}
}
}
Expand Down Expand Up @@ -522,8 +502,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 << OF);
self.w.bits |= (REG::Ux::from(value) & REG::Ux::ONE) << OF;
self.w
}
/// Writes `variant` to the field
Expand Down Expand Up @@ -551,13 +531,13 @@ where
/// 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 << OF;
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 << OF);
self.w
}
}
Expand All @@ -570,7 +550,7 @@ where
/// 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 << OF;
self.w
}
}
Expand All @@ -583,7 +563,7 @@ where
/// 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 << OF);
self.w
}
}
Expand All @@ -596,7 +576,7 @@ where
///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 << OF;
self.w
}
}
Expand All @@ -609,7 +589,7 @@ where
///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 << OF);
self.w
}
}
Expand All @@ -622,7 +602,7 @@ where
///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 << OF;
self.w
}
}
Expand All @@ -635,7 +615,7 @@ where
///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 << OF);
self.w
}
}
6 changes: 3 additions & 3 deletions src/generate/generic_atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ mod atomic {
{
let bits = f(&mut W {
bits: Default::default(),
_reg: marker::PhantomData,
_reg: Marker,
})
.bits;
REG::Ux::atomic_or(self.register.as_ptr(), bits);
Expand All @@ -73,7 +73,7 @@ mod atomic {
{
let bits = f(&mut W {
bits: !REG::Ux::default(),
_reg: marker::PhantomData,
_reg: Marker,
})
.bits;
REG::Ux::atomic_and(self.register.as_ptr(), bits);
Expand All @@ -92,7 +92,7 @@ mod atomic {
{
let bits = f(&mut W {
bits: Default::default(),
_reg: marker::PhantomData,
_reg: Marker,
})
.bits;
REG::Ux::atomic_xor(self.register.as_ptr(), bits);
Expand Down
4 changes: 2 additions & 2 deletions src/generate/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,8 @@ pub fn render_register_mod(
mod_items.extend(quote! {
#[doc = #doc]
impl crate::Writable for #regspec_ident {
const ZERO_TO_MODIFY_FIELDS_BITMAP: Self::Ux = #zero_to_modify_fields_bitmap;
const ONE_TO_MODIFY_FIELDS_BITMAP: Self::Ux = #one_to_modify_fields_bitmap;
const ZEROS_BITMAP: Self::Ux = #zero_to_modify_fields_bitmap;
const ONES_BITMAP: Self::Ux = #one_to_modify_fields_bitmap;
}
});
}
Expand Down

0 comments on commit 71ff37f

Please sign in to comment.