Skip to content

Commit 7e50031

Browse files
authored
Merge pull request #489 from stm32-rs/proper-resets
fix with_mode
2 parents 84ef991 + 747f286 commit 7e50031

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- `Pin::with_mode` [#489]
13+
1014
### Changed
1115

1216
- `Spi` can be operated as `Slave` [#487]
1317

1418
[#487]: https://github.com/stm32-rs/stm32f4xx-hal/pull/487
19+
[#489]: https://github.com/stm32-rs/stm32f4xx-hal/pull/489
1520

1621
## [v0.13.1] - 2022-04-20
1722

src/gpio/convert.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -255,15 +255,13 @@ where
255255
M: PinMode,
256256
F: FnOnce(&mut Pin<P, N, M>) -> R,
257257
{
258-
self.mode::<M>();
258+
self.mode::<M>(); // change physical mode, without changing typestate
259259

260260
// This will reset the pin back to the original mode when dropped.
261261
// (so either when `with_mode` returns or when `f` unwinds)
262-
let _resetti = ResetMode { pin: self };
263-
264-
let mut witness = Pin::new();
262+
let mut resetti = ResetMode::<P, N, M, MODE>::new();
265263

266-
f(&mut witness)
264+
f(&mut resetti.pin)
267265
}
268266

269267
/// Temporarily configures this pin as a input.
@@ -341,11 +339,22 @@ where
341339
}
342340
}
343341

344-
struct ResetMode<'a, const P: char, const N: u8, ORIG: PinMode> {
345-
pin: &'a mut Pin<P, N, ORIG>,
342+
/// Wrapper around a pin that transitions the pin to mode ORIG when dropped
343+
struct ResetMode<const P: char, const N: u8, CURRENT: PinMode, ORIG: PinMode> {
344+
pub pin: Pin<P, N, CURRENT>,
345+
_mode: PhantomData<ORIG>,
346346
}
347-
348-
impl<'a, const P: char, const N: u8, ORIG: PinMode> Drop for ResetMode<'a, P, N, ORIG> {
347+
impl<const P: char, const N: u8, CURRENT: PinMode, ORIG: PinMode> ResetMode<P, N, CURRENT, ORIG> {
348+
fn new() -> Self {
349+
Self {
350+
pin: Pin::new(),
351+
_mode: PhantomData,
352+
}
353+
}
354+
}
355+
impl<const P: char, const N: u8, CURRENT: PinMode, ORIG: PinMode> Drop
356+
for ResetMode<P, N, CURRENT, ORIG>
357+
{
349358
fn drop(&mut self) {
350359
self.pin.mode::<ORIG>();
351360
}

0 commit comments

Comments
 (0)