@@ -141,7 +141,11 @@ impl Cr2 {
141
141
142
142
pub fn read (& self ) -> Cr2R { .. }
143
143
144
- pub fn write (& mut self , value : Cr2W ) { .. }
144
+ pub fn write <F >(& mut self , f : F )
145
+ where F : FnOnce (& mut Cr2W ) -> & mut Cr2W ,
146
+ {
147
+ ..
148
+ }
145
149
}
146
150
```
147
151
@@ -172,10 +176,12 @@ if i2c1.c2r.read().sadd0() {
172
176
```
173
177
174
178
The ` write ` method performs a single, volatile ` STR ` instruction to write a value to the ` CR2 `
175
- register. This method takes a ` Cr2W ` struct that only allows constructing valid states of the ` CR2 `
176
- register. The only constructor that ` Cr2W ` provides is ` reset_value ` which returns the value of the
177
- ` CR2 ` register after a reset. The rest of ` Cr2W ` methods are "builder" like and can be used to set
178
- or reset the writable bits of the ` CR2 ` register.
179
+ register. This method involves the ` Cr2W ` struct which only allows constructing valid states of the
180
+ ` CR2 ` register.
181
+
182
+ The only constructor that ` Cr2W ` provides is ` reset_value ` which returns the value of the ` CR2 `
183
+ register after a reset. The rest of ` Cr2W ` methods are "builder" like and can be used to set or
184
+ reset the writable bits of the ` CR2 ` register.
179
185
180
186
``` rust
181
187
impl Cr2W {
@@ -192,10 +198,15 @@ impl Cr2W {
192
198
}
193
199
```
194
200
201
+ The ` write ` method takes a closure with signature ` &mut Cr2W -> &mut Cr2W ` . If passed the identity
202
+ closure, ` |w| w ` , the ` write ` method will set the ` CR2 ` register to its reset value. Otherwise, the
203
+ closure specifies how that reset value will be modified before it's written to ` CR2 ` .
204
+
195
205
Usage looks like this:
196
206
197
207
``` rust
198
- i2c1 . cr2. write (* Cr2W :: reset_value (). sadd0 (true ). sadd1 (0b0011110 ));
208
+ // Write to CR2, its reset value but with its SADD0 and SADD1 fields set to `true` and `0b0011110`
209
+ i2c1 . cr2. write (| w | w . sadd0 (true ). sadd1 (0b0011110 ));
199
210
```
200
211
201
212
Finally, the ` modify ` method performs a read-modify-write operation that involves at least a ` LDR `
0 commit comments