diff --git a/targets/TARGET_STM/TARGET_STM32F0/pinmap.c b/targets/TARGET_STM/TARGET_STM32F0/pinmap.c index 791b8736bd..414fe83fba 100644 --- a/targets/TARGET_STM/TARGET_STM32F0/pinmap.c +++ b/targets/TARGET_STM/TARGET_STM32F0/pinmap.c @@ -49,42 +49,7 @@ static const uint32_t gpio_mode[13] = { 0x10000000 // 12 = Reset IT and EVT (not in STM32Cube HAL) }; -// Enable GPIO clock and return GPIO base address -uint32_t Set_GPIO_Clock(uint32_t port_idx) { - uint32_t gpio_add = 0; - switch (port_idx) { - case PortA: - gpio_add = GPIOA_BASE; - __GPIOA_CLK_ENABLE(); - break; - case PortB: - gpio_add = GPIOB_BASE; - __GPIOB_CLK_ENABLE(); - break; -#if defined(GPIOC_BASE) - case PortC: - gpio_add = GPIOC_BASE; - __GPIOC_CLK_ENABLE(); - break; -#endif -#if defined(GPIOD_BASE) - case PortD: - gpio_add = GPIOD_BASE; - __GPIOD_CLK_ENABLE(); - break; -#endif -#if defined(GPIOF_BASE) - case PortF: - gpio_add = GPIOF_BASE; - __GPIOF_CLK_ENABLE(); - break; -#endif - default: - error("Pinmap error: wrong port number."); - break; - } - return gpio_add; -} +extern GPIO_TypeDef *Set_GPIO_Clock(uint32_t port_idx); /** * Configure pin (mode, speed, output type and pull-up/pull-down) @@ -101,8 +66,7 @@ void pin_function(PinName pin, int data) { uint32_t pin_index = STM_PIN(pin); // Enable GPIO clock - uint32_t gpio_add = Set_GPIO_Clock(port_index); - GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; + GPIO_TypeDef *gpio = Set_GPIO_Clock(port_index); // Configure GPIO GPIO_InitTypeDef GPIO_InitStructure; @@ -130,8 +94,7 @@ void pin_mode(PinName pin, PinMode mode) { uint32_t pin_index = STM_PIN(pin); // Enable GPIO clock - uint32_t gpio_add = Set_GPIO_Clock(port_index); - GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; + GPIO_TypeDef *gpio = Set_GPIO_Clock(port_index); // Configure pull-up/pull-down resistors uint32_t pupd = (uint32_t)mode; @@ -148,7 +111,7 @@ void pin_function_gpiomode(PinName pin, uint32_t gpiomode) { /* Read current pull state from HW to avoid over-write*/ uint32_t port_index = STM_PORT(pin); uint32_t pin_index = STM_PIN(pin); - GPIO_TypeDef *gpio = (GPIO_TypeDef *) Set_GPIO_Clock(port_index); + GPIO_TypeDef *gpio = Set_GPIO_Clock(port_index); uint32_t temp = gpio->PUPDR; uint32_t pull = (temp >> (pin_index * 2U)) & GPIO_PUPDR_PUPDR0; diff --git a/targets/TARGET_STM/gpio_api.c b/targets/TARGET_STM/gpio_api.c index 560350f13e..477043d1b3 100644 --- a/targets/TARGET_STM/gpio_api.c +++ b/targets/TARGET_STM/gpio_api.c @@ -32,7 +32,78 @@ #include "pinmap.h" #include "mbed_error.h" -extern uint32_t Set_GPIO_Clock(uint32_t port_idx); +// Enable GPIO clock and return GPIO base address +GPIO_TypeDef *Set_GPIO_Clock(uint32_t port_idx) { + uint32_t gpio_add = 0; + switch (port_idx) { + case PortA: + gpio_add = GPIOA_BASE; + __GPIOA_CLK_ENABLE(); + break; + case PortB: + gpio_add = GPIOB_BASE; + __GPIOB_CLK_ENABLE(); + break; +#if defined(GPIOC_BASE) + case PortC: + gpio_add = GPIOC_BASE; + __GPIOC_CLK_ENABLE(); + break; +#endif +#if defined GPIOD_BASE + case PortD: + gpio_add = GPIOD_BASE; + __GPIOD_CLK_ENABLE(); + break; +#endif +#if defined GPIOE_BASE + case PortE: + gpio_add = GPIOE_BASE; + __GPIOE_CLK_ENABLE(); + break; +#endif +#if defined GPIOF_BASE + case PortF: + gpio_add = GPIOF_BASE; + __GPIOF_CLK_ENABLE(); + break; +#endif +#if defined GPIOG_BASE + case PortG: + gpio_add = GPIOG_BASE; + __GPIOG_CLK_ENABLE(); + break; +#endif +#if defined GPIOH_BASE + case PortH: + gpio_add = GPIOH_BASE; + __GPIOH_CLK_ENABLE(); + break; +#endif +#if defined GPIOI_BASE + case PortI: + gpio_add = GPIOI_BASE; + __GPIOI_CLK_ENABLE(); + break; +#endif +#if defined GPIOJ_BASE + case PortJ: + gpio_add = GPIOJ_BASE; + __GPIOJ_CLK_ENABLE(); + break; +#endif +#if defined GPIOK_BASE + case PortK: + gpio_add = GPIOK_BASE; + __GPIOK_CLK_ENABLE(); + break; +#endif + default: + error("Pinmap error: wrong port number."); + break; + } + return (GPIO_TypeDef *) gpio_add; +} uint32_t gpio_set(PinName pin) { MBED_ASSERT(pin != (PinName)NC); @@ -51,8 +122,7 @@ void gpio_init(gpio_t *obj, PinName pin) { uint32_t port_index = STM_PORT(pin); // Enable GPIO clock - uint32_t gpio_add = Set_GPIO_Clock(port_index); - GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; + GPIO_TypeDef *gpio = Set_GPIO_Clock(port_index); // Fill GPIO object structure for future use obj->mask = gpio_set(pin); diff --git a/targets/TARGET_STM/gpio_irq_api.c b/targets/TARGET_STM/gpio_irq_api.c index 2c78b599f1..9af8f901e0 100644 --- a/targets/TARGET_STM/gpio_irq_api.c +++ b/targets/TARGET_STM/gpio_irq_api.c @@ -43,7 +43,7 @@ typedef struct gpio_channel { uint32_t pin_mask; // bitmask representing which pins are configured for receiving interrupts uint32_t channel_ids[MAX_PIN_LINE]; // mbed "gpio_irq_t gpio_irq" field of instance - uint32_t channel_gpio[MAX_PIN_LINE]; // base address of gpio port group + GPIO_TypeDef* channel_gpio[MAX_PIN_LINE]; // base address of gpio port group uint32_t channel_pin[MAX_PIN_LINE]; // pin number in port group } gpio_channel_t; @@ -159,7 +159,7 @@ static void gpio_irq6(void) } #endif -extern uint32_t Set_GPIO_Clock(uint32_t port_idx); +extern GPIO_TypeDef *Set_GPIO_Clock(uint32_t port_idx); extern void pin_function_gpiomode(PinName pin, uint32_t gpiomode); int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id) @@ -221,7 +221,7 @@ int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32 } // Enable GPIO clock - uint32_t gpio_add = Set_GPIO_Clock(port_index); + GPIO_TypeDef *gpio_add = Set_GPIO_Clock(port_index); // Save informations for future use obj->irq_n = pin_lines_desc[pin_index].irq_n; diff --git a/targets/TARGET_STM/port_api.c b/targets/TARGET_STM/port_api.c index 532b57e8fa..04c7c8a280 100644 --- a/targets/TARGET_STM/port_api.c +++ b/targets/TARGET_STM/port_api.c @@ -34,7 +34,7 @@ #if DEVICE_PORTIN || DEVICE_PORTOUT -extern uint32_t Set_GPIO_Clock(uint32_t port_idx); +extern GPIO_TypeDef *Set_GPIO_Clock(uint32_t port_idx); // high nibble = port number (0=A, 1=B, 2=C, 3=D, 4=E, 5=F, ...) // low nibble = pin number @@ -48,8 +48,7 @@ void port_init(port_t *obj, PortName port, int mask, PinDirection dir) uint32_t port_index = (uint32_t)port; // Enable GPIO clock - uint32_t gpio_add = Set_GPIO_Clock(port_index); - GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; + GPIO_TypeDef *gpio = Set_GPIO_Clock(port_index); // Fill PORT object structure for future use obj->port = port;