diff --git a/stmhal/adc.c b/stmhal/adc.c index 57dc212d6e..c70d88eafc 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 669c43812c..f89549269f 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 81fc33e8f2..59f3a4fe4b 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 11a2ec4317..e3dbdb594f 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 73df2e626d..145ed8a1fb 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 d6ccba7ce4..86cf08a2fe 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 4be889eaf9..8ba6cce8ea 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 68ae239c94..c318d923c3 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 fb1386d041..9ff1668e3a 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 821df60107..225373ca82 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 312aca0581..bc117aef45 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 f5c6d18d7d..5874be3484 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);