xmc4700/
generic.rs

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