Skip to content

Updated digital traits for gpio to v2 #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
32 changes: 21 additions & 11 deletions src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -249,14 +249,18 @@ macro_rules! gpio {
}

impl<MODE> OutputPin for $PXx<Output<MODE>> {
fn set_high(&mut self) {
type Error = ();
Copy link
Contributor

@hellow554 hellow554 Sep 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO the Error should either be Infallible, Void or !, but not ().


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(())
}
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -450,25 +454,31 @@ macro_rules! gpio {
}

impl<MODE> OutputPin for $PXi<Output<MODE>> {
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<MODE> InputPin for $PXi<Input<MODE>> {
fn is_high(&self) -> bool {
!self.is_low()
type Error = ();

fn is_high(&self) -> Result<bool, Self::Error> {
Ok(!self.is_low().unwrap())
}

fn is_low(&self) -> bool {
fn is_low(&self) -> Result<bool, Self::Error> {
// 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 })
}
}

Expand Down
1 change: 1 addition & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down