Skip to main content

ra6m3_fsp_pac/
generic.rs

1use core::marker;
2/// Generic peripheral accessor
3pub struct Periph<RB, const A: usize> {
4    _marker: marker::PhantomData<RB>,
5}
6unsafe impl<RB, const A: usize> Send for Periph<RB, A> {}
7impl<RB, const A: usize> Periph<RB, A> {
8    ///Pointer to the register block
9    pub const PTR: *const RB = A as *const _;
10    ///Return the pointer to the register block
11    #[inline(always)]
12    pub const fn ptr() -> *const RB {
13        Self::PTR
14    }
15    /// Steal an instance of this peripheral
16    ///
17    /// # Safety
18    ///
19    /// Ensure that the new instance of the peripheral cannot be used in a way
20    /// that may race with any existing instances, for example by only
21    /// accessing read-only or write-only registers, or by consuming the
22    /// original peripheral and using critical sections to coordinate
23    /// access between multiple new instances.
24    ///
25    /// Additionally, other software such as HALs may rely on only one
26    /// peripheral instance existing to ensure memory safety; ensure
27    /// no stolen instances are passed to such software.
28    pub unsafe fn steal() -> Self {
29        Self {
30            _marker: marker::PhantomData,
31        }
32    }
33}
34impl<RB, const A: usize> core::ops::Deref for Periph<RB, A> {
35    type Target = RB;
36    #[inline(always)]
37    fn deref(&self) -> &Self::Target {
38        unsafe { &*Self::PTR }
39    }
40}
41/// Raw register type (`u8`, `u16`, `u32`, ...)
42pub trait RawReg:
43    Copy
44    + From<bool>
45    + core::ops::BitOr<Output = Self>
46    + core::ops::BitAnd<Output = Self>
47    + core::ops::BitOrAssign
48    + core::ops::BitAndAssign
49    + core::ops::Not<Output = Self>
50    + core::ops::Shl<u8, Output = Self>
51{
52    /// Mask for bits of width `WI`
53    fn mask<const WI: u8>() -> Self;
54    /// `0`
55    const ZERO: Self;
56    /// `1`
57    const ONE: Self;
58}
59macro_rules! raw_reg {
60    ($U:ty, $size:literal, $mask:ident) => {
61        impl RawReg for $U {
62            #[inline(always)]
63            fn mask<const WI: u8>() -> Self {
64                $mask::<WI>()
65            }
66            const ZERO: Self = 0;
67            const ONE: Self = 1;
68        }
69        const fn $mask<const WI: u8>() -> $U {
70            <$U>::MAX >> ($size - WI)
71        }
72        impl FieldSpec for $U {
73            type Ux = $U;
74        }
75    };
76}
77raw_reg!(u8, 8, mask_u8);
78raw_reg!(u16, 16, mask_u16);
79raw_reg!(u32, 32, mask_u32);
80raw_reg!(u64, 64, mask_u64);
81/// Raw register type
82pub trait RegisterSpec {
83    /// Raw register type (`u8`, `u16`, `u32`, ...).
84    type Ux: RawReg;
85}
86/// Raw field type
87pub trait FieldSpec: Sized {
88    /// Raw field type (`u8`, `u16`, `u32`, ...).
89    type Ux: Copy + core::fmt::Debug + PartialEq + From<Self>;
90}
91/// Marker for fields with fixed values
92pub trait IsEnum: FieldSpec {}
93/// Trait implemented by readable registers to enable the `read` method.
94///
95/// Registers marked with `Writable` can be also be `modify`'ed.
96pub trait Readable: RegisterSpec {}
97/// Trait implemented by writeable registers.
98///
99/// This enables the  `write`, `write_with_zero` and `reset` methods.
100///
101/// Registers marked with `Readable` can be also be `modify`'ed.
102pub trait Writable: RegisterSpec {
103    /// Is it safe to write any bits to register
104    type Safety;
105    /// Specifies the register bits that are not changed if you pass `1` and are changed if you pass `0`
106    const ZERO_TO_MODIFY_FIELDS_BITMAP: Self::Ux = Self::Ux::ZERO;
107    /// Specifies the register bits that are not changed if you pass `0` and are changed if you pass `1`
108    const ONE_TO_MODIFY_FIELDS_BITMAP: Self::Ux = Self::Ux::ZERO;
109}
110/// Reset value of the register.
111///
112/// This value is the initial value for the `write` method. It can also be directly written to the
113/// register by using the `reset` method.
114pub trait Resettable: RegisterSpec {
115    /// Reset value of the register.
116    const RESET_VALUE: Self::Ux = Self::Ux::ZERO;
117    /// Reset value of the register.
118    #[inline(always)]
119    fn reset_value() -> Self::Ux {
120        Self::RESET_VALUE
121    }
122}
123#[doc(hidden)]
124pub mod raw;
125/// Register reader.
126///
127/// Result of the `read` methods of registers. Also used as a closure argument in the `modify`
128/// method.
129pub type R<REG> = raw::R<REG>;
130impl<REG: RegisterSpec> R<REG> {
131    /// Reads raw bits from register.
132    #[inline(always)]
133    pub const fn bits(&self) -> REG::Ux {
134        self.bits
135    }
136}
137impl<REG: RegisterSpec, FI> PartialEq<FI> for R<REG>
138where
139    REG::Ux: PartialEq,
140    FI: Copy,
141    REG::Ux: From<FI>,
142{
143    #[inline(always)]
144    fn eq(&self, other: &FI) -> bool {
145        self.bits.eq(&REG::Ux::from(*other))
146    }
147}
148/// Register writer.
149///
150/// Used as an argument to the closures in the `write` and `modify` methods of the register.
151pub type W<REG> = raw::W<REG>;
152impl<REG: Writable> W<REG> {
153    /// Writes raw bits to the register.
154    ///
155    /// # Safety
156    ///
157    /// Passing incorrect value can cause undefined behaviour. See reference manual
158    #[inline(always)]
159    pub unsafe fn bits(&mut self, bits: REG::Ux) -> &mut Self {
160        self.bits = bits;
161        self
162    }
163}
164impl<REG> W<REG>
165where
166    REG: Writable<Safety = Safe>,
167{
168    /// Writes raw bits to the register.
169    #[inline(always)]
170    pub fn set(&mut self, bits: REG::Ux) -> &mut Self {
171        self.bits = bits;
172        self
173    }
174}
175/// Field reader.
176///
177/// Result of the `read` methods of fields.
178pub type FieldReader<FI = u8> = raw::FieldReader<FI>;
179/// Bit-wise field reader
180pub type BitReader<FI = bool> = raw::BitReader<FI>;
181impl<FI: FieldSpec> FieldReader<FI> {
182    /// Reads raw bits from field.
183    #[inline(always)]
184    pub const fn bits(&self) -> FI::Ux {
185        self.bits
186    }
187}
188impl<FI: FieldSpec> core::fmt::Debug for FieldReader<FI> {
189    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
190        core::fmt::Debug::fmt(&self.bits, f)
191    }
192}
193impl<FI> PartialEq<FI> for FieldReader<FI>
194where
195    FI: FieldSpec + Copy,
196{
197    #[inline(always)]
198    fn eq(&self, other: &FI) -> bool {
199        self.bits.eq(&FI::Ux::from(*other))
200    }
201}
202impl<FI> PartialEq<FI> for BitReader<FI>
203where
204    FI: Copy,
205    bool: From<FI>,
206{
207    #[inline(always)]
208    fn eq(&self, other: &FI) -> bool {
209        self.bits.eq(&bool::from(*other))
210    }
211}
212impl<FI> BitReader<FI> {
213    /// Value of the field as raw bits.
214    #[inline(always)]
215    pub const fn bit(&self) -> bool {
216        self.bits
217    }
218    /// Returns `true` if the bit is clear (0).
219    #[inline(always)]
220    pub const fn bit_is_clear(&self) -> bool {
221        !self.bit()
222    }
223    /// Returns `true` if the bit is set (1).
224    #[inline(always)]
225    pub const fn bit_is_set(&self) -> bool {
226        self.bit()
227    }
228}
229impl<FI> core::fmt::Debug for BitReader<FI> {
230    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
231        core::fmt::Debug::fmt(&self.bits, f)
232    }
233}
234/// Marker for register/field writers which can take any value of specified width
235pub struct Safe;
236/// You should check that value is allowed to pass to register/field writer marked with this
237pub struct Unsafe;
238/// Marker for field writers are safe to write in specified inclusive range
239pub struct Range<const MIN: u64, const MAX: u64>;
240/// Marker for field writers are safe to write in specified inclusive range
241pub struct RangeFrom<const MIN: u64>;
242/// Marker for field writers are safe to write in specified inclusive range
243pub struct RangeTo<const MAX: u64>;
244/// Write field Proxy
245pub type FieldWriter<'a, REG, const WI: u8, FI = u8, Safety = Unsafe> =
246    raw::FieldWriter<'a, REG, WI, FI, Safety>;
247impl<REG, const WI: u8, FI, Safety> FieldWriter<'_, REG, WI, FI, Safety>
248where
249    REG: Writable + RegisterSpec,
250    FI: FieldSpec,
251{
252    /// Field width
253    pub const WIDTH: u8 = WI;
254    /// Field width
255    #[inline(always)]
256    pub const fn width(&self) -> u8 {
257        WI
258    }
259    /// Field offset
260    #[inline(always)]
261    pub const fn offset(&self) -> u8 {
262        self.o
263    }
264}
265impl<'a, REG, const WI: u8, FI, Safety> FieldWriter<'a, REG, WI, FI, Safety>
266where
267    REG: Writable + RegisterSpec,
268    FI: FieldSpec,
269    REG::Ux: From<FI::Ux>,
270{
271    /// Writes raw bits to the field
272    ///
273    /// # Safety
274    ///
275    /// Passing incorrect value can cause undefined behaviour. See reference manual
276    #[inline(always)]
277    pub unsafe fn bits(self, value: FI::Ux) -> &'a mut W<REG> {
278        self.w.bits &= !(REG::Ux::mask::<WI>() << self.o);
279        self.w.bits |= (REG::Ux::from(value) & REG::Ux::mask::<WI>()) << self.o;
280        self.w
281    }
282}
283impl<'a, REG, const WI: u8, FI> FieldWriter<'a, REG, WI, FI, Safe>
284where
285    REG: Writable + RegisterSpec,
286    FI: FieldSpec,
287    REG::Ux: From<FI::Ux>,
288{
289    /// Writes raw bits to the field
290    #[inline(always)]
291    pub fn set(self, value: FI::Ux) -> &'a mut W<REG> {
292        unsafe { self.bits(value) }
293    }
294}
295impl<'a, REG, const WI: u8, FI, const MIN: u64, const MAX: u64>
296    FieldWriter<'a, REG, WI, FI, Range<MIN, MAX>>
297where
298    REG: Writable + RegisterSpec,
299    FI: FieldSpec,
300    REG::Ux: From<FI::Ux>,
301    u64: From<FI::Ux>,
302{
303    /// Writes raw bits to the field
304    #[inline(always)]
305    pub fn set(self, value: FI::Ux) -> &'a mut W<REG> {
306        {
307            let value = u64::from(value);
308            assert!(value >= MIN && value <= MAX);
309        }
310        unsafe { self.bits(value) }
311    }
312}
313impl<'a, REG, const WI: u8, FI, const MIN: u64> FieldWriter<'a, REG, WI, FI, RangeFrom<MIN>>
314where
315    REG: Writable + RegisterSpec,
316    FI: FieldSpec,
317    REG::Ux: From<FI::Ux>,
318    u64: From<FI::Ux>,
319{
320    /// Writes raw bits to the field
321    #[inline(always)]
322    pub fn set(self, value: FI::Ux) -> &'a mut W<REG> {
323        {
324            let value = u64::from(value);
325            assert!(value >= MIN);
326        }
327        unsafe { self.bits(value) }
328    }
329}
330impl<'a, REG, const WI: u8, FI, const MAX: u64> FieldWriter<'a, REG, WI, FI, RangeTo<MAX>>
331where
332    REG: Writable + RegisterSpec,
333    FI: FieldSpec,
334    REG::Ux: From<FI::Ux>,
335    u64: From<FI::Ux>,
336{
337    /// Writes raw bits to the field
338    #[inline(always)]
339    pub fn set(self, value: FI::Ux) -> &'a mut W<REG> {
340        {
341            let value = u64::from(value);
342            assert!(value <= MAX);
343        }
344        unsafe { self.bits(value) }
345    }
346}
347impl<'a, REG, const WI: u8, FI, Safety> FieldWriter<'a, REG, WI, FI, Safety>
348where
349    REG: Writable + RegisterSpec,
350    FI: IsEnum,
351    REG::Ux: From<FI::Ux>,
352{
353    /// Writes `variant` to the field
354    #[inline(always)]
355    pub fn variant(self, variant: FI) -> &'a mut W<REG> {
356        unsafe { self.bits(FI::Ux::from(variant)) }
357    }
358}
359macro_rules! bit_proxy {
360    ($writer:ident, $mwv:ident) => {
361        #[doc(hidden)]
362        pub struct $mwv;
363        #[doc = " Bit-wise write field proxy"]
364        pub type $writer<'a, REG, FI = bool> = raw::BitWriter<'a, REG, FI, $mwv>;
365        impl<'a, REG, FI> $writer<'a, REG, FI>
366        where
367            REG: Writable + RegisterSpec,
368            bool: From<FI>,
369        {
370            #[doc = " Field width"]
371            pub const WIDTH: u8 = 1;
372            #[doc = " Field width"]
373            #[inline(always)]
374            pub const fn width(&self) -> u8 {
375                Self::WIDTH
376            }
377            #[doc = " Field offset"]
378            #[inline(always)]
379            pub const fn offset(&self) -> u8 {
380                self.o
381            }
382            #[doc = " Writes bit to the field"]
383            #[inline(always)]
384            pub fn bit(self, value: bool) -> &'a mut W<REG> {
385                self.w.bits &= !(REG::Ux::ONE << self.o);
386                self.w.bits |= (REG::Ux::from(value) & REG::Ux::ONE) << self.o;
387                self.w
388            }
389            #[doc = " Writes `variant` to the field"]
390            #[inline(always)]
391            pub fn variant(self, variant: FI) -> &'a mut W<REG> {
392                self.bit(bool::from(variant))
393            }
394        }
395    };
396}
397bit_proxy!(BitWriter, BitM);
398bit_proxy!(BitWriter1S, Bit1S);
399bit_proxy!(BitWriter0C, Bit0C);
400bit_proxy!(BitWriter1C, Bit1C);
401bit_proxy!(BitWriter0S, Bit0S);
402bit_proxy!(BitWriter1T, Bit1T);
403bit_proxy!(BitWriter0T, Bit0T);
404impl<'a, REG, FI> BitWriter<'a, REG, FI>
405where
406    REG: Writable + RegisterSpec,
407    bool: From<FI>,
408{
409    /// Sets the field bit
410    #[inline(always)]
411    pub fn set_bit(self) -> &'a mut W<REG> {
412        self.w.bits |= REG::Ux::ONE << self.o;
413        self.w
414    }
415    /// Clears the field bit
416    #[inline(always)]
417    pub fn clear_bit(self) -> &'a mut W<REG> {
418        self.w.bits &= !(REG::Ux::ONE << self.o);
419        self.w
420    }
421}
422impl<'a, REG, FI> BitWriter1S<'a, REG, FI>
423where
424    REG: Writable + RegisterSpec,
425    bool: From<FI>,
426{
427    /// Sets the field bit
428    #[inline(always)]
429    pub fn set_bit(self) -> &'a mut W<REG> {
430        self.w.bits |= REG::Ux::ONE << self.o;
431        self.w
432    }
433}
434impl<'a, REG, FI> BitWriter0C<'a, REG, FI>
435where
436    REG: Writable + RegisterSpec,
437    bool: From<FI>,
438{
439    /// Clears the field bit
440    #[inline(always)]
441    pub fn clear_bit(self) -> &'a mut W<REG> {
442        self.w.bits &= !(REG::Ux::ONE << self.o);
443        self.w
444    }
445}
446impl<'a, REG, FI> BitWriter1C<'a, REG, FI>
447where
448    REG: Writable + RegisterSpec,
449    bool: From<FI>,
450{
451    ///Clears the field bit by passing one
452    #[inline(always)]
453    pub fn clear_bit_by_one(self) -> &'a mut W<REG> {
454        self.w.bits |= REG::Ux::ONE << self.o;
455        self.w
456    }
457}
458impl<'a, REG, FI> BitWriter0S<'a, REG, FI>
459where
460    REG: Writable + RegisterSpec,
461    bool: From<FI>,
462{
463    ///Sets the field bit by passing zero
464    #[inline(always)]
465    pub fn set_bit_by_zero(self) -> &'a mut W<REG> {
466        self.w.bits &= !(REG::Ux::ONE << self.o);
467        self.w
468    }
469}
470impl<'a, REG, FI> BitWriter1T<'a, REG, FI>
471where
472    REG: Writable + RegisterSpec,
473    bool: From<FI>,
474{
475    ///Toggle the field bit by passing one
476    #[inline(always)]
477    pub fn toggle_bit(self) -> &'a mut W<REG> {
478        self.w.bits |= REG::Ux::ONE << self.o;
479        self.w
480    }
481}
482impl<'a, REG, FI> BitWriter0T<'a, REG, FI>
483where
484    REG: Writable + RegisterSpec,
485    bool: From<FI>,
486{
487    ///Toggle the field bit by passing zero
488    #[inline(always)]
489    pub fn toggle_bit(self) -> &'a mut W<REG> {
490        self.w.bits &= !(REG::Ux::ONE << self.o);
491        self.w
492    }
493}
494/// This structure provides volatile access to registers.
495#[repr(transparent)]
496pub struct Reg<REG: RegisterSpec> {
497    register: vcell::VolatileCell<REG::Ux>,
498    _marker: marker::PhantomData<REG>,
499}
500unsafe impl<REG: RegisterSpec> Send for Reg<REG> where REG::Ux: Send {}
501impl<REG: RegisterSpec> Reg<REG> {
502    /// Returns the underlying memory address of register.
503    ///
504    /// ```ignore
505    /// let reg_ptr = periph.reg.as_ptr();
506    /// ```
507    #[inline(always)]
508    pub fn as_ptr(&self) -> *mut REG::Ux {
509        self.register.as_ptr()
510    }
511}
512impl<REG: Readable> Reg<REG> {
513    /// Reads the contents of a `Readable` register.
514    ///
515    /// You can read the raw contents of a register by using `bits`:
516    /// ```ignore
517    /// let bits = periph.reg.read().bits();
518    /// ```
519    /// or get the content of a particular field of a register:
520    /// ```ignore
521    /// let reader = periph.reg.read();
522    /// let bits = reader.field1().bits();
523    /// let flag = reader.field2().bit_is_set();
524    /// ```
525    #[inline(always)]
526    pub fn read(&self) -> R<REG> {
527        R {
528            bits: self.register.get(),
529            _reg: marker::PhantomData,
530        }
531    }
532}
533impl<REG: Resettable + Writable> Reg<REG> {
534    /// Writes the reset value to `Writable` register.
535    ///
536    /// Resets the register to its initial state.
537    #[inline(always)]
538    pub fn reset(&self) {
539        self.register.set(REG::RESET_VALUE)
540    }
541    /// Writes bits to a `Writable` register.
542    ///
543    /// You can write raw bits into a register:
544    /// ```ignore
545    /// periph.reg.write(|w| unsafe { w.bits(rawbits) });
546    /// ```
547    /// or write only the fields you need:
548    /// ```ignore
549    /// periph.reg.write(|w| w
550    ///     .field1().bits(newfield1bits)
551    ///     .field2().set_bit()
552    ///     .field3().variant(VARIANT)
553    /// );
554    /// ```
555    /// or an alternative way of saying the same:
556    /// ```ignore
557    /// periph.reg.write(|w| {
558    ///     w.field1().bits(newfield1bits);
559    ///     w.field2().set_bit();
560    ///     w.field3().variant(VARIANT)
561    /// });
562    /// ```
563    /// In the latter case, other fields will be set to their reset value.
564    #[inline(always)]
565    pub fn write<F>(&self, f: F) -> REG::Ux
566    where
567        F: FnOnce(&mut W<REG>) -> &mut W<REG>,
568    {
569        let value = f(&mut W {
570            bits: REG::RESET_VALUE & !REG::ONE_TO_MODIFY_FIELDS_BITMAP
571                | REG::ZERO_TO_MODIFY_FIELDS_BITMAP,
572            _reg: marker::PhantomData,
573        })
574        .bits;
575        self.register.set(value);
576        value
577    }
578    /// Writes bits to a `Writable` register and produce a value.
579    ///
580    /// You can write raw bits into a register:
581    /// ```ignore
582    /// periph.reg.write_and(|w| unsafe { w.bits(rawbits); });
583    /// ```
584    /// or write only the fields you need:
585    /// ```ignore
586    /// periph.reg.write_and(|w| {
587    ///     w.field1().bits(newfield1bits)
588    ///         .field2().set_bit()
589    ///         .field3().variant(VARIANT);
590    /// });
591    /// ```
592    /// or an alternative way of saying the same:
593    /// ```ignore
594    /// periph.reg.write_and(|w| {
595    ///     w.field1().bits(newfield1bits);
596    ///     w.field2().set_bit();
597    ///     w.field3().variant(VARIANT);
598    /// });
599    /// ```
600    /// In the latter case, other fields will be set to their reset value.
601    ///
602    /// Values can be returned from the closure:
603    /// ```ignore
604    /// let state = periph.reg.write_and(|w| State::set(w.field1()));
605    /// ```
606    #[inline(always)]
607    pub fn from_write<F, T>(&self, f: F) -> T
608    where
609        F: FnOnce(&mut W<REG>) -> T,
610    {
611        let mut writer = W {
612            bits: REG::RESET_VALUE & !REG::ONE_TO_MODIFY_FIELDS_BITMAP
613                | REG::ZERO_TO_MODIFY_FIELDS_BITMAP,
614            _reg: marker::PhantomData,
615        };
616        let result = f(&mut writer);
617        self.register.set(writer.bits);
618        result
619    }
620}
621impl<REG: Writable> Reg<REG> {
622    /// Writes 0 to a `Writable` register.
623    ///
624    /// Similar to `write`, but unused bits will contain 0.
625    ///
626    /// # Safety
627    ///
628    /// Unsafe to use with registers which don't allow to write 0.
629    #[inline(always)]
630    pub unsafe fn write_with_zero<F>(&self, f: F) -> REG::Ux
631    where
632        F: FnOnce(&mut W<REG>) -> &mut W<REG>,
633    {
634        let value = f(&mut W {
635            bits: REG::Ux::ZERO,
636            _reg: marker::PhantomData,
637        })
638        .bits;
639        self.register.set(value);
640        value
641    }
642    /// Writes 0 to a `Writable` register and produces a value.
643    ///
644    /// Similar to `write`, but unused bits will contain 0.
645    ///
646    /// # Safety
647    ///
648    /// Unsafe to use with registers which don't allow to write 0.
649    #[inline(always)]
650    pub unsafe fn from_write_with_zero<F, T>(&self, f: F) -> T
651    where
652        F: FnOnce(&mut W<REG>) -> T,
653    {
654        let mut writer = W {
655            bits: REG::Ux::ZERO,
656            _reg: marker::PhantomData,
657        };
658        let result = f(&mut writer);
659        self.register.set(writer.bits);
660        result
661    }
662}
663impl<REG: Readable + Writable> Reg<REG> {
664    /// Modifies the contents of the register by reading and then writing it.
665    ///
666    /// E.g. to do a read-modify-write sequence to change parts of a register:
667    /// ```ignore
668    /// periph.reg.modify(|r, w| unsafe { w.bits(
669    ///    r.bits() | 3
670    /// ) });
671    /// ```
672    /// or
673    /// ```ignore
674    /// periph.reg.modify(|_, w| w
675    ///     .field1().bits(newfield1bits)
676    ///     .field2().set_bit()
677    ///     .field3().variant(VARIANT)
678    /// );
679    /// ```
680    /// or an alternative way of saying the same:
681    /// ```ignore
682    /// periph.reg.modify(|_, w| {
683    ///     w.field1().bits(newfield1bits);
684    ///     w.field2().set_bit();
685    ///     w.field3().variant(VARIANT)
686    /// });
687    /// ```
688    /// Other fields will have the value they had before the call to `modify`.
689    #[inline(always)]
690    pub fn modify<F>(&self, f: F) -> REG::Ux
691    where
692        for<'w> F: FnOnce(&R<REG>, &'w mut W<REG>) -> &'w mut W<REG>,
693    {
694        let bits = self.register.get();
695        let value = f(
696            &R {
697                bits,
698                _reg: marker::PhantomData,
699            },
700            &mut W {
701                bits: bits & !REG::ONE_TO_MODIFY_FIELDS_BITMAP | REG::ZERO_TO_MODIFY_FIELDS_BITMAP,
702                _reg: marker::PhantomData,
703            },
704        )
705        .bits;
706        self.register.set(value);
707        value
708    }
709    /// Modifies the contents of the register by reading and then writing it
710    /// and produces a value.
711    ///
712    /// E.g. to do a read-modify-write sequence to change parts of a register:
713    /// ```ignore
714    /// let bits = periph.reg.modify(|r, w| {
715    ///     let new_bits = r.bits() | 3;
716    ///     unsafe {
717    ///         w.bits(new_bits);
718    ///     }
719    ///
720    ///     new_bits
721    /// });
722    /// ```
723    /// or
724    /// ```ignore
725    /// periph.reg.modify(|_, w| {
726    ///     w.field1().bits(newfield1bits)
727    ///         .field2().set_bit()
728    ///         .field3().variant(VARIANT);
729    /// });
730    /// ```
731    /// or an alternative way of saying the same:
732    /// ```ignore
733    /// periph.reg.modify(|_, w| {
734    ///     w.field1().bits(newfield1bits);
735    ///     w.field2().set_bit();
736    ///     w.field3().variant(VARIANT);
737    /// });
738    /// ```
739    /// Other fields will have the value they had before the call to `modify`.
740    #[inline(always)]
741    pub fn from_modify<F, T>(&self, f: F) -> T
742    where
743        for<'w> F: FnOnce(&R<REG>, &'w mut W<REG>) -> T,
744    {
745        let bits = self.register.get();
746        let mut writer = W {
747            bits: bits & !REG::ONE_TO_MODIFY_FIELDS_BITMAP | REG::ZERO_TO_MODIFY_FIELDS_BITMAP,
748            _reg: marker::PhantomData,
749        };
750        let result = f(
751            &R {
752                bits,
753                _reg: marker::PhantomData,
754            },
755            &mut writer,
756        );
757        self.register.set(writer.bits);
758        result
759    }
760}
761impl<REG: Readable> core::fmt::Debug for crate::generic::Reg<REG>
762where
763    R<REG>: core::fmt::Debug,
764{
765    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
766        core::fmt::Debug::fmt(&self.read(), f)
767    }
768}
769#[cfg(feature = "atomics")]
770mod atomic;