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> = raw::FieldWriter<'a, REG, WI, FI, Safety>;
246impl<REG, const WI: u8, FI, Safety> FieldWriter<'_, REG, WI, FI, Safety>
247where
248 REG: Writable + RegisterSpec,
249 FI: FieldSpec,
250{
251 #[doc = " Field width"]
252 pub const WIDTH: u8 = WI;
253 #[doc = " Field width"]
254 #[inline(always)]
255 pub const fn width(&self) -> u8 {
256 WI
257 }
258 #[doc = " Field offset"]
259 #[inline(always)]
260 pub const fn offset(&self) -> u8 {
261 self.o
262 }
263}
264impl<'a, REG, const WI: u8, FI, Safety> FieldWriter<'a, REG, WI, FI, Safety>
265where
266 REG: Writable + RegisterSpec,
267 FI: FieldSpec,
268 REG::Ux: From<FI::Ux>,
269{
270 #[doc = " Writes raw bits to the field"]
271 #[doc = ""]
272 #[doc = " # Safety"]
273 #[doc = ""]
274 #[doc = " Passing incorrect value can cause undefined behaviour. See reference manual"]
275 #[inline(always)]
276 pub unsafe fn bits(self, value: FI::Ux) -> &'a mut W<REG> {
277 self.w.bits &= !(REG::Ux::mask::<WI>() << self.o);
278 self.w.bits |= (REG::Ux::from(value) & REG::Ux::mask::<WI>()) << self.o;
279 self.w
280 }
281}
282impl<'a, REG, const WI: u8, FI> FieldWriter<'a, REG, WI, FI, Safe>
283where
284 REG: Writable + RegisterSpec,
285 FI: FieldSpec,
286 REG::Ux: From<FI::Ux>,
287{
288 #[doc = " Writes raw bits to the field"]
289 #[inline(always)]
290 pub fn set(self, value: FI::Ux) -> &'a mut W<REG> {
291 unsafe { self.bits(value) }
292 }
293}
294impl<'a, REG, const WI: u8, FI, const MIN: u64, const MAX: u64> FieldWriter<'a, REG, WI, FI, Range<MIN, MAX>>
295where
296 REG: Writable + RegisterSpec,
297 FI: FieldSpec,
298 REG::Ux: From<FI::Ux>,
299 u64: From<FI::Ux>,
300{
301 #[doc = " Writes raw bits to the field"]
302 #[inline(always)]
303 pub fn set(self, value: FI::Ux) -> &'a mut W<REG> {
304 {
305 let value = u64::from(value);
306 assert!(value >= MIN && value <= MAX);
307 }
308 unsafe { self.bits(value) }
309 }
310}
311impl<'a, REG, const WI: u8, FI, const MIN: u64> FieldWriter<'a, REG, WI, FI, RangeFrom<MIN>>
312where
313 REG: Writable + RegisterSpec,
314 FI: FieldSpec,
315 REG::Ux: From<FI::Ux>,
316 u64: From<FI::Ux>,
317{
318 #[doc = " Writes raw bits to the field"]
319 #[inline(always)]
320 pub fn set(self, value: FI::Ux) -> &'a mut W<REG> {
321 {
322 let value = u64::from(value);
323 assert!(value >= MIN);
324 }
325 unsafe { self.bits(value) }
326 }
327}
328impl<'a, REG, const WI: u8, FI, const MAX: u64> FieldWriter<'a, REG, WI, FI, RangeTo<MAX>>
329where
330 REG: Writable + RegisterSpec,
331 FI: FieldSpec,
332 REG::Ux: From<FI::Ux>,
333 u64: From<FI::Ux>,
334{
335 #[doc = " Writes raw bits to the field"]
336 #[inline(always)]
337 pub fn set(self, value: FI::Ux) -> &'a mut W<REG> {
338 {
339 let value = u64::from(value);
340 assert!(value <= MAX);
341 }
342 unsafe { self.bits(value) }
343 }
344}
345impl<'a, REG, const WI: u8, FI, Safety> FieldWriter<'a, REG, WI, FI, Safety>
346where
347 REG: Writable + RegisterSpec,
348 FI: IsEnum,
349 REG::Ux: From<FI::Ux>,
350{
351 #[doc = " Writes `variant` to the field"]
352 #[inline(always)]
353 pub fn variant(self, variant: FI) -> &'a mut W<REG> {
354 unsafe { self.bits(FI::Ux::from(variant)) }
355 }
356}
357macro_rules! bit_proxy {
358 ($ writer : ident , $ mwv : ident) => {
359 #[doc(hidden)]
360 pub struct $mwv;
361 #[doc = " Bit-wise write field proxy"]
362 pub type $writer<'a, REG, FI = bool> = raw::BitWriter<'a, REG, FI, $mwv>;
363 impl<'a, REG, FI> $writer<'a, REG, FI>
364 where
365 REG: Writable + RegisterSpec,
366 bool: From<FI>,
367 {
368 #[doc = " Field width"]
369 pub const WIDTH: u8 = 1;
370 #[doc = " Field width"]
371 #[inline(always)]
372 pub const fn width(&self) -> u8 {
373 Self::WIDTH
374 }
375 #[doc = " Field offset"]
376 #[inline(always)]
377 pub const fn offset(&self) -> u8 {
378 self.o
379 }
380 #[doc = " Writes bit to the field"]
381 #[inline(always)]
382 pub fn bit(self, value: bool) -> &'a mut W<REG> {
383 self.w.bits &= !(REG::Ux::ONE << self.o);
384 self.w.bits |= (REG::Ux::from(value) & REG::Ux::ONE) << self.o;
385 self.w
386 }
387 #[doc = " Writes `variant` to the field"]
388 #[inline(always)]
389 pub fn variant(self, variant: FI) -> &'a mut W<REG> {
390 self.bit(bool::from(variant))
391 }
392 }
393 };
394}
395bit_proxy!(BitWriter, BitM);
396bit_proxy!(BitWriter1S, Bit1S);
397bit_proxy!(BitWriter0C, Bit0C);
398bit_proxy!(BitWriter1C, Bit1C);
399bit_proxy!(BitWriter0S, Bit0S);
400bit_proxy!(BitWriter1T, Bit1T);
401bit_proxy!(BitWriter0T, Bit0T);
402impl<'a, REG, FI> BitWriter<'a, REG, FI>
403where
404 REG: Writable + RegisterSpec,
405 bool: From<FI>,
406{
407 #[doc = " Sets the field bit"]
408 #[inline(always)]
409 pub fn set_bit(self) -> &'a mut W<REG> {
410 self.w.bits |= REG::Ux::ONE << self.o;
411 self.w
412 }
413 #[doc = " Clears the field bit"]
414 #[inline(always)]
415 pub fn clear_bit(self) -> &'a mut W<REG> {
416 self.w.bits &= !(REG::Ux::ONE << self.o);
417 self.w
418 }
419}
420impl<'a, REG, FI> BitWriter1S<'a, REG, FI>
421where
422 REG: Writable + RegisterSpec,
423 bool: From<FI>,
424{
425 #[doc = " Sets the field bit"]
426 #[inline(always)]
427 pub fn set_bit(self) -> &'a mut W<REG> {
428 self.w.bits |= REG::Ux::ONE << self.o;
429 self.w
430 }
431}
432impl<'a, REG, FI> BitWriter0C<'a, REG, FI>
433where
434 REG: Writable + RegisterSpec,
435 bool: From<FI>,
436{
437 #[doc = " Clears the field bit"]
438 #[inline(always)]
439 pub fn clear_bit(self) -> &'a mut W<REG> {
440 self.w.bits &= !(REG::Ux::ONE << self.o);
441 self.w
442 }
443}
444impl<'a, REG, FI> BitWriter1C<'a, REG, FI>
445where
446 REG: Writable + RegisterSpec,
447 bool: From<FI>,
448{
449 #[doc = "Clears the field bit by passing one"]
450 #[inline(always)]
451 pub fn clear_bit_by_one(self) -> &'a mut W<REG> {
452 self.w.bits |= REG::Ux::ONE << self.o;
453 self.w
454 }
455}
456impl<'a, REG, FI> BitWriter0S<'a, REG, FI>
457where
458 REG: Writable + RegisterSpec,
459 bool: From<FI>,
460{
461 #[doc = "Sets the field bit by passing zero"]
462 #[inline(always)]
463 pub fn set_bit_by_zero(self) -> &'a mut W<REG> {
464 self.w.bits &= !(REG::Ux::ONE << self.o);
465 self.w
466 }
467}
468impl<'a, REG, FI> BitWriter1T<'a, REG, FI>
469where
470 REG: Writable + RegisterSpec,
471 bool: From<FI>,
472{
473 #[doc = "Toggle the field bit by passing one"]
474 #[inline(always)]
475 pub fn toggle_bit(self) -> &'a mut W<REG> {
476 self.w.bits |= REG::Ux::ONE << self.o;
477 self.w
478 }
479}
480impl<'a, REG, FI> BitWriter0T<'a, REG, FI>
481where
482 REG: Writable + RegisterSpec,
483 bool: From<FI>,
484{
485 #[doc = "Toggle the field bit by passing zero"]
486 #[inline(always)]
487 pub fn toggle_bit(self) -> &'a mut W<REG> {
488 self.w.bits &= !(REG::Ux::ONE << self.o);
489 self.w
490 }
491}
492#[doc = " This structure provides volatile access to registers."]
493#[repr(transparent)]
494pub struct Reg<REG: RegisterSpec> {
495 register: vcell::VolatileCell<REG::Ux>,
496 _marker: marker::PhantomData<REG>,
497}
498unsafe impl<REG: RegisterSpec> Send for Reg<REG> where REG::Ux: Send {}
499impl<REG: RegisterSpec> Reg<REG> {
500 #[doc = " Returns the underlying memory address of register."]
501 #[doc = ""]
502 #[doc = " ```ignore"]
503 #[doc = " let reg_ptr = periph.reg.as_ptr();"]
504 #[doc = " ```"]
505 #[inline(always)]
506 pub fn as_ptr(&self) -> *mut REG::Ux {
507 self.register.as_ptr()
508 }
509}
510impl<REG: Readable> Reg<REG> {
511 #[doc = " Reads the contents of a `Readable` register."]
512 #[doc = ""]
513 #[doc = " You can read the raw contents of a register by using `bits`:"]
514 #[doc = " ```ignore"]
515 #[doc = " let bits = periph.reg.read().bits();"]
516 #[doc = " ```"]
517 #[doc = " or get the content of a particular field of a register:"]
518 #[doc = " ```ignore"]
519 #[doc = " let reader = periph.reg.read();"]
520 #[doc = " let bits = reader.field1().bits();"]
521 #[doc = " let flag = reader.field2().bit_is_set();"]
522 #[doc = " ```"]
523 #[inline(always)]
524 pub fn read(&self) -> R<REG> {
525 R {
526 bits: self.register.get(),
527 _reg: marker::PhantomData,
528 }
529 }
530}
531impl<REG: Resettable + Writable> Reg<REG> {
532 #[doc = " Writes the reset value to `Writable` register."]
533 #[doc = ""]
534 #[doc = " Resets the register to its initial state."]
535 #[inline(always)]
536 pub fn reset(&self) {
537 self.register.set(REG::RESET_VALUE)
538 }
539 #[doc = " Writes bits to a `Writable` register."]
540 #[doc = ""]
541 #[doc = " You can write raw bits into a register:"]
542 #[doc = " ```ignore"]
543 #[doc = " periph.reg.write(|w| unsafe { w.bits(rawbits) });"]
544 #[doc = " ```"]
545 #[doc = " or write only the fields you need:"]
546 #[doc = " ```ignore"]
547 #[doc = " periph.reg.write(|w| w"]
548 #[doc = " .field1().bits(newfield1bits)"]
549 #[doc = " .field2().set_bit()"]
550 #[doc = " .field3().variant(VARIANT)"]
551 #[doc = " );"]
552 #[doc = " ```"]
553 #[doc = " or an alternative way of saying the same:"]
554 #[doc = " ```ignore"]
555 #[doc = " periph.reg.write(|w| {"]
556 #[doc = " w.field1().bits(newfield1bits);"]
557 #[doc = " w.field2().set_bit();"]
558 #[doc = " w.field3().variant(VARIANT)"]
559 #[doc = " });"]
560 #[doc = " ```"]
561 #[doc = " In the latter case, other fields will be set to their reset value."]
562 #[inline(always)]
563 pub fn write<F>(&self, f: F) -> REG::Ux
564 where
565 F: FnOnce(&mut W<REG>) -> &mut W<REG>,
566 {
567 let value = f(&mut W {
568 bits: REG::RESET_VALUE & !REG::ONE_TO_MODIFY_FIELDS_BITMAP | REG::ZERO_TO_MODIFY_FIELDS_BITMAP,
569 _reg: marker::PhantomData,
570 })
571 .bits;
572 self.register.set(value);
573 value
574 }
575 #[doc = " Writes bits to a `Writable` register and produce a value."]
576 #[doc = ""]
577 #[doc = " You can write raw bits into a register:"]
578 #[doc = " ```ignore"]
579 #[doc = " periph.reg.write_and(|w| unsafe { w.bits(rawbits); });"]
580 #[doc = " ```"]
581 #[doc = " or write only the fields you need:"]
582 #[doc = " ```ignore"]
583 #[doc = " periph.reg.write_and(|w| {"]
584 #[doc = " w.field1().bits(newfield1bits)"]
585 #[doc = " .field2().set_bit()"]
586 #[doc = " .field3().variant(VARIANT);"]
587 #[doc = " });"]
588 #[doc = " ```"]
589 #[doc = " or an alternative way of saying the same:"]
590 #[doc = " ```ignore"]
591 #[doc = " periph.reg.write_and(|w| {"]
592 #[doc = " w.field1().bits(newfield1bits);"]
593 #[doc = " w.field2().set_bit();"]
594 #[doc = " w.field3().variant(VARIANT);"]
595 #[doc = " });"]
596 #[doc = " ```"]
597 #[doc = " In the latter case, other fields will be set to their reset value."]
598 #[doc = ""]
599 #[doc = " Values can be returned from the closure:"]
600 #[doc = " ```ignore"]
601 #[doc = " let state = periph.reg.write_and(|w| State::set(w.field1()));"]
602 #[doc = " ```"]
603 #[inline(always)]
604 pub fn from_write<F, T>(&self, f: F) -> T
605 where
606 F: FnOnce(&mut W<REG>) -> T,
607 {
608 let mut writer = W {
609 bits: REG::RESET_VALUE & !REG::ONE_TO_MODIFY_FIELDS_BITMAP | REG::ZERO_TO_MODIFY_FIELDS_BITMAP,
610 _reg: marker::PhantomData,
611 };
612 let result = f(&mut writer);
613 self.register.set(writer.bits);
614 result
615 }
616}
617impl<REG: Writable> Reg<REG> {
618 #[doc = " Writes 0 to a `Writable` register."]
619 #[doc = ""]
620 #[doc = " Similar to `write`, but unused bits will contain 0."]
621 #[doc = ""]
622 #[doc = " # Safety"]
623 #[doc = ""]
624 #[doc = " Unsafe to use with registers which don't allow to write 0."]
625 #[inline(always)]
626 pub unsafe fn write_with_zero<F>(&self, f: F) -> REG::Ux
627 where
628 F: FnOnce(&mut W<REG>) -> &mut W<REG>,
629 {
630 let value = f(&mut W {
631 bits: REG::Ux::ZERO,
632 _reg: marker::PhantomData,
633 })
634 .bits;
635 self.register.set(value);
636 value
637 }
638 #[doc = " Writes 0 to a `Writable` register and produces a value."]
639 #[doc = ""]
640 #[doc = " Similar to `write`, but unused bits will contain 0."]
641 #[doc = ""]
642 #[doc = " # Safety"]
643 #[doc = ""]
644 #[doc = " Unsafe to use with registers which don't allow to write 0."]
645 #[inline(always)]
646 pub unsafe fn from_write_with_zero<F, T>(&self, f: F) -> T
647 where
648 F: FnOnce(&mut W<REG>) -> T,
649 {
650 let mut writer = W {
651 bits: REG::Ux::ZERO,
652 _reg: marker::PhantomData,
653 };
654 let result = f(&mut writer);
655 self.register.set(writer.bits);
656 result
657 }
658}
659impl<REG: Readable + Writable> Reg<REG> {
660 #[doc = " Modifies the contents of the register by reading and then writing it."]
661 #[doc = ""]
662 #[doc = " E.g. to do a read-modify-write sequence to change parts of a register:"]
663 #[doc = " ```ignore"]
664 #[doc = " periph.reg.modify(|r, w| unsafe { w.bits("]
665 #[doc = " r.bits() | 3"]
666 #[doc = " ) });"]
667 #[doc = " ```"]
668 #[doc = " or"]
669 #[doc = " ```ignore"]
670 #[doc = " periph.reg.modify(|_, w| w"]
671 #[doc = " .field1().bits(newfield1bits)"]
672 #[doc = " .field2().set_bit()"]
673 #[doc = " .field3().variant(VARIANT)"]
674 #[doc = " );"]
675 #[doc = " ```"]
676 #[doc = " or an alternative way of saying the same:"]
677 #[doc = " ```ignore"]
678 #[doc = " periph.reg.modify(|_, w| {"]
679 #[doc = " w.field1().bits(newfield1bits);"]
680 #[doc = " w.field2().set_bit();"]
681 #[doc = " w.field3().variant(VARIANT)"]
682 #[doc = " });"]
683 #[doc = " ```"]
684 #[doc = " Other fields will have the value they had before the call to `modify`."]
685 #[inline(always)]
686 pub fn modify<F>(&self, f: F) -> REG::Ux
687 where
688 for<'w> F: FnOnce(&R<REG>, &'w mut W<REG>) -> &'w mut W<REG>,
689 {
690 let bits = self.register.get();
691 let value = f(
692 &R {
693 bits,
694 _reg: marker::PhantomData,
695 },
696 &mut W {
697 bits: bits & !REG::ONE_TO_MODIFY_FIELDS_BITMAP | REG::ZERO_TO_MODIFY_FIELDS_BITMAP,
698 _reg: marker::PhantomData,
699 },
700 )
701 .bits;
702 self.register.set(value);
703 value
704 }
705 #[doc = " Modifies the contents of the register by reading and then writing it"]
706 #[doc = " and produces a value."]
707 #[doc = ""]
708 #[doc = " E.g. to do a read-modify-write sequence to change parts of a register:"]
709 #[doc = " ```ignore"]
710 #[doc = " let bits = periph.reg.modify(|r, w| {"]
711 #[doc = " let new_bits = r.bits() | 3;"]
712 #[doc = " unsafe {"]
713 #[doc = " w.bits(new_bits);"]
714 #[doc = " }"]
715 #[doc = ""]
716 #[doc = " new_bits"]
717 #[doc = " });"]
718 #[doc = " ```"]
719 #[doc = " or"]
720 #[doc = " ```ignore"]
721 #[doc = " periph.reg.modify(|_, w| {"]
722 #[doc = " w.field1().bits(newfield1bits)"]
723 #[doc = " .field2().set_bit()"]
724 #[doc = " .field3().variant(VARIANT);"]
725 #[doc = " });"]
726 #[doc = " ```"]
727 #[doc = " or an alternative way of saying the same:"]
728 #[doc = " ```ignore"]
729 #[doc = " periph.reg.modify(|_, w| {"]
730 #[doc = " w.field1().bits(newfield1bits);"]
731 #[doc = " w.field2().set_bit();"]
732 #[doc = " w.field3().variant(VARIANT);"]
733 #[doc = " });"]
734 #[doc = " ```"]
735 #[doc = " Other fields will have the value they had before the call to `modify`."]
736 #[inline(always)]
737 pub fn from_modify<F, T>(&self, f: F) -> T
738 where
739 for<'w> F: FnOnce(&R<REG>, &'w mut W<REG>) -> T,
740 {
741 let bits = self.register.get();
742 let mut writer = W {
743 bits: bits & !REG::ONE_TO_MODIFY_FIELDS_BITMAP | REG::ZERO_TO_MODIFY_FIELDS_BITMAP,
744 _reg: marker::PhantomData,
745 };
746 let result = f(
747 &R {
748 bits,
749 _reg: marker::PhantomData,
750 },
751 &mut writer,
752 );
753 self.register.set(writer.bits);
754 result
755 }
756}
757impl<REG: Readable> core::fmt::Debug for crate::generic::Reg<REG>
758where
759 R<REG>: core::fmt::Debug,
760{
761 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
762 core::fmt::Debug::fmt(&self.read(), f)
763 }
764}