Skip to content

Commit 99e3073

Browse files
authored
Merge pull request #873 from rust-embedded/write-return-raw
return raw value from write/modify
2 parents 92ff0c7 + 394a389 commit 99e3073

File tree

3 files changed

+35
-34
lines changed

3 files changed

+35
-34
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
1818
- Skip generating `.add(0)` and `1 *` in accessors
1919
- Bump MSRV of generated code to 1.76
2020
- move `must_use` from methods to generic type
21-
- Add `write_and`, `write_with_zero_and`, and `modify_and` register modifiers
21+
- *breaking change* Return raw writtened value
22+
- Add `from_write`, `from_write_with_zero`, and `from_modify` register modifiers
23+
with generic return value
2224

2325
## [v0.33.5] - 2024-10-12
2426

ci/script.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ main() {
589589
test_patched_stm32 stm32f7x3
590590
test_patched_stm32 stm32g070
591591
test_patched_stm32 stm32g473
592-
test_patched_stm32 stm32h753
592+
test_patched_stm32 stm32h743
593593
test_patched_stm32 stm32l0x3
594594
test_patched_stm32 stm32l162
595595
test_patched_stm32 stm32l4x6

src/generate/generic_reg_vcell.rs

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,18 @@ impl<REG: Resettable + Writable> Reg<REG> {
7474
/// ```
7575
/// In the latter case, other fields will be set to their reset value.
7676
#[inline(always)]
77-
pub fn write<F>(&self, f: F)
77+
pub fn write<F>(&self, f: F) -> REG::Ux
7878
where
7979
F: FnOnce(&mut W<REG>) -> &mut W<REG>,
8080
{
81-
self.register.set(
82-
f(&mut W {
83-
bits: REG::RESET_VALUE & !REG::ONE_TO_MODIFY_FIELDS_BITMAP
84-
| REG::ZERO_TO_MODIFY_FIELDS_BITMAP,
85-
_reg: marker::PhantomData,
86-
})
87-
.bits,
88-
);
81+
let value = f(&mut W {
82+
bits: REG::RESET_VALUE & !REG::ONE_TO_MODIFY_FIELDS_BITMAP
83+
| REG::ZERO_TO_MODIFY_FIELDS_BITMAP,
84+
_reg: marker::PhantomData,
85+
})
86+
.bits;
87+
self.register.set(value);
88+
value
8989
}
9090

9191
/// Writes bits to a `Writable` register and produce a value.
@@ -143,17 +143,17 @@ impl<REG: Writable> Reg<REG> {
143143
///
144144
/// Unsafe to use with registers which don't allow to write 0.
145145
#[inline(always)]
146-
pub unsafe fn write_with_zero<F>(&self, f: F)
146+
pub unsafe fn write_with_zero<F>(&self, f: F) -> REG::Ux
147147
where
148148
F: FnOnce(&mut W<REG>) -> &mut W<REG>,
149149
{
150-
self.register.set(
151-
f(&mut W {
152-
bits: REG::Ux::default(),
153-
_reg: marker::PhantomData,
154-
})
155-
.bits,
156-
);
150+
let value = f(&mut W {
151+
bits: REG::Ux::default(),
152+
_reg: marker::PhantomData,
153+
})
154+
.bits;
155+
self.register.set(value);
156+
value
157157
}
158158

159159
/// Writes 0 to a `Writable` register and produces a value.
@@ -208,25 +208,24 @@ impl<REG: Readable + Writable> Reg<REG> {
208208
/// ```
209209
/// Other fields will have the value they had before the call to `modify`.
210210
#[inline(always)]
211-
pub fn modify<F>(&self, f: F)
211+
pub fn modify<F>(&self, f: F) -> REG::Ux
212212
where
213213
for<'w> F: FnOnce(&R<REG>, &'w mut W<REG>) -> &'w mut W<REG>,
214214
{
215215
let bits = self.register.get();
216-
self.register.set(
217-
f(
218-
&R {
219-
bits,
220-
_reg: marker::PhantomData,
221-
},
222-
&mut W {
223-
bits: bits & !REG::ONE_TO_MODIFY_FIELDS_BITMAP
224-
| REG::ZERO_TO_MODIFY_FIELDS_BITMAP,
225-
_reg: marker::PhantomData,
226-
},
227-
)
228-
.bits,
229-
);
216+
let value = f(
217+
&R {
218+
bits,
219+
_reg: marker::PhantomData,
220+
},
221+
&mut W {
222+
bits: bits & !REG::ONE_TO_MODIFY_FIELDS_BITMAP | REG::ZERO_TO_MODIFY_FIELDS_BITMAP,
223+
_reg: marker::PhantomData,
224+
},
225+
)
226+
.bits;
227+
self.register.set(value);
228+
value
230229
}
231230

232231
/// Modifies the contents of the register by reading and then writing it

0 commit comments

Comments
 (0)