diff --git a/stmhal/i2c.c b/stmhal/i2c.c
index 0af22321435e701592ad9a095b8caa2545a0b820..9b7ec4dabd0a0760b44bc1b83b53ea6311ca9caf 100644
--- a/stmhal/i2c.c
+++ b/stmhal/i2c.c
@@ -203,12 +203,6 @@ void i2c_init0(void) {
 }
 
 void i2c_init(I2C_HandleTypeDef *i2c) {
-    // init the GPIO lines
-    GPIO_InitTypeDef GPIO_InitStructure;
-    GPIO_InitStructure.Mode = GPIO_MODE_AF_OD;
-    GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
-    GPIO_InitStructure.Pull = GPIO_NOPULL; // have external pull-up resistors on both lines
-
     int i2c_unit;
     const pin_obj_t *scl_pin;
     const pin_obj_t *sda_pin;
@@ -241,8 +235,10 @@ void i2c_init(I2C_HandleTypeDef *i2c) {
     }
 
     // init the GPIO lines
-    mp_hal_pin_set_af(scl_pin, &GPIO_InitStructure, AF_FN_I2C, i2c_unit);
-    mp_hal_pin_set_af(sda_pin, &GPIO_InitStructure, AF_FN_I2C, i2c_unit);
+    uint32_t mode = MP_HAL_PIN_MODE_ALT_OPEN_DRAIN;
+    uint32_t pull = MP_HAL_PIN_PULL_NONE; // have external pull-up resistors on both lines
+    mp_hal_pin_config_alt(scl_pin, mode, pull, AF_FN_I2C, i2c_unit);
+    mp_hal_pin_config_alt(sda_pin, mode, pull, AF_FN_I2C, i2c_unit);
 
     // init the I2C device
     if (HAL_I2C_Init(i2c) != HAL_OK) {
diff --git a/stmhal/mphalport.c b/stmhal/mphalport.c
index 8d6aa1a7596e2b8b6df9987b1dcd7914697e4822..028756c76c44a00fc859f5a00b72e0edaf1ee56b 100644
--- a/stmhal/mphalport.c
+++ b/stmhal/mphalport.c
@@ -142,16 +142,11 @@ void mp_hal_pin_config(mp_hal_pin_obj_t pin_obj, uint32_t mode, uint32_t pull, u
     gpio->AFR[pin >> 3] = (gpio->AFR[pin >> 3] & ~(15 << (4 * (pin & 7)))) | (alt << (4 * (pin & 7)));
 }
 
-bool mp_hal_pin_set_af(mp_hal_pin_obj_t pin, GPIO_InitTypeDef *init, uint8_t fn, uint8_t unit) {
-    mp_hal_gpio_clock_enable(pin->gpio);
-
+bool mp_hal_pin_config_alt(mp_hal_pin_obj_t pin, uint32_t mode, uint32_t pull, uint8_t fn, uint8_t unit) {
     const pin_af_obj_t *af = pin_find_af(pin, fn, unit);
     if (af == NULL) {
         return false;
     }
-    init->Pin = pin->pin_mask;
-    init->Alternate = af->idx;
-    HAL_GPIO_Init(pin->gpio, init);
-
+    mp_hal_pin_config(pin, mode, pull, af->idx);
     return true;
 }
diff --git a/stmhal/mphalport.h b/stmhal/mphalport.h
index e58bb33b5f0e05b14abfcb4c49f3f9c724a173d6..0d73186b10300ca23aadceca150f0bd4fef383d5 100644
--- a/stmhal/mphalport.h
+++ b/stmhal/mphalport.h
@@ -42,11 +42,21 @@ static inline mp_uint_t mp_hal_ticks_cpu(void) {
 
 #include "stmhal/pin.h"
 
+#define MP_HAL_PIN_MODE_INPUT           (0)
+#define MP_HAL_PIN_MODE_OUTPUT          (1)
+#define MP_HAL_PIN_MODE_ALT             (2)
+#define MP_HAL_PIN_MODE_ANALOG          (3)
+#define MP_HAL_PIN_MODE_OPEN_DRAIN      (5)
+#define MP_HAL_PIN_MODE_ALT_OPEN_DRAIN  (6)
+#define MP_HAL_PIN_PULL_NONE            (0)
+#define MP_HAL_PIN_PULL_UP              (1)
+#define MP_HAL_PIN_PULL_DOWN            (2)
+
 #define mp_hal_pin_obj_t const pin_obj_t*
 #define mp_hal_get_pin_obj(o)   pin_find(o)
-#define mp_hal_pin_input(p)     mp_hal_pin_config((p), 0, 0, 0)
-#define mp_hal_pin_output(p)    mp_hal_pin_config((p), 1, 0, 0)
-#define mp_hal_pin_open_drain(p) mp_hal_pin_config((p), 5, 0, 0)
+#define mp_hal_pin_input(p)     mp_hal_pin_config((p), MP_HAL_PIN_MODE_INPUT, MP_HAL_PIN_PULL_NONE, 0)
+#define mp_hal_pin_output(p)    mp_hal_pin_config((p), MP_HAL_PIN_MODE_OUTPUT, MP_HAL_PIN_PULL_NONE, 0)
+#define mp_hal_pin_open_drain(p) mp_hal_pin_config((p), MP_HAL_PIN_MODE_OPEN_DRAIN, MP_HAL_PIN_PULL_NONE, 0)
 #if defined(MCU_SERIES_F7) || defined(MCU_SERIES_L4)
 #define mp_hal_pin_high(p)      (((p)->gpio->BSRR) = (p)->pin_mask)
 #define mp_hal_pin_low(p)       (((p)->gpio->BSRR) = ((p)->pin_mask << 16))
@@ -61,4 +71,4 @@ static inline mp_uint_t mp_hal_ticks_cpu(void) {
 
 void mp_hal_gpio_clock_enable(GPIO_TypeDef *gpio);
 void mp_hal_pin_config(mp_hal_pin_obj_t pin, uint32_t mode, uint32_t pull, uint32_t alt);
-bool mp_hal_pin_set_af(mp_hal_pin_obj_t pin, GPIO_InitTypeDef *init, uint8_t fn, uint8_t unit);
+bool mp_hal_pin_config_alt(mp_hal_pin_obj_t pin, uint32_t mode, uint32_t pull, uint8_t fn, uint8_t unit);
diff --git a/stmhal/spi.c b/stmhal/spi.c
index e72810d2873dbd3d6ae70435904c8460e643161d..12a261afbb9673fd8180ba242024af64108b7bdb 100644
--- a/stmhal/spi.c
+++ b/stmhal/spi.c
@@ -243,12 +243,6 @@ STATIC void spi_set_params(SPI_HandleTypeDef *spi, uint32_t prescale, int32_t ba
 
 // TODO allow to take a list of pins to use
 void spi_init(SPI_HandleTypeDef *spi, bool enable_nss_pin) {
-    // init the GPIO lines
-    GPIO_InitTypeDef GPIO_InitStructure;
-    GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
-    GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
-    GPIO_InitStructure.Pull = spi->Init.CLKPolarity == SPI_POLARITY_LOW ? GPIO_PULLDOWN : GPIO_PULLUP;
-
     const pyb_spi_obj_t *self;
     const pin_obj_t *pins[4];
     pins[0] = NULL;
@@ -331,8 +325,11 @@ void spi_init(SPI_HandleTypeDef *spi, bool enable_nss_pin) {
         return;
     }
 
+    // init the GPIO lines
+    uint32_t mode = MP_HAL_PIN_MODE_ALT;
+    uint32_t pull = spi->Init.CLKPolarity == SPI_POLARITY_LOW ? MP_HAL_PIN_PULL_DOWN : MP_HAL_PIN_PULL_UP;
     for (uint i = (enable_nss_pin && pins[0] ? 0 : 1); i < 4; i++) {
-        mp_hal_pin_set_af(pins[i], &GPIO_InitStructure, AF_FN_SPI, (self - &pyb_spi_obj[0]) + 1);
+        mp_hal_pin_config_alt(pins[i], mode, pull, AF_FN_SPI, (self - &pyb_spi_obj[0]) + 1);
     }
 
     // init the SPI device