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