1use core::marker;
2#[doc = " Generic peripheral accessor"]
3pub 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 #[doc = "Pointer to the register block"]
9 pub const PTR: *const RB = A as *const _;
10 #[doc = "Return the pointer to the register block"]
11 #[inline(always)]
12 pub const fn ptr() -> *const RB {
13 Self::PTR
14 }
15 #[doc = " Steal an instance of this peripheral"]
16 #[doc = ""]
17 #[doc = " # Safety"]
18 #[doc = ""]
19 #[doc = " Ensure that the new instance of the peripheral cannot be used in a way"]
20 #[doc = " that may race with any existing instances, for example by only"]
21 #[doc = " accessing read-only or write-only registers, or by consuming the"]
22 #[doc = " original peripheral and using critical sections to coordinate"]
23 #[doc = " access between multiple new instances."]
24 #[doc = ""]
25 #[doc = " Additionally, other software such as HALs may rely on only one"]
26 #[doc = " peripheral instance existing to ensure memory safety; ensure"]
27 #[doc = " no stolen instances are passed to such software."]
28 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}
41#[doc = " Raw register type (`u8`, `u16`, `u32`, ...)"]
42pub 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 #[doc = " Mask for bits of width `WI`"]
53 fn mask<const WI: u8>() -> Self;
54 #[doc = " `0`"]
55 const ZERO: Self;
56 #[doc = " `1`"]
57 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);
81#[doc = " Raw register type"]
82pub trait RegisterSpec {
83 #[doc = " Raw register type (`u8`, `u16`, `u32`, ...)."]
84 type Ux: RawReg;
85}
86#[doc = " Raw field type"]
87pub trait FieldSpec: Sized {
88 #[doc = " Raw field type (`u8`, `u16`, `u32`, ...)."]
89 type Ux: Copy + core::fmt::Debug + PartialEq + From<Self>;
90}
91#[doc = " Marker for fields with fixed values"]
92pub trait IsEnum: FieldSpec {}
93#[doc = " Trait implemented by readable registers to enable the `read` method."]
94#[doc = ""]
95#[doc = " Registers marked with `Writable` can be also be `modify`'ed."]
96pub trait Readable: RegisterSpec {}
97#[doc = " Trait implemented by writeable registers."]
98#[doc = ""]
99#[doc = " This enables the `write`, `write_with_zero` and `reset` methods."]
100#[doc = ""]
101#[doc = " Registers marked with `Readable` can be also be `modify`'ed."]
102pub trait Writable: RegisterSpec {
103 #[doc = " Is it safe to write any bits to register"]
104 type Safety;
105 #[doc = " Specifies the register bits that are not changed if you pass `1` and are changed if you pass `0`"]
106 const ZERO_TO_MODIFY_FIELDS_BITMAP: Self::Ux = Self::Ux::ZERO;
107 #[doc = " Specifies the register bits that are not changed if you pass `0` and are changed if you pass `1`"]
108 const ONE_TO_MODIFY_FIELDS_BITMAP: Self::Ux = Self::Ux::ZERO;
109}
110#[doc = " Reset value of the register."]
111#[doc = ""]
112#[doc = " This value is the initial value for the `write` method. It can also be directly written to the"]
113#[doc = " register by using the `reset` method."]
114pub trait Resettable: RegisterSpec {
115 #[doc = " Reset value of the register."]
116 const RESET_VALUE: Self::Ux = Self::Ux::ZERO;
117 #[doc = " Reset value of the register."]
118 #[inline(always)]
119 fn reset_value() -> Self::Ux {
120 Self::RESET_VALUE
121 }
122}
123#[doc(hidden)]
124pub mod raw;
125#[doc = " Register reader."]
126#[doc = ""]
127#[doc = " Result of the `read` methods of registers. Also used as a closure argument in the `modify`"]
128#[doc = " method."]
129pub type R<REG> = raw::R<REG>;
130impl<REG: RegisterSpec> R<REG> {
131 #[doc = " Reads raw bits from register."]
132 #[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}
148#[doc = " Register writer."]
149#[doc = ""]
150#[doc = " Used as an argument to the closures in the `write` and `modify` methods of the register."]
151pub type W<REG> = raw::W<REG>;
152impl<REG: Writable> W<REG> {
153 #[doc = " Writes raw bits to the register."]
154 #[doc = ""]
155 #[doc = " # Safety"]
156 #[doc = ""]
157 #[doc = " Passing incorrect value can cause undefined behaviour. See reference manual"]
158 #[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 #[doc = " Writes raw bits to the register."]
169 #[inline(always)]
170 pub fn set(&mut self, bits: REG::Ux) -> &mut Self {
171 self.bits = bits;
172 self
173 }
174}
175#[doc = " Field reader."]
176#[doc = ""]
177#[doc = " Result of the `read` methods of fields."]
178pub type FieldReader<FI = u8> = raw::FieldReader<FI>;
179#[doc = " Bit-wise field reader"]
180pub type BitReader<FI = bool> = raw::BitReader<FI>;
181impl<FI: FieldSpec> FieldReader<FI> {
182 #[doc = " Reads raw bits from field."]
183 #[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 #[doc = " Value of the field as raw bits."]
214 #[inline(always)]
215 pub const fn bit(&self) -> bool {
216 self.bits
217 }
218 #[doc = " Returns `true` if the bit is clear (0)."]
219 #[inline(always)]
220 pub const fn bit_is_clear(&self) -> bool {
221 !self.bit()
222 }
223 #[doc = " Returns `true` if the bit is set (1)."]
224 #[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}
234#[doc = " Marker for register/field writers which can take any value of specified width"]
235pub struct Safe;
236#[doc = " You should check that value is allowed to pass to register/field writer marked with this"]
237pub struct Unsafe;
238#[doc = " Marker for field writers are safe to write in specified inclusive range"]
239pub struct Range<const MIN: u64, const MAX: u64>;
240#[doc = " Marker for field writers are safe to write in specified inclusive range"]
241pub struct RangeFrom<const MIN: u64>;
242#[doc = " Marker for field writers are safe to write in specified inclusive range"]
243pub struct RangeTo<const MAX: u64>;
244#[doc = " Write field Proxy"]
245pub 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 #[doc = " Field width"]
253 pub const WIDTH: u8 = WI;
254 #[doc = " Field width"]
255 #[inline(always)]
256 pub const fn width(&self) -> u8 {
257 WI
258 }
259 #[doc = " Field offset"]
260 #[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 #[doc = " Writes raw bits to the field"]
272 #[doc = ""]
273 #[doc = " # Safety"]
274 #[doc = ""]
275 #[doc = " Passing incorrect value can cause undefined behaviour. See reference manual"]
276 #[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 #[doc = " Writes raw bits to the field"]
290 #[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 #[doc = " Writes raw bits to the field"]
304 #[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 #[doc = " Writes raw bits to the field"]
321 #[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 #[doc = " Writes raw bits to the field"]
338 #[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 #[doc = " Writes `variant` to the field"]
354 #[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 #[doc = " Sets the field bit"]
410 #[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 #[doc = " Clears the field bit"]
416 #[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 #[doc = " Sets the field bit"]
428 #[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 #[doc = " Clears the field bit"]
440 #[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 #[doc = "Clears the field bit by passing one"]
452 #[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 #[doc = "Sets the field bit by passing zero"]
464 #[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 #[doc = "Toggle the field bit by passing one"]
476 #[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 #[doc = "Toggle the field bit by passing zero"]
488 #[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#[doc = " This structure provides volatile access to registers."]
495#[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 #[doc = " Returns the underlying memory address of register."]
503 #[doc = ""]
504 #[doc = " ```ignore"]
505 #[doc = " let reg_ptr = periph.reg.as_ptr();"]
506 #[doc = " ```"]
507 #[inline(always)]
508 pub fn as_ptr(&self) -> *mut REG::Ux {
509 self.register.as_ptr()
510 }
511}
512impl<REG: Readable> Reg<REG> {
513 #[doc = " Reads the contents of a `Readable` register."]
514 #[doc = ""]
515 #[doc = " You can read the raw contents of a register by using `bits`:"]
516 #[doc = " ```ignore"]
517 #[doc = " let bits = periph.reg.read().bits();"]
518 #[doc = " ```"]
519 #[doc = " or get the content of a particular field of a register:"]
520 #[doc = " ```ignore"]
521 #[doc = " let reader = periph.reg.read();"]
522 #[doc = " let bits = reader.field1().bits();"]
523 #[doc = " let flag = reader.field2().bit_is_set();"]
524 #[doc = " ```"]
525 #[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 #[doc = " Writes the reset value to `Writable` register."]
535 #[doc = ""]
536 #[doc = " Resets the register to its initial state."]
537 #[inline(always)]
538 pub fn reset(&self) {
539 self.register.set(REG::RESET_VALUE)
540 }
541 #[doc = " Writes bits to a `Writable` register."]
542 #[doc = ""]
543 #[doc = " You can write raw bits into a register:"]
544 #[doc = " ```ignore"]
545 #[doc = " periph.reg.write(|w| unsafe { w.bits(rawbits) });"]
546 #[doc = " ```"]
547 #[doc = " or write only the fields you need:"]
548 #[doc = " ```ignore"]
549 #[doc = " periph.reg.write(|w| w"]
550 #[doc = " .field1().bits(newfield1bits)"]
551 #[doc = " .field2().set_bit()"]
552 #[doc = " .field3().variant(VARIANT)"]
553 #[doc = " );"]
554 #[doc = " ```"]
555 #[doc = " or an alternative way of saying the same:"]
556 #[doc = " ```ignore"]
557 #[doc = " periph.reg.write(|w| {"]
558 #[doc = " w.field1().bits(newfield1bits);"]
559 #[doc = " w.field2().set_bit();"]
560 #[doc = " w.field3().variant(VARIANT)"]
561 #[doc = " });"]
562 #[doc = " ```"]
563 #[doc = " In the latter case, other fields will be set to their reset value."]
564 #[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 #[doc = " Writes bits to a `Writable` register and produce a value."]
579 #[doc = ""]
580 #[doc = " You can write raw bits into a register:"]
581 #[doc = " ```ignore"]
582 #[doc = " periph.reg.write_and(|w| unsafe { w.bits(rawbits); });"]
583 #[doc = " ```"]
584 #[doc = " or write only the fields you need:"]
585 #[doc = " ```ignore"]
586 #[doc = " periph.reg.write_and(|w| {"]
587 #[doc = " w.field1().bits(newfield1bits)"]
588 #[doc = " .field2().set_bit()"]
589 #[doc = " .field3().variant(VARIANT);"]
590 #[doc = " });"]
591 #[doc = " ```"]
592 #[doc = " or an alternative way of saying the same:"]
593 #[doc = " ```ignore"]
594 #[doc = " periph.reg.write_and(|w| {"]
595 #[doc = " w.field1().bits(newfield1bits);"]
596 #[doc = " w.field2().set_bit();"]
597 #[doc = " w.field3().variant(VARIANT);"]
598 #[doc = " });"]
599 #[doc = " ```"]
600 #[doc = " In the latter case, other fields will be set to their reset value."]
601 #[doc = ""]
602 #[doc = " Values can be returned from the closure:"]
603 #[doc = " ```ignore"]
604 #[doc = " let state = periph.reg.write_and(|w| State::set(w.field1()));"]
605 #[doc = " ```"]
606 #[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 #[doc = " Writes 0 to a `Writable` register."]
623 #[doc = ""]
624 #[doc = " Similar to `write`, but unused bits will contain 0."]
625 #[doc = ""]
626 #[doc = " # Safety"]
627 #[doc = ""]
628 #[doc = " Unsafe to use with registers which don't allow to write 0."]
629 #[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 #[doc = " Writes 0 to a `Writable` register and produces a value."]
643 #[doc = ""]
644 #[doc = " Similar to `write`, but unused bits will contain 0."]
645 #[doc = ""]
646 #[doc = " # Safety"]
647 #[doc = ""]
648 #[doc = " Unsafe to use with registers which don't allow to write 0."]
649 #[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 #[doc = " Modifies the contents of the register by reading and then writing it."]
665 #[doc = ""]
666 #[doc = " E.g. to do a read-modify-write sequence to change parts of a register:"]
667 #[doc = " ```ignore"]
668 #[doc = " periph.reg.modify(|r, w| unsafe { w.bits("]
669 #[doc = " r.bits() | 3"]
670 #[doc = " ) });"]
671 #[doc = " ```"]
672 #[doc = " or"]
673 #[doc = " ```ignore"]
674 #[doc = " periph.reg.modify(|_, w| w"]
675 #[doc = " .field1().bits(newfield1bits)"]
676 #[doc = " .field2().set_bit()"]
677 #[doc = " .field3().variant(VARIANT)"]
678 #[doc = " );"]
679 #[doc = " ```"]
680 #[doc = " or an alternative way of saying the same:"]
681 #[doc = " ```ignore"]
682 #[doc = " periph.reg.modify(|_, w| {"]
683 #[doc = " w.field1().bits(newfield1bits);"]
684 #[doc = " w.field2().set_bit();"]
685 #[doc = " w.field3().variant(VARIANT)"]
686 #[doc = " });"]
687 #[doc = " ```"]
688 #[doc = " Other fields will have the value they had before the call to `modify`."]
689 #[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 #[doc = " Modifies the contents of the register by reading and then writing it"]
710 #[doc = " and produces a value."]
711 #[doc = ""]
712 #[doc = " E.g. to do a read-modify-write sequence to change parts of a register:"]
713 #[doc = " ```ignore"]
714 #[doc = " let bits = periph.reg.modify(|r, w| {"]
715 #[doc = " let new_bits = r.bits() | 3;"]
716 #[doc = " unsafe {"]
717 #[doc = " w.bits(new_bits);"]
718 #[doc = " }"]
719 #[doc = ""]
720 #[doc = " new_bits"]
721 #[doc = " });"]
722 #[doc = " ```"]
723 #[doc = " or"]
724 #[doc = " ```ignore"]
725 #[doc = " periph.reg.modify(|_, w| {"]
726 #[doc = " w.field1().bits(newfield1bits)"]
727 #[doc = " .field2().set_bit()"]
728 #[doc = " .field3().variant(VARIANT);"]
729 #[doc = " });"]
730 #[doc = " ```"]
731 #[doc = " or an alternative way of saying the same:"]
732 #[doc = " ```ignore"]
733 #[doc = " periph.reg.modify(|_, w| {"]
734 #[doc = " w.field1().bits(newfield1bits);"]
735 #[doc = " w.field2().set_bit();"]
736 #[doc = " w.field3().variant(VARIANT);"]
737 #[doc = " });"]
738 #[doc = " ```"]
739 #[doc = " Other fields will have the value they had before the call to `modify`."]
740 #[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}