Skip to content

Commit 7e345c6

Browse files
committed
Changed implementation to implement digital v2 interface
Signed-off-by: Daniel Egger <[email protected]>
1 parent 2116717 commit 7e345c6

File tree

3 files changed

+55
-34
lines changed

3 files changed

+55
-34
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Changed
1111

12+
- Changed digital pin functionality to implement v2 versions
1213
- Fixed a few deprecation warning and lints
1314
- Enabled commented out and now available GPIOE support for 07x and 09x families
1415
- Extract register block address only once

src/gpio.rs

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! General Purpose Input / Output
22
33
use core::marker::PhantomData;
4+
use core::convert::Infallible;
45

56
use crate::rcc::Rcc;
67

@@ -61,7 +62,7 @@ pub struct Output<MODE> {
6162
/// Push pull output (type state)
6263
pub struct PushPull;
6364

64-
use embedded_hal::digital::{toggleable, InputPin, OutputPin, StatefulOutputPin};
65+
use embedded_hal::digital::v2::{toggleable, InputPin, OutputPin, StatefulOutputPin};
6566

6667
/// Fully erased pin
6768
pub struct Pin<MODE> {
@@ -78,51 +79,57 @@ unsafe impl<MODE> Send for Pin<MODE> {}
7879

7980
impl<MODE> StatefulOutputPin for Pin<Output<MODE>> {
8081
#[inline(always)]
81-
fn is_set_high(&self) -> bool {
82-
!self.is_set_low()
82+
fn is_set_high(&self) -> Result<bool, Self::Error> {
83+
self.is_set_low().map(|v| !v)
8384
}
8485

8586
#[inline(always)]
86-
fn is_set_low(&self) -> bool {
87-
unsafe { (*self.port).is_set_low(self.i) }
87+
fn is_set_low(&self) -> Result<bool, Self::Error> {
88+
Ok(unsafe { (*self.port).is_set_low(self.i) })
8889
}
8990
}
9091

9192
impl<MODE> OutputPin for Pin<Output<MODE>> {
93+
type Error = Infallible;
94+
9295
#[inline(always)]
93-
fn set_high(&mut self) {
94-
unsafe { (*self.port).set_high(self.i) }
96+
fn set_high(&mut self) -> Result<(), Self::Error> {
97+
Ok(unsafe { (*self.port).set_high(self.i) })
9598
}
9699

97100
#[inline(always)]
98-
fn set_low(&mut self) {
99-
unsafe { (*self.port).set_low(self.i) }
101+
fn set_low(&mut self) -> Result<(), Self::Error>{
102+
Ok(unsafe { (*self.port).set_low(self.i) })
100103
}
101104
}
102105

103106
impl<MODE> toggleable::Default for Pin<Output<MODE>> {}
104107

105108
impl InputPin for Pin<Output<OpenDrain>> {
109+
type Error = Infallible;
110+
106111
#[inline(always)]
107-
fn is_high(&self) -> bool {
108-
!self.is_low()
112+
fn is_high(&self) -> Result<bool, Self::Error> {
113+
self.is_low().map(|v| !v)
109114
}
110115

111116
#[inline(always)]
112-
fn is_low(&self) -> bool {
113-
unsafe { (*self.port).is_low(self.i) }
117+
fn is_low(&self) -> Result<bool, Self::Error> {
118+
Ok(unsafe { (*self.port).is_low(self.i) })
114119
}
115120
}
116121

117122
impl<MODE> InputPin for Pin<Input<MODE>> {
123+
type Error = Infallible;
124+
118125
#[inline(always)]
119-
fn is_high(&self) -> bool {
120-
!self.is_low()
126+
fn is_high(&self) -> Result<bool, Self::Error> {
127+
self.is_low().map(|v| !v)
121128
}
122129

123130
#[inline(always)]
124-
fn is_low(&self) -> bool {
125-
unsafe { (*self.port).is_low(self.i) }
131+
fn is_low(&self) -> Result<bool, Self::Error> {
132+
Ok(unsafe { (*self.port).is_low(self.i) })
126133
}
127134
}
128135

@@ -162,8 +169,9 @@ macro_rules! gpio {
162169
/// GPIO
163170
pub mod $gpiox {
164171
use core::marker::PhantomData;
172+
use core::convert::Infallible;
165173

166-
use embedded_hal::digital::{InputPin, OutputPin, StatefulOutputPin, toggleable};
174+
use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin, toggleable};
167175
use crate::{
168176
rcc::Rcc,
169177
stm32::$GPIOX
@@ -483,34 +491,38 @@ macro_rules! gpio {
483491
}
484492

485493
impl<MODE> StatefulOutputPin for $PXi<Output<MODE>> {
486-
fn is_set_high(&self) -> bool {
487-
!self.is_set_low()
494+
fn is_set_high(&self) -> Result<bool, Self::Error> {
495+
self.is_set_low().map(|v| !v)
488496
}
489497

490-
fn is_set_low(&self) -> bool {
491-
unsafe { (*$GPIOX::ptr()).is_set_low($i) }
498+
fn is_set_low(&self) -> Result<bool, Self::Error> {
499+
Ok(unsafe { (*$GPIOX::ptr()).is_set_low($i) })
492500
}
493501
}
494502

495503
impl<MODE> OutputPin for $PXi<Output<MODE>> {
496-
fn set_high(&mut self) {
497-
unsafe { (*$GPIOX::ptr()).set_high($i) }
504+
type Error = Infallible;
505+
506+
fn set_high(&mut self) -> Result<(), Self::Error> {
507+
Ok(unsafe { (*$GPIOX::ptr()).set_high($i) })
498508
}
499509

500-
fn set_low(&mut self) {
501-
unsafe { (*$GPIOX::ptr()).set_low($i) }
510+
fn set_low(&mut self) -> Result<(), Self::Error> {
511+
Ok(unsafe { (*$GPIOX::ptr()).set_low($i) })
502512
}
503513
}
504514

505515
impl<MODE> toggleable::Default for $PXi<Output<MODE>> {}
506516

507517
impl InputPin for $PXi<Output<OpenDrain>> {
508-
fn is_high(&self) -> bool {
509-
!self.is_low()
518+
type Error = Infallible;
519+
520+
fn is_high(&self) -> Result<bool, Self::Error> {
521+
self.is_low().map(|v| !v)
510522
}
511523

512-
fn is_low(&self) -> bool {
513-
unsafe { (*$GPIOX::ptr()).is_low($i) }
524+
fn is_low(&self) -> Result<bool, Self::Error> {
525+
Ok(unsafe { (*$GPIOX::ptr()).is_low($i) })
514526
}
515527
}
516528

@@ -529,12 +541,14 @@ macro_rules! gpio {
529541
}
530542

531543
impl<MODE> InputPin for $PXi<Input<MODE>> {
532-
fn is_high(&self) -> bool {
533-
!self.is_low()
544+
type Error = Infallible;
545+
546+
fn is_high(&self) -> Result<bool, Self::Error> {
547+
self.is_low().map(|v| !v)
534548
}
535549

536-
fn is_low(&self) -> bool {
537-
unsafe { (*$GPIOX::ptr()).is_low($i) }
550+
fn is_low(&self) -> Result<bool, Self::Error> {
551+
Ok(unsafe { (*$GPIOX::ptr()).is_low($i) })
538552
}
539553
}
540554
)+

src/prelude.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
pub use embedded_hal::prelude::*;
2+
23
// TODO for some reason, watchdog isn't in the embedded_hal prelude
34
pub use embedded_hal::watchdog::Watchdog as _stm32f0xx_hal_embedded_hal_watchdog_Watchdog;
45
pub use embedded_hal::watchdog::WatchdogEnable as _stm32f0xx_hal_embedded_hal_watchdog_WatchdogEnable;
56

67
pub use embedded_hal::adc::OneShot as _embedded_hal_adc_OneShot;
78

9+
pub use embedded_hal::digital::v2::InputPin as _embedded_hal_gpio_InputPin;
10+
pub use embedded_hal::digital::v2::OutputPin as _embedded_hal_gpio_OutputPin;
11+
pub use embedded_hal::digital::v2::StatefulOutputPin as _embedded_hal_gpio_StatefulOutputPin;
12+
pub use embedded_hal::digital::v2::ToggleableOutputPin as _embedded_hal_gpio_ToggleableOutputPin;
13+
814
pub use crate::gpio::GpioExt as _stm32f0xx_hal_gpio_GpioExt;
915
pub use crate::rcc::RccExt as _stm32f0xx_hal_rcc_RccExt;
1016
pub use crate::time::U32Ext as _stm32f0xx_hal_time_U32Ext;

0 commit comments

Comments
 (0)