Skip to content

Commit 028a95b

Browse files
author
Jorge Aparicio
committed
.write(): change signature to take a closure that modifies the reset
value closes #2
1 parent 65da1fd commit 028a95b

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

README.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,11 @@ impl Cr2 {
141141

142142
pub fn read(&self) -> Cr2R { .. }
143143

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+
}
145149
}
146150
```
147151

@@ -172,10 +176,12 @@ if i2c1.c2r.read().sadd0() {
172176
```
173177

174178
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.
179185

180186
``` rust
181187
impl Cr2W {
@@ -192,10 +198,15 @@ impl Cr2W {
192198
}
193199
```
194200

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+
195205
Usage looks like this:
196206

197207
``` 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));
199210
```
200211

201212
Finally, the `modify` method performs a read-modify-write operation that involves at least a `LDR`

src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,12 @@ pub fn gen_register(cx: &ExtCtxt, r: &Register, d: &Defaults) -> Vec<P<Item>> {
167167
$name_r { bits: self.register.read() }
168168
}
169169

170-
pub fn write(&mut self, value: $name_w) {
171-
self.register.write(value.bits)
170+
pub fn write<F>(&mut self, f: F)
171+
where F: FnOnce(&mut $name_w) -> &mut $name_w,
172+
{
173+
let mut w = $name_w::reset_value();
174+
f(&mut w);
175+
self.register.write(w.bits);
172176
}
173177
})
174178
.unwrap());

0 commit comments

Comments
 (0)