da14699-pac 0.2.0

Peripheral Access Crate (PAC) for DA14699.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
/*
DISCLAIMER
This software is supplied by Renesas Electronics Corporation and is only intended for use with Renesas products.
No other uses are authorized. This software is owned by Renesas Electronics Corporation and is protected under all
applicable laws, including copyright laws.
THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING THIS SOFTWARE, WHETHER EXPRESS, IMPLIED
OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NON-INFRINGEMENT.  ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY
LAW, NEITHER RENESAS ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE FOR ANY DIRECT,
INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR
ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
Renesas reserves the right, without notice, to make changes to this software and to discontinue the availability
of this software. By using this software, you agree to the additional terms and conditions found by accessing the
following link:
http://www.renesas.com/disclaimer

*/
// Generated from SVD 1.2, with svd2pac 0.6.0 on Thu, 24 Jul 2025 04:45:45 +0000

use core::convert::From;
use core::marker::PhantomData;

#[cfg(feature = "tracing")]
use crate::tracing;

#[derive(Copy, Clone, PartialEq, Eq)]
pub struct RW;
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct R;
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct W;

pub(crate) mod sealed {
    use super::*;
    pub trait Access {}
    impl Access for R {}
    impl Access for W {}
    impl Access for RW {}
    use core::ops::{BitAnd, BitAndAssign, BitOrAssign, Not, Shl, Shr};

    // It would be better with const fn
    // waiting for RFC: const functions in traits #3490
    pub trait CastFrom<A> {
        fn cast_from(val: A) -> Self;
    }

    impl CastFrom<u64> for u8 {
        #[inline(always)]
        fn cast_from(val: u64) -> Self {
            val as Self
        }
    }

    impl CastFrom<u64> for u16 {
        #[inline(always)]
        fn cast_from(val: u64) -> Self {
            val as Self
        }
    }

    impl CastFrom<u64> for u32 {
        #[inline(always)]
        fn cast_from(val: u64) -> Self {
            val as Self
        }
    }

    impl CastFrom<u64> for u64 {
        #[inline(always)]
        fn cast_from(val: u64) -> Self {
            val as Self
        }
    }

    pub trait RegNumberT:
        Copy
        + From<u8>
        + Into<u64>
        + CastFrom<u64>
        + Shr<usize, Output = Self>
        + Shl<usize, Output = Self>
        + BitAndAssign
        + BitAnd<Output = Self>
        + Not<Output = Self>
        + BitOrAssign
    {
    }
    impl RegNumberT for u8 {}
    impl RegNumberT for u16 {}
    impl RegNumberT for u32 {}
    impl RegNumberT for u64 {}

    pub trait RegSpec {
        type DataType: RegNumberT;
    }
}

pub trait Access: sealed::Access + Copy {}
impl Access for R {}
impl Access for W {}
impl Access for RW {}

pub trait Read: Access {}
impl Read for RW {}
impl Read for R {}

pub trait Write: Access {}
impl Write for RW {}
impl Write for W {}

#[derive(Copy, Clone, PartialEq, Eq)]
pub struct Reg<T, A: Access> {
    phantom: PhantomData<*mut (T, A)>,
}
unsafe impl<T, A: Access> Send for Reg<T, A> {}
unsafe impl<T, A: Access> Sync for Reg<T, A> {}

use sealed::CastFrom;

use sealed::{RegNumberT, RegSpec};
#[doc(hidden)]
#[derive(Copy, Clone)]
pub struct RegValueT<Reg: sealed::RegSpec> {
    pub(crate) data: Reg::DataType,
    pub(crate) mask: Reg::DataType,
}

pub trait RegisterValue<T: RegSpec> {
    /// Create a register value that could be written to a register from raw integer
    ///
    /// ```rust, ignore
    /// // example with generic names
    /// // needs: use test_pac::{timer, RegisterValue, TIMER}
    /// let to_write = timer::BitfieldReg::new(0xdeadbeef);
    /// TIMER.bitfield_reg().write(to_write);
    /// let to_write = to_write.boolw().set(true);
    /// TIMER.bitfield_reg().write(to_write);
    /// ```
    #[must_use]
    fn new(data: T::DataType) -> Self;

    /// Get raw integer from value read from register
    ///
    /// ```rust,ignore
    /// // example with generic names
    /// // needs: use pac::{RegisterValue, TIMER}
    /// let x = TIMER.bitfield_reg().read().get_raw();
    /// ```
    #[must_use]
    fn get_raw(&self) -> T::DataType;

    /// Prepare a register value that could be written to a register with an arbitrary value
    ///
    /// Use this function for setting a register to a custom value, independent
    /// of bitfields, enumerations, etc. No checks are performed on the passed
    /// value. The whole register is updated on write.
    ///
    /// ```rust,ignore
    /// // example with generic names
    /// // needs: use pac::{RegisterValue, TIMER}
    /// TIMER.bitfield_reg().init(|r| r.set_raw(0xdeadbeef))
    /// ```
    #[must_use]
    fn set_raw(self, value: T::DataType) -> Self;
}

impl<T: RegSpec> RegisterValue<T> for RegValueT<T> {
    /// Create a register value that could be written to a register from raw integer
    ///
    /// ```rust, ignore
    /// // example with generic names
    /// // needs: use pac::{timer, RegisterValue, TIMER}
    /// let to_write = timer::BitfieldReg::new(0xdeadbeef);
    /// TIMER.bitfield_reg().write(to_write);
    /// let to_write = to_write.boolw().set(true);
    /// TIMER.bitfield_reg().write(to_write);
    /// ```
    #[inline(always)]
    fn new(data: T::DataType) -> RegValueT<T> {
        Self {
            data,
            mask: 0x0u8.into(),
        }
    }

    /// Get raw integer from value read from register
    ///
    /// ```rust,ignore
    /// // example with generic names
    /// // needs: use pac::{RegisterValue, TIMER}
    /// let x = TIMER.bitfield_reg().read().get_raw();
    /// ```
    #[inline(always)]
    fn get_raw(&self) -> T::DataType {
        self.data
    }

    /// Prepare a register value that could be written to a register with an arbitrary value
    ///
    /// Use this function for setting a register to a custom value, independent
    /// of bitfields, enumerations, etc. No checks are performed on the passed
    /// value.
    ///
    /// ```rust,ignore
    /// // example with generic names
    /// // needs: use pac::{RegisterValue, TIMER}
    /// TIMER.bitfield_reg().init(|r| r.set_raw(0xdeadbeef))
    /// ```
    #[inline(always)]
    fn set_raw(mut self, value: T::DataType) -> Self {
        self.data = value;
        self.mask = !(Into::<T::DataType>::into(0x0u8));
        self
    }
}

pub trait NoBitfieldReg<Reg: RegSpec>: RegisterValue<Reg>
where
    Self: Sized,
{
    /// Get value read from register
    ///
    /// ```rust,ignore
    /// // example with generic names
    /// // needs: use pac::{NoBitfieldReg, TIMER}
    /// let x = TIMER.nobitfield_reg().read().get();
    /// ```
    #[inline(always)]
    #[must_use]
    fn get(&self) -> Reg::DataType {
        self.get_raw()
    }

    /// Prepare value to be written to register
    ///
    /// ```rust,ignore
    /// // example with generic names
    /// // needs: use pac::{NoBitfieldReg, TIMER}
    /// TIMER.nobitfield_reg().init(|r| r.set(0xc0ffee));
    /// ```
    #[inline(always)]
    #[must_use]
    fn set(self, value: Reg::DataType) -> Self {
        self.set_raw(value)
    }
}

impl<T, A> Reg<T, A>
where
    T: RegSpec,
    A: Access,
{
    #[allow(dead_code)]
    #[inline(always)]
    #[must_use]
    #[allow(dead_code)]
    pub(crate) const fn from_ptr(ptr: *mut u8) -> &'static Self {
        unsafe { &*(ptr as *const Self) }
    }

    #[inline(always)]
    #[must_use]
    pub const fn ptr(&self) -> *mut T::DataType {
        self as *const _ as *mut T::DataType
    }

    /// Returns the address of the register.
    pub fn addr(&self) -> usize {
        (self as *const _) as usize
    }
}

impl<T, A> Reg<T, A>
where
    T: RegSpec,
    A: Read,
{
    /// Read register and return a register value
    ///
    /// # Safety
    /// Read operation could cause undefined behavior for some peripheral. Developer shall read device user manual.
    /// Register is Send and Sync to allow complete freedom. Developer is responsible of proper use in interrupt and thread.
    ///
    /// # Example
    /// ```rust,ignore
    /// // example with generic names
    /// let reg = unsafe { TIMER.bitfield_reg().read() };
    /// if reg.boolr().get() { /* ... */ }
    /// ```
    #[inline(always)]
    #[must_use]
    pub unsafe fn read(&self) -> RegValueT<T> {
        unsafe {
            #[cfg(feature = "tracing")]
            let val = {
                let mut buf: u64 = 0x0;
                tracing::READ_FN.with(|rf| {
                if let Some(rf) = rf.get() {
                    buf = rf(self.addr(), std::mem::size_of::<T::DataType>());
                } else {
                    #[cfg(not(feature = "tracing_dummy"))]
                    panic!(
                        "Please, provide an handler for read with tracing::set_read_fn(callback);"
                    );
                }
            });
                T::DataType::cast_from(buf)
            };
            #[cfg(not(feature = "tracing"))]
            let val = self.ptr().read_volatile();
            RegValueT::<T>::new(val)
        }
    }
}

impl<T, A> Reg<T, A>
where
    T: RegSpec,
    A: Write,
{
    /// Write register value back to register
    ///
    /// # Arguments
    ///
    /// * `reg_value` - A string slice that holds the name of the person
    ///
    /// # Safety
    /// Write operation could cause undefined behavior for some peripheral. Developers shall read the device user manual.
    /// Register is Send and Sync to allow complete freedom. Developers are responsible of proper use in interrupt and thread.
    ///
    /// # Example
    /// ```rust,ignore
    /// // example with generic names
    /// // write with a previously read value
    /// let reg = unsafe { TIMER.bitfield_reg().read() };
    /// // or start with a known value
    /// let reg = timer::BitfieldReg::new(0).bitfieldw().set(0x55);
    /// // or start with the register default
    /// let reg = timer::BitfieldReg::default();
    ///
    /// let reg = reg.bitfieldrw().set(0x77);
    ///
    /// // no change has taken place to the register due to `set` calls - do that now by writing back the result
    /// unsafe { TIMER.bitfield_reg().write(reg) }
    /// ```
    /// See also: [`Reg<T, A>::init`] which provides the default value to a closure
    #[inline(always)]
    pub unsafe fn write(&self, reg_value: RegValueT<T>) {
        unsafe {
            #[cfg(feature = "tracing")]
            tracing::WRITE_FN.with(|wf| {
                if let Some(wf) = wf.get() {
                    wf(
                        self.addr(),
                        std::mem::size_of::<T::DataType>(),
                        reg_value.data.into(),
                    )
                } else {
                    #[cfg(not(feature = "tracing_dummy"))]
                    panic!(
                        "Please, provide an handler for read with tracing::set_read_fn(callback);"
                    );
                }
            });
            #[cfg(not(feature = "tracing"))]
            self.ptr().write_volatile(reg_value.data);
        }
    }

    /// Write an arbitrary integer to register
    ///
    /// Use this function when e.g. loading data to be written from a config-page.
    /// For normal use prefer either [`Reg<T, A>::write`] if the value was read before, or [`Reg<T, A>::init`],
    /// both of which provide some restrictions available register fields, enums, etc.
    ///
    /// # Arguments
    ///
    /// * `value` - The unchecked value to be written to the register
    ///
    /// # Safety
    ///
    /// Write operation could cause undefined behavior for some peripheral. Developers shall read the device user manual.
    /// Register is Send and Sync to allow complete freedom. Developers are responsible of proper use in interrupt and thread.
    ///
    /// # Example
    /// ```rust,ignore
    /// // example with generic names
    /// unsafe { TIMER.bitfield_reg().write_raw(0xdead) }
    /// ```
    /// See also [`Reg<T, A>::init`] and [`Reg<T, A>::write`] both of which are the safe, preferred functions.
    #[inline(always)]
    pub unsafe fn write_raw(&self, value: T::DataType) {
        unsafe {
            #[cfg(feature = "tracing")]
            tracing::WRITE_FN.with(|wf| {
                if let Some(wf) = wf.get() {
                    wf(
                        self.addr(),
                        std::mem::size_of::<T::DataType>(),
                        value.into(),
                    )
                } else {
                    #[cfg(not(feature = "tracing_dummy"))]
                    panic!(
                        "Please, provide an handler for read with tracing::set_read_fn(callback);"
                    );
                }
            });
            #[cfg(not(feature = "tracing"))]
            self.ptr().write_volatile(value);
        }
    }
}

impl<T, A> Reg<T, A>
where
    T: RegSpec,
    A: Write,
    RegValueT<T>: Default,
{
    /// Write register with register value built from default register value
    ///
    /// # Arguments
    ///
    /// * `f` - Closure that receive as input a register value initialized with register value at Power On Reset.
    ///
    /// # Safety
    /// Write operation could cause undefined behavior for some peripheral. Developer shall read device user manual.
    /// Register is Send and Sync to allow complete freedom. Developer is responsible of proper use in interrupt and thread.
    ///
    /// # Example
    /// ```rust,ignore
    /// // example with generic names
    /// TIMER
    ///     .bitfield_reg()
    ///     .init(|r| r.bitfieldw().set(0b1010).boolw().set(true));
    /// ```
    #[inline(always)]
    /// Write value computed by closure that receive as input the reset value of register
    pub unsafe fn init(&self, f: impl FnOnce(RegValueT<T>) -> RegValueT<T>) {
        unsafe {
            let val = RegValueT::<T>::default();
            let res = f(val);
            self.write(res);
        }
    }
}

impl<T, A> Reg<T, A>
where
    T: RegSpec,
    A: Read + Write,
{
    /// Read/modify/write register
    ///
    /// # Arguments
    ///
    /// * `f` - Closure that receive as input a register value read from register. The result of the closure
    ///   is written back to the register.
    ///
    /// # Safety
    /// Write operation could cause undefined behavior for some peripheral. Developer shall read device user manual.
    /// Register is Send and Sync to allow complete freedom. Developer is responsible of proper use in interrupt and thread.
    ///
    /// # Example
    /// ```rust,ignore
    /// // example with generic names
    /// TIMER
    ///     .bitfield_reg()
    ///     .modify(|r| r.boolrw().set(!r.boolrw().get()));
    /// ```
    #[inline(always)]
    pub unsafe fn modify(&self, f: impl FnOnce(RegValueT<T>) -> RegValueT<T>) {
        unsafe {
            let val = self.read();
            let res = f(val);
            self.write(res);
        }
    }
}

/// Proxy struct for enumerated bitfields
#[repr(transparent)]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct EnumBitfieldStruct<Q: RegNumberT, T>(pub Q, PhantomData<T>);

impl<Q: RegNumberT, T> EnumBitfieldStruct<Q, T> {
    pub const fn new(value: Q) -> Self {
        Self(value, PhantomData)
    }
}

impl<Q: RegNumberT, T> From<EnumBitfieldStruct<Q, T>> for u64 {
    #[inline(always)]
    fn from(value: EnumBitfieldStruct<Q, T>) -> Self {
        value.0.into()
    }
}
impl<Q: RegNumberT, T> CastFrom<u64> for EnumBitfieldStruct<Q, T> {
    #[inline(always)]
    fn cast_from(val: u64) -> Self {
        Self(Q::cast_from(val), PhantomData)
    }
}

impl<Q: RegNumberT, T> From<Q> for EnumBitfieldStruct<Q, T> {
    #[inline(always)]
    fn from(value: Q) -> Self {
        Self(value, PhantomData)
    }
}

/// Proxy struct for numeric bitfields
pub struct RegisterField<
    const START_OFFSET: usize,
    const MASK: u64,
    const DIM: u8,
    const DIM_INCREMENT: u8,
    ValueTypeRead,
    ValueTypeWrite,
    T,
    A,
> where
    T: RegSpec,
    A: Access,
{
    data: RegValueT<T>,
    index: u8,
    marker: PhantomData<(ValueTypeRead, ValueTypeWrite, A)>,
}

impl<
    const START_OFFSET: usize,
    const MASK: u64,
    const DIM: u8,
    const DIM_INCREMENT: u8,
    ValueTypeRead,
    ValueTypeWrite,
    T,
    A,
> RegisterField<START_OFFSET, MASK, DIM, DIM_INCREMENT, ValueTypeRead, ValueTypeWrite, T, A>
where
    T: RegSpec,
    A: Access,
{
    #[allow(dead_code)]
    #[inline(always)]
    pub(crate) fn from_register(data: RegValueT<T>, index: u8) -> Self {
        Self {
            data,
            index,
            marker: PhantomData,
        }
    }

    /// Get mask for bitfield, the mask is unshifted and at offset 0
    ///
    /// Prefer the use of [`RegisterField<START_OFFSET, MASK, DIM, DIM_INCREMENT, ValueTypeRead,ValueTypeWrite, T, A>::get()`] to
    /// extract a bitfield value.
    #[inline(always)]
    #[must_use]
    pub fn mask(&self) -> T::DataType {
        T::DataType::cast_from(MASK)
    }

    /// Get offset of bitfield in containing register
    ///
    /// Prefer the use of [`RegisterField<START_OFFSET, MASK, DIM, DIM_INCREMENT, ValueTypeRead,ValueTypeWrite, T, A>::get()`] to
    /// extract a bitfield value.
    #[inline(always)]
    #[must_use]
    pub const fn offset(&self) -> usize {
        START_OFFSET + (self.index * DIM_INCREMENT) as usize
    }
}

impl<
    const START_OFFSET: usize,
    const MASK: u64,
    const DIM: u8,
    const DIM_INCREMENT: u8,
    ValueTypeRead,
    ValueTypeWrite,
    T,
    A,
> RegisterField<START_OFFSET, MASK, DIM, DIM_INCREMENT, ValueTypeRead, ValueTypeWrite, T, A>
where
    T: RegSpec,
    A: Read,
    ValueTypeRead: CastFrom<u64>,
{
    /// Extract bitfield from read register value
    #[inline(always)]
    pub fn get(&self) -> ValueTypeRead {
        let offset = START_OFFSET + (self.index * DIM_INCREMENT) as usize;
        let filtered: T::DataType = (self.data.data >> offset) & T::DataType::cast_from(MASK);
        ValueTypeRead::cast_from(filtered.into())
    }
}

impl<
    const START_OFFSET: usize,
    const MASK: u64,
    const DIM: u8,
    const DIM_INCREMENT: u8,
    ValueTypeRead,
    ValueTypeWrite,
    T,
    A,
> RegisterField<START_OFFSET, MASK, DIM, DIM_INCREMENT, ValueTypeRead, ValueTypeWrite, T, A>
where
    T: RegSpec,
    A: Write,
    u64: From<ValueTypeWrite>,
{
    /// Prepare bitfield value that could be written to register
    ///
    /// # Example
    /// ```rust,ignore
    /// // example with generic names
    /// // get an instance by reading
    /// let values = TIMER.bitfield_reg().read();
    /// // or by starting with a known value
    /// let value = timer::BitfieldReg::new(0);
    /// // or by starting with the default
    /// let value = timer::BitfieldReg::default();
    ///
    /// // set bitfields
    /// let value = value
    ///     // set numeric bitfield
    ///     .bitfieldw()
    ///     .set(0x55)
    ///     // set enumerated bitfield with enumeration
    ///     .bitfieldenumerated()
    ///     .set(timer::bitfield_reg::BitfieldEnumerated::GPIOA_0)
    ///     // set enumerated bitfield from integer
    ///     .bitfieldenumerated()
    ///     .set(1.into());
    ///
    /// // up until now no hardware change has taken place, do that now by writing
    /// TIMER.bitfield_reg().write(value);
    /// ```
    #[inline(always)]
    #[must_use]
    pub fn set(mut self, value: ValueTypeWrite) -> RegValueT<T> {
        let mask = T::DataType::cast_from(MASK);
        let value: T::DataType = T::DataType::cast_from(Into::<u64>::into(value)) & mask;
        let offset = START_OFFSET + (self.index * DIM_INCREMENT) as usize;
        let masked_offset: T::DataType = mask << offset;
        self.data.mask |= masked_offset;
        self.data.data &= !masked_offset;
        self.data.data |= value << offset;
        self.data
    }
}

/// Proxy struct for boolean bitfields
pub struct RegisterFieldBool<
    const START_OFFSET: usize,
    const DIM: u8,
    const DIM_INCREMENT: u8,
    T,
    A,
> where
    T: RegSpec,
    A: Access,
{
    data: RegValueT<T>,
    index: u8,
    marker: PhantomData<A>,
}

impl<const START_OFFSET: usize, const DIM: u8, const DIM_INCREMENT: u8, T, A>
    RegisterFieldBool<START_OFFSET, DIM, DIM_INCREMENT, T, A>
where
    T: RegSpec,
    A: Read,
{
    /// Extract bitfield from read register value
    #[inline(always)]
    pub fn get(&self) -> bool {
        let offset = START_OFFSET + (self.index * DIM_INCREMENT) as usize;
        let filtered = (self.data.data.into() >> offset) & 1;
        filtered == 1
    }
}

impl<const START_OFFSET: usize, const DIM: u8, const DIM_INCREMENT: u8, T, A>
    RegisterFieldBool<START_OFFSET, DIM, DIM_INCREMENT, T, A>
where
    T: RegSpec,
    A: Write,
{
    /// Prepare bitfield value to be written to register
    ///
    /// # Example
    /// ```rust,ignore
    /// // example with generic names
    /// // get an instance by reading
    /// let values = TIMER.bitfield_reg().read();
    /// // or by starting with a known value
    /// let value = timer::BitfieldReg::new(0);
    /// // or by starting with the default
    /// let value = timer::BitfieldReg::default();
    ///
    /// // set bitfield
    /// let value = value
    ///     .boolrw()
    ///     .set(true);
    ///
    /// // up until now no hardware change has taken place, do that now by writing
    /// TIMER.bitfield_reg().write(value);
    /// ```
    #[inline(always)]
    #[must_use]
    pub fn set(mut self, value: bool) -> RegValueT<T> {
        let value: T::DataType = if value {
            T::DataType::cast_from(1u64)
        } else {
            T::DataType::cast_from(0u64)
        };
        let offset = START_OFFSET + (self.index * DIM_INCREMENT) as usize;
        let masked_offset = T::DataType::cast_from(0x1u64) << offset;
        self.data.mask |= masked_offset;
        self.data.data &= !masked_offset;
        self.data.data |= value << offset;
        self.data
    }
}

impl<const START_OFFSET: usize, const DIM: u8, const DIM_INCREMENT: u8, T, A>
    RegisterFieldBool<START_OFFSET, DIM, DIM_INCREMENT, T, A>
where
    T: RegSpec,
    A: Access,
{
    #[inline(always)]
    #[allow(dead_code)]
    pub(crate) fn from_register(data: RegValueT<T>, index: u8) -> Self {
        Self {
            data,
            index,
            marker: PhantomData,
        }
    }

    /// Get mask for bitfield, the mask is unshifted and at offset 0
    ///
    /// Prefer the use of [`RegisterField<START_OFFSET, MASK, DIM, DIM_INCREMENT, ValueType, T, A>::get()`] to
    /// extract a bitfield value.
    #[inline(always)]
    #[must_use]
    pub fn mask(&self) -> T::DataType {
        T::DataType::cast_from(1)
    }

    /// Get offset of bitfield in containing register
    ///
    /// Prefer the use of [`RegisterField<START_OFFSET, MASK, DIM, DIM_INCREMENT, ValueType, T, A>::get()`] to
    /// extract a bitfield value.
    #[inline(always)]
    #[must_use]
    pub const fn offset(&self) -> usize {
        START_OFFSET + (self.index * DIM_INCREMENT) as usize
    }
}

/// An array of identical register clusters.
pub struct ClusterRegisterArray<T: Sized, const DIM: usize, const DIM_INCREMENT: usize> {
    _t: ::core::marker::PhantomData<T>,
}

impl<T: Sized, const DIM: usize, const DIM_INCREMENT: usize>
    ClusterRegisterArray<T, DIM, DIM_INCREMENT>
{
    /// Returns the number of register blocks in the cluster.
    #[inline(always)]
    pub const fn len(&self) -> usize {
        DIM
    }

    /// Returns whether the cluster is empty (DIM == 0).
    #[inline(always)]
    pub const fn is_empty(&self) -> bool {
        DIM == 0
    }

    /// Returns an iterator over the elements of this cluster.
    #[inline(always)]
    pub fn iter(&self) -> impl ::core::iter::ExactSizeIterator<Item = &T> {
        self.into_iter()
    }

    /// Returns the cluster element with the specified index.
    ///
    /// Panics if the index is out of bounds.
    #[inline]
    pub const fn get(&self, index: usize) -> &T {
        assert!(index < DIM);
        unsafe { self.get_unchecked(index) }
    }

    /// Returns the cluster element with the specified index.
    ///
    /// # Safety
    ///
    /// `index` must be less than `DIM`.
    #[inline(always)]
    pub const unsafe fn get_unchecked(&self, index: usize) -> &T {
        unsafe { &*(self.as_ptr().add(index * DIM_INCREMENT) as *const _) }
    }

    #[allow(dead_code)]
    #[inline(always)]
    pub(crate) const unsafe fn from_ptr(ptr: *mut u8) -> &'static Self {
        unsafe { &*(ptr as *const Self) }
    }

    #[inline(always)]
    const fn as_ptr(&self) -> *mut u8 {
        self as *const _ as *mut _
    }
}

impl<T: Sized, const DIM: usize, const DIM_INCREMENT: usize> ::core::ops::Index<usize>
    for ClusterRegisterArray<T, DIM, DIM_INCREMENT>
{
    type Output = T;

    #[inline(always)]
    fn index(&self, index: usize) -> &T {
        self.get(index)
    }
}

impl<'a, T: Sized, const DIM: usize, const DIM_INCREMENT: usize> IntoIterator
    for &'a ClusterRegisterArray<T, DIM, DIM_INCREMENT>
{
    type Item = &'a T;
    type IntoIter = ClusterRegisterArrayIterator<'a, T, DIM, DIM_INCREMENT>;

    #[inline(always)]
    fn into_iter(self) -> Self::IntoIter {
        ClusterRegisterArrayIterator {
            array: self,
            index: 0,
        }
    }
}

pub struct ClusterRegisterArrayIterator<'a, T: Sized, const DIM: usize, const DIM_INCREMENT: usize>
{
    array: &'a ClusterRegisterArray<T, DIM, DIM_INCREMENT>,
    index: usize,
}

impl<'a, T: Sized, const DIM: usize, const DIM_INCREMENT: usize> Iterator
    for ClusterRegisterArrayIterator<'a, T, DIM, DIM_INCREMENT>
{
    type Item = &'a T;
    #[inline(always)]
    fn next(&mut self) -> Option<&'a T> {
        if self.index < self.array.len() {
            let result = &self.array[self.index];
            self.index += 1;
            Some(result)
        } else {
            None
        }
    }

    #[inline(always)]
    fn size_hint(&self) -> (usize, Option<usize>) {
        let len = self.array.len() - self.index;
        (len, Some(len))
    }
}

impl<T: Sized, const DIM: usize, const DIM_INCREMENT: usize> ExactSizeIterator
    for ClusterRegisterArrayIterator<'_, T, DIM, DIM_INCREMENT>
{
}