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