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