1use core::marker;
2
3pub trait RegisterSpec {
5 type Ux: Copy;
7}
8
9pub trait Readable: RegisterSpec {
13 type Reader: From<R<Self>> + core::ops::Deref<Target = R<Self>>;
15}
16
17pub trait Writable: RegisterSpec {
23 type Writer: From<W<Self>> + core::ops::DerefMut<Target = W<Self>>;
25}
26
27pub trait Resettable: RegisterSpec {
32 fn reset_value() -> Self::Ux;
34}
35
36#[repr(transparent)]
38pub struct Reg<REG: RegisterSpec> {
39 register: vcell::VolatileCell<REG::Ux>,
40 _marker: marker::PhantomData<REG>,
41}
42
43unsafe impl<REG: RegisterSpec> Send for Reg<REG> where REG::Ux: Send {}
44
45impl<REG: RegisterSpec> Reg<REG> {
46 #[inline(always)]
52 pub fn as_ptr(&self) -> *mut REG::Ux {
53 self.register.as_ptr()
54 }
55}
56
57impl<REG: Readable> Reg<REG> {
58 #[inline(always)]
71 pub fn read(&self) -> REG::Reader {
72 REG::Reader::from(R {
73 bits: self.register.get(),
74 _reg: marker::PhantomData,
75 })
76 }
77}
78
79impl<REG: Resettable + Writable> Reg<REG> {
80 #[inline(always)]
84 pub fn reset(&self) {
85 self.register.set(REG::reset_value())
86 }
87
88 #[inline(always)]
112 pub fn write<F>(&self, f: F)
113 where
114 F: FnOnce(&mut REG::Writer) -> &mut W<REG>,
115 {
116 self.register.set(
117 f(&mut REG::Writer::from(W {
118 bits: REG::reset_value(),
119 _reg: marker::PhantomData,
120 }))
121 .bits,
122 );
123 }
124}
125
126impl<REG: Writable> Reg<REG>
127where
128 REG::Ux: Default,
129{
130 #[inline(always)]
138 pub unsafe fn write_with_zero<F>(&self, f: F)
139 where
140 F: FnOnce(&mut REG::Writer) -> &mut W<REG>,
141 {
142 self.register.set(
143 (*f(&mut REG::Writer::from(W {
144 bits: REG::Ux::default(),
145 _reg: marker::PhantomData,
146 })))
147 .bits,
148 );
149 }
150}
151
152impl<REG: Readable + Writable> Reg<REG> {
153 #[inline(always)]
179 pub fn modify<F>(&self, f: F)
180 where
181 for<'w> F: FnOnce(®::Reader, &'w mut REG::Writer) -> &'w mut W<REG>,
182 {
183 let bits = self.register.get();
184 self.register.set(
185 f(
186 ®::Reader::from(R {
187 bits,
188 _reg: marker::PhantomData,
189 }),
190 &mut REG::Writer::from(W {
191 bits,
192 _reg: marker::PhantomData,
193 }),
194 )
195 .bits,
196 );
197 }
198}
199
200pub struct R<REG: RegisterSpec + ?Sized> {
205 pub(crate) bits: REG::Ux,
206 _reg: marker::PhantomData<REG>,
207}
208
209impl<REG: RegisterSpec> R<REG> {
210 #[inline(always)]
212 pub fn bits(&self) -> REG::Ux {
213 self.bits
214 }
215}
216
217impl<REG: RegisterSpec, FI> PartialEq<FI> for R<REG>
218where
219 REG::Ux: PartialEq,
220 FI: Copy + Into<REG::Ux>,
221{
222 #[inline(always)]
223 fn eq(&self, other: &FI) -> bool {
224 self.bits.eq(&(*other).into())
225 }
226}
227
228pub struct W<REG: RegisterSpec + ?Sized> {
232 pub(crate) bits: REG::Ux,
234 _reg: marker::PhantomData<REG>,
235}
236
237impl<REG: RegisterSpec> W<REG> {
238 #[inline(always)]
244 pub unsafe fn bits(&mut self, bits: REG::Ux) -> &mut Self {
245 self.bits = bits;
246 self
247 }
248}
249
250#[doc(hidden)]
251pub struct FieldReaderRaw<U, T> {
252 pub(crate) bits: U,
253 _reg: marker::PhantomData<T>,
254}
255
256impl<U, FI> FieldReaderRaw<U, FI>
257where
258 U: Copy,
259{
260 #[allow(unused)]
262 #[inline(always)]
263 pub(crate) fn new(bits: U) -> Self {
264 Self {
265 bits,
266 _reg: marker::PhantomData,
267 }
268 }
269}
270
271#[doc(hidden)]
272pub struct BitReaderRaw<T> {
273 pub(crate) bits: bool,
274 _reg: marker::PhantomData<T>,
275}
276
277impl<FI> BitReaderRaw<FI> {
278 #[allow(unused)]
280 #[inline(always)]
281 pub(crate) fn new(bits: bool) -> Self {
282 Self {
283 bits,
284 _reg: marker::PhantomData,
285 }
286 }
287}
288
289pub type FieldReader<U, FI> = FieldReaderRaw<U, FI>;
293
294pub type BitReader<FI> = BitReaderRaw<FI>;
296
297impl<U, FI> FieldReader<U, FI>
298where
299 U: Copy,
300{
301 #[inline(always)]
303 pub fn bits(&self) -> U {
304 self.bits
305 }
306}
307
308impl<U, FI> PartialEq<FI> for FieldReader<U, FI>
309where
310 U: PartialEq,
311 FI: Copy + Into<U>,
312{
313 #[inline(always)]
314 fn eq(&self, other: &FI) -> bool {
315 self.bits.eq(&(*other).into())
316 }
317}
318
319impl<FI> PartialEq<FI> for BitReader<FI>
320where
321 FI: Copy + Into<bool>,
322{
323 #[inline(always)]
324 fn eq(&self, other: &FI) -> bool {
325 self.bits.eq(&(*other).into())
326 }
327}
328
329impl<FI> BitReader<FI> {
330 #[inline(always)]
332 pub fn bit(&self) -> bool {
333 self.bits
334 }
335 #[inline(always)]
337 pub fn bit_is_clear(&self) -> bool {
338 !self.bit()
339 }
340 #[inline(always)]
342 pub fn bit_is_set(&self) -> bool {
343 self.bit()
344 }
345}
346
347#[doc(hidden)]
348pub struct Safe;
349#[doc(hidden)]
350pub struct Unsafe;
351
352#[doc(hidden)]
353pub struct FieldWriterRaw<'a, U, REG, N, FI, Safety, const WI: u8, const O: u8>
354where
355 REG: Writable + RegisterSpec<Ux = U>,
356 FI: Into<N>,
357{
358 pub(crate) w: &'a mut REG::Writer,
359 _field: marker::PhantomData<(N, FI, Safety)>,
360}
361
362impl<'a, U, REG, N, FI, Safety, const WI: u8, const O: u8>
363 FieldWriterRaw<'a, U, REG, N, FI, Safety, WI, O>
364where
365 REG: Writable + RegisterSpec<Ux = U>,
366 FI: Into<N>,
367{
368 #[allow(unused)]
370 #[inline(always)]
371 pub(crate) fn new(w: &'a mut REG::Writer) -> Self {
372 Self {
373 w,
374 _field: marker::PhantomData,
375 }
376 }
377}
378
379#[doc(hidden)]
380pub struct BitWriterRaw<'a, U, REG, FI, M, const O: u8>
381where
382 REG: Writable + RegisterSpec<Ux = U>,
383 FI: Into<bool>,
384{
385 pub(crate) w: &'a mut REG::Writer,
386 _field: marker::PhantomData<(FI, M)>,
387}
388
389impl<'a, U, REG, FI, M, const O: u8> BitWriterRaw<'a, U, REG, FI, M, O>
390where
391 REG: Writable + RegisterSpec<Ux = U>,
392 FI: Into<bool>,
393{
394 #[allow(unused)]
396 #[inline(always)]
397 pub(crate) fn new(w: &'a mut REG::Writer) -> Self {
398 Self {
399 w,
400 _field: marker::PhantomData,
401 }
402 }
403}
404
405pub type FieldWriter<'a, U, REG, N, FI, const WI: u8, const O: u8> =
407 FieldWriterRaw<'a, U, REG, N, FI, Unsafe, WI, O>;
408pub type FieldWriterSafe<'a, U, REG, N, FI, const WI: u8, const O: u8> =
410 FieldWriterRaw<'a, U, REG, N, FI, Safe, WI, O>;
411
412impl<'a, U, REG, N, FI, const WI: u8, const OF: u8>
413 FieldWriter<'a, U, REG, N, FI, WI, OF>
414where
415 REG: Writable + RegisterSpec<Ux = U>,
416 FI: Into<N>,
417{
418 pub const WIDTH: u8 = WI;
420 pub const OFFSET: u8 = OF;
422}
423
424impl<'a, U, REG, N, FI, const WI: u8, const OF: u8>
425 FieldWriterSafe<'a, U, REG, N, FI, WI, OF>
426where
427 REG: Writable + RegisterSpec<Ux = U>,
428 FI: Into<N>,
429{
430 pub const WIDTH: u8 = WI;
432 pub const OFFSET: u8 = OF;
434}
435
436macro_rules! bit_proxy {
437 ($writer:ident, $mwv:ident) => {
438 #[doc(hidden)]
439 pub struct $mwv;
440
441 pub type $writer<'a, U, REG, FI, const O: u8> =
443 BitWriterRaw<'a, U, REG, FI, $mwv, O>;
444
445 impl<'a, U, REG, FI, const OF: u8> $writer<'a, U, REG, FI, OF>
446 where
447 REG: Writable + RegisterSpec<Ux = U>,
448 FI: Into<bool>,
449 {
450 pub const WIDTH: u8 = 1;
452 pub const OFFSET: u8 = OF;
454 }
455 };
456}
457
458macro_rules! impl_bit_proxy {
459 ($writer:ident, $U:ty) => {
460 impl<'a, REG, FI, const OF: u8> $writer<'a, $U, REG, FI, OF>
461 where
462 REG: Writable + RegisterSpec<Ux = $U>,
463 FI: Into<bool>,
464 {
465 #[inline(always)]
467 pub fn bit(self, value: bool) -> &'a mut REG::Writer {
468 self.w.bits = (self.w.bits & !(1 << { OF }))
469 | ((<$U>::from(value) & 1) << { OF });
470 self.w
471 }
472 #[inline(always)]
474 pub fn variant(self, variant: FI) -> &'a mut REG::Writer {
475 self.bit(variant.into())
476 }
477 }
478 };
479}
480
481bit_proxy!(BitWriter, BitM);
482bit_proxy!(BitWriter1S, Bit1S);
483bit_proxy!(BitWriter0C, Bit0C);
484bit_proxy!(BitWriter1C, Bit1C);
485bit_proxy!(BitWriter0S, Bit0S);
486bit_proxy!(BitWriter1T, Bit1T);
487bit_proxy!(BitWriter0T, Bit0T);
488
489macro_rules! impl_proxy {
490 ($U:ty) => {
491 impl<'a, REG, N, FI, const WI: u8, const OF: u8>
492 FieldWriter<'a, $U, REG, N, FI, WI, OF>
493 where
494 REG: Writable + RegisterSpec<Ux = $U>,
495 N: Into<$U>,
496 FI: Into<N>,
497 {
498 const MASK: $U =
499 <$U>::MAX >> (<$U>::MAX.leading_ones() as u8 - { WI });
500 #[inline(always)]
506 pub unsafe fn bits(self, value: N) -> &'a mut REG::Writer {
507 self.w.bits = (self.w.bits & !(Self::MASK << { OF }))
508 | ((value.into() & Self::MASK) << { OF });
509 self.w
510 }
511 #[inline(always)]
513 pub fn variant(self, variant: FI) -> &'a mut REG::Writer {
514 unsafe { self.bits(variant.into()) }
515 }
516 }
517 impl<'a, REG, N, FI, const WI: u8, const OF: u8>
518 FieldWriterSafe<'a, $U, REG, N, FI, WI, OF>
519 where
520 REG: Writable + RegisterSpec<Ux = $U>,
521 N: Into<$U>,
522 FI: Into<N>,
523 {
524 const MASK: $U =
525 <$U>::MAX >> (<$U>::MAX.leading_ones() as u8 - { WI });
526 #[inline(always)]
528 pub fn bits(self, value: N) -> &'a mut REG::Writer {
529 self.w.bits = (self.w.bits & !(Self::MASK << { OF }))
530 | ((value.into() & Self::MASK) << { OF });
531 self.w
532 }
533 #[inline(always)]
535 pub fn variant(self, variant: FI) -> &'a mut REG::Writer {
536 self.bits(variant.into())
537 }
538 }
539 impl_bit_proxy!(BitWriter, $U);
540 impl_bit_proxy!(BitWriter1S, $U);
541 impl_bit_proxy!(BitWriter0C, $U);
542 impl_bit_proxy!(BitWriter1C, $U);
543 impl_bit_proxy!(BitWriter0S, $U);
544 impl_bit_proxy!(BitWriter1T, $U);
545 impl_bit_proxy!(BitWriter0T, $U);
546 impl<'a, REG, FI, const OF: u8> BitWriter<'a, $U, REG, FI, OF>
547 where
548 REG: Writable + RegisterSpec<Ux = $U>,
549 FI: Into<bool>,
550 {
551 #[inline(always)]
553 pub fn set_bit(self) -> &'a mut REG::Writer {
554 self.bit(true)
555 }
556 #[inline(always)]
558 pub fn clear_bit(self) -> &'a mut REG::Writer {
559 self.bit(false)
560 }
561 }
562 impl<'a, REG, FI, const OF: u8> BitWriter1S<'a, $U, REG, FI, OF>
563 where
564 REG: Writable + RegisterSpec<Ux = $U>,
565 FI: Into<bool>,
566 {
567 #[inline(always)]
569 pub fn set_bit(self) -> &'a mut REG::Writer {
570 self.bit(true)
571 }
572 }
573 impl<'a, REG, FI, const OF: u8> BitWriter0C<'a, $U, REG, FI, OF>
574 where
575 REG: Writable + RegisterSpec<Ux = $U>,
576 FI: Into<bool>,
577 {
578 #[inline(always)]
580 pub fn clear_bit(self) -> &'a mut REG::Writer {
581 self.bit(false)
582 }
583 }
584 impl<'a, REG, FI, const OF: u8> BitWriter1C<'a, $U, REG, FI, OF>
585 where
586 REG: Writable + RegisterSpec<Ux = $U>,
587 FI: Into<bool>,
588 {
589 #[inline(always)]
591 pub fn clear_bit_by_one(self) -> &'a mut REG::Writer {
592 self.bit(true)
593 }
594 }
595 impl<'a, REG, FI, const OF: u8> BitWriter0S<'a, $U, REG, FI, OF>
596 where
597 REG: Writable + RegisterSpec<Ux = $U>,
598 FI: Into<bool>,
599 {
600 #[inline(always)]
602 pub fn set_bit_by_zero(self) -> &'a mut REG::Writer {
603 self.bit(false)
604 }
605 }
606 impl<'a, REG, FI, const OF: u8> BitWriter1T<'a, $U, REG, FI, OF>
607 where
608 REG: Writable + RegisterSpec<Ux = $U>,
609 FI: Into<bool>,
610 {
611 #[inline(always)]
613 pub fn toggle_bit(self) -> &'a mut REG::Writer {
614 self.bit(true)
615 }
616 }
617 impl<'a, REG, FI, const OF: u8> BitWriter0T<'a, $U, REG, FI, OF>
618 where
619 REG: Writable + RegisterSpec<Ux = $U>,
620 FI: Into<bool>,
621 {
622 #[inline(always)]
624 pub fn toggle_bit(self) -> &'a mut REG::Writer {
625 self.bit(false)
626 }
627 }
628 };
629}
630
631impl_proxy!(u8);
632impl_proxy!(u16);