Skip to content

Commit 062d0b9

Browse files
authored
Merge pull request #731 from rust-embedded/remove-wrappers
generic REG in field writers
2 parents 5fa99aa + d77344a commit 062d0b9

File tree

8 files changed

+109
-183
lines changed

8 files changed

+109
-183
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ jobs:
7575
- { rust: stable, vendor: Spansion, options: "--atomics" }
7676
- { rust: stable, vendor: STMicro, options: "" }
7777
- { rust: stable, vendor: STMicro, options: "--atomics" }
78-
- { rust: stable, vendor: STM32-patched, options: "--strict --const_generic --derive_more --pascal_enum_values --max_cluster_size --atomics" }
78+
- { rust: stable, vendor: STM32-patched, options: "--strict --const_generic --pascal_enum_values --max_cluster_size --atomics" }
7979
- { rust: stable, vendor: Toshiba, options: all }
8080
- { rust: stable, vendor: Toshiba, options: "" }
8181
# Test MSRV

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
77

88
## [Unreleased]
99

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

ci/script.sh

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ main() {
3131

3232
case $OPTIONS in
3333
all)
34-
options="--const_generic --strict --derive_more --atomics"
34+
options="--const_generic --strict --atomics"
3535
;;
3636
*)
3737
options=$OPTIONS
@@ -43,9 +43,6 @@ main() {
4343
echo 'cortex-m = "0.7.4"' >> $td/Cargo.toml
4444
echo 'cortex-m-rt = "0.7.1"' >> $td/Cargo.toml
4545
echo 'vcell = "0.1.3"' >> $td/Cargo.toml
46-
if [[ "$options" == *"--derive_more"* ]]; then
47-
echo 'derive_more = "0.99"' >> $td/Cargo.toml
48-
fi
4946
if [[ "$options" == *"--atomics"* ]]; then
5047
echo 'portable-atomic = { version = "0.3.16", default-features = false }' >> $td/Cargo.toml
5148
fi

src/generate/generic.rs

Lines changed: 41 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,14 @@ pub trait FieldSpec: Sized {
5959
/// Trait implemented by readable registers to enable the `read` method.
6060
///
6161
/// Registers marked with `Writable` can be also be `modify`'ed.
62-
pub trait Readable: RegisterSpec {
63-
/// Result from a call to `read` and argument to `modify`.
64-
type Reader: From<R<Self>> + core::ops::Deref<Target = R<Self>>;
65-
}
62+
pub trait Readable: RegisterSpec {}
6663

6764
/// Trait implemented by writeable registers.
6865
///
6966
/// This enables the `write`, `write_with_zero` and `reset` methods.
7067
///
7168
/// Registers marked with `Readable` can be also be `modify`'ed.
7269
pub trait Writable: RegisterSpec {
73-
/// Writer type argument to `write`, et al.
74-
type Writer: From<W<Self>> + core::ops::DerefMut<Target = W<Self>>;
75-
7670
/// Specifies the register bits that are not changed if you pass `1` and are changed if you pass `0`
7771
const ZERO_TO_MODIFY_FIELDS_BITMAP: Self::Ux;
7872

@@ -130,11 +124,11 @@ impl<REG: Readable> Reg<REG> {
130124
/// let flag = reader.field2().bit_is_set();
131125
/// ```
132126
#[inline(always)]
133-
pub fn read(&self) -> REG::Reader {
134-
REG::Reader::from(R {
127+
pub fn read(&self) -> R<REG> {
128+
R {
135129
bits: self.register.get(),
136130
_reg: marker::PhantomData,
137-
})
131+
}
138132
}
139133
}
140134

@@ -173,14 +167,14 @@ impl<REG: Resettable + Writable> Reg<REG> {
173167
#[inline(always)]
174168
pub fn write<F>(&self, f: F)
175169
where
176-
F: FnOnce(&mut REG::Writer) -> &mut W<REG>,
170+
F: FnOnce(&mut W<REG>) -> &mut W<REG>,
177171
{
178172
self.register.set(
179-
f(&mut REG::Writer::from(W {
173+
f(&mut W {
180174
bits: REG::RESET_VALUE & !REG::ONE_TO_MODIFY_FIELDS_BITMAP
181175
| REG::ZERO_TO_MODIFY_FIELDS_BITMAP,
182176
_reg: marker::PhantomData,
183-
}))
177+
})
184178
.bits,
185179
);
186180
}
@@ -197,13 +191,13 @@ impl<REG: Writable> Reg<REG> {
197191
#[inline(always)]
198192
pub unsafe fn write_with_zero<F>(&self, f: F)
199193
where
200-
F: FnOnce(&mut REG::Writer) -> &mut W<REG>,
194+
F: FnOnce(&mut W<REG>) -> &mut W<REG>,
201195
{
202196
self.register.set(
203-
f(&mut REG::Writer::from(W {
197+
f(&mut W {
204198
bits: REG::Ux::default(),
205199
_reg: marker::PhantomData,
206-
}))
200+
})
207201
.bits,
208202
);
209203
}
@@ -238,20 +232,20 @@ impl<REG: Readable + Writable> Reg<REG> {
238232
#[inline(always)]
239233
pub fn modify<F>(&self, f: F)
240234
where
241-
for<'w> F: FnOnce(&REG::Reader, &'w mut REG::Writer) -> &'w mut W<REG>,
235+
for<'w> F: FnOnce(&R<REG>, &'w mut W<REG>) -> &'w mut W<REG>,
242236
{
243237
let bits = self.register.get();
244238
self.register.set(
245239
f(
246-
&REG::Reader::from(R {
240+
&R {
247241
bits,
248242
_reg: marker::PhantomData,
249-
}),
250-
&mut REG::Writer::from(W {
243+
},
244+
&mut W {
251245
bits: bits & !REG::ONE_TO_MODIFY_FIELDS_BITMAP
252246
| REG::ZERO_TO_MODIFY_FIELDS_BITMAP,
253247
_reg: marker::PhantomData,
254-
}),
248+
},
255249
)
256250
.bits,
257251
);
@@ -262,7 +256,10 @@ impl<REG: Readable + Writable> Reg<REG> {
262256
///
263257
/// Result of the `read` methods of registers. Also used as a closure argument in the `modify`
264258
/// method.
265-
pub struct R<REG: RegisterSpec + ?Sized> {
259+
pub type R<REG> = RRaw<REG>;
260+
261+
#[doc(hidden)]
262+
pub struct RRaw<REG: RegisterSpec> {
266263
pub(crate) bits: REG::Ux,
267264
_reg: marker::PhantomData<REG>,
268265
}
@@ -290,25 +287,15 @@ where
290287
/// Register writer.
291288
///
292289
/// Used as an argument to the closures in the `write` and `modify` methods of the register.
293-
pub struct W<REG: RegisterSpec + ?Sized> {
290+
pub type W<REG> = WRaw<REG>;
291+
292+
#[doc(hidden)]
293+
pub struct WRaw<REG: RegisterSpec> {
294294
///Writable bits
295295
pub(crate) bits: REG::Ux,
296296
_reg: marker::PhantomData<REG>,
297297
}
298298

299-
impl<REG: RegisterSpec> W<REG> {
300-
/// Writes raw bits to the register.
301-
///
302-
/// # Safety
303-
///
304-
/// Read datasheet or reference manual to find what values are allowed to pass.
305-
#[inline(always)]
306-
pub unsafe fn bits(&mut self, bits: REG::Ux) -> &mut Self {
307-
self.bits = bits;
308-
self
309-
}
310-
}
311-
312299
#[doc(hidden)]
313300
pub struct FieldReaderRaw<FI = u8>
314301
where
@@ -414,7 +401,7 @@ where
414401
REG: Writable + RegisterSpec,
415402
FI: FieldSpec,
416403
{
417-
pub(crate) w: &'a mut REG::Writer,
404+
pub(crate) w: &'a mut W<REG>,
418405
_field: marker::PhantomData<(FI, Safety)>,
419406
}
420407

@@ -427,7 +414,7 @@ where
427414
/// Creates a new instance of the writer
428415
#[allow(unused)]
429416
#[inline(always)]
430-
pub(crate) fn new(w: &'a mut REG::Writer) -> Self {
417+
pub(crate) fn new(w: &'a mut W<REG>) -> Self {
431418
Self {
432419
w,
433420
_field: marker::PhantomData,
@@ -441,7 +428,7 @@ where
441428
REG: Writable + RegisterSpec,
442429
bool: From<FI>,
443430
{
444-
pub(crate) w: &'a mut REG::Writer,
431+
pub(crate) w: &'a mut W<REG>,
445432
_field: marker::PhantomData<(FI, M)>,
446433
}
447434

@@ -453,7 +440,7 @@ where
453440
/// Creates a new instance of the writer
454441
#[allow(unused)]
455442
#[inline(always)]
456-
pub(crate) fn new(w: &'a mut REG::Writer) -> Self {
443+
pub(crate) fn new(w: &'a mut W<REG>) -> Self {
457444
Self {
458445
w,
459446
_field: marker::PhantomData,
@@ -514,14 +501,14 @@ macro_rules! impl_bit_proxy {
514501
{
515502
/// Writes bit to the field
516503
#[inline(always)]
517-
pub fn bit(self, value: bool) -> &'a mut REG::Writer {
504+
pub fn bit(self, value: bool) -> &'a mut W<REG> {
518505
self.w.bits &= !(REG::Ux::one() << OF);
519506
self.w.bits |= (REG::Ux::from(value) & REG::Ux::one()) << OF;
520507
self.w
521508
}
522509
/// Writes `variant` to the field
523510
#[inline(always)]
524-
pub fn variant(self, variant: FI) -> &'a mut REG::Writer {
511+
pub fn variant(self, variant: FI) -> &'a mut W<REG> {
525512
self.bit(bool::from(variant))
526513
}
527514
}
@@ -548,14 +535,14 @@ where
548535
///
549536
/// Passing incorrect value can cause undefined behaviour. See reference manual
550537
#[inline(always)]
551-
pub unsafe fn bits(self, value: FI::Ux) -> &'a mut REG::Writer {
538+
pub unsafe fn bits(self, value: FI::Ux) -> &'a mut W<REG> {
552539
self.w.bits &= !(REG::Ux::mask::<WI>() << OF);
553540
self.w.bits |= (REG::Ux::from(value) & REG::Ux::mask::<WI>()) << OF;
554541
self.w
555542
}
556543
/// Writes `variant` to the field
557544
#[inline(always)]
558-
pub fn variant(self, variant: FI) -> &'a mut REG::Writer {
545+
pub fn variant(self, variant: FI) -> &'a mut W<REG> {
559546
unsafe { self.bits(FI::Ux::from(variant)) }
560547
}
561548
}
@@ -567,14 +554,14 @@ where
567554
{
568555
/// Writes raw bits to the field
569556
#[inline(always)]
570-
pub fn bits(self, value: FI::Ux) -> &'a mut REG::Writer {
557+
pub fn bits(self, value: FI::Ux) -> &'a mut W<REG> {
571558
self.w.bits &= !(REG::Ux::mask::<WI>() << OF);
572559
self.w.bits |= (REG::Ux::from(value) & REG::Ux::mask::<WI>()) << OF;
573560
self.w
574561
}
575562
/// Writes `variant` to the field
576563
#[inline(always)]
577-
pub fn variant(self, variant: FI) -> &'a mut REG::Writer {
564+
pub fn variant(self, variant: FI) -> &'a mut W<REG> {
578565
self.bits(FI::Ux::from(variant))
579566
}
580567
}
@@ -594,13 +581,13 @@ where
594581
{
595582
/// Sets the field bit
596583
#[inline(always)]
597-
pub fn set_bit(self) -> &'a mut REG::Writer {
584+
pub fn set_bit(self) -> &'a mut W<REG> {
598585
self.w.bits |= REG::Ux::one() << OF;
599586
self.w
600587
}
601588
/// Clears the field bit
602589
#[inline(always)]
603-
pub fn clear_bit(self) -> &'a mut REG::Writer {
590+
pub fn clear_bit(self) -> &'a mut W<REG> {
604591
self.w.bits &= !(REG::Ux::one() << OF);
605592
self.w
606593
}
@@ -613,7 +600,7 @@ where
613600
{
614601
/// Sets the field bit
615602
#[inline(always)]
616-
pub fn set_bit(self) -> &'a mut REG::Writer {
603+
pub fn set_bit(self) -> &'a mut W<REG> {
617604
self.w.bits |= REG::Ux::one() << OF;
618605
self.w
619606
}
@@ -626,7 +613,7 @@ where
626613
{
627614
/// Clears the field bit
628615
#[inline(always)]
629-
pub fn clear_bit(self) -> &'a mut REG::Writer {
616+
pub fn clear_bit(self) -> &'a mut W<REG> {
630617
self.w.bits &= !(REG::Ux::one() << OF);
631618
self.w
632619
}
@@ -639,7 +626,7 @@ where
639626
{
640627
///Clears the field bit by passing one
641628
#[inline(always)]
642-
pub fn clear_bit_by_one(self) -> &'a mut REG::Writer {
629+
pub fn clear_bit_by_one(self) -> &'a mut W<REG> {
643630
self.w.bits |= REG::Ux::one() << OF;
644631
self.w
645632
}
@@ -652,7 +639,7 @@ where
652639
{
653640
///Sets the field bit by passing zero
654641
#[inline(always)]
655-
pub fn set_bit_by_zero(self) -> &'a mut REG::Writer {
642+
pub fn set_bit_by_zero(self) -> &'a mut W<REG> {
656643
self.w.bits &= !(REG::Ux::one() << OF);
657644
self.w
658645
}
@@ -665,7 +652,7 @@ where
665652
{
666653
///Toggle the field bit by passing one
667654
#[inline(always)]
668-
pub fn toggle_bit(self) -> &'a mut REG::Writer {
655+
pub fn toggle_bit(self) -> &'a mut W<REG> {
669656
self.w.bits |= REG::Ux::one() << OF;
670657
self.w
671658
}
@@ -678,7 +665,7 @@ where
678665
{
679666
///Toggle the field bit by passing zero
680667
#[inline(always)]
681-
pub fn toggle_bit(self) -> &'a mut REG::Writer {
668+
pub fn toggle_bit(self) -> &'a mut W<REG> {
682669
self.w.bits &= !(REG::Ux::one() << OF);
683670
self.w
684671
}

src/generate/generic_atomic.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ where
5151
#[inline(always)]
5252
pub unsafe fn set_bits<F>(&self, f: F)
5353
where
54-
F: FnOnce(&mut REG::Writer) -> &mut W<REG>,
54+
F: FnOnce(&mut W<REG>) -> &mut W<REG>,
5555
{
56-
let bits = f(&mut REG::Writer::from(W {
56+
let bits = f(&mut W {
5757
bits: Default::default(),
5858
_reg: marker::PhantomData,
59-
}))
59+
})
6060
.bits;
6161
REG::Ux::atomic_or(self.register.as_ptr(), bits);
6262
}
@@ -70,12 +70,12 @@ where
7070
#[inline(always)]
7171
pub unsafe fn clear_bits<F>(&self, f: F)
7272
where
73-
F: FnOnce(&mut REG::Writer) -> &mut W<REG>,
73+
F: FnOnce(&mut W<REG>) -> &mut W<REG>,
7474
{
75-
let bits = f(&mut REG::Writer::from(W {
75+
let bits = f(&mut W {
7676
bits: !REG::Ux::default(),
7777
_reg: marker::PhantomData,
78-
}))
78+
})
7979
.bits;
8080
REG::Ux::atomic_and(self.register.as_ptr(), bits);
8181
}
@@ -89,12 +89,12 @@ where
8989
#[inline(always)]
9090
pub unsafe fn toggle_bits<F>(&self, f: F)
9191
where
92-
F: FnOnce(&mut REG::Writer) -> &mut W<REG>,
92+
F: FnOnce(&mut W<REG>) -> &mut W<REG>,
9393
{
94-
let bits = f(&mut REG::Writer::from(W {
94+
let bits = f(&mut W {
9595
bits: Default::default(),
9696
_reg: marker::PhantomData,
97-
}))
97+
})
9898
.bits;
9999
REG::Ux::atomic_xor(self.register.as_ptr(), bits);
100100
}

0 commit comments

Comments
 (0)