Skip to content

Commit f8a5479

Browse files
authored
Merge pull request #751 from rust-embedded/writer-offset
writer offset as a struct field
2 parents 1ca55ee + 7980b26 commit f8a5479

File tree

3 files changed

+124
-85
lines changed

3 files changed

+124
-85
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
88
## [Unreleased]
99

1010
- rename `const-generic` feature to `array_proxy`
11+
- `FieldWriter` takes offset as struct field instead of const generic.
12+
Improves SVD field array access
13+
Add `width`, `offset` methods
14+
- *breaking change* Always numerates field arrays from 0
1115

1216
## [v0.30.3] - 2023-11-19
1317

src/generate/generic.rs

Lines changed: 73 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -304,51 +304,55 @@ pub mod raw {
304304
}
305305
}
306306

307-
pub struct FieldWriter<'a, REG, const WI: u8, const O: u8, FI = u8, Safety = Unsafe>
307+
pub struct FieldWriter<'a, REG, const WI: u8, FI = u8, Safety = Unsafe>
308308
where
309309
REG: Writable + RegisterSpec,
310310
FI: FieldSpec,
311311
{
312312
pub(crate) w: &'a mut W<REG>,
313+
pub(crate) o: u8,
313314
_field: marker::PhantomData<(FI, Safety)>,
314315
}
315316

316-
impl<'a, REG, const WI: u8, const O: u8, FI, Safety> FieldWriter<'a, REG, WI, O, FI, Safety>
317+
impl<'a, REG, const WI: u8, FI, Safety> FieldWriter<'a, REG, WI, FI, Safety>
317318
where
318319
REG: Writable + RegisterSpec,
319320
FI: FieldSpec,
320321
{
321322
/// Creates a new instance of the writer
322323
#[allow(unused)]
323324
#[inline(always)]
324-
pub(crate) fn new(w: &'a mut W<REG>) -> Self {
325+
pub(crate) fn new(w: &'a mut W<REG>, o: u8) -> Self {
325326
Self {
326327
w,
328+
o,
327329
_field: marker::PhantomData,
328330
}
329331
}
330332
}
331333

332-
pub struct BitWriter<'a, REG, const O: u8, FI = bool, M = BitM>
334+
pub struct BitWriter<'a, REG, FI = bool, M = BitM>
333335
where
334336
REG: Writable + RegisterSpec,
335337
bool: From<FI>,
336338
{
337339
pub(crate) w: &'a mut W<REG>,
340+
pub(crate) o: u8,
338341
_field: marker::PhantomData<(FI, M)>,
339342
}
340343

341-
impl<'a, REG, const O: u8, FI, M> BitWriter<'a, REG, O, FI, M>
344+
impl<'a, REG, FI, M> BitWriter<'a, REG, FI, M>
342345
where
343346
REG: Writable + RegisterSpec,
344347
bool: From<FI>,
345348
{
346349
/// Creates a new instance of the writer
347350
#[allow(unused)]
348351
#[inline(always)]
349-
pub(crate) fn new(w: &'a mut W<REG>) -> Self {
352+
pub(crate) fn new(w: &'a mut W<REG>, o: u8) -> Self {
350353
Self {
351354
w,
355+
o,
352356
_field: marker::PhantomData,
353357
}
354358
}
@@ -447,13 +451,11 @@ pub struct Safe;
447451
pub struct Unsafe;
448452

449453
/// Write field Proxy with unsafe `bits`
450-
pub type FieldWriter<'a, REG, const WI: u8, const O: u8, FI = u8> =
451-
raw::FieldWriter<'a, REG, WI, O, FI, Unsafe>;
454+
pub type FieldWriter<'a, REG, const WI: u8, FI = u8> = raw::FieldWriter<'a, REG, WI, FI, Unsafe>;
452455
/// Write field Proxy with safe `bits`
453-
pub type FieldWriterSafe<'a, REG, const WI: u8, const O: u8, FI = u8> =
454-
raw::FieldWriter<'a, REG, WI, O, FI, Safe>;
456+
pub type FieldWriterSafe<'a, REG, const WI: u8, FI = u8> = raw::FieldWriter<'a, REG, WI, FI, Safe>;
455457

456-
impl<'a, REG, const WI: u8, const OF: u8, FI> FieldWriter<'a, REG, WI, OF, FI>
458+
impl<'a, REG, const WI: u8, FI> FieldWriter<'a, REG, WI, FI>
457459
where
458460
REG: Writable + RegisterSpec,
459461
FI: FieldSpec,
@@ -462,15 +464,27 @@ where
462464
/// Field width
463465
pub const WIDTH: u8 = WI;
464466

467+
/// Field width
468+
#[inline(always)]
469+
pub const fn width(&self) -> u8 {
470+
WI
471+
}
472+
473+
/// Field offset
474+
#[inline(always)]
475+
pub const fn offset(&self) -> u8 {
476+
self.o
477+
}
478+
465479
/// Writes raw bits to the field
466480
///
467481
/// # Safety
468482
///
469483
/// Passing incorrect value can cause undefined behaviour. See reference manual
470484
#[inline(always)]
471485
pub unsafe fn bits(self, value: FI::Ux) -> &'a mut W<REG> {
472-
self.w.bits &= !(REG::Ux::mask::<WI>() << OF);
473-
self.w.bits |= (REG::Ux::from(value) & REG::Ux::mask::<WI>()) << OF;
486+
self.w.bits &= !(REG::Ux::mask::<WI>() << self.o);
487+
self.w.bits |= (REG::Ux::from(value) & REG::Ux::mask::<WI>()) << self.o;
474488
self.w
475489
}
476490
/// Writes `variant` to the field
@@ -480,7 +494,7 @@ where
480494
}
481495
}
482496

483-
impl<'a, REG, const WI: u8, const OF: u8, FI> FieldWriterSafe<'a, REG, WI, OF, FI>
497+
impl<'a, REG, const WI: u8, FI> FieldWriterSafe<'a, REG, WI, FI>
484498
where
485499
REG: Writable + RegisterSpec,
486500
FI: FieldSpec,
@@ -489,11 +503,23 @@ where
489503
/// Field width
490504
pub const WIDTH: u8 = WI;
491505

506+
/// Field width
507+
#[inline(always)]
508+
pub const fn width(&self) -> u8 {
509+
WI
510+
}
511+
512+
/// Field offset
513+
#[inline(always)]
514+
pub const fn offset(&self) -> u8 {
515+
self.o
516+
}
517+
492518
/// Writes raw bits to the field
493519
#[inline(always)]
494520
pub fn bits(self, value: FI::Ux) -> &'a mut W<REG> {
495-
self.w.bits &= !(REG::Ux::mask::<WI>() << OF);
496-
self.w.bits |= (REG::Ux::from(value) & REG::Ux::mask::<WI>()) << OF;
521+
self.w.bits &= !(REG::Ux::mask::<WI>() << self.o);
522+
self.w.bits |= (REG::Ux::from(value) & REG::Ux::mask::<WI>()) << self.o;
497523
self.w
498524
}
499525
/// Writes `variant` to the field
@@ -509,21 +535,33 @@ macro_rules! bit_proxy {
509535
pub struct $mwv;
510536

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

514-
impl<'a, REG, const OF: u8, FI> $writer<'a, REG, OF, FI>
540+
impl<'a, REG, FI> $writer<'a, REG, FI>
515541
where
516542
REG: Writable + RegisterSpec,
517543
bool: From<FI>,
518544
{
519545
/// Field width
520546
pub const WIDTH: u8 = 1;
521547

548+
/// Field width
549+
#[inline(always)]
550+
pub const fn width(&self) -> u8 {
551+
Self::WIDTH
552+
}
553+
554+
/// Field offset
555+
#[inline(always)]
556+
pub const fn offset(&self) -> u8 {
557+
self.o
558+
}
559+
522560
/// Writes bit to the field
523561
#[inline(always)]
524562
pub fn bit(self, value: bool) -> &'a mut W<REG> {
525-
self.w.bits &= !(REG::Ux::one() << OF);
526-
self.w.bits |= (REG::Ux::from(value) & REG::Ux::one()) << OF;
563+
self.w.bits &= !(REG::Ux::one() << self.o);
564+
self.w.bits |= (REG::Ux::from(value) & REG::Ux::one()) << self.o;
527565
self.w
528566
}
529567
/// Writes `variant` to the field
@@ -543,99 +581,99 @@ bit_proxy!(BitWriter0S, Bit0S);
543581
bit_proxy!(BitWriter1T, Bit1T);
544582
bit_proxy!(BitWriter0T, Bit0T);
545583

546-
impl<'a, REG, const OF: u8, FI> BitWriter<'a, REG, OF, FI>
584+
impl<'a, REG, FI> BitWriter<'a, REG, FI>
547585
where
548586
REG: Writable + RegisterSpec,
549587
bool: From<FI>,
550588
{
551589
/// Sets the field bit
552590
#[inline(always)]
553591
pub fn set_bit(self) -> &'a mut W<REG> {
554-
self.w.bits |= REG::Ux::one() << OF;
592+
self.w.bits |= REG::Ux::one() << self.o;
555593
self.w
556594
}
557595
/// Clears the field bit
558596
#[inline(always)]
559597
pub fn clear_bit(self) -> &'a mut W<REG> {
560-
self.w.bits &= !(REG::Ux::one() << OF);
598+
self.w.bits &= !(REG::Ux::one() << self.o);
561599
self.w
562600
}
563601
}
564602

565-
impl<'a, REG, const OF: u8, FI> BitWriter1S<'a, REG, OF, FI>
603+
impl<'a, REG, FI> BitWriter1S<'a, REG, FI>
566604
where
567605
REG: Writable + RegisterSpec,
568606
bool: From<FI>,
569607
{
570608
/// Sets the field bit
571609
#[inline(always)]
572610
pub fn set_bit(self) -> &'a mut W<REG> {
573-
self.w.bits |= REG::Ux::one() << OF;
611+
self.w.bits |= REG::Ux::one() << self.o;
574612
self.w
575613
}
576614
}
577615

578-
impl<'a, REG, const OF: u8, FI> BitWriter0C<'a, REG, OF, FI>
616+
impl<'a, REG, FI> BitWriter0C<'a, REG, FI>
579617
where
580618
REG: Writable + RegisterSpec,
581619
bool: From<FI>,
582620
{
583621
/// Clears the field bit
584622
#[inline(always)]
585623
pub fn clear_bit(self) -> &'a mut W<REG> {
586-
self.w.bits &= !(REG::Ux::one() << OF);
624+
self.w.bits &= !(REG::Ux::one() << self.o);
587625
self.w
588626
}
589627
}
590628

591-
impl<'a, REG, const OF: u8, FI> BitWriter1C<'a, REG, OF, FI>
629+
impl<'a, REG, FI> BitWriter1C<'a, REG, FI>
592630
where
593631
REG: Writable + RegisterSpec,
594632
bool: From<FI>,
595633
{
596634
///Clears the field bit by passing one
597635
#[inline(always)]
598636
pub fn clear_bit_by_one(self) -> &'a mut W<REG> {
599-
self.w.bits |= REG::Ux::one() << OF;
637+
self.w.bits |= REG::Ux::one() << self.o;
600638
self.w
601639
}
602640
}
603641

604-
impl<'a, REG, const OF: u8, FI> BitWriter0S<'a, REG, OF, FI>
642+
impl<'a, REG, FI> BitWriter0S<'a, REG, FI>
605643
where
606644
REG: Writable + RegisterSpec,
607645
bool: From<FI>,
608646
{
609647
///Sets the field bit by passing zero
610648
#[inline(always)]
611649
pub fn set_bit_by_zero(self) -> &'a mut W<REG> {
612-
self.w.bits &= !(REG::Ux::one() << OF);
650+
self.w.bits &= !(REG::Ux::one() << self.o);
613651
self.w
614652
}
615653
}
616654

617-
impl<'a, REG, const OF: u8, FI> BitWriter1T<'a, REG, OF, FI>
655+
impl<'a, REG, FI> BitWriter1T<'a, REG, FI>
618656
where
619657
REG: Writable + RegisterSpec,
620658
bool: From<FI>,
621659
{
622660
///Toggle the field bit by passing one
623661
#[inline(always)]
624662
pub fn toggle_bit(self) -> &'a mut W<REG> {
625-
self.w.bits |= REG::Ux::one() << OF;
663+
self.w.bits |= REG::Ux::one() << self.o;
626664
self.w
627665
}
628666
}
629667

630-
impl<'a, REG, const OF: u8, FI> BitWriter0T<'a, REG, OF, FI>
668+
impl<'a, REG, FI> BitWriter0T<'a, REG, FI>
631669
where
632670
REG: Writable + RegisterSpec,
633671
bool: From<FI>,
634672
{
635673
///Toggle the field bit by passing zero
636674
#[inline(always)]
637675
pub fn toggle_bit(self) -> &'a mut W<REG> {
638-
self.w.bits &= !(REG::Ux::one() << OF);
676+
self.w.bits &= !(REG::Ux::one() << self.o);
639677
self.w
640678
}
641679
}

0 commit comments

Comments
 (0)