1 | // Note: This file is copied and modified from fdcan crate by Richard Meadows |
2 | |
3 | use core::marker; |
4 | |
5 | ///This trait shows that register has `read` method |
6 | /// |
7 | ///Registers marked with `Writable` can be also `modify`'ed |
8 | pub trait Readable {} |
9 | |
10 | ///This trait shows that register has `write`, `write_with_zero` and `reset` method |
11 | /// |
12 | ///Registers marked with `Readable` can be also `modify`'ed |
13 | pub trait Writable {} |
14 | |
15 | ///Reset value of the register |
16 | /// |
17 | ///This value is initial value for `write` method. |
18 | ///It can be also directly writed to register by `reset` method. |
19 | pub trait ResetValue { |
20 | ///Register size |
21 | type Type; |
22 | ///Reset value of the register |
23 | fn reset_value() -> Self::Type; |
24 | } |
25 | |
26 | ///This structure provides volatile access to register |
27 | pub struct Reg<U, REG> { |
28 | register: vcell::VolatileCell<U>, |
29 | _marker: marker::PhantomData<REG>, |
30 | } |
31 | |
32 | unsafe impl<U: Send, REG> Send for Reg<U, REG> {} |
33 | |
34 | impl<U, REG> Reg<U, REG> |
35 | where |
36 | Self: Readable, |
37 | U: Copy, |
38 | { |
39 | ///Reads the contents of `Readable` register |
40 | /// |
41 | ///You can read the contents of a register in such way: |
42 | ///```ignore |
43 | ///let bits = periph.reg.read().bits(); |
44 | ///``` |
45 | ///or get the content of a particular field of a register. |
46 | ///```ignore |
47 | ///let reader = periph.reg.read(); |
48 | ///let bits = reader.field1().bits(); |
49 | ///let flag = reader.field2().bit_is_set(); |
50 | ///``` |
51 | #[inline (always)] |
52 | pub fn read(&self) -> R<U, Self> { |
53 | R { |
54 | bits: self.register.get(), |
55 | _reg: marker::PhantomData, |
56 | } |
57 | } |
58 | } |
59 | |
60 | impl<U, REG> Reg<U, REG> |
61 | where |
62 | Self: ResetValue<Type = U> + Writable, |
63 | U: Copy, |
64 | { |
65 | ///Writes the reset value to `Writable` register |
66 | /// |
67 | ///Resets the register to its initial state |
68 | #[inline (always)] |
69 | pub fn reset(&self) { |
70 | self.register.set(Self::reset_value()) |
71 | } |
72 | } |
73 | |
74 | impl<U, REG> Reg<U, REG> |
75 | where |
76 | Self: ResetValue<Type = U> + Writable, |
77 | U: Copy, |
78 | { |
79 | ///Writes bits to `Writable` register |
80 | /// |
81 | ///You can write raw bits into a register: |
82 | ///```ignore |
83 | ///periph.reg.write(|w| unsafe { w.bits(rawbits) }); |
84 | ///``` |
85 | ///or write only the fields you need: |
86 | ///```ignore |
87 | ///periph.reg.write(|w| w |
88 | /// .field1().bits(newfield1bits) |
89 | /// .field2().set_bit() |
90 | /// .field3().variant(VARIANT) |
91 | ///); |
92 | ///``` |
93 | ///Other fields will have reset value. |
94 | #[inline (always)] |
95 | pub fn write<F>(&self, f: F) |
96 | where |
97 | F: FnOnce(&mut W<U, Self>) -> &mut W<U, Self>, |
98 | { |
99 | self.register.set( |
100 | f(&mut W { |
101 | bits: Self::reset_value(), |
102 | _reg: marker::PhantomData, |
103 | }) |
104 | .bits, |
105 | ); |
106 | } |
107 | } |
108 | |
109 | ///Register/field reader |
110 | /// |
111 | ///Result of the [`read`](Reg::read) method of a register. |
112 | ///Also it can be used in the [`modify`](Reg::read) method |
113 | pub struct R<U, T> { |
114 | pub(crate) bits: U, |
115 | _reg: marker::PhantomData<T>, |
116 | } |
117 | |
118 | impl<U, T> R<U, T> |
119 | where |
120 | U: Copy, |
121 | { |
122 | ///Create new instance of reader |
123 | #[inline (always)] |
124 | pub(crate) fn new(bits: U) -> Self { |
125 | Self { |
126 | bits, |
127 | _reg: marker::PhantomData, |
128 | } |
129 | } |
130 | ///Read raw bits from register/field |
131 | #[inline (always)] |
132 | pub fn bits(&self) -> U { |
133 | self.bits |
134 | } |
135 | } |
136 | |
137 | impl<U, T, FI> PartialEq<FI> for R<U, T> |
138 | where |
139 | U: PartialEq, |
140 | FI: Copy + Into<U>, |
141 | { |
142 | #[inline (always)] |
143 | fn eq(&self, other: &FI) -> bool { |
144 | self.bits.eq(&(*other).into()) |
145 | } |
146 | } |
147 | |
148 | impl<FI> R<bool, FI> { |
149 | ///Value of the field as raw bits |
150 | #[inline (always)] |
151 | pub fn bit(&self) -> bool { |
152 | self.bits |
153 | } |
154 | ///Returns `true` if the bit is clear (0) |
155 | #[inline (always)] |
156 | pub fn bit_is_clear(&self) -> bool { |
157 | !self.bit() |
158 | } |
159 | } |
160 | |
161 | ///Register writer |
162 | /// |
163 | ///Used as an argument to the closures in the [`write`](Reg::write) and [`modify`](Reg::modify) methods of the register |
164 | pub struct W<U, REG> { |
165 | ///Writable bits |
166 | pub(crate) bits: U, |
167 | _reg: marker::PhantomData<REG>, |
168 | } |
169 | |