1use core::marker;
2#[doc = " Trait implemented by readable registers to enable the `read` method."]
3#[doc = ""]
4#[doc = " Registers marked with `Writable` can be also `modify`'ed."]
5pub trait Readable {}
6#[doc = " Trait implemented by writeable registers."]
7#[doc = ""]
8#[doc = " This enables the `write`, `write_with_zero` and `reset` methods."]
9#[doc = ""]
10#[doc = " Registers marked with `Readable` can be also `modify`'ed."]
11pub trait Writable {}
12#[doc = " Reset value of the register."]
13#[doc = ""]
14#[doc = " This value is the initial value for the `write` method. It can also be directly written to the"]
15#[doc = " register by using the `reset` method."]
16pub trait ResetValue {
17 #[doc = " Raw register type (`u8`, `u16`, `u32`, ...)."]
18 type Type;
19 #[doc = " Reset value of the register."]
20 fn reset_value() -> Self::Type;
21}
22#[doc = " This structure provides volatile access to registers."]
23pub struct Reg<U, REG> {
24 register: vcell::VolatileCell<U>,
25 _marker: marker::PhantomData<REG>,
26}
27unsafe impl<U: Send, REG> Send for Reg<U, REG> {}
28impl<U, REG> Reg<U, REG>
29where
30 U: Copy,
31{
32 #[doc = " Returns the underlying memory address of register."]
33 #[doc = ""]
34 #[doc = " ```ignore"]
35 #[doc = " let reg_ptr = periph.reg.as_ptr();"]
36 #[doc = " ```"]
37 #[inline(always)]
38 pub fn as_ptr(&self) -> *mut U {
39 self.register.as_ptr()
40 }
41}
42impl<U, REG> Reg<U, REG>
43where
44 Self: Readable,
45 U: Copy,
46{
47 #[doc = " Reads the contents of a `Readable` register."]
48 #[doc = ""]
49 #[doc = " You can read the raw contents of a register by using `bits`:"]
50 #[doc = " ```ignore"]
51 #[doc = " let bits = periph.reg.read().bits();"]
52 #[doc = " ```"]
53 #[doc = " or get the content of a particular field of a register:"]
54 #[doc = " ```ignore"]
55 #[doc = " let reader = periph.reg.read();"]
56 #[doc = " let bits = reader.field1().bits();"]
57 #[doc = " let flag = reader.field2().bit_is_set();"]
58 #[doc = " ```"]
59 #[inline(always)]
60 pub fn read(&self) -> R<U, Self> {
61 R {
62 bits: self.register.get(),
63 _reg: marker::PhantomData,
64 }
65 }
66}
67impl<U, REG> Reg<U, REG>
68where
69 Self: ResetValue<Type = U> + Writable,
70 U: Copy,
71{
72 #[doc = " Writes the reset value to `Writable` register."]
73 #[doc = ""]
74 #[doc = " Resets the register to its initial state."]
75 #[inline(always)]
76 pub fn reset(&self) {
77 self.register.set(Self::reset_value())
78 }
79}
80impl<U, REG> Reg<U, REG>
81where
82 Self: ResetValue<Type = U> + Writable,
83 U: Copy,
84{
85 #[doc = " Writes bits to a `Writable` register."]
86 #[doc = ""]
87 #[doc = " You can write raw bits into a register:"]
88 #[doc = " ```ignore"]
89 #[doc = " periph.reg.write(|w| unsafe { w.bits(rawbits) });"]
90 #[doc = " ```"]
91 #[doc = " or write only the fields you need:"]
92 #[doc = " ```ignore"]
93 #[doc = " periph.reg.write(|w| w"]
94 #[doc = " .field1().bits(newfield1bits)"]
95 #[doc = " .field2().set_bit()"]
96 #[doc = " .field3().variant(VARIANT)"]
97 #[doc = " );"]
98 #[doc = " ```"]
99 #[doc = " In the latter case, other fields will be set to their reset value."]
100 #[inline(always)]
101 pub fn write<F>(&self, f: F)
102 where
103 F: FnOnce(&mut W<U, Self>) -> &mut W<U, Self>,
104 {
105 self.register.set(
106 f(&mut W {
107 bits: Self::reset_value(),
108 _reg: marker::PhantomData,
109 })
110 .bits,
111 );
112 }
113}
114impl<U, REG> Reg<U, REG>
115where
116 Self: Writable,
117 U: Copy + 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 fn write_with_zero<F>(&self, f: F)
124 where
125 F: FnOnce(&mut W<U, Self>) -> &mut W<U, Self>,
126 {
127 self.register.set(
128 f(&mut W {
129 bits: U::default(),
130 _reg: marker::PhantomData,
131 })
132 .bits,
133 );
134 }
135}
136impl<U, REG> Reg<U, REG>
137where
138 Self: Readable + Writable,
139 U: Copy,
140{
141 #[doc = " Modifies the contents of the register by reading and then writing it."]
142 #[doc = ""]
143 #[doc = " E.g. to do a read-modify-write sequence to change parts of a register:"]
144 #[doc = " ```ignore"]
145 #[doc = " periph.reg.modify(|r, w| unsafe { w.bits("]
146 #[doc = " r.bits() | 3"]
147 #[doc = " ) });"]
148 #[doc = " ```"]
149 #[doc = " or"]
150 #[doc = " ```ignore"]
151 #[doc = " periph.reg.modify(|_, w| w"]
152 #[doc = " .field1().bits(newfield1bits)"]
153 #[doc = " .field2().set_bit()"]
154 #[doc = " .field3().variant(VARIANT)"]
155 #[doc = " );"]
156 #[doc = " ```"]
157 #[doc = " Other fields will have the value they had before the call to `modify`."]
158 #[inline(always)]
159 pub fn modify<F>(&self, f: F)
160 where
161 for<'w> F: FnOnce(&R<U, Self>, &'w mut W<U, Self>) -> &'w mut W<U, Self>,
162 {
163 let bits = self.register.get();
164 self.register.set(
165 f(
166 &R {
167 bits,
168 _reg: marker::PhantomData,
169 },
170 &mut W {
171 bits,
172 _reg: marker::PhantomData,
173 },
174 )
175 .bits,
176 );
177 }
178}
179#[doc = " Register/field reader."]
180#[doc = ""]
181#[doc = " Result of the `read` methods of registers. Also used as a closure argument in the `modify`"]
182#[doc = " method."]
183pub struct R<U, T> {
184 pub(crate) bits: U,
185 _reg: marker::PhantomData<T>,
186}
187impl<U, T> R<U, T>
188where
189 U: Copy,
190{
191 #[doc = " Creates a new instance of the reader."]
192 #[inline(always)]
193 pub(crate) fn new(bits: U) -> Self {
194 Self {
195 bits,
196 _reg: marker::PhantomData,
197 }
198 }
199 #[doc = " Reads raw bits from register/field."]
200 #[inline(always)]
201 pub fn bits(&self) -> U {
202 self.bits
203 }
204}
205impl<U, T, FI> PartialEq<FI> for R<U, T>
206where
207 U: PartialEq,
208 FI: Copy + Into<U>,
209{
210 #[inline(always)]
211 fn eq(&self, other: &FI) -> bool {
212 self.bits.eq(&(*other).into())
213 }
214}
215impl<FI> R<bool, FI> {
216 #[doc = " Value of the field as raw bits."]
217 #[inline(always)]
218 pub fn bit(&self) -> bool {
219 self.bits
220 }
221 #[doc = " Returns `true` if the bit is clear (0)."]
222 #[inline(always)]
223 pub fn bit_is_clear(&self) -> bool {
224 !self.bit()
225 }
226 #[doc = " Returns `true` if the bit is set (1)."]
227 #[inline(always)]
228 pub fn bit_is_set(&self) -> bool {
229 self.bit()
230 }
231}
232#[doc = " Register writer."]
233#[doc = ""]
234#[doc = " Used as an argument to the closures in the `write` and `modify` methods of the register."]
235pub struct W<U, REG> {
236 #[doc = "Writable bits"]
237 pub(crate) bits: U,
238 _reg: marker::PhantomData<REG>,
239}
240impl<U, REG> W<U, REG> {
241 #[doc = " Writes raw bits to the register."]
242 #[inline(always)]
243 pub unsafe fn bits(&mut self, bits: U) -> &mut Self {
244 self.bits = bits;
245 self
246 }
247}
248#[doc = " Used if enumerated values cover not the whole range."]
249#[derive(Clone, Copy, PartialEq)]
250pub enum Variant<U, T> {
251 #[doc = " Expected variant."]
252 Val(T),
253 #[doc = " Raw bits."]
254 Res(U),
255}