Skip to main content
Explorer
March 25, 2024
Solved

Array for GPIO pins

  • March 25, 2024
  • 3 replies
  • 7653 views

Hello,

 

I am new to STM world.

I am trying to make an array of GPIO pin in this format {{PIN1_GPIO_PORT, PIN1_PIN}.....

This would then be used in custom function so I can dynamically access this pins.

How should I declare such array and how would i use them with HAL_GPIO_WRITEPIN function.

Main use of this is to select SPI slave to communicate with.

 

Thanks in advance

 

    This topic has been closed for replies.
    Best answer by Andrew Neil

    @Huzo wrote:

    I am new to STM world.


    Do you have any experience with any other microcontrollers?

    Do you have any experience with programming in general? With C programming?

     


    @Huzo wrote:

    I am trying to make an array of GPIO pin in this format {{PIN1_GPIO_PORT, PIN1_PIN}.....


    I would have a struct for the {port,pin} pair, and then make an array of those structs ...

    As you want to use it with HAL_GPIO_WritePin, make the types compatible with what that function requires:

    void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)

     

    So you'd have something like

    typedef struct 
    {
     GPIO_TypeDef* GPIOx;
     uint16_t GPIO_Pin;
    } port_and_pin_t;
    
    port_and_pin_t ports_and_pins[length];
    
    :
    :
    
    HAL_GPIO_WritePin( ports_and_pins[x].GPIOx, ports_and_pins[x].GPIO_Pin, value );

     

    You might want to make a "wrapper" function which just takes the struct, breaks-out the 2 elements, and passes them to HAL_GPIO_WritePin ...

    3 replies

    Graduate
    March 25, 2024

    <deleted - the right answer was posted below>

     

    struct pin_ {
        GPIO_TypeDef *port;
        uint16_t pinmask;
    };

    Super User
    March 25, 2024

    @gbm wrote:

    <deleted - the right answer was posted below>


    For one thing, I think your choice of "pinmask" as the name for the pin element is better than mine!

    I always find the ST documentation very unclear on whether these HAL functions expect a pin mask or a pin number.

    :frowning_face:

    HuzoAuthor
    Explorer
    March 25, 2024

    There is no perfect documentation. I will try to make the structure and use without my function, for testing purposes, and will come here with a reply. Thanks.

    Graduate II
    March 25, 2024

    Create a structure, array the structure. More C than STM32

    Super User
    March 25, 2024

    @Huzo wrote:

    I am new to STM world.


    Do you have any experience with any other microcontrollers?

    Do you have any experience with programming in general? With C programming?

     


    @Huzo wrote:

    I am trying to make an array of GPIO pin in this format {{PIN1_GPIO_PORT, PIN1_PIN}.....


    I would have a struct for the {port,pin} pair, and then make an array of those structs ...

    As you want to use it with HAL_GPIO_WritePin, make the types compatible with what that function requires:

    void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)

     

    So you'd have something like

    typedef struct 
    {
     GPIO_TypeDef* GPIOx;
     uint16_t GPIO_Pin;
    } port_and_pin_t;
    
    port_and_pin_t ports_and_pins[length];
    
    :
    :
    
    HAL_GPIO_WritePin( ports_and_pins[x].GPIOx, ports_and_pins[x].GPIO_Pin, value );

     

    You might want to make a "wrapper" function which just takes the struct, breaks-out the 2 elements, and passes them to HAL_GPIO_WritePin ...

    Super User
    March 25, 2024

    Note that the GPIO_Pin parameter of HAL_GPIO_WritePin is a bitmask - not a pin number ...

     

     

    /** @defgroup GPIO_pins GPIO pins
     * @{
     */
    #define GPIO_PIN_0 ((uint16_t)0x0001U) /* Pin 0 selected */
    #define GPIO_PIN_1 ((uint16_t)0x0002U) /* Pin 1 selected */
    #define GPIO_PIN_2 ((uint16_t)0x0004U) /* Pin 2 selected */
    #define GPIO_PIN_3 ((uint16_t)0x0008U) /* Pin 3 selected */
    #define GPIO_PIN_4 ((uint16_t)0x0010U) /* Pin 4 selected */
    #define GPIO_PIN_5 ((uint16_t)0x0020U) /* Pin 5 selected */
    #define GPIO_PIN_6 ((uint16_t)0x0040U) /* Pin 6 selected */
    #define GPIO_PIN_7 ((uint16_t)0x0080U) /* Pin 7 selected */
    #define GPIO_PIN_8 ((uint16_t)0x0100U) /* Pin 8 selected */
    #define GPIO_PIN_9 ((uint16_t)0x0200U) /* Pin 9 selected */
    #define GPIO_PIN_10 ((uint16_t)0x0400U) /* Pin 10 selected */
    #define GPIO_PIN_11 ((uint16_t)0x0800U) /* Pin 11 selected */
    #define GPIO_PIN_12 ((uint16_t)0x1000U) /* Pin 12 selected */
    #define GPIO_PIN_13 ((uint16_t)0x2000U) /* Pin 13 selected */
    #define GPIO_PIN_14 ((uint16_t)0x4000U) /* Pin 14 selected */
    #define GPIO_PIN_15 ((uint16_t)0x8000U) /* Pin 15 selected */
    #define GPIO_PIN_All ((uint16_t)0xFFFFU) /* All pins selected */

     

     

    EDIT

    As @gbm suggested, calling the member of your struct something like "pinmask" would help.