Skip to content
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

Add analog gpio mode #33

Merged
merged 2 commits into from
Nov 26, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -549,6 +552,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