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.
This commit is contained in:
Damien George 2015-08-03 00:05:16 +01:00
parent 6f1c00869c
commit 0851751615
12 changed files with 68 additions and 35 deletions

View File

@ -34,6 +34,7 @@
#include "adc.h" #include "adc.h"
#include "pin.h" #include "pin.h"
#include "genhdr/pins.h" #include "genhdr/pins.h"
#include "mphal.h"
#include "timer.h" #include "timer.h"
/// \moduleref pyb /// \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 // Channels 0-16 correspond to real pins. Configure the GPIO pin in
// ADC mode. // ADC mode.
const pin_obj_t *pin = pin_adc1[adc_obj->channel]; const pin_obj_t *pin = pin_adc1[adc_obj->channel];
mp_hal_gpio_clock_enable(pin->gpio);
GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Pin = pin->pin_mask; GPIO_InitStructure.Pin = pin->pin_mask;
GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; 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 // Channels 0-16 correspond to real pins. Configure the GPIO pin in
// ADC mode. // ADC mode.
const pin_obj_t *pin = pin_adc1[channel]; const pin_obj_t *pin = pin_adc1[channel];
mp_hal_gpio_clock_enable(pin->gpio);
GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Pin = pin->pin_mask; GPIO_InitStructure.Pin = pin->pin_mask;
GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;

View File

@ -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) { if (*cb != mp_const_none) {
mp_hal_gpio_clock_enable(pin->gpio);
GPIO_InitTypeDef exti; GPIO_InitTypeDef exti;
exti.Pin = pin->pin_mask; exti.Pin = pin->pin_mask;
exti.Mode = mode; exti.Mode = mode;

View File

@ -196,6 +196,7 @@ void i2c_init(I2C_HandleTypeDef *i2c) {
// init the GPIO lines // init the GPIO lines
for (uint i = 0; i < 2; i++) { for (uint i = 0; i < 2; i++) {
mp_hal_gpio_clock_enable(pins[i]->gpio);
GPIO_InitStructure.Pin = pins[i]->pin_mask; GPIO_InitStructure.Pin = pins[i]->pin_mask;
HAL_GPIO_Init(pins[i]->gpio, &GPIO_InitStructure); HAL_GPIO_Init(pins[i]->gpio, &GPIO_InitStructure);
} }

View File

@ -33,6 +33,7 @@
#include "led.h" #include "led.h"
#include "pin.h" #include "pin.h"
#include "genhdr/pins.h" #include "genhdr/pins.h"
#include "mphal.h"
#if defined(MICROPY_HW_LED1) #if defined(MICROPY_HW_LED1)
@ -78,6 +79,7 @@ void led_init(void) {
/* Turn off LEDs and initialize */ /* Turn off LEDs and initialize */
for (int led = 0; led < NUM_LEDS; led++) { for (int led = 0; led < NUM_LEDS; led++) {
const pin_obj_t *led_pin = pyb_led_obj[led].led_pin; 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); MICROPY_HW_LED_OFF(led_pin);
GPIO_InitStructure.Pin = led_pin->pin_mask; GPIO_InitStructure.Pin = led_pin->pin_mask;
HAL_GPIO_Init(led_pin->gpio, &GPIO_InitStructure); HAL_GPIO_Init(led_pin->gpio, &GPIO_InitStructure);

View File

@ -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); 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
}
}

View File

@ -12,6 +12,8 @@
#endif #endif
#define GPIO_read_output_pin(gpio, pin) (((gpio)->ODR >> (pin)) & 1) #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]; extern const byte mp_hal_status_to_errno_table[4];
NORETURN void mp_hal_raise(HAL_StatusTypeDef status); NORETURN void mp_hal_raise(HAL_StatusTypeDef status);

View File

@ -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 // enable the peripheral clock for the port of this pin
switch (self->port) { mp_hal_gpio_clock_enable(self->gpio);
#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
}
// configure the GPIO as requested // configure the GPIO as requested
GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitTypeDef GPIO_InitStructure;

View File

@ -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++) { 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; GPIO_InitStructure.Pin = pins[i]->pin_mask;
HAL_GPIO_Init(pins[i]->gpio, &GPIO_InitStructure); HAL_GPIO_Init(pins[i]->gpio, &GPIO_InitStructure);
} }

View File

@ -233,6 +233,7 @@ STATIC bool uart_init2(pyb_uart_obj_t *uart_obj) {
uart_obj->uart.Instance = UARTx; uart_obj->uart.Instance = UARTx;
// init GPIO // init GPIO
mp_hal_gpio_clock_enable(GPIO_Port);
GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.Pin = GPIO_Pin; GPIO_InitStructure.Pin = GPIO_Pin;
GPIO_InitStructure.Speed = GPIO_SPEED_HIGH; GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;

View File

@ -30,6 +30,7 @@
#include "extint.h" #include "extint.h"
#include "pin.h" #include "pin.h"
#include "genhdr/pins.h" #include "genhdr/pins.h"
#include "mphal.h"
#include "usrsw.h" #include "usrsw.h"
#if MICROPY_HW_HAS_SWITCH #if MICROPY_HW_HAS_SWITCH
@ -53,6 +54,7 @@
// this function inits the switch GPIO so that it can be used // this function inits the switch GPIO so that it can be used
void switch_init0(void) { void switch_init0(void) {
mp_hal_gpio_clock_enable(MICROPY_HW_USRSW_PIN.gpio);
GPIO_InitTypeDef init; GPIO_InitTypeDef init;
init.Pin = MICROPY_HW_USRSW_PIN.pin_mask; init.Pin = MICROPY_HW_USRSW_PIN.pin_mask;
init.Mode = GPIO_MODE_INPUT; init.Mode = GPIO_MODE_INPUT;

View File

@ -55,3 +55,6 @@ void mp_hal_stdout_tx_strn_cooked(const char *str, uint32_t len) {
usb_vcp_send_strn_cooked(str, len); usb_vcp_send_strn_cooked(str, len);
} }
} }
void mp_hal_gpio_clock_enable(GPIO_TypeDef *gpio) {
}

View File

@ -116,6 +116,8 @@ uint32_t HAL_GetTick(void);
void HAL_Delay(uint32_t Delay); void HAL_Delay(uint32_t Delay);
void mp_hal_set_interrupt_char(int c); void mp_hal_set_interrupt_char(int c);
void mp_hal_gpio_clock_enable(GPIO_TypeDef *gpio);
int mp_hal_stdin_rx_chr(void); int mp_hal_stdin_rx_chr(void);
void mp_hal_stdout_tx_str(const char *str); void mp_hal_stdout_tx_str(const char *str);
void mp_hal_stdout_tx_strn(const char *str, uint32_t len); void mp_hal_stdout_tx_strn(const char *str, uint32_t len);