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(®::Reader, &'w mut REG::Writer) -> &'w mut W<REG>,
150 {
151 let bits = self.register.get();
152 self.register.set(
153 f(
154 ®::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(hidden)]
209pub struct FieldReaderRaw<U, T> {
210 pub(crate) bits: U,
211 _reg: marker::PhantomData<T>,
212}
213impl<U, FI> FieldReaderRaw<U, FI>
214where
215 U: Copy,
216{
217 #[doc = " Creates a new instance of the reader."]
218 #[allow(unused)]
219 #[inline(always)]
220 pub(crate) fn new(bits: U) -> Self {
221 Self {
222 bits,
223 _reg: marker::PhantomData,
224 }
225 }
226}
227#[doc(hidden)]
228pub struct BitReaderRaw<T> {
229 pub(crate) bits: bool,
230 _reg: marker::PhantomData<T>,
231}
232impl<FI> BitReaderRaw<FI> {
233 #[doc = " Creates a new instance of the reader."]
234 #[allow(unused)]
235 #[inline(always)]
236 pub(crate) fn new(bits: bool) -> Self {
237 Self {
238 bits,
239 _reg: marker::PhantomData,
240 }
241 }
242}
243#[doc = " Field reader."]
244#[doc = ""]
245#[doc = " Result of the `read` methods of fields."]
246pub type FieldReader<U, FI> = FieldReaderRaw<U, FI>;
247#[doc = " Bit-wise field reader"]
248pub type BitReader<FI> = BitReaderRaw<FI>;
249impl<U, FI> FieldReader<U, FI>
250where
251 U: Copy,
252{
253 #[doc = " Reads raw bits from field."]
254 #[inline(always)]
255 pub fn bits(&self) -> U {
256 self.bits
257 }
258}
259impl<U, FI> PartialEq<FI> for FieldReader<U, FI>
260where
261 U: PartialEq,
262 FI: Copy + Into<U>,
263{
264 #[inline(always)]
265 fn eq(&self, other: &FI) -> bool {
266 self.bits.eq(&(*other).into())
267 }
268}
269impl<FI> PartialEq<FI> for BitReader<FI>
270where
271 FI: Copy + Into<bool>,
272{
273 #[inline(always)]
274 fn eq(&self, other: &FI) -> bool {
275 self.bits.eq(&(*other).into())
276 }
277}
278impl<FI> BitReader<FI> {
279 #[doc = " Value of the field as raw bits."]
280 #[inline(always)]
281 pub fn bit(&self) -> bool {
282 self.bits
283 }
284 #[doc = " Returns `true` if the bit is clear (0)."]
285 #[inline(always)]
286 pub fn bit_is_clear(&self) -> bool {
287 !self.bit()
288 }
289 #[doc = " Returns `true` if the bit is set (1)."]
290 #[inline(always)]
291 pub fn bit_is_set(&self) -> bool {
292 self.bit()
293 }
294}
295#[doc(hidden)]
296pub struct Safe;
297#[doc(hidden)]
298pub struct Unsafe;
299#[doc(hidden)]
300pub struct FieldWriterRaw<'a, U, REG, N, FI, Safety, const WI: u8, const O: u8>
301where
302 REG: Writable + RegisterSpec<Ux = U>,
303 FI: Into<N>,
304{
305 pub(crate) w: &'a mut REG::Writer,
306 _field: marker::PhantomData<(N, FI, Safety)>,
307}
308impl<'a, U, REG, N, FI, Safety, const WI: u8, const O: u8>
309 FieldWriterRaw<'a, U, REG, N, FI, Safety, WI, O>
310where
311 REG: Writable + RegisterSpec<Ux = U>,
312 FI: Into<N>,
313{
314 #[doc = " Creates a new instance of the writer"]
315 #[allow(unused)]
316 #[inline(always)]
317 pub(crate) fn new(w: &'a mut REG::Writer) -> Self {
318 Self {
319 w,
320 _field: marker::PhantomData,
321 }
322 }
323}
324#[doc(hidden)]
325pub struct BitWriterRaw<'a, U, REG, FI, M, const O: u8>
326where
327 REG: Writable + RegisterSpec<Ux = U>,
328 FI: Into<bool>,
329{
330 pub(crate) w: &'a mut REG::Writer,
331 _field: marker::PhantomData<(FI, M)>,
332}
333impl<'a, U, REG, FI, M, const O: u8> BitWriterRaw<'a, U, REG, FI, M, O>
334where
335 REG: Writable + RegisterSpec<Ux = U>,
336 FI: Into<bool>,
337{
338 #[doc = " Creates a new instance of the writer"]
339 #[allow(unused)]
340 #[inline(always)]
341 pub(crate) fn new(w: &'a mut REG::Writer) -> Self {
342 Self {
343 w,
344 _field: marker::PhantomData,
345 }
346 }
347}
348#[doc = " Write field Proxy with unsafe `bits`"]
349pub type FieldWriter<'a, U, REG, N, FI, const WI: u8, const O: u8> =
350 FieldWriterRaw<'a, U, REG, N, FI, Unsafe, WI, O>;
351#[doc = " Write field Proxy with safe `bits`"]
352pub type FieldWriterSafe<'a, U, REG, N, FI, const WI: u8, const O: u8> =
353 FieldWriterRaw<'a, U, REG, N, FI, Safe, WI, O>;
354impl<'a, U, REG, N, FI, const WI: u8, const OF: u8> FieldWriter<'a, U, REG, N, FI, WI, OF>
355where
356 REG: Writable + RegisterSpec<Ux = U>,
357 FI: Into<N>,
358{
359 #[doc = " Field width"]
360 pub const WIDTH: u8 = WI;
361 #[doc = " Field offset"]
362 pub const OFFSET: u8 = OF;
363}
364impl<'a, U, REG, N, FI, const WI: u8, const OF: u8> FieldWriterSafe<'a, U, REG, N, FI, WI, OF>
365where
366 REG: Writable + RegisterSpec<Ux = U>,
367 FI: Into<N>,
368{
369 #[doc = " Field width"]
370 pub const WIDTH: u8 = WI;
371 #[doc = " Field offset"]
372 pub const OFFSET: u8 = OF;
373}
374macro_rules! bit_proxy {
375 ( $ writer : ident , $ mwv : ident ) => {
376 #[doc(hidden)]
377 pub struct $mwv;
378 #[doc = " Bit-wise write field proxy"]
379 pub type $writer<'a, U, REG, FI, const O: u8> = BitWriterRaw<'a, U, REG, FI, $mwv, O>;
380 impl<'a, U, REG, FI, const OF: u8> $writer<'a, U, REG, FI, OF>
381 where
382 REG: Writable + RegisterSpec<Ux = U>,
383 FI: Into<bool>,
384 {
385 #[doc = " Field width"]
386 pub const WIDTH: u8 = 1;
387 #[doc = " Field offset"]
388 pub const OFFSET: u8 = OF;
389 }
390 };
391}
392macro_rules! impl_bit_proxy {
393 ( $ writer : ident , $ U : ty ) => {
394 impl<'a, REG, FI, const OF: u8> $writer<'a, $U, REG, FI, OF>
395 where
396 REG: Writable + RegisterSpec<Ux = $U>,
397 FI: Into<bool>,
398 {
399 #[doc = " Writes bit to the field"]
400 #[inline(always)]
401 pub fn bit(self, value: bool) -> &'a mut REG::Writer {
402 self.w.bits = (self.w.bits & !(1 << { OF })) | ((<$U>::from(value) & 1) << { OF });
403 self.w
404 }
405 #[doc = " Writes `variant` to the field"]
406 #[inline(always)]
407 pub fn variant(self, variant: FI) -> &'a mut REG::Writer {
408 self.bit(variant.into())
409 }
410 }
411 };
412}
413bit_proxy!(BitWriter, BitM);
414bit_proxy!(BitWriter1S, Bit1S);
415bit_proxy!(BitWriter0C, Bit0C);
416bit_proxy!(BitWriter1C, Bit1C);
417bit_proxy!(BitWriter0S, Bit0S);
418bit_proxy!(BitWriter1T, Bit1T);
419bit_proxy!(BitWriter0T, Bit0T);
420macro_rules! impl_proxy {
421 ( $ U : ty ) => {
422 impl<'a, REG, N, FI, const WI: u8, const OF: u8> FieldWriter<'a, $U, REG, N, FI, WI, OF>
423 where
424 REG: Writable + RegisterSpec<Ux = $U>,
425 N: Into<$U>,
426 FI: Into<N>,
427 {
428 const MASK: $U = <$U>::MAX >> (<$U>::MAX.leading_ones() as u8 - { WI });
429 #[doc = " Writes raw bits to the field"]
430 #[doc = ""]
431 #[doc = " # Safety"]
432 #[doc = ""]
433 #[doc = " Passing incorrect value can cause undefined behaviour. See reference manual"]
434 #[inline(always)]
435 pub unsafe fn bits(self, value: N) -> &'a mut REG::Writer {
436 self.w.bits = (self.w.bits & !(Self::MASK << { OF }))
437 | ((value.into() & Self::MASK) << { OF });
438 self.w
439 }
440 #[doc = " Writes `variant` to the field"]
441 #[inline(always)]
442 pub fn variant(self, variant: FI) -> &'a mut REG::Writer {
443 unsafe { self.bits(variant.into()) }
444 }
445 }
446 impl<'a, REG, N, FI, const WI: u8, const OF: u8> FieldWriterSafe<'a, $U, REG, N, FI, WI, OF>
447 where
448 REG: Writable + RegisterSpec<Ux = $U>,
449 N: Into<$U>,
450 FI: Into<N>,
451 {
452 const MASK: $U = <$U>::MAX >> (<$U>::MAX.leading_ones() as u8 - { WI });
453 #[doc = " Writes raw bits to the field"]
454 #[inline(always)]
455 pub fn bits(self, value: N) -> &'a mut REG::Writer {
456 self.w.bits = (self.w.bits & !(Self::MASK << { OF }))
457 | ((value.into() & Self::MASK) << { OF });
458 self.w
459 }
460 #[doc = " Writes `variant` to the field"]
461 #[inline(always)]
462 pub fn variant(self, variant: FI) -> &'a mut REG::Writer {
463 self.bits(variant.into())
464 }
465 }
466 impl_bit_proxy!(BitWriter, $U);
467 impl_bit_proxy!(BitWriter1S, $U);
468 impl_bit_proxy!(BitWriter0C, $U);
469 impl_bit_proxy!(BitWriter1C, $U);
470 impl_bit_proxy!(BitWriter0S, $U);
471 impl_bit_proxy!(BitWriter1T, $U);
472 impl_bit_proxy!(BitWriter0T, $U);
473 impl<'a, REG, FI, const OF: u8> BitWriter<'a, $U, REG, FI, OF>
474 where
475 REG: Writable + RegisterSpec<Ux = $U>,
476 FI: Into<bool>,
477 {
478 #[doc = " Sets the field bit"]
479 #[inline(always)]
480 pub fn set_bit(self) -> &'a mut REG::Writer {
481 self.bit(true)
482 }
483 #[doc = " Clears the field bit"]
484 #[inline(always)]
485 pub fn clear_bit(self) -> &'a mut REG::Writer {
486 self.bit(false)
487 }
488 }
489 impl<'a, REG, FI, const OF: u8> BitWriter1S<'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 }
500 impl<'a, REG, FI, const OF: u8> BitWriter0C<'a, $U, REG, FI, OF>
501 where
502 REG: Writable + RegisterSpec<Ux = $U>,
503 FI: Into<bool>,
504 {
505 #[doc = " Clears the field bit"]
506 #[inline(always)]
507 pub fn clear_bit(self) -> &'a mut REG::Writer {
508 self.bit(false)
509 }
510 }
511 impl<'a, REG, FI, const OF: u8> BitWriter1C<'a, $U, REG, FI, OF>
512 where
513 REG: Writable + RegisterSpec<Ux = $U>,
514 FI: Into<bool>,
515 {
516 #[doc = "Clears the field bit by passing one"]
517 #[inline(always)]
518 pub fn clear_bit_by_one(self) -> &'a mut REG::Writer {
519 self.bit(true)
520 }
521 }
522 impl<'a, REG, FI, const OF: u8> BitWriter0S<'a, $U, REG, FI, OF>
523 where
524 REG: Writable + RegisterSpec<Ux = $U>,
525 FI: Into<bool>,
526 {
527 #[doc = "Sets the field bit by passing zero"]
528 #[inline(always)]
529 pub fn set_bit_by_zero(self) -> &'a mut REG::Writer {
530 self.bit(false)
531 }
532 }
533 impl<'a, REG, FI, const OF: u8> BitWriter1T<'a, $U, REG, FI, OF>
534 where
535 REG: Writable + RegisterSpec<Ux = $U>,
536 FI: Into<bool>,
537 {
538 #[doc = "Toggle the field bit by passing one"]
539 #[inline(always)]
540 pub fn toggle_bit(self) -> &'a mut REG::Writer {
541 self.bit(true)
542 }
543 }
544 impl<'a, REG, FI, const OF: u8> BitWriter0T<'a, $U, REG, FI, OF>
545 where
546 REG: Writable + RegisterSpec<Ux = $U>,
547 FI: Into<bool>,
548 {
549 #[doc = "Toggle the field bit by passing zero"]
550 #[inline(always)]
551 pub fn toggle_bit(self) -> &'a mut REG::Writer {
552 self.bit(false)
553 }
554 }
555 };
556}
557impl_proxy!(u32);