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