mimxrt595s/
generic.rs

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