Skip to content

Commit

Permalink
Add analog gpio mode (#33)
Browse files Browse the repository at this point in the history
Add analog gpio mode
  • Loading branch information
dfrankland committed Nov 26, 2019
2 parents 487ce60 + a5979a5 commit 5bd959a
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ pub struct PushPull;
/// Open drain output (type state)
pub struct OpenDrain;

/// Analog mode (type state)
pub struct Analog;

/// Alternate function 0 (type state)
pub struct AF0;

Expand Down Expand Up @@ -235,7 +238,7 @@ macro_rules! gpio {
#[allow(unused_imports)]
use super::{AF0, AF1, AF2, AF3, AF4, AF5, AF6, AF7, AF8, AF9, AF10, AF11, AF12, AF13, AF14, AF15};
use super::{
Floating, GpioExt, Input, OpenDrain, Output,
Floating, GpioExt, Input, OpenDrain, Output, Analog,
PullDown, PullUp, PushPull,
PXx, Gpio,
};
Expand Down Expand Up @@ -570,6 +573,28 @@ macro_rules! gpio {

$PXi { _mode: PhantomData }
}

/// Configures the pin to operate as analog, with disabled schmitt trigger.
/// This mode is suitable when the pin is connected to the DAC or ADC.
pub fn into_analog(
self,
moder: &mut MODER,
pupdr: &mut PUPDR,
) -> $PXi<Analog> {
let offset = 2 * $i;

// floating - this is necessary for analog mode
pupdr.pupdr().modify(|r, w| unsafe {
w.bits(r.bits() & !(0b11 << offset) | (0b00 << offset))
});

// analog mode
moder.moder().modify(|r, w| unsafe {
w.bits((r.bits() & !(0b11 << offset)) | (0b11 << offset))
});

$PXi { _mode: PhantomData }
}
}

impl $PXi<Output<OpenDrain>> {
Expand Down

0 comments on commit 5bd959a

Please sign in to comment.