From a4305b2e84db3f08bb4b97a2076cc66686ca983c Mon Sep 17 00:00:00 2001 From: Russell McClellan Date: Sun, 24 Nov 2019 19:04:40 -0500 Subject: [PATCH 1/2] Add analog gpio mode --- src/gpio.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/gpio.rs b/src/gpio.rs index 80ed442bf..8360df2f4 100644 --- a/src/gpio.rs +++ b/src/gpio.rs @@ -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; @@ -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, }; @@ -549,6 +552,23 @@ 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. + pub fn into_analog( + self, + moder: &mut MODER, + ) -> $PXi { + let offset = 2 * $i; + + // analog mode + let mode = 0b11; + moder.moder().modify(|r, w| unsafe { + w.bits((r.bits() & !(0b11 << offset)) | (mode << offset)) + }); + + $PXi { _mode: PhantomData } + } } impl $PXi> { From a5979a55f98f10291bb292ecfd4830e179fd90b3 Mon Sep 17 00:00:00 2001 From: Russell McClellan Date: Mon, 25 Nov 2019 22:21:18 -0500 Subject: [PATCH 2/2] Change pupdr to floating as required by reference manual --- src/gpio.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/gpio.rs b/src/gpio.rs index 8360df2f4..cc8554598 100644 --- a/src/gpio.rs +++ b/src/gpio.rs @@ -554,17 +554,22 @@ macro_rules! gpio { } /// Configures the pin to operate as analog, with disabled schmitt trigger. - /// This mode is suitable when the pin is connected to the DAC. + /// 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 { 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 - let mode = 0b11; moder.moder().modify(|r, w| unsafe { - w.bits((r.bits() & !(0b11 << offset)) | (mode << offset)) + w.bits((r.bits() & !(0b11 << offset)) | (0b11 << offset)) }); $PXi { _mode: PhantomData }