nrf52840_pac/
generic.rs

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