diff --git a/stmhal/adc.c b/stmhal/adc.c
index 57dc212d6edd5bc96e2e6f9ca9fa177afbed5a14..c70d88eafc93097bd64f03af5fb495cc32a17cf8 100644
--- a/stmhal/adc.c
+++ b/stmhal/adc.c
@@ -34,6 +34,7 @@
 #include "adc.h"
 #include "pin.h"
 #include "genhdr/pins.h"
+#include "mphal.h"
 #include "timer.h"
 
 /// \moduleref pyb
@@ -89,6 +90,7 @@ STATIC void adc_init_single(pyb_obj_adc_t *adc_obj) {
       // Channels 0-16 correspond to real pins. Configure the GPIO pin in
       // ADC mode.
       const pin_obj_t *pin = pin_adc1[adc_obj->channel];
+      mp_hal_gpio_clock_enable(pin->gpio);
       GPIO_InitTypeDef GPIO_InitStructure;
       GPIO_InitStructure.Pin = pin->pin_mask;
       GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
@@ -348,6 +350,7 @@ void adc_init_all(pyb_adc_all_obj_t *adc_all, uint32_t resolution) {
         // Channels 0-16 correspond to real pins. Configure the GPIO pin in
         // ADC mode.
         const pin_obj_t *pin = pin_adc1[channel];
+        mp_hal_gpio_clock_enable(pin->gpio);
         GPIO_InitTypeDef GPIO_InitStructure;
         GPIO_InitStructure.Pin = pin->pin_mask;
         GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
diff --git a/stmhal/extint.c b/stmhal/extint.c
index 669c43812ce9a32ddf9422ad3329a76ae175ad28..f89549269fb0c89d57df22b31d237a1a5356523e 100644
--- a/stmhal/extint.c
+++ b/stmhal/extint.c
@@ -167,6 +167,7 @@ uint extint_register(mp_obj_t pin_obj, uint32_t mode, uint32_t pull, mp_obj_t ca
 
     if (*cb != mp_const_none) {
 
+        mp_hal_gpio_clock_enable(pin->gpio);
         GPIO_InitTypeDef exti;
         exti.Pin = pin->pin_mask;
         exti.Mode = mode;
diff --git a/stmhal/i2c.c b/stmhal/i2c.c
index 81fc33e8f20fc538c38208b6322715f4d4c9ef25..59f3a4fe4bbf736c1612ac96f1b653087333823f 100644
--- a/stmhal/i2c.c
+++ b/stmhal/i2c.c
@@ -196,6 +196,7 @@ void i2c_init(I2C_HandleTypeDef *i2c) {
 
     // init the GPIO lines
     for (uint i = 0; i < 2; i++) {
+        mp_hal_gpio_clock_enable(pins[i]->gpio);
         GPIO_InitStructure.Pin = pins[i]->pin_mask;
         HAL_GPIO_Init(pins[i]->gpio, &GPIO_InitStructure);
     }
diff --git a/stmhal/led.c b/stmhal/led.c
index 11a2ec4317639f2d52b6a6830b6221b328b205df..e3dbdb594f6b7a0150326b18ad147f86907efb7b 100644
--- a/stmhal/led.c
+++ b/stmhal/led.c
@@ -33,6 +33,7 @@
 #include "led.h"
 #include "pin.h"
 #include "genhdr/pins.h"
+#include "mphal.h"
 
 #if defined(MICROPY_HW_LED1)
 
@@ -78,6 +79,7 @@ void led_init(void) {
     /* Turn off LEDs and initialize */
     for (int led = 0; led < NUM_LEDS; led++) {
         const pin_obj_t *led_pin = pyb_led_obj[led].led_pin;
+        mp_hal_gpio_clock_enable(led_pin->gpio);
         MICROPY_HW_LED_OFF(led_pin);
         GPIO_InitStructure.Pin = led_pin->pin_mask;
         HAL_GPIO_Init(led_pin->gpio, &GPIO_InitStructure);
diff --git a/stmhal/mphal.c b/stmhal/mphal.c
index 73df2e626d5e4544a3d6f31cb0fc62b6d9b90677..145ed8a1fb67952a7f1de1f79b4f155377000d87 100644
--- a/stmhal/mphal.c
+++ b/stmhal/mphal.c
@@ -69,3 +69,52 @@ void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len) {
         usb_vcp_send_strn_cooked(str, len);
     }
 }
+
+void mp_hal_gpio_clock_enable(GPIO_TypeDef *gpio) {
+    if (0) {
+    #ifdef __GPIOA_CLK_ENABLE
+    } else if (gpio == GPIOA) {
+        __GPIOA_CLK_ENABLE();
+    #endif
+    #ifdef __GPIOB_CLK_ENABLE
+    } else if (gpio == GPIOB) {
+        __GPIOB_CLK_ENABLE();
+    #endif
+    #ifdef __GPIOC_CLK_ENABLE
+    } else if (gpio == GPIOC) {
+        __GPIOC_CLK_ENABLE();
+    #endif
+    #ifdef __GPIOD_CLK_ENABLE
+    } else if (gpio == GPIOD) {
+        __GPIOD_CLK_ENABLE();
+    #endif
+    #ifdef __GPIOE_CLK_ENABLE
+    } else if (gpio == GPIOE) {
+        __GPIOE_CLK_ENABLE();
+    #endif
+    #ifdef __GPIOF_CLK_ENABLE
+    } else if (gpio == GPIOF) {
+        __GPIOF_CLK_ENABLE();
+    #endif
+    #ifdef __GPIOG_CLK_ENABLE
+    } else if (gpio == GPIOG) {
+        __GPIOG_CLK_ENABLE();
+    #endif
+    #ifdef __GPIOH_CLK_ENABLE
+    } else if (gpio == GPIOH) {
+        __GPIOH_CLK_ENABLE();
+    #endif
+    #ifdef __GPIOI_CLK_ENABLE
+    } else if (gpio == GPIOI) {
+        __GPIOI_CLK_ENABLE();
+    #endif
+    #ifdef __GPIOJ_CLK_ENABLE
+    } else if (gpio == GPIOJ) {
+        __GPIOJ_CLK_ENABLE();
+    #endif
+    #ifdef __GPIOK_CLK_ENABLE
+    } else if (gpio == GPIOK) {
+        __GPIOK_CLK_ENABLE();
+    #endif
+    }
+}
diff --git a/stmhal/mphal.h b/stmhal/mphal.h
index d6ccba7ce41b9182fdf16f385a2713313fd0502a..86cf08a2fe8bf82dbcd74611303583849b8c4022 100644
--- a/stmhal/mphal.h
+++ b/stmhal/mphal.h
@@ -12,6 +12,8 @@
 #endif
 #define GPIO_read_output_pin(gpio, pin) (((gpio)->ODR >> (pin)) & 1)
 
+void mp_hal_gpio_clock_enable(GPIO_TypeDef *gpio);
+
 extern const byte mp_hal_status_to_errno_table[4];
 
 NORETURN void mp_hal_raise(HAL_StatusTypeDef status);
diff --git a/stmhal/pin.c b/stmhal/pin.c
index 4be889eaf95c2b3ced8ce1c9bde2aa6e42bfb5bc..8ba6cce8eac1a10e42a6b540fbaf1665fced763c 100644
--- a/stmhal/pin.c
+++ b/stmhal/pin.c
@@ -359,41 +359,7 @@ STATIC mp_obj_t pin_obj_init_helper(const pin_obj_t *self, mp_uint_t n_args, con
     }
 
     // enable the peripheral clock for the port of this pin
-    switch (self->port) {
-        #ifdef __GPIOA_CLK_ENABLE
-        case PORT_A: __GPIOA_CLK_ENABLE(); break;
-        #endif
-        #ifdef __GPIOB_CLK_ENABLE
-        case PORT_B: __GPIOB_CLK_ENABLE(); break;
-        #endif
-        #ifdef __GPIOC_CLK_ENABLE
-        case PORT_C: __GPIOC_CLK_ENABLE(); break;
-        #endif
-        #ifdef __GPIOD_CLK_ENABLE
-        case PORT_D: __GPIOD_CLK_ENABLE(); break;
-        #endif
-        #ifdef __GPIOE_CLK_ENABLE
-        case PORT_E: __GPIOE_CLK_ENABLE(); break;
-        #endif
-        #ifdef __GPIOF_CLK_ENABLE
-        case PORT_F: __GPIOF_CLK_ENABLE(); break;
-        #endif
-        #ifdef __GPIOG_CLK_ENABLE
-        case PORT_G: __GPIOG_CLK_ENABLE(); break;
-        #endif
-        #ifdef __GPIOH_CLK_ENABLE
-        case PORT_H: __GPIOH_CLK_ENABLE(); break;
-        #endif
-        #ifdef __GPIOI_CLK_ENABLE
-        case PORT_I: __GPIOI_CLK_ENABLE(); break;
-        #endif
-        #ifdef __GPIOJ_CLK_ENABLE
-        case PORT_J: __GPIOJ_CLK_ENABLE(); break;
-        #endif
-        #ifdef __GPIOK_CLK_ENABLE
-        case PORT_K: __GPIOK_CLK_ENABLE(); break;
-        #endif
-    }
+    mp_hal_gpio_clock_enable(self->gpio);
 
     // configure the GPIO as requested
     GPIO_InitTypeDef GPIO_InitStructure;
diff --git a/stmhal/spi.c b/stmhal/spi.c
index 68ae239c94cf69513c8f61ce5625a8d0a94b6ee7..c318d923c37b8104591a4db334efc6c91916f7d9 100644
--- a/stmhal/spi.c
+++ b/stmhal/spi.c
@@ -179,6 +179,7 @@ void spi_init(SPI_HandleTypeDef *spi, bool enable_nss_pin) {
     }
 
     for (uint i = (enable_nss_pin ? 0 : 1); i < 4; i++) {
+        mp_hal_gpio_clock_enable(pins[i]->gpio);
         GPIO_InitStructure.Pin = pins[i]->pin_mask;
         HAL_GPIO_Init(pins[i]->gpio, &GPIO_InitStructure);
     }
diff --git a/stmhal/uart.c b/stmhal/uart.c
index fb1386d0414a2ca783c8038d9ec616d5bca663bf..9ff1668e3a8d29df512262715db73c7e767edaae 100644
--- a/stmhal/uart.c
+++ b/stmhal/uart.c
@@ -233,6 +233,7 @@ STATIC bool uart_init2(pyb_uart_obj_t *uart_obj) {
     uart_obj->uart.Instance = UARTx;
 
     // init GPIO
+    mp_hal_gpio_clock_enable(GPIO_Port);
     GPIO_InitTypeDef GPIO_InitStructure;
     GPIO_InitStructure.Pin = GPIO_Pin;
     GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
diff --git a/stmhal/usrsw.c b/stmhal/usrsw.c
index 821df6010705b71e0061a66e66091da7f6cfbb8a..225373ca828ee68458933df0618def0b4118fcd9 100644
--- a/stmhal/usrsw.c
+++ b/stmhal/usrsw.c
@@ -30,6 +30,7 @@
 #include "extint.h"
 #include "pin.h"
 #include "genhdr/pins.h"
+#include "mphal.h"
 #include "usrsw.h"
 
 #if MICROPY_HW_HAS_SWITCH
@@ -53,6 +54,7 @@
 
 // this function inits the switch GPIO so that it can be used
 void switch_init0(void) {
+    mp_hal_gpio_clock_enable(MICROPY_HW_USRSW_PIN.gpio);
     GPIO_InitTypeDef init;
     init.Pin = MICROPY_HW_USRSW_PIN.pin_mask;
     init.Mode = GPIO_MODE_INPUT;
diff --git a/teensy/teensy_hal.c b/teensy/teensy_hal.c
index 312aca058109c8e8fe89756babf90c0066707e3d..bc117aef4599a9046a5cc1093012b9e6526173d2 100644
--- a/teensy/teensy_hal.c
+++ b/teensy/teensy_hal.c
@@ -55,3 +55,6 @@ void mp_hal_stdout_tx_strn_cooked(const char *str, uint32_t len) {
         usb_vcp_send_strn_cooked(str, len);
     }
 }
+
+void mp_hal_gpio_clock_enable(GPIO_TypeDef *gpio) {
+}
diff --git a/teensy/teensy_hal.h b/teensy/teensy_hal.h
index f5c6d18d7db55c19aa4414c49e22607796179197..5874be3484420b024d6803e49918ef64d03195c8 100644
--- a/teensy/teensy_hal.h
+++ b/teensy/teensy_hal.h
@@ -116,6 +116,8 @@ uint32_t HAL_GetTick(void);
 void     HAL_Delay(uint32_t Delay);
 void mp_hal_set_interrupt_char(int c);
 
+void mp_hal_gpio_clock_enable(GPIO_TypeDef *gpio);
+
 int mp_hal_stdin_rx_chr(void);
 void mp_hal_stdout_tx_str(const char *str);
 void mp_hal_stdout_tx_strn(const char *str, uint32_t len);