Skip to content

Commit 31d6fe3

Browse files
authored
Merge pull request #208 from GFsignet/toggleable-pin
Implement `StatefulOutputPin` and `ToggleableOutputPin` for GPIO Outputs
2 parents 266e62f + 5125552 commit 31d6fe3

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/gpio.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ macro_rules! gpio {
176176
use core::marker::PhantomData;
177177
use core::convert::Infallible;
178178

179-
use crate::hal::digital::v2::{OutputPin, InputPin};
179+
use crate::hal::digital::v2::{OutputPin, StatefulOutputPin, toggleable, InputPin};
180180
use crate::stm32::{$gpioy, $GPIOX, EXTI, SYSCFG};
181181

182182
use crate::rcc::{AHB2, APB2};
@@ -668,6 +668,19 @@ macro_rules! gpio {
668668
}
669669
}
670670

671+
impl<MODE> StatefulOutputPin for $PXi<Output<MODE>> {
672+
fn is_set_high(&self) -> Result<bool, Self::Error> {
673+
Ok(!self.is_set_low().unwrap())
674+
}
675+
676+
fn is_set_low(&self) -> Result<bool, Self::Error> {
677+
// NOTE(unsafe) atomic read with no side effects
678+
Ok(unsafe { (*$GPIOX::ptr()).odr.read().bits() & (1 << $i) == 0 })
679+
}
680+
}
681+
682+
impl<MODE> toggleable::Default for $PXi<Output<MODE>> {}
683+
671684
impl<MODE> InputPin for $PXi<Input<MODE>> {
672685
type Error = Infallible;
673686

0 commit comments

Comments
 (0)