From b0ffd9a1cc6ae19f4698f704df037e5dd6f5fb97 Mon Sep 17 00:00:00 2001
From: chemicstry <chemicstry@gmail.com>
Date: Mon, 6 Jun 2022 17:05:37 +0300
Subject: [PATCH] Fix AF pullup configuration for GPIOv1

---
 embassy-stm32/src/gpio.rs | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)

diff --git a/embassy-stm32/src/gpio.rs b/embassy-stm32/src/gpio.rs
index b6982e91a..f7a5da0a8 100644
--- a/embassy-stm32/src/gpio.rs
+++ b/embassy-stm32/src/gpio.rs
@@ -424,9 +424,14 @@ pub(crate) mod sealed {
             }
         }
 
+        #[inline]
+        unsafe fn set_as_af(&self, af_num: u8, af_type: AFType) {
+            self.set_as_af_pull(af_num, af_type, Pull::None);
+        }
+
         #[cfg(gpio_v1)]
         #[inline]
-        unsafe fn set_as_af(&self, _af_num: u8, af_type: AFType) {
+        unsafe fn set_as_af_pull(&self, _af_num: u8, af_type: AFType, pull: Pull) {
             // F1 uses the AFIO register for remapping.
             // For now, this is not implemented, so af_num is ignored
             // _af_num should be zero here, since it is not set by stm32-data
@@ -435,9 +440,21 @@ pub(crate) mod sealed {
             let crlh = if n < 8 { 0 } else { 1 };
             match af_type {
                 AFType::Input => {
+                    let cnf = match pull {
+                        Pull::Up => {
+                            r.bsrr().write(|w| w.set_bs(n, true));
+                            vals::CnfIn::PULL
+                        }
+                        Pull::Down => {
+                            r.bsrr().write(|w| w.set_br(n, true));
+                            vals::CnfIn::PULL
+                        }
+                        Pull::None => vals::CnfIn::FLOATING,
+                    };
+
                     r.cr(crlh).modify(|w| {
                         w.set_mode(n % 8, vals::Mode::INPUT);
-                        w.set_cnf_in(n % 8, vals::CnfIn::FLOATING);
+                        w.set_cnf_in(n % 8, cnf);
                     });
                 }
                 AFType::OutputPushPull => {
@@ -455,12 +472,6 @@ pub(crate) mod sealed {
             }
         }
 
-        #[cfg(gpio_v2)]
-        #[inline]
-        unsafe fn set_as_af(&self, af_num: u8, af_type: AFType) {
-            self.set_as_af_pull(af_num, af_type, Pull::None);
-        }
-
         #[cfg(gpio_v2)]
         #[inline]
         unsafe fn set_as_af_pull(&self, af_num: u8, af_type: AFType, pull: Pull) {