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