Skip to content

Commit 8232126

Browse files
authored
Merge pull request #114 from ra-kete/inputpin-for-outputpin
Implement InputPin for Output<OpenDrain> pins
2 parents 753d61c + 0e656a7 commit 8232126

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

CHANGELOG.md

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

88
## [Unreleased]
99

10+
### Added
11+
12+
- Implement `InputPin` for `Output<OpenDrain>` pins ([#114](https://github.com/stm32-rs/stm32f3xx-hal/pull/114))
13+
1014
### Fixed
1115

1216
- `PLL` was calculated wrong for devices, which do not divide `HSI` ([#67](https://github.com/stm32-rs/stm32f3xx-hal/pull/67))
@@ -28,7 +32,7 @@ let clocks = rcc
2832
.use_hse(32.mhz())
2933
.sysclk(72.mhz())
3034
```
31-
This is possible through utilizing the divider, which can devide the
35+
This is possible through utilizing the divider, which can divide the
3236
external oscillator clock on most devices. Some devices have even the
3337
possibility to divide the internal oscillator clock.
3438

src/gpio.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,31 @@ macro_rules! gpio {
206206
}
207207
}
208208

209+
#[cfg(feature = "unproven")]
210+
impl InputPin for PXx<Output<OpenDrain>> {
211+
type Error = Infallible;
212+
213+
fn is_high(&self) -> Result<bool, Self::Error> {
214+
Ok(!self.is_low()?)
215+
}
216+
217+
fn is_low(&self) -> Result<bool, Self::Error> {
218+
// NOTE(unsafe) atomic read with no side effects
219+
Ok(unsafe {
220+
match &self.gpio {
221+
$(
222+
#[cfg(all(any(
223+
$(feature = $device,)+
224+
), not(any(
225+
$(feature = $device_except,)*
226+
))))]
227+
Gpio::$GPIOX => (*$GPIOX::ptr()).idr.read().bits() & (1 << self.i) == 0,
228+
)+
229+
}
230+
})
231+
}
232+
}
233+
209234
#[cfg(feature = "unproven")]
210235
impl <MODE> StatefulOutputPin for PXx<Output<MODE>> {
211236
fn is_set_high(&self) -> Result<bool, Self::Error> {
@@ -412,6 +437,20 @@ macro_rules! gpio {
412437
}
413438
}
414439

440+
#[cfg(feature = "unproven")]
441+
impl InputPin for $PXx<Output<OpenDrain>> {
442+
type Error = Infallible;
443+
444+
fn is_high(&self) -> Result<bool, Self::Error> {
445+
Ok(!self.is_low()?)
446+
}
447+
448+
fn is_low(&self) -> Result<bool, Self::Error> {
449+
// NOTE(unsafe) atomic read with no side effects
450+
Ok(unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << self.i) == 0 })
451+
}
452+
}
453+
415454
#[cfg(feature = "unproven")]
416455
impl<MODE> StatefulOutputPin for $PXx<Output<MODE>> {
417456
fn is_set_high(&self) -> Result<bool, Self::Error> {
@@ -598,6 +637,20 @@ macro_rules! gpio {
598637
}
599638
}
600639

640+
#[cfg(feature = "unproven")]
641+
impl InputPin for $PXi<Output<OpenDrain>> {
642+
type Error = Infallible;
643+
644+
fn is_high(&self) -> Result<bool, Self::Error> {
645+
Ok(!self.is_low()?)
646+
}
647+
648+
fn is_low(&self) -> Result<bool, Self::Error> {
649+
// NOTE(unsafe) atomic read with no side effects
650+
Ok(unsafe { (*$GPIOX::ptr()).idr.read().$idri().is_low()})
651+
}
652+
}
653+
601654
#[cfg(feature = "unproven")]
602655
impl<MODE> StatefulOutputPin for $PXi<Output<MODE>> {
603656
fn is_set_high(&self) -> Result<bool, Self::Error> {

0 commit comments

Comments
 (0)