This is an automated email from the ASF dual-hosted git repository.

kasjer pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git

commit 1dcb119ed88506401fd5283902cfebd00b7c4a97
Author: Jerzy Kasenberg <[email protected]>
AuthorDate: Thu Jun 4 22:45:38 2026 +0200

    stm32/gpio: Add function to configure pin function
    
    There is hal_gpio_init_af() function that configures
    pin function.
    
    Now additional function hal_gpio_init_fun() is added
    that has only one argument instead of 4.
    Pin number, AF number pull-up/down and open drain specification
    is combined in one uint32_t value.
    
    This will make it easy to have pin function configuration
    in syscfg as single value created with new macro:
    MCU_AFIO_GPIO_PP()
    
    Before that if pin for some was configurable in syscfg
    additionally AF number could be needed for some function
    that has different AF numbers for same function on different
    pins.
    
    i.e.
    STM32_QSPI_FLASH_CS_PIN: MCU_AFIO_GPIO_PP(MCU_GPIO_PORTC(2), 10, 0, 0)
    
    could be used and later pin can be configured like this
    
    hal_gpio_init_fun(MYNEWT_VAL_STM32_QSPI_FLASH_CS_PIN)
    
    No need to extract additional information from syscfg
    
    Signed-off-by: Jerzy Kasenberg <[email protected]>
---
 hw/mcu/stm/stm32_common/include/stm32_common/mcu.h | 11 +++++++++++
 hw/mcu/stm/stm32_common/src/hal_gpio.c             | 12 ++++++++++++
 2 files changed, 23 insertions(+)

diff --git a/hw/mcu/stm/stm32_common/include/stm32_common/mcu.h 
b/hw/mcu/stm/stm32_common/include/stm32_common/mcu.h
index f7fd8246b..1a0b58192 100644
--- a/hw/mcu/stm/stm32_common/include/stm32_common/mcu.h
+++ b/hw/mcu/stm/stm32_common/include/stm32_common/mcu.h
@@ -24,6 +24,7 @@
 extern "C" {
 #endif
 
+#include <stdint.h>
 #include <hal/hal_gpio.h>
 /*
  * Note that not all processors have support for all ports.
@@ -73,6 +74,8 @@ extern "C" {
  */
 #define MCU_GPIO_PIN_NONE       (0xFFFF)
 
+#define MCU_AFIO_GPIO_PP(pin, af, pu, od)                                     \
+    (((0x0F & af) << 12) | (0x00FF & pin) | ((3 & pu) << 8) | (1 & od) << 10)
 
 /*
  * Mapping of a GPIO pin to an alternate function.
@@ -121,6 +124,14 @@ void stm32_start_bootloader(void);
  */
 int hal_gpio_init_af(int pin, uint8_t af_type, enum hal_gpio_pull pull, 
uint8_t od);
 
+/**
+ * Init alternate function on specified pin
+ * @param pin pin config created by MCU_AFIO_PORTx(pin, af) macro or
+ *            MCU_AFIO_GPIO_PP(pin, af, pull, od)
+ * @return 0 on success -1 on failure
+ */
+int hal_gpio_init_fun(uint32_t pin);
+
 #if MYNEWT_VAL_STM32_WFI_FROM_RAM
 extern void stm32_wfi_from_ram(void);
 #define STM32_WFI stm32_wfi_from_ram
diff --git a/hw/mcu/stm/stm32_common/src/hal_gpio.c 
b/hw/mcu/stm/stm32_common/src/hal_gpio.c
index 112c5aa0b..195db2422 100644
--- a/hw/mcu/stm/stm32_common/src/hal_gpio.c
+++ b/hw/mcu/stm/stm32_common/src/hal_gpio.c
@@ -775,6 +775,18 @@ hal_gpio_init_af(int pin, uint8_t af_type, enum 
hal_gpio_pull pull, uint8_t od)
     return hal_gpio_init_stm(pin, &gpio);
 }
 
+/**
+ * gpio init af
+ *
+ * Configure the specified pin for AF.
+ */
+int
+hal_gpio_init_fun(uint32_t pin)
+{
+    return hal_gpio_init_af(pin & 0xFF, (pin >> 12) & 0xF, (pin >> 8) & 3,
+                            (pin >> 10) & 1);
+}
+
 /**
  * Deinitialize the specified pin to revert to default configuration
  *

Reply via email to