lpc82x_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 = " In the latter case, other fields will be set to their reset value."]
93    #[inline(always)]
94    pub fn write<F>(&self, f: F)
95    where
96        F: FnOnce(&mut REG::Writer) -> &mut W<REG>,
97    {
98        self.register.set(
99            f(&mut REG::Writer::from(W {
100                bits: REG::reset_value(),
101                _reg: marker::PhantomData,
102            }))
103            .bits,
104        );
105    }
106}
107impl<REG: Writable> Reg<REG>
108where
109    REG::Ux: Default,
110{
111    #[doc = " Writes 0 to a `Writable` register."]
112    #[doc = ""]
113    #[doc = " Similar to `write`, but unused bits will contain 0."]
114    #[inline(always)]
115    pub unsafe fn write_with_zero<F>(&self, f: F)
116    where
117        F: FnOnce(&mut REG::Writer) -> &mut W<REG>,
118    {
119        self.register.set(
120            (*f(&mut REG::Writer::from(W {
121                bits: REG::Ux::default(),
122                _reg: marker::PhantomData,
123            })))
124            .bits,
125        );
126    }
127}
128impl<REG: Readable + Writable> Reg<REG> {
129    #[doc = " Modifies the contents of the register by reading and then writing it."]
130    #[doc = ""]
131    #[doc = " E.g. to do a read-modify-write sequence to change parts of a register:"]
132    #[doc = " ```ignore"]
133    #[doc = " periph.reg.modify(|r, w| unsafe { w.bits("]
134    #[doc = "    r.bits() | 3"]
135    #[doc = " ) });"]
136    #[doc = " ```"]
137    #[doc = " or"]
138    #[doc = " ```ignore"]
139    #[doc = " periph.reg.modify(|_, w| w"]
140    #[doc = "     .field1().bits(newfield1bits)"]
141    #[doc = "     .field2().set_bit()"]
142    #[doc = "     .field3().variant(VARIANT)"]
143    #[doc = " );"]
144    #[doc = " ```"]
145    #[doc = " Other fields will have the value they had before the call to `modify`."]
146    #[inline(always)]
147    pub fn modify<F>(&self, f: F)
148    where
149        for<'w> F: FnOnce(&REG::Reader, &'w mut REG::Writer) -> &'w mut W<REG>,
150    {
151        let bits = self.register.get();
152        self.register.set(
153            f(
154                &REG::Reader::from(R {
155                    bits,
156                    _reg: marker::PhantomData,
157                }),
158                &mut REG::Writer::from(W {
159                    bits,
160                    _reg: marker::PhantomData,
161                }),
162            )
163            .bits,
164        );
165    }
166}
167#[doc = " Register reader."]
168#[doc = ""]
169#[doc = " Result of the `read` methods of registers. Also used as a closure argument in the `modify`"]
170#[doc = " method."]
171pub struct R<REG: RegisterSpec + ?Sized> {
172    pub(crate) bits: REG::Ux,
173    _reg: marker::PhantomData<REG>,
174}
175impl<REG: RegisterSpec> R<REG> {
176    #[doc = " Reads raw bits from register."]
177    #[inline(always)]
178    pub fn bits(&self) -> REG::Ux {
179        self.bits
180    }
181}
182impl<REG: RegisterSpec, FI> PartialEq<FI> for R<REG>
183where
184    REG::Ux: PartialEq,
185    FI: Copy + Into<REG::Ux>,
186{
187    #[inline(always)]
188    fn eq(&self, other: &FI) -> bool {
189        self.bits.eq(&(*other).into())
190    }
191}
192#[doc = " Register writer."]
193#[doc = ""]
194#[doc = " Used as an argument to the closures in the `write` and `modify` methods of the register."]
195pub struct W<REG: RegisterSpec + ?Sized> {
196    #[doc = "Writable bits"]
197    pub(crate) bits: REG::Ux,
198    _reg: marker::PhantomData<REG>,
199}
200impl<REG: RegisterSpec> W<REG> {
201    #[doc = " Writes raw bits to the register."]
202    #[inline(always)]
203    pub unsafe fn bits(&mut self, bits: REG::Ux) -> &mut Self {
204        self.bits = bits;
205        self
206    }
207}
208#[doc = " Field reader."]
209#[doc = ""]
210#[doc = " Result of the `read` methods of fields."]
211pub struct FieldReader<U, T> {
212    pub(crate) bits: U,
213    _reg: marker::PhantomData<T>,
214}
215impl<U, T> FieldReader<U, T>
216where
217    U: Copy,
218{
219    #[doc = " Creates a new instance of the reader."]
220    #[allow(unused)]
221    #[inline(always)]
222    pub(crate) fn new(bits: U) -> Self {
223        Self {
224            bits,
225            _reg: marker::PhantomData,
226        }
227    }
228    #[doc = " Reads raw bits from field."]
229    #[inline(always)]
230    pub fn bits(&self) -> U {
231        self.bits
232    }
233}
234impl<U, T, FI> PartialEq<FI> for FieldReader<U, T>
235where
236    U: PartialEq,
237    FI: Copy + Into<U>,
238{
239    #[inline(always)]
240    fn eq(&self, other: &FI) -> bool {
241        self.bits.eq(&(*other).into())
242    }
243}
244impl<FI> FieldReader<bool, FI> {
245    #[doc = " Value of the field as raw bits."]
246    #[inline(always)]
247    pub fn bit(&self) -> bool {
248        self.bits
249    }
250    #[doc = " Returns `true` if the bit is clear (0)."]
251    #[inline(always)]
252    pub fn bit_is_clear(&self) -> bool {
253        !self.bit()
254    }
255    #[doc = " Returns `true` if the bit is set (1)."]
256    #[inline(always)]
257    pub fn bit_is_set(&self) -> bool {
258        self.bit()
259    }
260}