Skip to content

Commit 0851751

Browse files
committed
stmhal: Factor GPIO clock enable logic into mp_hal_gpio_clock_enable.
Extracted GPIO clock enable logic into mp_hal_gpio_clock_enable and called from anyplace which might need to use GPIO functions on ports other than A-D. Thanks to Dave Hylands for the patch.
1 parent 6f1c008 commit 0851751

12 files changed

Lines changed: 68 additions & 35 deletions

File tree

stmhal/adc.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "adc.h"
3535
#include "pin.h"
3636
#include "genhdr/pins.h"
37+
#include "mphal.h"
3738
#include "timer.h"
3839

3940
/// \moduleref pyb
@@ -89,6 +90,7 @@ STATIC void adc_init_single(pyb_obj_adc_t *adc_obj) {
8990
// Channels 0-16 correspond to real pins. Configure the GPIO pin in
9091
// ADC mode.
9192
const pin_obj_t *pin = pin_adc1[adc_obj->channel];
93+
mp_hal_gpio_clock_enable(pin->gpio);
9294
GPIO_InitTypeDef GPIO_InitStructure;
9395
GPIO_InitStructure.Pin = pin->pin_mask;
9496
GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
@@ -348,6 +350,7 @@ void adc_init_all(pyb_adc_all_obj_t *adc_all, uint32_t resolution) {
348350
// Channels 0-16 correspond to real pins. Configure the GPIO pin in
349351
// ADC mode.
350352
const pin_obj_t *pin = pin_adc1[channel];
353+
mp_hal_gpio_clock_enable(pin->gpio);
351354
GPIO_InitTypeDef GPIO_InitStructure;
352355
GPIO_InitStructure.Pin = pin->pin_mask;
353356
GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;

stmhal/extint.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ uint extint_register(mp_obj_t pin_obj, uint32_t mode, uint32_t pull, mp_obj_t ca
167167

168168
if (*cb != mp_const_none) {
169169

170+
mp_hal_gpio_clock_enable(pin->gpio);
170171
GPIO_InitTypeDef exti;
171172
exti.Pin = pin->pin_mask;
172173
exti.Mode = mode;

stmhal/i2c.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ void i2c_init(I2C_HandleTypeDef *i2c) {
196196

197197
// init the GPIO lines
198198
for (uint i = 0; i < 2; i++) {
199+
mp_hal_gpio_clock_enable(pins[i]->gpio);
199200
GPIO_InitStructure.Pin = pins[i]->pin_mask;
200201
HAL_GPIO_Init(pins[i]->gpio, &GPIO_InitStructure);
201202
}

stmhal/led.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "led.h"
3434
#include "pin.h"
3535
#include "genhdr/pins.h"
36+
#include "mphal.h"
3637

3738
#if defined(MICROPY_HW_LED1)
3839

@@ -78,6 +79,7 @@ void led_init(void) {
7879
/* Turn off LEDs and initialize */
7980
for (int led = 0; led < NUM_LEDS; led++) {
8081
const pin_obj_t *led_pin = pyb_led_obj[led].led_pin;
82+
mp_hal_gpio_clock_enable(led_pin->gpio);
8183
MICROPY_HW_LED_OFF(led_pin);
8284
GPIO_InitStructure.Pin = led_pin->pin_mask;
8385
HAL_GPIO_Init(led_pin->gpio, &GPIO_InitStructure);

stmhal/mphal.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,52 @@ void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len) {
6969
usb_vcp_send_strn_cooked(str, len);
7070
}
7171
}
72+
73+
void mp_hal_gpio_clock_enable(GPIO_TypeDef *gpio) {
74+
if (0) {
75+
#ifdef __GPIOA_CLK_ENABLE
76+
} else if (gpio == GPIOA) {
77+
__GPIOA_CLK_ENABLE();
78+
#endif
79+
#ifdef __GPIOB_CLK_ENABLE
80+
} else if (gpio == GPIOB) {
81+
__GPIOB_CLK_ENABLE();
82+
#endif
83+
#ifdef __GPIOC_CLK_ENABLE
84+
} else if (gpio == GPIOC) {
85+
__GPIOC_CLK_ENABLE();
86+
#endif
87+
#ifdef __GPIOD_CLK_ENABLE
88+
} else if (gpio == GPIOD) {
89+
__GPIOD_CLK_ENABLE();
90+
#endif
91+
#ifdef __GPIOE_CLK_ENABLE
92+
} else if (gpio == GPIOE) {
93+
__GPIOE_CLK_ENABLE();
94+
#endif
95+
#ifdef __GPIOF_CLK_ENABLE
96+
} else if (gpio == GPIOF) {
97+
__GPIOF_CLK_ENABLE();
98+
#endif
99+
#ifdef __GPIOG_CLK_ENABLE
100+
} else if (gpio == GPIOG) {
101+
__GPIOG_CLK_ENABLE();
102+
#endif
103+
#ifdef __GPIOH_CLK_ENABLE
104+
} else if (gpio == GPIOH) {
105+
__GPIOH_CLK_ENABLE();
106+
#endif
107+
#ifdef __GPIOI_CLK_ENABLE
108+
} else if (gpio == GPIOI) {
109+
__GPIOI_CLK_ENABLE();
110+
#endif
111+
#ifdef __GPIOJ_CLK_ENABLE
112+
} else if (gpio == GPIOJ) {
113+
__GPIOJ_CLK_ENABLE();
114+
#endif
115+
#ifdef __GPIOK_CLK_ENABLE
116+
} else if (gpio == GPIOK) {
117+
__GPIOK_CLK_ENABLE();
118+
#endif
119+
}
120+
}

stmhal/mphal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#endif
1313
#define GPIO_read_output_pin(gpio, pin) (((gpio)->ODR >> (pin)) & 1)
1414

15+
void mp_hal_gpio_clock_enable(GPIO_TypeDef *gpio);
16+
1517
extern const byte mp_hal_status_to_errno_table[4];
1618

1719
NORETURN void mp_hal_raise(HAL_StatusTypeDef status);

stmhal/pin.c

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -359,41 +359,7 @@ STATIC mp_obj_t pin_obj_init_helper(const pin_obj_t *self, mp_uint_t n_args, con
359359
}
360360

361361
// enable the peripheral clock for the port of this pin
362-
switch (self->port) {
363-
#ifdef __GPIOA_CLK_ENABLE
364-
case PORT_A: __GPIOA_CLK_ENABLE(); break;
365-
#endif
366-
#ifdef __GPIOB_CLK_ENABLE
367-
case PORT_B: __GPIOB_CLK_ENABLE(); break;
368-
#endif
369-
#ifdef __GPIOC_CLK_ENABLE
370-
case PORT_C: __GPIOC_CLK_ENABLE(); break;
371-
#endif
372-
#ifdef __GPIOD_CLK_ENABLE
373-
case PORT_D: __GPIOD_CLK_ENABLE(); break;
374-
#endif
375-
#ifdef __GPIOE_CLK_ENABLE
376-
case PORT_E: __GPIOE_CLK_ENABLE(); break;
377-
#endif
378-
#ifdef __GPIOF_CLK_ENABLE
379-
case PORT_F: __GPIOF_CLK_ENABLE(); break;
380-
#endif
381-
#ifdef __GPIOG_CLK_ENABLE
382-
case PORT_G: __GPIOG_CLK_ENABLE(); break;
383-
#endif
384-
#ifdef __GPIOH_CLK_ENABLE
385-
case PORT_H: __GPIOH_CLK_ENABLE(); break;
386-
#endif
387-
#ifdef __GPIOI_CLK_ENABLE
388-
case PORT_I: __GPIOI_CLK_ENABLE(); break;
389-
#endif
390-
#ifdef __GPIOJ_CLK_ENABLE
391-
case PORT_J: __GPIOJ_CLK_ENABLE(); break;
392-
#endif
393-
#ifdef __GPIOK_CLK_ENABLE
394-
case PORT_K: __GPIOK_CLK_ENABLE(); break;
395-
#endif
396-
}
362+
mp_hal_gpio_clock_enable(self->gpio);
397363

398364
// configure the GPIO as requested
399365
GPIO_InitTypeDef GPIO_InitStructure;

stmhal/spi.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ void spi_init(SPI_HandleTypeDef *spi, bool enable_nss_pin) {
179179
}
180180

181181
for (uint i = (enable_nss_pin ? 0 : 1); i < 4; i++) {
182+
mp_hal_gpio_clock_enable(pins[i]->gpio);
182183
GPIO_InitStructure.Pin = pins[i]->pin_mask;
183184
HAL_GPIO_Init(pins[i]->gpio, &GPIO_InitStructure);
184185
}

stmhal/uart.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ STATIC bool uart_init2(pyb_uart_obj_t *uart_obj) {
233233
uart_obj->uart.Instance = UARTx;
234234

235235
// init GPIO
236+
mp_hal_gpio_clock_enable(GPIO_Port);
236237
GPIO_InitTypeDef GPIO_InitStructure;
237238
GPIO_InitStructure.Pin = GPIO_Pin;
238239
GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;

stmhal/usrsw.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "extint.h"
3131
#include "pin.h"
3232
#include "genhdr/pins.h"
33+
#include "mphal.h"
3334
#include "usrsw.h"
3435

3536
#if MICROPY_HW_HAS_SWITCH
@@ -53,6 +54,7 @@
5354

5455
// this function inits the switch GPIO so that it can be used
5556
void switch_init0(void) {
57+
mp_hal_gpio_clock_enable(MICROPY_HW_USRSW_PIN.gpio);
5658
GPIO_InitTypeDef init;
5759
init.Pin = MICROPY_HW_USRSW_PIN.pin_mask;
5860
init.Mode = GPIO_MODE_INPUT;

0 commit comments

Comments
 (0)