Skip to content

Commit

Permalink
Merge pull request #731 from rust-embedded/remove-wrappers
Browse files Browse the repository at this point in the history
generic REG in field writers
  • Loading branch information
therealprof authored Jun 13, 2023
2 parents 5fa99aa + d77344a commit 062d0b9
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 183 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ jobs:
- { rust: stable, vendor: Spansion, options: "--atomics" }
- { rust: stable, vendor: STMicro, options: "" }
- { rust: stable, vendor: STMicro, options: "--atomics" }
- { rust: stable, vendor: STM32-patched, options: "--strict --const_generic --derive_more --pascal_enum_values --max_cluster_size --atomics" }
- { rust: stable, vendor: STM32-patched, options: "--strict --const_generic --pascal_enum_values --max_cluster_size --atomics" }
- { rust: stable, vendor: Toshiba, options: all }
- { rust: stable, vendor: Toshiba, options: "" }
# Test MSRV
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).

## [Unreleased]

- removed register writer & reader wrappers, generic `REG` in field writers (#731)
- Updated syn to version 2 (#732)
- Let readable field fetch doc from svd description (#734)

Expand Down
5 changes: 1 addition & 4 deletions ci/script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ main() {

case $OPTIONS in
all)
options="--const_generic --strict --derive_more --atomics"
options="--const_generic --strict --atomics"
;;
*)
options=$OPTIONS
Expand All @@ -43,9 +43,6 @@ main() {
echo 'cortex-m = "0.7.4"' >> $td/Cargo.toml
echo 'cortex-m-rt = "0.7.1"' >> $td/Cargo.toml
echo 'vcell = "0.1.3"' >> $td/Cargo.toml
if [[ "$options" == *"--derive_more"* ]]; then
echo 'derive_more = "0.99"' >> $td/Cargo.toml
fi
if [[ "$options" == *"--atomics"* ]]; then
echo 'portable-atomic = { version = "0.3.16", default-features = false }' >> $td/Cargo.toml
fi
Expand Down
95 changes: 41 additions & 54 deletions src/generate/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,14 @@ pub trait FieldSpec: Sized {
/// Trait implemented by readable registers to enable the `read` method.
///
/// Registers marked with `Writable` can be also be `modify`'ed.
pub trait Readable: RegisterSpec {
/// Result from a call to `read` and argument to `modify`.
type Reader: From<R<Self>> + core::ops::Deref<Target = R<Self>>;
}
pub trait Readable: RegisterSpec {}

/// Trait implemented by writeable registers.
///
/// This enables the `write`, `write_with_zero` and `reset` methods.
///
/// Registers marked with `Readable` can be also be `modify`'ed.
pub trait Writable: RegisterSpec {
/// Writer type argument to `write`, et al.
type Writer: From<W<Self>> + core::ops::DerefMut<Target = W<Self>>;

/// 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;

Expand Down Expand Up @@ -130,11 +124,11 @@ impl<REG: Readable> Reg<REG> {
/// let flag = reader.field2().bit_is_set();
/// ```
#[inline(always)]
pub fn read(&self) -> REG::Reader {
REG::Reader::from(R {
pub fn read(&self) -> R<REG> {
R {
bits: self.register.get(),
_reg: marker::PhantomData,
})
}
}
}

Expand Down Expand Up @@ -173,14 +167,14 @@ impl<REG: Resettable + Writable> Reg<REG> {
#[inline(always)]
pub fn write<F>(&self, f: F)
where
F: FnOnce(&mut REG::Writer) -> &mut W<REG>,
F: FnOnce(&mut W<REG>) -> &mut W<REG>,
{
self.register.set(
f(&mut REG::Writer::from(W {
f(&mut W {
bits: REG::RESET_VALUE & !REG::ONE_TO_MODIFY_FIELDS_BITMAP
| REG::ZERO_TO_MODIFY_FIELDS_BITMAP,
_reg: marker::PhantomData,
}))
})
.bits,
);
}
Expand All @@ -197,13 +191,13 @@ impl<REG: Writable> Reg<REG> {
#[inline(always)]
pub unsafe fn write_with_zero<F>(&self, f: F)
where
F: FnOnce(&mut REG::Writer) -> &mut W<REG>,
F: FnOnce(&mut W<REG>) -> &mut W<REG>,
{
self.register.set(
f(&mut REG::Writer::from(W {
f(&mut W {
bits: REG::Ux::default(),
_reg: marker::PhantomData,
}))
})
.bits,
);
}
Expand Down Expand Up @@ -238,20 +232,20 @@ impl<REG: Readable + Writable> Reg<REG> {
#[inline(always)]
pub fn modify<F>(&self, f: F)
where
for<'w> F: FnOnce(&REG::Reader, &'w mut REG::Writer) -> &'w mut W<REG>,
for<'w> F: FnOnce(&R<REG>, &'w mut W<REG>) -> &'w mut W<REG>,
{
let bits = self.register.get();
self.register.set(
f(
&REG::Reader::from(R {
&R {
bits,
_reg: marker::PhantomData,
}),
&mut REG::Writer::from(W {
},
&mut W {
bits: bits & !REG::ONE_TO_MODIFY_FIELDS_BITMAP
| REG::ZERO_TO_MODIFY_FIELDS_BITMAP,
_reg: marker::PhantomData,
}),
},
)
.bits,
);
Expand All @@ -262,7 +256,10 @@ impl<REG: Readable + Writable> Reg<REG> {
///
/// Result of the `read` methods of registers. Also used as a closure argument in the `modify`
/// method.
pub struct R<REG: RegisterSpec + ?Sized> {
pub type R<REG> = RRaw<REG>;

#[doc(hidden)]
pub struct RRaw<REG: RegisterSpec> {
pub(crate) bits: REG::Ux,
_reg: marker::PhantomData<REG>,
}
Expand Down Expand Up @@ -290,25 +287,15 @@ where
/// Register writer.
///
/// Used as an argument to the closures in the `write` and `modify` methods of the register.
pub struct W<REG: RegisterSpec + ?Sized> {
pub type W<REG> = WRaw<REG>;

#[doc(hidden)]
pub struct WRaw<REG: RegisterSpec> {
///Writable bits
pub(crate) bits: REG::Ux,
_reg: marker::PhantomData<REG>,
}

impl<REG: RegisterSpec> W<REG> {
/// Writes raw bits to the register.
///
/// # Safety
///
/// Read datasheet or reference manual to find what values are allowed to pass.
#[inline(always)]
pub unsafe fn bits(&mut self, bits: REG::Ux) -> &mut Self {
self.bits = bits;
self
}
}

#[doc(hidden)]
pub struct FieldReaderRaw<FI = u8>
where
Expand Down Expand Up @@ -414,7 +401,7 @@ where
REG: Writable + RegisterSpec,
FI: FieldSpec,
{
pub(crate) w: &'a mut REG::Writer,
pub(crate) w: &'a mut W<REG>,
_field: marker::PhantomData<(FI, Safety)>,
}

Expand All @@ -427,7 +414,7 @@ where
/// Creates a new instance of the writer
#[allow(unused)]
#[inline(always)]
pub(crate) fn new(w: &'a mut REG::Writer) -> Self {
pub(crate) fn new(w: &'a mut W<REG>) -> Self {
Self {
w,
_field: marker::PhantomData,
Expand All @@ -441,7 +428,7 @@ where
REG: Writable + RegisterSpec,
bool: From<FI>,
{
pub(crate) w: &'a mut REG::Writer,
pub(crate) w: &'a mut W<REG>,
_field: marker::PhantomData<(FI, M)>,
}

Expand All @@ -453,7 +440,7 @@ where
/// Creates a new instance of the writer
#[allow(unused)]
#[inline(always)]
pub(crate) fn new(w: &'a mut REG::Writer) -> Self {
pub(crate) fn new(w: &'a mut W<REG>) -> Self {
Self {
w,
_field: marker::PhantomData,
Expand Down Expand Up @@ -514,14 +501,14 @@ macro_rules! impl_bit_proxy {
{
/// Writes bit to the field
#[inline(always)]
pub fn bit(self, value: bool) -> &'a mut REG::Writer {
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
}
/// Writes `variant` to the field
#[inline(always)]
pub fn variant(self, variant: FI) -> &'a mut REG::Writer {
pub fn variant(self, variant: FI) -> &'a mut W<REG> {
self.bit(bool::from(variant))
}
}
Expand All @@ -548,14 +535,14 @@ where
///
/// Passing incorrect value can cause undefined behaviour. See reference manual
#[inline(always)]
pub unsafe fn bits(self, value: FI::Ux) -> &'a mut REG::Writer {
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
}
/// Writes `variant` to the field
#[inline(always)]
pub fn variant(self, variant: FI) -> &'a mut REG::Writer {
pub fn variant(self, variant: FI) -> &'a mut W<REG> {
unsafe { self.bits(FI::Ux::from(variant)) }
}
}
Expand All @@ -567,14 +554,14 @@ where
{
/// Writes raw bits to the field
#[inline(always)]
pub fn bits(self, value: FI::Ux) -> &'a mut REG::Writer {
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
}
/// Writes `variant` to the field
#[inline(always)]
pub fn variant(self, variant: FI) -> &'a mut REG::Writer {
pub fn variant(self, variant: FI) -> &'a mut W<REG> {
self.bits(FI::Ux::from(variant))
}
}
Expand All @@ -594,13 +581,13 @@ where
{
/// Sets the field bit
#[inline(always)]
pub fn set_bit(self) -> &'a mut REG::Writer {
pub fn set_bit(self) -> &'a mut W<REG> {
self.w.bits |= REG::Ux::one() << OF;
self.w
}
/// Clears the field bit
#[inline(always)]
pub fn clear_bit(self) -> &'a mut REG::Writer {
pub fn clear_bit(self) -> &'a mut W<REG> {
self.w.bits &= !(REG::Ux::one() << OF);
self.w
}
Expand All @@ -613,7 +600,7 @@ where
{
/// Sets the field bit
#[inline(always)]
pub fn set_bit(self) -> &'a mut REG::Writer {
pub fn set_bit(self) -> &'a mut W<REG> {
self.w.bits |= REG::Ux::one() << OF;
self.w
}
Expand All @@ -626,7 +613,7 @@ where
{
/// Clears the field bit
#[inline(always)]
pub fn clear_bit(self) -> &'a mut REG::Writer {
pub fn clear_bit(self) -> &'a mut W<REG> {
self.w.bits &= !(REG::Ux::one() << OF);
self.w
}
Expand All @@ -639,7 +626,7 @@ where
{
///Clears the field bit by passing one
#[inline(always)]
pub fn clear_bit_by_one(self) -> &'a mut REG::Writer {
pub fn clear_bit_by_one(self) -> &'a mut W<REG> {
self.w.bits |= REG::Ux::one() << OF;
self.w
}
Expand All @@ -652,7 +639,7 @@ where
{
///Sets the field bit by passing zero
#[inline(always)]
pub fn set_bit_by_zero(self) -> &'a mut REG::Writer {
pub fn set_bit_by_zero(self) -> &'a mut W<REG> {
self.w.bits &= !(REG::Ux::one() << OF);
self.w
}
Expand All @@ -665,7 +652,7 @@ where
{
///Toggle the field bit by passing one
#[inline(always)]
pub fn toggle_bit(self) -> &'a mut REG::Writer {
pub fn toggle_bit(self) -> &'a mut W<REG> {
self.w.bits |= REG::Ux::one() << OF;
self.w
}
Expand All @@ -678,7 +665,7 @@ where
{
///Toggle the field bit by passing zero
#[inline(always)]
pub fn toggle_bit(self) -> &'a mut REG::Writer {
pub fn toggle_bit(self) -> &'a mut W<REG> {
self.w.bits &= !(REG::Ux::one() << OF);
self.w
}
Expand Down
18 changes: 9 additions & 9 deletions src/generate/generic_atomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ where
#[inline(always)]
pub unsafe fn set_bits<F>(&self, f: F)
where
F: FnOnce(&mut REG::Writer) -> &mut W<REG>,
F: FnOnce(&mut W<REG>) -> &mut W<REG>,
{
let bits = f(&mut REG::Writer::from(W {
let bits = f(&mut W {
bits: Default::default(),
_reg: marker::PhantomData,
}))
})
.bits;
REG::Ux::atomic_or(self.register.as_ptr(), bits);
}
Expand All @@ -70,12 +70,12 @@ where
#[inline(always)]
pub unsafe fn clear_bits<F>(&self, f: F)
where
F: FnOnce(&mut REG::Writer) -> &mut W<REG>,
F: FnOnce(&mut W<REG>) -> &mut W<REG>,
{
let bits = f(&mut REG::Writer::from(W {
let bits = f(&mut W {
bits: !REG::Ux::default(),
_reg: marker::PhantomData,
}))
})
.bits;
REG::Ux::atomic_and(self.register.as_ptr(), bits);
}
Expand All @@ -89,12 +89,12 @@ where
#[inline(always)]
pub unsafe fn toggle_bits<F>(&self, f: F)
where
F: FnOnce(&mut REG::Writer) -> &mut W<REG>,
F: FnOnce(&mut W<REG>) -> &mut W<REG>,
{
let bits = f(&mut REG::Writer::from(W {
let bits = f(&mut W {
bits: Default::default(),
_reg: marker::PhantomData,
}))
})
.bits;
REG::Ux::atomic_xor(self.register.as_ptr(), bits);
}
Expand Down
Loading

0 comments on commit 062d0b9

Please sign in to comment.