From bfc5929f50c766a88c3c5120aa3210d3f1f21f5c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jo=C3=ABl=20Schulz-Andres?= <joel@systemscape.de>
Date: Fri, 24 May 2024 13:46:55 +0200
Subject: [PATCH] gpiov1: Do not call set_speed for AFType::Input

Co-authored-by: Toby Fleming <tobywf@users.noreply.github.com>
---
 embassy-stm32/src/gpio.rs   | 5 +++++
 embassy-stm32/src/macros.rs | 6 +++++-
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/embassy-stm32/src/gpio.rs b/embassy-stm32/src/gpio.rs
index 214813a42..81cb09b24 100644
--- a/embassy-stm32/src/gpio.rs
+++ b/embassy-stm32/src/gpio.rs
@@ -661,6 +661,11 @@ pub(crate) trait SealedPin {
         self.set_as_analog();
     }
 
+    /// Sets the speed of the output pin.
+    ///
+    /// This should never be called for AFType::Input on the STM32F1 series, since MODE and
+    /// CNF bits are not independent. If the CNF bits are altered afterwards as well, this
+    /// will put the pin into output mode.
     #[inline]
     fn set_speed(&self, speed: Speed) {
         let pin = self._pin() as usize;
diff --git a/embassy-stm32/src/macros.rs b/embassy-stm32/src/macros.rs
index 7f8076043..8166dff6a 100644
--- a/embassy-stm32/src/macros.rs
+++ b/embassy-stm32/src/macros.rs
@@ -106,7 +106,11 @@ macro_rules! new_pin {
     ($name:ident, $aftype:expr, $speed:expr, $pull:expr) => {{
         let pin = $name.into_ref();
         pin.set_as_af_pull(pin.af_num(), $aftype, $pull);
-        pin.set_speed($speed);
+        // Do not call set_speed on AFType::Input, as MODE and CNF bits are not independent
+        // for gpio_v1
+        if $aftype != crate::gpio::low_level::AFType::Input {
+            pin.set_speed($speed);
+        }
         Some(pin.map_into())
     }};
 }