diff --git a/Cargo.toml b/Cargo.toml index 09fa88bf..493cd3ae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,7 +39,7 @@ default-features = false version = "1.1" [dependencies.embedded-hal] -version = "0.2.2" +version = "0.2.3" features = ["unproven"] [package.metadata.docs.rs] diff --git a/src/gpio.rs b/src/gpio.rs index b277455b..efbbe17d 100644 --- a/src/gpio.rs +++ b/src/gpio.rs @@ -137,7 +137,7 @@ macro_rules! gpio { pub mod $gpiox { use core::marker::PhantomData; - use crate::hal::digital::{OutputPin, InputPin}; + use crate::hal::digital::v2::{OutputPin, InputPin}; use crate::stm32::{$gpioy, $GPIOX}; use crate::rcc::AHB2; @@ -249,14 +249,18 @@ macro_rules! gpio { } impl OutputPin for $PXx> { - fn set_high(&mut self) { + type Error = (); + + fn set_high(&mut self) -> Result<(), Self::Error> { // NOTE(unsafe) atomic write to a stateless register unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << self.i)) } + Ok(()) } - fn set_low(&mut self) { + fn set_low(&mut self) -> Result<(), Self::Error> { // NOTE(unsafe) atomic write to a stateless register unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << (16 + self.i))) } + Ok(()) } } @@ -376,8 +380,8 @@ macro_rules! gpio { // spurious signals (e.g. LED flash) // TODO: I still see a flash of LED using this order match initial_state { - State::High => res.set_high(), - State::Low => res.set_low(), + State::High => res.set_high().unwrap(), + State::Low => res.set_low().unwrap(), } let offset = 2 * $i; @@ -450,25 +454,31 @@ macro_rules! gpio { } impl OutputPin for $PXi> { - fn set_high(&mut self) { + type Error = (); + + fn set_high(&mut self) -> Result<(), Self::Error> { // NOTE(unsafe) atomic write to a stateless register unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << $i)) } + Ok(()) } - fn set_low(&mut self) { + fn set_low(&mut self) -> Result<(), Self::Error> { // NOTE(unsafe) atomic write to a stateless register unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << (16 + $i))) } + Ok(()) } } impl InputPin for $PXi> { - fn is_high(&self) -> bool { - !self.is_low() + type Error = (); + + fn is_high(&self) -> Result { + Ok(!self.is_low().unwrap()) } - fn is_low(&self) -> bool { + fn is_low(&self) -> Result { // NOTE(unsafe) atomic read with no side effects - unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << $i) == 0 } + Ok(unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << $i) == 0 }) } } diff --git a/src/prelude.rs b/src/prelude.rs index ac4f4b92..64e56a3e 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -1,6 +1,7 @@ //! Prelude - Include traits for hal pub use crate::hal::prelude::*; // embedded hal traits +pub use crate::hal::digital::v2::*; // for some reason v2 is not exported in the ehal prelude pub use crate::rcc::RccExt as _stm32l4_hal_RccExt; pub use crate::flash::FlashExt as _stm32l4_hal_FlashExt;