1use core::marker;
2pub struct Periph<RB, const A: usize> {
4 _marker: marker::PhantomData<RB>,
5}
6unsafe impl<RB, const A: usize> Send for Periph<RB, A> {}
7impl<RB, const A: usize> Periph<RB, A> {
8 pub const PTR: *const RB = A as *const _;
10 #[inline(always)]
12 pub const fn ptr() -> *const RB {
13 Self::PTR
14 }
15 pub unsafe fn steal() -> Self {
29 Self {
30 _marker: marker::PhantomData,
31 }
32 }
33}
34impl<RB, const A: usize> core::ops::Deref for Periph<RB, A> {
35 type Target = RB;
36 #[inline(always)]
37 fn deref(&self) -> &Self::Target {
38 unsafe { &*Self::PTR }
39 }
40}
41pub trait RawReg:
43 Copy
44 + From<bool>
45 + core::ops::BitOr<Output = Self>
46 + core::ops::BitAnd<Output = Self>
47 + core::ops::BitOrAssign
48 + core::ops::BitAndAssign
49 + core::ops::Not<Output = Self>
50 + core::ops::Shl<u8, Output = Self>
51{
52 fn mask<const WI: u8>() -> Self;
54 const ZERO: Self;
56 const ONE: Self;
58}
59macro_rules! raw_reg {
60 ($U:ty, $size:literal, $mask:ident) => {
61 impl RawReg for $U {
62 #[inline(always)]
63 fn mask<const WI: u8>() -> Self {
64 $mask::<WI>()
65 }
66 const ZERO: Self = 0;
67 const ONE: Self = 1;
68 }
69 const fn $mask<const WI: u8>() -> $U {
70 <$U>::MAX >> ($size - WI)
71 }
72 impl FieldSpec for $U {
73 type Ux = $U;
74 }
75 };
76}
77raw_reg!(u8, 8, mask_u8);
78raw_reg!(u16, 16, mask_u16);
79raw_reg!(u32, 32, mask_u32);
80raw_reg!(u64, 64, mask_u64);
81pub trait RegisterSpec {
83 type Ux: RawReg;
85}
86pub trait FieldSpec: Sized {
88 type Ux: Copy + core::fmt::Debug + PartialEq + From<Self>;
90}
91pub trait IsEnum: FieldSpec {}
93pub trait Readable: RegisterSpec {}
97pub trait Writable: RegisterSpec {
103 type Safety;
105 const ZERO_TO_MODIFY_FIELDS_BITMAP: Self::Ux = Self::Ux::ZERO;
107 const ONE_TO_MODIFY_FIELDS_BITMAP: Self::Ux = Self::Ux::ZERO;
109}
110pub trait Resettable: RegisterSpec {
115 const RESET_VALUE: Self::Ux = Self::Ux::ZERO;
117 #[inline(always)]
119 fn reset_value() -> Self::Ux {
120 Self::RESET_VALUE
121 }
122}
123#[doc(hidden)]
124pub mod raw;
125pub type R<REG> = raw::R<REG>;
130impl<REG: RegisterSpec> R<REG> {
131 #[inline(always)]
133 pub const fn bits(&self) -> REG::Ux {
134 self.bits
135 }
136}
137impl<REG: RegisterSpec, FI> PartialEq<FI> for R<REG>
138where
139 REG::Ux: PartialEq,
140 FI: Copy,
141 REG::Ux: From<FI>,
142{
143 #[inline(always)]
144 fn eq(&self, other: &FI) -> bool {
145 self.bits.eq(®::Ux::from(*other))
146 }
147}
148pub type W<REG> = raw::W<REG>;
152impl<REG: Writable> W<REG> {
153 #[inline(always)]
159 pub unsafe fn bits(&mut self, bits: REG::Ux) -> &mut Self {
160 self.bits = bits;
161 self
162 }
163}
164impl<REG> W<REG>
165where
166 REG: Writable<Safety = Safe>,
167{
168 #[inline(always)]
170 pub fn set(&mut self, bits: REG::Ux) -> &mut Self {
171 self.bits = bits;
172 self
173 }
174}
175pub type FieldReader<FI = u8> = raw::FieldReader<FI>;
179pub type BitReader<FI = bool> = raw::BitReader<FI>;
181impl<FI: FieldSpec> FieldReader<FI> {
182 #[inline(always)]
184 pub const fn bits(&self) -> FI::Ux {
185 self.bits
186 }
187}
188impl<FI: FieldSpec> core::fmt::Debug for FieldReader<FI> {
189 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
190 core::fmt::Debug::fmt(&self.bits, f)
191 }
192}
193impl<FI> PartialEq<FI> for FieldReader<FI>
194where
195 FI: FieldSpec + Copy,
196{
197 #[inline(always)]
198 fn eq(&self, other: &FI) -> bool {
199 self.bits.eq(&FI::Ux::from(*other))
200 }
201}
202impl<FI> PartialEq<FI> for BitReader<FI>
203where
204 FI: Copy,
205 bool: From<FI>,
206{
207 #[inline(always)]
208 fn eq(&self, other: &FI) -> bool {
209 self.bits.eq(&bool::from(*other))
210 }
211}
212impl<FI> BitReader<FI> {
213 #[inline(always)]
215 pub const fn bit(&self) -> bool {
216 self.bits
217 }
218 #[inline(always)]
220 pub const fn bit_is_clear(&self) -> bool {
221 !self.bit()
222 }
223 #[inline(always)]
225 pub const fn bit_is_set(&self) -> bool {
226 self.bit()
227 }
228}
229impl<FI> core::fmt::Debug for BitReader<FI> {
230 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
231 core::fmt::Debug::fmt(&self.bits, f)
232 }
233}
234pub struct Safe;
236pub struct Unsafe;
238pub struct Range<const MIN: u64, const MAX: u64>;
240pub struct RangeFrom<const MIN: u64>;
242pub struct RangeTo<const MAX: u64>;
244pub type FieldWriter<'a, REG, const WI: u8, FI = u8, Safety = Unsafe> =
246 raw::FieldWriter<'a, REG, WI, FI, Safety>;
247impl<REG, const WI: u8, FI, Safety> FieldWriter<'_, REG, WI, FI, Safety>
248where
249 REG: Writable + RegisterSpec,
250 FI: FieldSpec,
251{
252 pub const WIDTH: u8 = WI;
254 #[inline(always)]
256 pub const fn width(&self) -> u8 {
257 WI
258 }
259 #[inline(always)]
261 pub const fn offset(&self) -> u8 {
262 self.o
263 }
264}
265impl<'a, REG, const WI: u8, FI, Safety> FieldWriter<'a, REG, WI, FI, Safety>
266where
267 REG: Writable + RegisterSpec,
268 FI: FieldSpec,
269 REG::Ux: From<FI::Ux>,
270{
271 #[inline(always)]
277 pub unsafe fn bits(self, value: FI::Ux) -> &'a mut W<REG> {
278 self.w.bits &= !(REG::Ux::mask::<WI>() << self.o);
279 self.w.bits |= (REG::Ux::from(value) & REG::Ux::mask::<WI>()) << self.o;
280 self.w
281 }
282}
283impl<'a, REG, const WI: u8, FI> FieldWriter<'a, REG, WI, FI, Safe>
284where
285 REG: Writable + RegisterSpec,
286 FI: FieldSpec,
287 REG::Ux: From<FI::Ux>,
288{
289 #[inline(always)]
291 pub fn set(self, value: FI::Ux) -> &'a mut W<REG> {
292 unsafe { self.bits(value) }
293 }
294}
295impl<'a, REG, const WI: u8, FI, const MIN: u64, const MAX: u64>
296 FieldWriter<'a, REG, WI, FI, Range<MIN, MAX>>
297where
298 REG: Writable + RegisterSpec,
299 FI: FieldSpec,
300 REG::Ux: From<FI::Ux>,
301 u64: From<FI::Ux>,
302{
303 #[inline(always)]
305 pub fn set(self, value: FI::Ux) -> &'a mut W<REG> {
306 {
307 let value = u64::from(value);
308 assert!(value >= MIN && value <= MAX);
309 }
310 unsafe { self.bits(value) }
311 }
312}
313impl<'a, REG, const WI: u8, FI, const MIN: u64> FieldWriter<'a, REG, WI, FI, RangeFrom<MIN>>
314where
315 REG: Writable + RegisterSpec,
316 FI: FieldSpec,
317 REG::Ux: From<FI::Ux>,
318 u64: From<FI::Ux>,
319{
320 #[inline(always)]
322 pub fn set(self, value: FI::Ux) -> &'a mut W<REG> {
323 {
324 let value = u64::from(value);
325 assert!(value >= MIN);
326 }
327 unsafe { self.bits(value) }
328 }
329}
330impl<'a, REG, const WI: u8, FI, const MAX: u64> FieldWriter<'a, REG, WI, FI, RangeTo<MAX>>
331where
332 REG: Writable + RegisterSpec,
333 FI: FieldSpec,
334 REG::Ux: From<FI::Ux>,
335 u64: From<FI::Ux>,
336{
337 #[inline(always)]
339 pub fn set(self, value: FI::Ux) -> &'a mut W<REG> {
340 {
341 let value = u64::from(value);
342 assert!(value <= MAX);
343 }
344 unsafe { self.bits(value) }
345 }
346}
347impl<'a, REG, const WI: u8, FI, Safety> FieldWriter<'a, REG, WI, FI, Safety>
348where
349 REG: Writable + RegisterSpec,
350 FI: IsEnum,
351 REG::Ux: From<FI::Ux>,
352{
353 #[inline(always)]
355 pub fn variant(self, variant: FI) -> &'a mut W<REG> {
356 unsafe { self.bits(FI::Ux::from(variant)) }
357 }
358}
359macro_rules! bit_proxy {
360 ($writer:ident, $mwv:ident) => {
361 #[doc(hidden)]
362 pub struct $mwv;
363 #[doc = " Bit-wise write field proxy"]
364 pub type $writer<'a, REG, FI = bool> = raw::BitWriter<'a, REG, FI, $mwv>;
365 impl<'a, REG, FI> $writer<'a, REG, FI>
366 where
367 REG: Writable + RegisterSpec,
368 bool: From<FI>,
369 {
370 #[doc = " Field width"]
371 pub const WIDTH: u8 = 1;
372 #[doc = " Field width"]
373 #[inline(always)]
374 pub const fn width(&self) -> u8 {
375 Self::WIDTH
376 }
377 #[doc = " Field offset"]
378 #[inline(always)]
379 pub const fn offset(&self) -> u8 {
380 self.o
381 }
382 #[doc = " Writes bit to the field"]
383 #[inline(always)]
384 pub fn bit(self, value: bool) -> &'a mut W<REG> {
385 self.w.bits &= !(REG::Ux::ONE << self.o);
386 self.w.bits |= (REG::Ux::from(value) & REG::Ux::ONE) << self.o;
387 self.w
388 }
389 #[doc = " Writes `variant` to the field"]
390 #[inline(always)]
391 pub fn variant(self, variant: FI) -> &'a mut W<REG> {
392 self.bit(bool::from(variant))
393 }
394 }
395 };
396}
397bit_proxy!(BitWriter, BitM);
398bit_proxy!(BitWriter1S, Bit1S);
399bit_proxy!(BitWriter0C, Bit0C);
400bit_proxy!(BitWriter1C, Bit1C);
401bit_proxy!(BitWriter0S, Bit0S);
402bit_proxy!(BitWriter1T, Bit1T);
403bit_proxy!(BitWriter0T, Bit0T);
404impl<'a, REG, FI> BitWriter<'a, REG, FI>
405where
406 REG: Writable + RegisterSpec,
407 bool: From<FI>,
408{
409 #[inline(always)]
411 pub fn set_bit(self) -> &'a mut W<REG> {
412 self.w.bits |= REG::Ux::ONE << self.o;
413 self.w
414 }
415 #[inline(always)]
417 pub fn clear_bit(self) -> &'a mut W<REG> {
418 self.w.bits &= !(REG::Ux::ONE << self.o);
419 self.w
420 }
421}
422impl<'a, REG, FI> BitWriter1S<'a, REG, FI>
423where
424 REG: Writable + RegisterSpec,
425 bool: From<FI>,
426{
427 #[inline(always)]
429 pub fn set_bit(self) -> &'a mut W<REG> {
430 self.w.bits |= REG::Ux::ONE << self.o;
431 self.w
432 }
433}
434impl<'a, REG, FI> BitWriter0C<'a, REG, FI>
435where
436 REG: Writable + RegisterSpec,
437 bool: From<FI>,
438{
439 #[inline(always)]
441 pub fn clear_bit(self) -> &'a mut W<REG> {
442 self.w.bits &= !(REG::Ux::ONE << self.o);
443 self.w
444 }
445}
446impl<'a, REG, FI> BitWriter1C<'a, REG, FI>
447where
448 REG: Writable + RegisterSpec,
449 bool: From<FI>,
450{
451 #[inline(always)]
453 pub fn clear_bit_by_one(self) -> &'a mut W<REG> {
454 self.w.bits |= REG::Ux::ONE << self.o;
455 self.w
456 }
457}
458impl<'a, REG, FI> BitWriter0S<'a, REG, FI>
459where
460 REG: Writable + RegisterSpec,
461 bool: From<FI>,
462{
463 #[inline(always)]
465 pub fn set_bit_by_zero(self) -> &'a mut W<REG> {
466 self.w.bits &= !(REG::Ux::ONE << self.o);
467 self.w
468 }
469}
470impl<'a, REG, FI> BitWriter1T<'a, REG, FI>
471where
472 REG: Writable + RegisterSpec,
473 bool: From<FI>,
474{
475 #[inline(always)]
477 pub fn toggle_bit(self) -> &'a mut W<REG> {
478 self.w.bits |= REG::Ux::ONE << self.o;
479 self.w
480 }
481}
482impl<'a, REG, FI> BitWriter0T<'a, REG, FI>
483where
484 REG: Writable + RegisterSpec,
485 bool: From<FI>,
486{
487 #[inline(always)]
489 pub fn toggle_bit(self) -> &'a mut W<REG> {
490 self.w.bits &= !(REG::Ux::ONE << self.o);
491 self.w
492 }
493}
494#[repr(transparent)]
496pub struct Reg<REG: RegisterSpec> {
497 register: vcell::VolatileCell<REG::Ux>,
498 _marker: marker::PhantomData<REG>,
499}
500unsafe impl<REG: RegisterSpec> Send for Reg<REG> where REG::Ux: Send {}
501impl<REG: RegisterSpec> Reg<REG> {
502 #[inline(always)]
508 pub fn as_ptr(&self) -> *mut REG::Ux {
509 self.register.as_ptr()
510 }
511}
512impl<REG: Readable> Reg<REG> {
513 #[inline(always)]
526 pub fn read(&self) -> R<REG> {
527 R {
528 bits: self.register.get(),
529 _reg: marker::PhantomData,
530 }
531 }
532}
533impl<REG: Resettable + Writable> Reg<REG> {
534 #[inline(always)]
538 pub fn reset(&self) {
539 self.register.set(REG::RESET_VALUE)
540 }
541 #[inline(always)]
565 pub fn write<F>(&self, f: F) -> REG::Ux
566 where
567 F: FnOnce(&mut W<REG>) -> &mut W<REG>,
568 {
569 let value = f(&mut W {
570 bits: REG::RESET_VALUE & !REG::ONE_TO_MODIFY_FIELDS_BITMAP
571 | REG::ZERO_TO_MODIFY_FIELDS_BITMAP,
572 _reg: marker::PhantomData,
573 })
574 .bits;
575 self.register.set(value);
576 value
577 }
578 #[inline(always)]
607 pub fn from_write<F, T>(&self, f: F) -> T
608 where
609 F: FnOnce(&mut W<REG>) -> T,
610 {
611 let mut writer = W {
612 bits: REG::RESET_VALUE & !REG::ONE_TO_MODIFY_FIELDS_BITMAP
613 | REG::ZERO_TO_MODIFY_FIELDS_BITMAP,
614 _reg: marker::PhantomData,
615 };
616 let result = f(&mut writer);
617 self.register.set(writer.bits);
618 result
619 }
620}
621impl<REG: Writable> Reg<REG> {
622 #[inline(always)]
630 pub unsafe fn write_with_zero<F>(&self, f: F) -> REG::Ux
631 where
632 F: FnOnce(&mut W<REG>) -> &mut W<REG>,
633 {
634 let value = f(&mut W {
635 bits: REG::Ux::ZERO,
636 _reg: marker::PhantomData,
637 })
638 .bits;
639 self.register.set(value);
640 value
641 }
642 #[inline(always)]
650 pub unsafe fn from_write_with_zero<F, T>(&self, f: F) -> T
651 where
652 F: FnOnce(&mut W<REG>) -> T,
653 {
654 let mut writer = W {
655 bits: REG::Ux::ZERO,
656 _reg: marker::PhantomData,
657 };
658 let result = f(&mut writer);
659 self.register.set(writer.bits);
660 result
661 }
662}
663impl<REG: Readable + Writable> Reg<REG> {
664 #[inline(always)]
690 pub fn modify<F>(&self, f: F) -> REG::Ux
691 where
692 for<'w> F: FnOnce(&R<REG>, &'w mut W<REG>) -> &'w mut W<REG>,
693 {
694 let bits = self.register.get();
695 let value = f(
696 &R {
697 bits,
698 _reg: marker::PhantomData,
699 },
700 &mut W {
701 bits: bits & !REG::ONE_TO_MODIFY_FIELDS_BITMAP | REG::ZERO_TO_MODIFY_FIELDS_BITMAP,
702 _reg: marker::PhantomData,
703 },
704 )
705 .bits;
706 self.register.set(value);
707 value
708 }
709 #[inline(always)]
741 pub fn from_modify<F, T>(&self, f: F) -> T
742 where
743 for<'w> F: FnOnce(&R<REG>, &'w mut W<REG>) -> T,
744 {
745 let bits = self.register.get();
746 let mut writer = W {
747 bits: bits & !REG::ONE_TO_MODIFY_FIELDS_BITMAP | REG::ZERO_TO_MODIFY_FIELDS_BITMAP,
748 _reg: marker::PhantomData,
749 };
750 let result = f(
751 &R {
752 bits,
753 _reg: marker::PhantomData,
754 },
755 &mut writer,
756 );
757 self.register.set(writer.bits);
758 result
759 }
760}
761impl<REG: Readable> core::fmt::Debug for crate::generic::Reg<REG>
762where
763 R<REG>: core::fmt::Debug,
764{
765 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
766 core::fmt::Debug::fmt(&self.read(), f)
767 }
768}
769#[cfg(feature = "atomics")]
770mod atomic;