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)]
104 pub fn write<F>(&self, f: F)
105 where
106 F: FnOnce(&mut REG::Writer) -> &mut W<REG>,
107 {
108 self.register.set(
109 f(&mut REG::Writer::from(W {
110 bits: REG::reset_value(),
111 _reg: marker::PhantomData,
112 }))
113 .bits,
114 );
115 }
116}
117
118impl<REG: Writable> Reg<REG>
119where
120 REG::Ux: Default,
121{
122 #[inline(always)]
126 pub unsafe fn write_with_zero<F>(&self, f: F)
127 where
128 F: FnOnce(&mut REG::Writer) -> &mut W<REG>,
129 {
130 self.register.set(
131 (*f(&mut REG::Writer::from(W {
132 bits: REG::Ux::default(),
133 _reg: marker::PhantomData,
134 })))
135 .bits,
136 );
137 }
138}
139
140impl<REG: Readable + Writable> Reg<REG> {
141 #[inline(always)]
159 pub fn modify<F>(&self, f: F)
160 where
161 for<'w> F: FnOnce(®::Reader, &'w mut REG::Writer) -> &'w mut W<REG>,
162 {
163 let bits = self.register.get();
164 self.register.set(
165 f(
166 ®::Reader::from(R {
167 bits,
168 _reg: marker::PhantomData,
169 }),
170 &mut REG::Writer::from(W {
171 bits,
172 _reg: marker::PhantomData,
173 }),
174 )
175 .bits,
176 );
177 }
178}
179
180pub struct R<REG: RegisterSpec + ?Sized> {
185 pub(crate) bits: REG::Ux,
186 _reg: marker::PhantomData<REG>,
187}
188
189impl<REG: RegisterSpec> R<REG> {
190 #[inline(always)]
192 pub fn bits(&self) -> REG::Ux {
193 self.bits
194 }
195}
196
197impl<REG: RegisterSpec, FI> PartialEq<FI> for R<REG>
198where
199 REG::Ux: PartialEq,
200 FI: Copy + Into<REG::Ux>,
201{
202 #[inline(always)]
203 fn eq(&self, other: &FI) -> bool {
204 self.bits.eq(&(*other).into())
205 }
206}
207
208pub struct W<REG: RegisterSpec + ?Sized> {
212 pub(crate) bits: REG::Ux,
214 _reg: marker::PhantomData<REG>,
215}
216
217impl<REG: RegisterSpec> W<REG> {
218 #[inline(always)]
220 pub unsafe fn bits(&mut self, bits: REG::Ux) -> &mut Self {
221 self.bits = bits;
222 self
223 }
224}
225
226#[doc(hidden)]
227pub struct FieldReaderRaw<U, T> {
228 pub(crate) bits: U,
229 _reg: marker::PhantomData<T>,
230}
231
232impl<U, FI> FieldReaderRaw<U, FI>
233where
234 U: Copy,
235{
236 #[allow(unused)]
238 #[inline(always)]
239 pub(crate) fn new(bits: U) -> Self {
240 Self {
241 bits,
242 _reg: marker::PhantomData,
243 }
244 }
245}
246
247#[doc(hidden)]
248pub struct BitReaderRaw<T> {
249 pub(crate) bits: bool,
250 _reg: marker::PhantomData<T>,
251}
252
253impl<FI> BitReaderRaw<FI> {
254 #[allow(unused)]
256 #[inline(always)]
257 pub(crate) fn new(bits: bool) -> Self {
258 Self {
259 bits,
260 _reg: marker::PhantomData,
261 }
262 }
263}
264
265pub type FieldReader<U, FI> = FieldReaderRaw<U, FI>;
269
270pub type BitReader<FI> = BitReaderRaw<FI>;
272
273impl<U, FI> FieldReader<U, FI>
274where
275 U: Copy,
276{
277 #[inline(always)]
279 pub fn bits(&self) -> U {
280 self.bits
281 }
282}
283
284impl<U, FI> PartialEq<FI> for FieldReader<U, FI>
285where
286 U: PartialEq,
287 FI: Copy + Into<U>,
288{
289 #[inline(always)]
290 fn eq(&self, other: &FI) -> bool {
291 self.bits.eq(&(*other).into())
292 }
293}
294
295impl<FI> PartialEq<FI> for BitReader<FI>
296where
297 FI: Copy + Into<bool>,
298{
299 #[inline(always)]
300 fn eq(&self, other: &FI) -> bool {
301 self.bits.eq(&(*other).into())
302 }
303}
304
305impl<FI> BitReader<FI> {
306 #[inline(always)]
308 pub fn bit(&self) -> bool {
309 self.bits
310 }
311 #[inline(always)]
313 pub fn bit_is_clear(&self) -> bool {
314 !self.bit()
315 }
316 #[inline(always)]
318 pub fn bit_is_set(&self) -> bool {
319 self.bit()
320 }
321}
322
323#[doc(hidden)]
324pub struct Safe;
325#[doc(hidden)]
326pub struct Unsafe;
327
328#[doc(hidden)]
329pub struct FieldWriterRaw<'a, U, REG, N, FI, Safety, const WI: u8, const O: u8>
330where
331 REG: Writable + RegisterSpec<Ux = U>,
332 FI: Into<N>,
333{
334 pub(crate) w: &'a mut REG::Writer,
335 _field: marker::PhantomData<(N, FI, Safety)>,
336}
337
338impl<'a, U, REG, N, FI, Safety, const WI: u8, const O: u8>
339 FieldWriterRaw<'a, U, REG, N, FI, Safety, WI, O>
340where
341 REG: Writable + RegisterSpec<Ux = U>,
342 FI: Into<N>,
343{
344 #[allow(unused)]
346 #[inline(always)]
347 pub(crate) fn new(w: &'a mut REG::Writer) -> Self {
348 Self {
349 w,
350 _field: marker::PhantomData,
351 }
352 }
353}
354
355#[doc(hidden)]
356pub struct BitWriterRaw<'a, U, REG, FI, M, const O: u8>
357where
358 REG: Writable + RegisterSpec<Ux = U>,
359 FI: Into<bool>,
360{
361 pub(crate) w: &'a mut REG::Writer,
362 _field: marker::PhantomData<(FI, M)>,
363}
364
365impl<'a, U, REG, FI, M, const O: u8> BitWriterRaw<'a, U, REG, FI, M, O>
366where
367 REG: Writable + RegisterSpec<Ux = U>,
368 FI: Into<bool>,
369{
370 #[allow(unused)]
372 #[inline(always)]
373 pub(crate) fn new(w: &'a mut REG::Writer) -> Self {
374 Self {
375 w,
376 _field: marker::PhantomData,
377 }
378 }
379}
380
381pub type FieldWriter<'a, U, REG, N, FI, const WI: u8, const O: u8> =
383 FieldWriterRaw<'a, U, REG, N, FI, Unsafe, WI, O>;
384pub type FieldWriterSafe<'a, U, REG, N, FI, const WI: u8, const O: u8> =
386 FieldWriterRaw<'a, U, REG, N, FI, Safe, WI, O>;
387
388impl<'a, U, REG, N, FI, const WI: u8, const OF: u8> FieldWriter<'a, U, REG, N, FI, WI, OF>
389where
390 REG: Writable + RegisterSpec<Ux = U>,
391 FI: Into<N>,
392{
393 pub const WIDTH: u8 = WI;
395 pub const OFFSET: u8 = OF;
397}
398
399impl<'a, U, REG, N, FI, const WI: u8, const OF: u8> FieldWriterSafe<'a, U, REG, N, FI, WI, OF>
400where
401 REG: Writable + RegisterSpec<Ux = U>,
402 FI: Into<N>,
403{
404 pub const WIDTH: u8 = WI;
406 pub const OFFSET: u8 = OF;
408}
409
410macro_rules! bit_proxy {
411 ($writer:ident, $mwv:ident) => {
412 #[doc(hidden)]
413 pub struct $mwv;
414
415 pub type $writer<'a, U, REG, FI, const O: u8> = BitWriterRaw<'a, U, REG, FI, $mwv, O>;
417
418 impl<'a, U, REG, FI, const OF: u8> $writer<'a, U, REG, FI, OF>
419 where
420 REG: Writable + RegisterSpec<Ux = U>,
421 FI: Into<bool>,
422 {
423 pub const WIDTH: u8 = 1;
425 pub const OFFSET: u8 = OF;
427 }
428 };
429}
430
431macro_rules! impl_bit_proxy {
432 ($writer:ident, $U:ty) => {
433 impl<'a, REG, FI, const OF: u8> $writer<'a, $U, REG, FI, OF>
434 where
435 REG: Writable + RegisterSpec<Ux = $U>,
436 FI: Into<bool>,
437 {
438 #[inline(always)]
440 pub fn bit(self, value: bool) -> &'a mut REG::Writer {
441 self.w.bits = (self.w.bits & !(1 << { OF })) | ((<$U>::from(value) & 1) << { OF });
442 self.w
443 }
444 #[inline(always)]
446 pub fn variant(self, variant: FI) -> &'a mut REG::Writer {
447 self.bit(variant.into())
448 }
449 }
450 };
451}
452
453bit_proxy!(BitWriter, BitM);
454bit_proxy!(BitWriter1S, Bit1S);
455bit_proxy!(BitWriter0C, Bit0C);
456bit_proxy!(BitWriter1C, Bit1C);
457bit_proxy!(BitWriter0S, Bit0S);
458bit_proxy!(BitWriter1T, Bit1T);
459bit_proxy!(BitWriter0T, Bit0T);
460
461macro_rules! impl_proxy {
462 ($U:ty) => {
463 impl<'a, REG, N, FI, const WI: u8, const OF: u8> FieldWriter<'a, $U, REG, N, FI, WI, OF>
464 where
465 REG: Writable + RegisterSpec<Ux = $U>,
466 N: Into<$U>,
467 FI: Into<N>,
468 {
469 const MASK: $U = <$U>::MAX >> (<$U>::MAX.leading_ones() as u8 - { WI });
470 #[inline(always)]
476 pub unsafe fn bits(self, value: N) -> &'a mut REG::Writer {
477 self.w.bits = (self.w.bits & !(Self::MASK << { OF }))
478 | ((value.into() & Self::MASK) << { OF });
479 self.w
480 }
481 #[inline(always)]
483 pub fn variant(self, variant: FI) -> &'a mut REG::Writer {
484 unsafe { self.bits(variant.into()) }
485 }
486 }
487 impl<'a, REG, N, FI, const WI: u8, const OF: u8> FieldWriterSafe<'a, $U, REG, N, FI, WI, OF>
488 where
489 REG: Writable + RegisterSpec<Ux = $U>,
490 N: Into<$U>,
491 FI: Into<N>,
492 {
493 const MASK: $U = <$U>::MAX >> (<$U>::MAX.leading_ones() as u8 - { WI });
494 #[inline(always)]
496 pub fn bits(self, value: N) -> &'a mut REG::Writer {
497 self.w.bits = (self.w.bits & !(Self::MASK << { OF }))
498 | ((value.into() & Self::MASK) << { OF });
499 self.w
500 }
501 #[inline(always)]
503 pub fn variant(self, variant: FI) -> &'a mut REG::Writer {
504 self.bits(variant.into())
505 }
506 }
507 impl_bit_proxy!(BitWriter, $U);
508 impl_bit_proxy!(BitWriter1S, $U);
509 impl_bit_proxy!(BitWriter0C, $U);
510 impl_bit_proxy!(BitWriter1C, $U);
511 impl_bit_proxy!(BitWriter0S, $U);
512 impl_bit_proxy!(BitWriter1T, $U);
513 impl_bit_proxy!(BitWriter0T, $U);
514 impl<'a, REG, FI, const OF: u8> BitWriter<'a, $U, REG, FI, OF>
515 where
516 REG: Writable + RegisterSpec<Ux = $U>,
517 FI: Into<bool>,
518 {
519 #[inline(always)]
521 pub fn set_bit(self) -> &'a mut REG::Writer {
522 self.bit(true)
523 }
524 #[inline(always)]
526 pub fn clear_bit(self) -> &'a mut REG::Writer {
527 self.bit(false)
528 }
529 }
530 impl<'a, REG, FI, const OF: u8> BitWriter1S<'a, $U, REG, FI, OF>
531 where
532 REG: Writable + RegisterSpec<Ux = $U>,
533 FI: Into<bool>,
534 {
535 #[inline(always)]
537 pub fn set_bit(self) -> &'a mut REG::Writer {
538 self.bit(true)
539 }
540 }
541 impl<'a, REG, FI, const OF: u8> BitWriter0C<'a, $U, REG, FI, OF>
542 where
543 REG: Writable + RegisterSpec<Ux = $U>,
544 FI: Into<bool>,
545 {
546 #[inline(always)]
548 pub fn clear_bit(self) -> &'a mut REG::Writer {
549 self.bit(false)
550 }
551 }
552 impl<'a, REG, FI, const OF: u8> BitWriter1C<'a, $U, REG, FI, OF>
553 where
554 REG: Writable + RegisterSpec<Ux = $U>,
555 FI: Into<bool>,
556 {
557 #[inline(always)]
559 pub fn clear_bit_by_one(self) -> &'a mut REG::Writer {
560 self.bit(true)
561 }
562 }
563 impl<'a, REG, FI, const OF: u8> BitWriter0S<'a, $U, REG, FI, OF>
564 where
565 REG: Writable + RegisterSpec<Ux = $U>,
566 FI: Into<bool>,
567 {
568 #[inline(always)]
570 pub fn set_bit_by_zero(self) -> &'a mut REG::Writer {
571 self.bit(false)
572 }
573 }
574 impl<'a, REG, FI, const OF: u8> BitWriter1T<'a, $U, REG, FI, OF>
575 where
576 REG: Writable + RegisterSpec<Ux = $U>,
577 FI: Into<bool>,
578 {
579 #[inline(always)]
581 pub fn toggle_bit(self) -> &'a mut REG::Writer {
582 self.bit(true)
583 }
584 }
585 impl<'a, REG, FI, const OF: u8> BitWriter0T<'a, $U, REG, FI, OF>
586 where
587 REG: Writable + RegisterSpec<Ux = $U>,
588 FI: Into<bool>,
589 {
590 #[inline(always)]
592 pub fn toggle_bit(self) -> &'a mut REG::Writer {
593 self.bit(false)
594 }
595 }
596 };
597}
598
599impl_proxy!(u16);
600impl_proxy!(u32);
601impl_proxy!(u8);