Skip to content

Commit f51652d

Browse files
authored
Merge pull request #71 from stm32-rs/upgrade-embedded-hal
Updated digital traits for gpio to v2
2 parents a694379 + e7eadf5 commit f51652d

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ default-features = false
3939
version = "1.1"
4040

4141
[dependencies.embedded-hal]
42-
version = "0.2.2"
42+
version = "0.2.3"
4343
features = ["unproven"]
4444

4545
[package.metadata.docs.rs]

src/gpio.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ macro_rules! gpio {
137137
pub mod $gpiox {
138138
use core::marker::PhantomData;
139139

140-
use crate::hal::digital::{OutputPin, InputPin};
140+
use crate::hal::digital::v2::{OutputPin, InputPin};
141141
use crate::stm32::{$gpioy, $GPIOX};
142142

143143
use crate::rcc::AHB2;
@@ -249,14 +249,18 @@ macro_rules! gpio {
249249
}
250250

251251
impl<MODE> OutputPin for $PXx<Output<MODE>> {
252-
fn set_high(&mut self) {
252+
type Error = ();
253+
254+
fn set_high(&mut self) -> Result<(), Self::Error> {
253255
// NOTE(unsafe) atomic write to a stateless register
254256
unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << self.i)) }
257+
Ok(())
255258
}
256259

257-
fn set_low(&mut self) {
260+
fn set_low(&mut self) -> Result<(), Self::Error> {
258261
// NOTE(unsafe) atomic write to a stateless register
259262
unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << (16 + self.i))) }
263+
Ok(())
260264
}
261265
}
262266

@@ -376,8 +380,8 @@ macro_rules! gpio {
376380
// spurious signals (e.g. LED flash)
377381
// TODO: I still see a flash of LED using this order
378382
match initial_state {
379-
State::High => res.set_high(),
380-
State::Low => res.set_low(),
383+
State::High => res.set_high().unwrap(),
384+
State::Low => res.set_low().unwrap(),
381385
}
382386

383387
let offset = 2 * $i;
@@ -450,25 +454,31 @@ macro_rules! gpio {
450454
}
451455

452456
impl<MODE> OutputPin for $PXi<Output<MODE>> {
453-
fn set_high(&mut self) {
457+
type Error = ();
458+
459+
fn set_high(&mut self) -> Result<(), Self::Error> {
454460
// NOTE(unsafe) atomic write to a stateless register
455461
unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << $i)) }
462+
Ok(())
456463
}
457464

458-
fn set_low(&mut self) {
465+
fn set_low(&mut self) -> Result<(), Self::Error> {
459466
// NOTE(unsafe) atomic write to a stateless register
460467
unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << (16 + $i))) }
468+
Ok(())
461469
}
462470
}
463471

464472
impl<MODE> InputPin for $PXi<Input<MODE>> {
465-
fn is_high(&self) -> bool {
466-
!self.is_low()
473+
type Error = ();
474+
475+
fn is_high(&self) -> Result<bool, Self::Error> {
476+
Ok(!self.is_low().unwrap())
467477
}
468478

469-
fn is_low(&self) -> bool {
479+
fn is_low(&self) -> Result<bool, Self::Error> {
470480
// NOTE(unsafe) atomic read with no side effects
471-
unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << $i) == 0 }
481+
Ok(unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << $i) == 0 })
472482
}
473483
}
474484

src/prelude.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Prelude - Include traits for hal
22
33
pub use crate::hal::prelude::*; // embedded hal traits
4+
pub use crate::hal::digital::v2::*; // for some reason v2 is not exported in the ehal prelude
45

56
pub use crate::rcc::RccExt as _stm32l4_hal_RccExt;
67
pub use crate::flash::FlashExt as _stm32l4_hal_FlashExt;

0 commit comments

Comments
 (0)