diff --git a/targets/TARGET_STM/TARGET_STM32F4/pinmap.c b/targets/TARGET_STM/TARGET_STM32F4/pinmap.c index dfc9ee1248..7f9fe49f38 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/pinmap.c +++ b/targets/TARGET_STM/TARGET_STM32F4/pinmap.c @@ -178,5 +178,20 @@ void pin_mode(PinName pin, PinMode mode) } gpio->PUPDR &= (uint32_t)(~(GPIO_PUPDR_PUPDR0 << (pin_index * 2))); gpio->PUPDR |= (uint32_t)(pupd << (pin_index * 2)); - +} + +/* Internal function for setting the gpiomode/function + * without changing Pull mode + */ +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); + uint32_t temp = gpio->PUPDR; + uint32_t pull = (temp >> (pin_index * 2U)) & GPIO_PUPDR_PUPDR0; + + /* Then re-use global function for updating the mode part*/ + pin_function(pin, STM_PIN_DATA(gpiomode, pull, 0)); }