42
42
{
43
43
///Reads the contents of `Readable` register
44
44
///
45
- ///See [reading](https://rust-embedded.github.io/book/start/registers.html#reading) in book.
45
+ ///You can read the contents of a register in such way:
46
+ ///```ignore
47
+ ///let bits = periph.reg.read().bits();
48
+ ///```
49
+ ///or get the content of a particular field of a register.
50
+ ///```ignore
51
+ ///let reader = periph.reg.read();
52
+ ///let bits = reader.field1().bits();
53
+ ///let flag = reader.field2().bit_is_set();
54
+ ///```
46
55
#[ inline( always) ]
47
56
pub fn read ( & self ) -> R < U , Self > {
48
57
R { bits : self . register . get ( ) , _reg : marker:: PhantomData }
55
64
U : Copy ,
56
65
{
57
66
///Writes the reset value to `Writable` register
67
+ ///
68
+ ///Resets the register to its initial state
58
69
#[ inline( always) ]
59
70
pub fn reset ( & self ) {
60
71
self . register . set ( Self :: reset_value ( ) )
68
79
{
69
80
///Writes bits to `Writable` register
70
81
///
71
- ///See [writing](https://rust-embedded.github.io/book/start/registers.html#writing) in book.
82
+ ///You can write raw bits into a register:
83
+ ///```ignore
84
+ ///periph.reg.write(|w| unsafe { w.bits(rawbits) });
85
+ ///```
86
+ ///or write only the fields you need:
87
+ ///```ignore
88
+ ///periph.reg.write(|w| w
89
+ /// .field1().bits(newfield1bits)
90
+ /// .field2().set_bit()
91
+ /// .field3().variant(VARIANT)
92
+ ///);
93
+ ///```
94
+ ///Other fields will have reset value.
72
95
#[ inline( always) ]
73
96
pub fn write < F > ( & self , f : F )
74
97
where
84
107
U : Copy + Default
85
108
{
86
109
///Writes Zero to `Writable` register
110
+ ///
111
+ ///Similar to `write`, but unused bits will contain 0.
87
112
#[ inline( always) ]
88
113
pub fn write_with_zero < F > ( & self , f : F )
89
114
where
@@ -100,7 +125,21 @@ where
100
125
{
101
126
///Modifies the contents of the register
102
127
///
103
- ///See [modifying](https://rust-embedded.github.io/book/start/registers.html#modifying) in book.
128
+ ///E.g. to do a read-modify-write sequence to change parts of a register:
129
+ ///```ignore
130
+ ///periph.reg.modify(|r, w| unsafe { w.bits(
131
+ /// r.bits() | 3
132
+ ///) });
133
+ ///```
134
+ ///or
135
+ ///```ignore
136
+ ///periph.reg.modify(|_, w| w
137
+ /// .field1().bits(newfield1bits)
138
+ /// .field2().set_bit()
139
+ /// .field3().variant(VARIANT)
140
+ ///);
141
+ ///```
142
+ ///Other fields will have value they had before call `modify`.
104
143
#[ inline( always) ]
105
144
pub fn modify < F > ( & self , f : F )
106
145
where
@@ -112,6 +151,9 @@ where
112
151
}
113
152
114
153
///Register/field reader
154
+ ///
155
+ ///Result of the [`read`](Reg::read) method of a register.
156
+ ///Also it can be used in the [`modify`](Reg::read) method
115
157
pub struct R < U , T > {
116
158
pub ( crate ) bits : U ,
117
159
_reg : marker:: PhantomData < T > ,
@@ -141,6 +183,7 @@ where
141
183
U : PartialEq ,
142
184
FI : ToBits < U >
143
185
{
186
+ #[ inline( always) ]
144
187
fn eq ( & self , other : & FI ) -> bool {
145
188
self . bits . eq ( & other. _bits ( ) )
146
189
}
@@ -165,16 +208,18 @@ impl<FI> R<bool, FI> {
165
208
}
166
209
167
210
///Register writer
211
+ ///
212
+ ///Used as an argument to the closures in the [`write`](Reg::write) and [`modify`](Reg::modify) methods of the register
168
213
pub struct W < U , REG > {
169
214
///Writable bits
170
- pub bits : U ,
215
+ pub ( crate ) bits : U ,
171
216
_reg : marker:: PhantomData < REG > ,
172
217
}
173
218
174
219
impl < U , REG > W < U , REG > {
175
220
///Writes raw bits to the register
176
221
#[ inline( always) ]
177
- pub fn bits ( & mut self , bits : U ) -> & mut Self {
222
+ pub unsafe fn bits ( & mut self , bits : U ) -> & mut Self {
178
223
self . bits = bits;
179
224
self
180
225
}
0 commit comments