Skip to content
This repository has been archived by the owner on Feb 13, 2019. It is now read-only.

Commit

Permalink
Merge pull request #51 from kellerkindt/patch-3
Browse files Browse the repository at this point in the history
Re-Introduce into_open_drain_output
  • Loading branch information
therealprof authored Oct 12, 2018
2 parents df1506b + 0cf6f85 commit 5118b93
Showing 1 changed file with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@ macro_rules! gpio {

impl <MODE> toggleable::Default for $PXx<Output<MODE>> {}

impl InputPin for $PXx<Output<OpenDrain>> {
fn is_high(&self) -> bool {
!self.is_low()
}

fn is_low(&self) -> bool {
// NOTE(unsafe) atomic read with no side effects
unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << self.i) == 0 }
}
}

$(
/// Pin
pub struct $PXi<MODE> {
Expand Down Expand Up @@ -284,27 +295,26 @@ macro_rules! gpio {
$PXi { _mode: PhantomData }
}

// /// Configures the pin to operate as an open drain output pin
// pub fn into_open_drain_output(
// self,
// moder: &mut MODER,
// otyper: &mut OTYPER,
// ) -> $PXi<Output<OpenDrain>> {
// let offset = 2 * $i;

// // general purpose output mode
// let mode = 0b01;
// moder.moder().modify(|r, w| unsafe {
// w.bits((r.bits() & !(0b11 << offset)) | (mode << offset))
// });
/// Configures the pin to operate as an open drain output pin
pub fn into_open_drain_output(
self,
cr: &mut $CR,
) -> $PXi<Output<OpenDrain>> {
let offset = (4 * $i) % 32;
// General purpose output open-drain
let cnf = 0b01;
// Open-Drain Output mode, max speed 50 MHz
let mode = 0b11;
let bits = (cnf << 2) | mode;

// // open drain output
// otyper
// .otyper()
// .modify(|r, w| unsafe { w.bits(r.bits() | (0b1 << $i)) });
cr
.cr()
.modify(|r, w| unsafe {
w.bits((r.bits() & !(0b1111 << offset)) | (bits << offset))
});

// $PXi { _mode: PhantomData }
// }
$PXi { _mode: PhantomData }
}

/// Configures the pin to operate as an push pull output pin
pub fn into_push_pull_output(
Expand Down Expand Up @@ -399,6 +409,17 @@ macro_rules! gpio {
unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << $i) == 0 }
}
}

impl InputPin for $PXi<Output<OpenDrain>> {
fn is_high(&self) -> bool {
!self.is_low()
}

fn is_low(&self) -> bool {
// NOTE(unsafe) atomic read with no side effects
unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << $i) == 0 }
}
}
)+
}
}
Expand Down

0 comments on commit 5118b93

Please sign in to comment.